@@ -17,7 +17,6 @@ import type {
1717import { Prisma } from '@prisma/client' ;
1818import type { $Enums , Session } from '@prisma/client' ;
1919import { isNumber , mergeWith , pickBy } from 'lodash-es' ;
20- import { err , ok } from 'neverthrow' ;
2120
2221import type { EntityOperationOptions } from '@/core/types' ;
2322import { GroupsService } from '@/groups/groups.service' ;
@@ -39,6 +38,17 @@ type RecordObject = {
3938 timestamp : string ;
4039} ;
4140
41+ type ExpandDataType =
42+ | {
43+ measure : string ;
44+ measureValue : boolean | Date | number | string | undefined ;
45+ success : true ;
46+ }
47+ | {
48+ message : string ;
49+ success : false ;
50+ } ;
51+
4252@Injectable ( )
4353export class InstrumentRecordsService {
4454 constructor (
@@ -167,20 +177,26 @@ export class InstrumentRecordsService {
167177 instruments . set ( record . instrumentId , instrument ) ;
168178 }
169179 for ( const [ measureKey , measureValue ] of Object . entries ( record . computedMeasures ) ) {
170- if ( Array . isArray ( measureValue ) ) {
171- const objectRecord : RecordObject = {
172- groupId : record . subject . groupIds [ 0 ] ?? DEFAULT_GROUP_NAME ,
173- sessionDate : record . session . date . toISOString ( ) ,
174- sessionId : record . session . id ,
175- sessionType : record . session . type ,
176- subjectAge : record . subject . dateOfBirth ? yearsPassed ( record . subject . dateOfBirth ) : null ,
177- subjectId : record . subject . id ,
178- subjectSex : record . subject . sex ,
179- timestamp : record . date . toISOString ( )
180- } ;
181- const arrayResult = this . expandData ( data , measureValue , instrument , objectRecord ) ;
182- if ( arrayResult . isErr ( ) ) {
183- throw new Error ( 'Error interpreting array computed measure' ) ;
180+ if ( Array . isArray ( measureValue ) && measureValue . length > 1 ) {
181+ const arrayResult = this . expandData ( measureValue ) ;
182+
183+ for ( const recordArrayItem of arrayResult ) {
184+ // eslint-disable-next-line max-depth
185+ if ( ! recordArrayItem . success ) throw new Error ( recordArrayItem . message ) ;
186+ data . push ( {
187+ groupId : record . subject . groupIds [ 0 ] ?? DEFAULT_GROUP_NAME ,
188+ instrumentEdition : instrument . internal . edition ,
189+ instrumentName : instrument . internal . name ,
190+ measure : recordArrayItem . measure ,
191+ sessionDate : record . session . date . toISOString ( ) ,
192+ sessionId : record . session . id ,
193+ sessionType : record . session . type ,
194+ subjectAge : record . subject . dateOfBirth ? yearsPassed ( record . subject . dateOfBirth ) : null ,
195+ subjectId : record . subject . id ,
196+ subjectSex : record . subject . sex ,
197+ timestamp : record . date . toISOString ( ) ,
198+ value : recordArrayItem . measureValue
199+ } ) ;
184200 }
185201 } else {
186202 data . push ( {
@@ -405,38 +421,30 @@ export class InstrumentRecordsService {
405421 }
406422 }
407423
408- private expandData (
409- data : InstrumentRecordsExport ,
410- listEntry : any [ ] ,
411- instrument : ScalarInstrument ,
412- record : RecordObject
413- ) {
414- if ( listEntry . length > 0 ) {
415- for ( const objectEntry of listEntry ) {
416- for ( const [ dataKey , dataValue ] of Object . entries ( objectEntry as { [ key : string ] : any } ) ) {
417- const parseResult = $RecordArrayFieldValue . safeParse ( dataValue ) ;
418- if ( ! parseResult . success ) return err ( 'Invalid data value' ) ;
419- data . push ( {
420- groupId : record . groupId ?? DEFAULT_GROUP_NAME ,
421- instrumentEdition : instrument . internal . edition ,
422- instrumentName : instrument . internal . name ,
423- measure : dataKey ,
424- sessionDate : record . sessionDate ,
425- sessionId : record . sessionId ,
426- sessionType : record . sessionType ,
427- subjectAge : record . subjectAge ,
428- subjectId : record . subjectId ,
429- subjectSex : record . subjectSex ,
430- timestamp : record . timestamp ,
431- value : typeof dataValue === 'string' ? dataValue : JSON . stringify ( dataValue )
424+ private expandData ( listEntry : any [ ] ) : ExpandDataType [ ] {
425+ const validRecordArrayList : ExpandDataType [ ] = [ ] ;
426+ if ( listEntry . length < 1 ) {
427+ throw new Error ( 'Record Array is Empty' ) ;
428+ }
429+ for ( const objectEntry of Object . values ( listEntry ) ) {
430+ for ( const [ dataKey , dataValue ] of Object . entries ( objectEntry as { [ key : string ] : any } ) ) {
431+ const parseResult = $RecordArrayFieldValue . safeParse ( dataValue ) ;
432+ if ( ! parseResult . success ) {
433+ validRecordArrayList . push ( {
434+ message : `Invalid data type ${ dataKey } for Record array entry at key ${ dataKey } ` ,
435+ success : false
432436 } ) ;
433437 }
438+ validRecordArrayList . push ( {
439+ measure : dataKey ,
440+ measureValue : parseResult . data ,
441+ success : true
442+ } ) ;
434443 }
435- return ok ( 'Success' ) ;
436- } else {
437- return err ( 'Error interpreting array' ) ;
438444 }
445+ return validRecordArrayList ;
439446 }
447+
440448 private getInstrumentById ( instrumentId : string ) {
441449 return this . instrumentsService
442450 . findById ( instrumentId )
0 commit comments