Skip to content

Commit 200cdb7

Browse files
authored
Merge pull request #993 from david-roper/upload-set-fix
2 parents a5a8edd + e33009e commit 200cdb7

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

apps/api/src/instrument-records/instrument-records.service.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { yearsPassed } from '@douglasneuroinformatics/libjs';
22
import { linearRegression } from '@douglasneuroinformatics/libstats';
33
import { Injectable, NotFoundException, UnprocessableEntityException } from '@nestjs/common';
4-
import type { ScalarInstrument } from '@opendatacapture/runtime-core';
4+
import type { Json, ScalarInstrument } from '@opendatacapture/runtime-core';
55
import type {
66
CreateInstrumentRecordData,
77
InstrumentRecord,
@@ -25,6 +25,7 @@ import { SubjectsService } from '@/subjects/subjects.service';
2525
import { VirtualizationService } from '@/virtualization/virtualization.service';
2626

2727
import { InstrumentMeasuresService } from './instrument-measures.service';
28+
import { reviver } from '@douglasneuroinformatics/libjs';
2829

2930
@Injectable()
3031
export class InstrumentRecordsService {
@@ -286,7 +287,9 @@ export class InstrumentRecordsService {
286287

287288
const sessionId = session.id;
288289

289-
if (!instrument.validationSchema.safeParse(data).success) {
290+
const revivedData: Json = JSON.parse(JSON.stringify(data), reviver);
291+
292+
if (!instrument.validationSchema.safeParse(revivedData).success) {
290293
throw new UnprocessableEntityException(
291294
`Data received for record at index '${i}' does not pass validation schema of instrument '${instrument.id}'`
292295
);
@@ -295,7 +298,7 @@ export class InstrumentRecordsService {
295298
const createdRecord = await this.instrumentRecordModel.create({
296299
data: {
297300
computedMeasures: instrument.measures
298-
? this.instrumentMeasuresService.computeMeasures(instrument.measures, data)
301+
? this.instrumentMeasuresService.computeMeasures(instrument.measures, revivedData)
299302
: null,
300303
data,
301304
date,

apps/web/src/features/upload/hooks/useUploadInstrumentRecords.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { useNotificationsStore } from '@douglasneuroinformatics/libui/hooks';
22
import type { UploadInstrumentRecordsData } from '@opendatacapture/schemas/instrument-records';
33
import { useMutation } from '@tanstack/react-query';
44
import axios from 'axios';
5+
import { replacer } from '@douglasneuroinformatics/libjs';
56

67
export function useUploadInstrumentRecords() {
78
const addNotification = useNotificationsStore((store) => store.addNotification);
89
return useMutation({
910
mutationFn: async (data: UploadInstrumentRecordsData) => {
10-
await axios.post('/v1/instrument-records/upload', data);
11+
const replacedData = JSON.parse(JSON.stringify(data, replacer));
12+
await axios.post('/v1/instrument-records/upload', replacedData);
1113
},
1214
onSuccess() {
1315
addNotification({ type: 'success' });

apps/web/src/features/upload/utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,17 @@ function generateSampleData({
362362
export function createUploadTemplateCSV(instrument: AnyUnilingualFormInstrument) {
363363
// TODO - type validationSchema as object
364364
const instrumentSchema = instrument.validationSchema as z.AnyZodObject;
365-
const shape = instrumentSchema.shape as { [key: string]: z.ZodTypeAny };
366365

367-
const columnNames = Object.keys(instrumentSchema.shape as z.AnyZodObject);
366+
let shape: { [key: string]: z.ZodTypeAny } = {};
367+
// TODO - include ZodEffect as a typename like our other types
368+
if ((instrumentSchema._def.typeName as string) === 'ZodEffects') {
369+
// TODO - find a type safe way to call this
370+
shape = instrumentSchema._def.schema._def.shape() as { [key: string]: z.ZodTypeAny };
371+
} else {
372+
shape = instrumentSchema.shape as { [key: string]: z.ZodTypeAny };
373+
}
374+
375+
const columnNames = Object.keys(shape);
368376

369377
const csvColumns = INTERNAL_HEADERS.concat(columnNames);
370378

0 commit comments

Comments
 (0)