1- import { yearsPassed } from '@douglasneuroinformatics/libjs' ;
1+ import { replacer , yearsPassed } from '@douglasneuroinformatics/libjs' ;
22import { reviver } from '@douglasneuroinformatics/libjs' ;
33import { linearRegression } from '@douglasneuroinformatics/libstats' ;
44import { Injectable , NotFoundException , UnprocessableEntityException } from '@nestjs/common' ;
5- import type { Json , ScalarInstrument } from '@opendatacapture/runtime-core' ;
5+ import type { ScalarInstrument } from '@opendatacapture/runtime-core' ;
66import type {
77 CreateInstrumentRecordData ,
88 InstrumentRecord ,
@@ -49,7 +49,7 @@ export class InstrumentRecordsService {
4949 }
5050
5151 async create (
52- { data, date, groupId, instrumentId, sessionId, subjectId } : CreateInstrumentRecordData ,
52+ { data : rawData , date, groupId, instrumentId, sessionId, subjectId } : CreateInstrumentRecordData ,
5353 options ?: EntityOperationOptions
5454 ) : Promise < InstrumentRecordModel > {
5555 if ( groupId ) {
@@ -64,17 +64,21 @@ export class InstrumentRecordsService {
6464
6565 await this . subjectsService . findById ( subjectId ) ;
6666 await this . sessionsService . findById ( sessionId ) ;
67- if ( ! instrument . validationSchema . safeParse ( data ) . success ) {
67+
68+ const parseResult = instrument . validationSchema . safeParse ( this . parseJson ( rawData ) ) ;
69+ if ( ! parseResult . success ) {
70+ console . error ( parseResult . error . issues ) ;
6871 throw new UnprocessableEntityException (
69- `Data received does not pass validation schema of instrument '${ instrument . id } '`
72+ `Data received for record does not pass validation schema of instrument '${ instrument . id } '`
7073 ) ;
7174 }
75+
7276 return this . instrumentRecordModel . create ( {
7377 data : {
7478 computedMeasures : instrument . measures
75- ? this . instrumentMeasuresService . computeMeasures ( instrument . measures , data )
79+ ? this . instrumentMeasuresService . computeMeasures ( instrument . measures , parseResult . data )
7680 : null ,
77- data,
81+ data : this . serializeData ( parseResult . data ) ,
7882 date,
7983 group : groupId
8084 ? {
@@ -271,7 +275,7 @@ export class InstrumentRecordsService {
271275
272276 try {
273277 for ( let i = 0 ; i < records . length ; i ++ ) {
274- const { data, date, subjectId } = records [ i ] ! ;
278+ const { data : rawData , date, subjectId } = records [ i ] ! ;
275279 await this . createSubjectIfNotFound ( subjectId ) ;
276280
277281 const session = await this . sessionsService . create ( {
@@ -287,9 +291,9 @@ export class InstrumentRecordsService {
287291
288292 const sessionId = session . id ;
289293
290- const revivedData = JSON . parse ( JSON . stringify ( data ) , reviver ) as Json ;
291-
292- if ( ! instrument . validationSchema . safeParse ( revivedData ) . success ) {
294+ const parseResult = instrument . validationSchema . safeParse ( this . parseJson ( rawData ) ) ;
295+ if ( ! parseResult . success ) {
296+ console . error ( parseResult . error . issues ) ;
293297 throw new UnprocessableEntityException (
294298 `Data received for record at index '${ i } ' does not pass validation schema of instrument '${ instrument . id } '`
295299 ) ;
@@ -298,9 +302,9 @@ export class InstrumentRecordsService {
298302 const createdRecord = await this . instrumentRecordModel . create ( {
299303 data : {
300304 computedMeasures : instrument . measures
301- ? this . instrumentMeasuresService . computeMeasures ( instrument . measures , revivedData )
305+ ? this . instrumentMeasuresService . computeMeasures ( instrument . measures , parseResult . data )
302306 : null ,
303- data,
307+ data : this . serializeData ( parseResult . data ) ,
304308 date,
305309 group : groupId
306310 ? {
@@ -356,4 +360,12 @@ export class InstrumentRecordsService {
356360 }
357361 }
358362 }
363+
364+ private parseJson ( data : unknown ) {
365+ return JSON . parse ( JSON . stringify ( data ) , reviver ) as unknown ;
366+ }
367+
368+ private serializeData ( data : unknown ) {
369+ return JSON . parse ( JSON . stringify ( data , replacer ) ) as unknown ;
370+ }
359371}
0 commit comments