@@ -27,16 +27,11 @@ import { SubjectsService } from '@/subjects/subjects.service';
2727
2828import { InstrumentMeasuresService } from './instrument-measures.service' ;
2929
30- type ExpandDataType =
31- | {
32- measure : string ;
33- measureValue : boolean | Date | number | string | undefined ;
34- success : true ;
35- }
36- | {
37- message : string ;
38- success : false ;
39- } ;
30+ type ExpandDataType = {
31+ measure : string ;
32+ measureValue : boolean | Date | number | string | undefined ;
33+ success : true ;
34+ } ;
4035
4136@Injectable ( )
4237export class InstrumentRecordsService {
@@ -166,26 +161,28 @@ export class InstrumentRecordsService {
166161 instruments . set ( record . instrumentId , instrument ) ;
167162 }
168163 for ( const [ measureKey , measureValue ] of Object . entries ( record . computedMeasures ) ) {
169- if ( Array . isArray ( measureValue ) && measureValue . length > 1 ) {
170- const arrayResult = this . expandData ( measureValue ) ;
171-
172- for ( const recordArrayItem of arrayResult ) {
173- // eslint-disable-next-line max-depth
174- if ( ! recordArrayItem . success ) throw new Error ( recordArrayItem . message ) ;
175- data . push ( {
176- groupId : record . subject . groupIds [ 0 ] ?? DEFAULT_GROUP_NAME ,
177- instrumentEdition : instrument . internal . edition ,
178- instrumentName : instrument . internal . name ,
179- measure : recordArrayItem . measure ,
180- sessionDate : record . session . date . toISOString ( ) ,
181- sessionId : record . session . id ,
182- sessionType : record . session . type ,
183- subjectAge : record . subject . dateOfBirth ? yearsPassed ( record . subject . dateOfBirth ) : null ,
184- subjectId : record . subject . id ,
185- subjectSex : record . subject . sex ,
186- timestamp : record . date . toISOString ( ) ,
187- value : recordArrayItem . measureValue
164+ //early return statement
165+ if ( Array . isArray ( measureValue ) && measureValue . length >= 1 ) {
166+ try {
167+ const arrayResult = this . expandData ( measureValue ) ;
168+ arrayResult . map ( ( arrayEntry : ExpandDataType ) => {
169+ data . push ( {
170+ groupId : record . subject . groupIds [ 0 ] ?? DEFAULT_GROUP_NAME ,
171+ instrumentEdition : instrument . internal . edition ,
172+ instrumentName : instrument . internal . name ,
173+ measure : arrayEntry . measure ,
174+ sessionDate : record . session . date . toISOString ( ) ,
175+ sessionId : record . session . id ,
176+ sessionType : record . session . type ,
177+ subjectAge : record . subject . dateOfBirth ? yearsPassed ( record . subject . dateOfBirth ) : null ,
178+ subjectId : record . subject . id ,
179+ subjectSex : record . subject . sex ,
180+ timestamp : record . date . toISOString ( ) ,
181+ value : arrayEntry . measureValue
182+ } ) ;
188183 } ) ;
184+ } catch ( e ) {
185+ throw new Error ( ( e as Error ) . message ) ;
189186 }
190187 } else {
191188 data . push ( {
@@ -419,10 +416,7 @@ export class InstrumentRecordsService {
419416 for ( const [ dataKey , dataValue ] of Object . entries ( objectEntry as { [ key : string ] : any } ) ) {
420417 const parseResult = $RecordArrayFieldValue . safeParse ( dataValue ) ;
421418 if ( ! parseResult . success ) {
422- validRecordArrayList . push ( {
423- message : `Invalid data type ${ dataKey } for Record array entry at key ${ dataKey } ` ,
424- success : false
425- } ) ;
419+ throw new Error ( `Invalid data type ${ dataKey } for Record array entry at key ${ dataKey } ` ) ;
426420 }
427421 validRecordArrayList . push ( {
428422 measure : dataKey ,
0 commit comments