11import { HybridCrypto } from '@douglasneuroinformatics/libcrypto' ;
2- import { Injectable , InternalServerErrorException , Logger , type OnApplicationBootstrap } from '@nestjs/common' ;
2+ import { LoggingService } from '@douglasneuroinformatics/libnest/logging' ;
3+ import { Injectable , InternalServerErrorException , type OnApplicationBootstrap } from '@nestjs/common' ;
34import type { RemoteAssignment } from '@opendatacapture/schemas/assignment' ;
45import { $Json } from '@opendatacapture/schemas/core' ;
56
@@ -9,13 +10,11 @@ import { InstrumentRecordsService } from '@/instrument-records/instrument-record
910import { InstrumentsService } from '@/instruments/instruments.service' ;
1011import { SessionsService } from '@/sessions/sessions.service' ;
1112import { SetupService } from '@/setup/setup.service' ;
12- import { VirtualizationService } from '@/virtualization/virtualization.service' ;
1313
1414import { GatewayService } from './gateway.service' ;
1515
1616@Injectable ( )
1717export class GatewaySynchronizer implements OnApplicationBootstrap {
18- private readonly logger = new Logger ( GatewaySynchronizer . name ) ;
1918 private readonly refreshInterval : number ;
2019
2120 constructor (
@@ -24,9 +23,9 @@ export class GatewaySynchronizer implements OnApplicationBootstrap {
2423 private readonly gatewayService : GatewayService ,
2524 private readonly instrumentsService : InstrumentsService ,
2625 private readonly instrumentRecordsService : InstrumentRecordsService ,
26+ private readonly loggingService : LoggingService ,
2727 private readonly sessionsService : SessionsService ,
28- private readonly setupService : SetupService ,
29- private readonly virtualizationService : VirtualizationService
28+ private readonly setupService : SetupService
3029 ) {
3130 this . refreshInterval = configurationService . get ( 'GATEWAY_REFRESH_INTERVAL' ) ;
3231 }
@@ -42,10 +41,10 @@ export class GatewaySynchronizer implements OnApplicationBootstrap {
4241 const assignment = await this . assignmentsService . findById ( remoteAssignment . id ) ;
4342
4443 if ( ! remoteAssignment . completedAt ) {
45- this . logger . error ( `Field 'completedAt' is null for assignment '${ assignment . id } '` ) ;
44+ this . loggingService . error ( `Field 'completedAt' is null for assignment '${ assignment . id } '` ) ;
4645 return ;
4746 } else if ( ! remoteAssignment . symmetricKey ) {
48- this . logger . error ( `Field 'symmetricKey' is null for assignment '${ assignment . id } '` ) ;
47+ this . loggingService . error ( `Field 'symmetricKey' is null for assignment '${ assignment . id } '` ) ;
4948 return ;
5049 }
5150
@@ -65,7 +64,7 @@ export class GatewaySynchronizer implements OnApplicationBootstrap {
6564
6665 if ( instrument . kind === 'SERIES' ) {
6766 if ( ! ( remoteAssignment . encryptedData . startsWith ( '$' ) && remoteAssignment . symmetricKey . startsWith ( '$' ) ) ) {
68- this . logger . error ( { remoteAssignment } ) ;
67+ this . loggingService . error ( { remoteAssignment } ) ;
6968 throw new InternalServerErrorException ( 'Malformed remote assignment for series instrument' ) ;
7069 }
7170 cipherTexts . push ( ...remoteAssignment . encryptedData . slice ( 1 ) . split ( '$' ) ) ;
@@ -80,7 +79,7 @@ export class GatewaySynchronizer implements OnApplicationBootstrap {
8079 ) ;
8180 }
8281 } else if ( remoteAssignment . encryptedData . includes ( '$' ) || remoteAssignment . symmetricKey . includes ( '$' ) ) {
83- this . logger . error ( { remoteAssignment } ) ;
82+ this . loggingService . error ( { remoteAssignment } ) ;
8483 throw new InternalServerErrorException ( 'Malformed remote assignment for scalar instrument' ) ;
8584 } else {
8685 cipherTexts . push ( remoteAssignment . encryptedData ) ;
@@ -113,12 +112,12 @@ export class GatewaySynchronizer implements OnApplicationBootstrap {
113112 sessionId : session . id ,
114113 subjectId : assignment . subjectId
115114 } ) ;
116- this . logger . log ( `Created record with ID: ${ record . id } ` ) ;
115+ this . loggingService . log ( `Created record with ID: ${ record . id } ` ) ;
117116 createdRecordIds . push ( record . id ) ;
118117 }
119118 await this . gatewayService . deleteRemoteAssignment ( assignment . id ) ;
120119 } catch ( err ) {
121- this . logger . error ( {
120+ this . loggingService . error ( {
122121 data : {
123122 assignment,
124123 cipherTexts,
@@ -127,10 +126,10 @@ export class GatewaySynchronizer implements OnApplicationBootstrap {
127126 } ,
128127 message : 'Failed to Process Data'
129128 } ) ;
130- this . logger . error ( err ) ;
129+ this . loggingService . error ( err ) ;
131130 for ( const id of createdRecordIds ) {
132131 await this . instrumentRecordsService . deleteById ( id ) ;
133- this . logger . log ( `Deleted Record with ID: ${ id } ` ) ;
132+ this . loggingService . log ( `Deleted Record with ID: ${ id } ` ) ;
134133 }
135134 throw err ;
136135 }
@@ -139,16 +138,19 @@ export class GatewaySynchronizer implements OnApplicationBootstrap {
139138 private async sync ( ) {
140139 const setupState = await this . setupService . getState ( ) ;
141140 if ( ! setupState . isSetup ) {
142- this . logger . log ( 'Will not attempt synchronizing with gateway: app is not setup' ) ;
141+ this . loggingService . log ( 'Will not attempt synchronizing with gateway: app is not setup' ) ;
143142 return ;
144143 }
145144
146- this . logger . log ( 'Synchronizing with gateway...' ) ;
145+ this . loggingService . log ( 'Synchronizing with gateway...' ) ;
147146 let remoteAssignments : RemoteAssignment [ ] ;
148147 try {
149148 remoteAssignments = await this . gatewayService . fetchRemoteAssignments ( ) ;
150149 } catch ( err ) {
151- this . logger . error ( 'Failed to Fetch Remote Assignments' , err ) ;
150+ this . loggingService . error ( {
151+ cause : err ,
152+ error : 'Failed to Fetch Remote Assignments'
153+ } ) ;
152154 return ;
153155 }
154156
@@ -164,6 +166,6 @@ export class GatewaySynchronizer implements OnApplicationBootstrap {
164166 status : assignment . status
165167 } ) ;
166168 }
167- this . logger . log ( 'Done synchronizing with gateway' ) ;
169+ this . loggingService . log ( 'Done synchronizing with gateway' ) ;
168170 }
169171}
0 commit comments