11import { yearsPassed } from '@douglasneuroinformatics/libjs' ;
22import { linearRegression } from '@douglasneuroinformatics/libstats' ;
3- import { Injectable , UnprocessableEntityException } from '@nestjs/common' ;
3+ import { Injectable , NotFoundException , UnprocessableEntityException } from '@nestjs/common' ;
44import type { ScalarInstrument } from '@opendatacapture/runtime-core' ;
55import type {
66 CreateInstrumentRecordData ,
77 InstrumentRecord ,
88 InstrumentRecordQueryParams ,
99 InstrumentRecordsExport ,
10- LinearRegressionResults
10+ LinearRegressionResults ,
11+ UploadInstrumentRecordData
1112} from '@opendatacapture/schemas/instrument-records' ;
13+ import type { SessionType } from '@opendatacapture/schemas/session' ;
14+ import type { CreateSubjectData } from '@opendatacapture/schemas/subject' ;
1215import type { InstrumentRecordModel , Prisma } from '@prisma/generated-client' ;
1316import { isNumber , pickBy } from 'lodash-es' ;
1417
@@ -19,6 +22,7 @@ import { InstrumentsService } from '@/instruments/instruments.service';
1922import { InjectModel } from '@/prisma/prisma.decorators' ;
2023import type { Model } from '@/prisma/prisma.types' ;
2124import { SessionsService } from '@/sessions/sessions.service' ;
25+ import type { CreateSubjectDto } from '@/subjects/dto/create-subject.dto' ;
2226import { SubjectsService } from '@/subjects/subjects.service' ;
2327import { VirtualizationService } from '@/virtualization/virtualization.service' ;
2428
@@ -61,7 +65,11 @@ export class InstrumentRecordsService {
6165
6266 await this . subjectsService . findById ( subjectId ) ;
6367 await this . sessionsService . findById ( sessionId ) ;
64-
68+ if ( ! instrument . validationSchema . safeParse ( data ) . success ) {
69+ throw new UnprocessableEntityException (
70+ `Data received does not pass validation schema of instrument '${ instrument . id } '`
71+ ) ;
72+ }
6573 return this . instrumentRecordModel . create ( {
6674 data : {
6775 computedMeasures : instrument . measures
@@ -243,4 +251,93 @@ export class InstrumentRecordsService {
243251 }
244252 return results ;
245253 }
254+
255+ async upload (
256+ { groupId, instrumentId, records } : UploadInstrumentRecordData ,
257+ options ?: EntityOperationOptions
258+ ) : Promise < InstrumentRecordModel [ ] > {
259+ if ( groupId ) {
260+ await this . groupsService . findById ( groupId , options ) ;
261+ }
262+
263+ const instrument = await this . instrumentsService . findById ( instrumentId ) ;
264+
265+ const createdModelsArray : InstrumentRecordModel [ ] = [ ] ;
266+ if ( instrument . kind === 'SERIES' ) {
267+ throw new UnprocessableEntityException (
268+ `Cannot create instrument record for series instrument '${ instrument . id } '`
269+ ) ;
270+ }
271+
272+ for ( const record of records ) {
273+ const { data, date, subjectId } = record ;
274+
275+ //create a new subject id in subjects service if they dont exist
276+ try {
277+ await this . subjectsService . findById ( subjectId ) ;
278+ } catch ( exception ) {
279+ if ( exception instanceof NotFoundException ) {
280+ const addedSubject : CreateSubjectDto = {
281+ id : subjectId
282+ } ;
283+ await this . subjectsService . create ( addedSubject ) ;
284+ } else {
285+ throw exception ;
286+ }
287+ }
288+
289+ let subjectInfo : CreateSubjectData = {
290+ dateOfBirth : null ,
291+ firstName : null ,
292+ id : subjectId ,
293+ lastName : null ,
294+ sex : null
295+ } ;
296+
297+ let sessionType : SessionType = 'RETROSPECTIVE' ;
298+
299+ let sessionId = (
300+ await this . sessionsService . create ( {
301+ date : date ,
302+ groupId : groupId ? groupId : null ,
303+ subjectData : subjectInfo ,
304+ type : sessionType
305+ } )
306+ ) . id ;
307+
308+ const createdModel = await this . instrumentRecordModel . create ( {
309+ data : {
310+ computedMeasures : instrument . measures
311+ ? this . instrumentMeasuresService . computeMeasures ( instrument . measures , data )
312+ : null ,
313+ data,
314+ date,
315+ group : groupId
316+ ? {
317+ connect : { id : groupId }
318+ }
319+ : undefined ,
320+ instrument : {
321+ connect : {
322+ id : instrumentId
323+ }
324+ } ,
325+ session : {
326+ connect : {
327+ id : sessionId
328+ }
329+ } ,
330+ subject : {
331+ connect : {
332+ id : subjectId
333+ }
334+ }
335+ }
336+ } ) ;
337+
338+ createdModelsArray . push ( createdModel ) ;
339+ }
340+
341+ return createdModelsArray ;
342+ }
246343}
0 commit comments