@@ -27,11 +27,16 @@ import { SubjectsService } from '@/subjects/subjects.service';
2727
2828import { InstrumentMeasuresService } from './instrument-measures.service' ;
2929
30- type ExpandDataType = {
31- measure : string ;
32- measureValue : boolean | Date | number | string | undefined ;
33- success : true ;
34- } ;
30+ type ExpandDataType =
31+ | {
32+ measure : string ;
33+ measureValue : boolean | Date | number | string | undefined ;
34+ success : true ;
35+ }
36+ | {
37+ success : false ;
38+ message : string ;
39+ } ;
3540
3641@Injectable ( )
3742export class InstrumentRecordsService {
@@ -161,30 +166,11 @@ export class InstrumentRecordsService {
161166 instruments . set ( record . instrumentId , instrument ) ;
162167 }
163168 for ( const [ measureKey , measureValue ] of Object . entries ( record . computedMeasures ) ) {
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- } ) ;
183- } ) ;
184- } catch ( e ) {
185- throw new Error ( ( e as Error ) . message ) ;
186- }
187- } else {
169+ if ( ! measureValue ) {
170+ continue ;
171+ }
172+
173+ if ( ! Array . isArray ( measureValue ) ) {
188174 data . push ( {
189175 groupId : record . subject . groupIds [ 0 ] ?? DEFAULT_GROUP_NAME ,
190176 instrumentEdition : instrument . internal . edition ,
@@ -200,6 +186,29 @@ export class InstrumentRecordsService {
200186 value : measureValue
201187 } ) ;
202188 }
189+
190+ if ( Array . isArray ( measureValue ) && measureValue . length < 1 ) continue ;
191+
192+ if ( Array . isArray ( measureValue ) && measureValue . length >= 1 ) {
193+ const arrayResult = this . expandData ( measureValue ) ;
194+ arrayResult . map ( ( arrayEntry : ExpandDataType ) => {
195+ if ( ! arrayEntry . success ) throw new Error ( arrayEntry . message ) ;
196+ data . push ( {
197+ groupId : record . subject . groupIds [ 0 ] ?? DEFAULT_GROUP_NAME ,
198+ instrumentEdition : instrument . internal . edition ,
199+ instrumentName : instrument . internal . name ,
200+ measure : arrayEntry . measure ,
201+ sessionDate : record . session . date . toISOString ( ) ,
202+ sessionId : record . session . id ,
203+ sessionType : record . session . type ,
204+ subjectAge : record . subject . dateOfBirth ? yearsPassed ( record . subject . dateOfBirth ) : null ,
205+ subjectId : record . subject . id ,
206+ subjectSex : record . subject . sex ,
207+ timestamp : record . date . toISOString ( ) ,
208+ value : arrayEntry . measureValue
209+ } ) ;
210+ } ) ;
211+ }
203212 }
204213 }
205214 return data ;
@@ -416,7 +425,10 @@ export class InstrumentRecordsService {
416425 for ( const [ dataKey , dataValue ] of Object . entries ( objectEntry as { [ key : string ] : any } ) ) {
417426 const parseResult = $RecordArrayFieldValue . safeParse ( dataValue ) ;
418427 if ( ! parseResult . success ) {
419- throw new Error ( `Invalid data type ${ dataKey } for Record array entry at key ${ dataKey } ` ) ;
428+ validRecordArrayList . push ( {
429+ message : `Error interpreting value ${ dataValue } and record array key ${ dataKey } ` ,
430+ success : false
431+ } ) ;
420432 }
421433 validRecordArrayList . push ( {
422434 measure : dataKey ,
0 commit comments