Skip to content

Commit 6b38c00

Browse files
committed
chore: deal with potential race condition in createSubjectIfNotFound code
1 parent 962b835 commit 6b38c00

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
LinearRegressionResults,
1212
UploadInstrumentRecordsData
1313
} from '@opendatacapture/schemas/instrument-records';
14-
import type { InstrumentRecordModel, Prisma, SessionModel } from '@prisma/generated-client';
14+
import { type InstrumentRecordModel, Prisma, type SessionModel } from '@prisma/generated-client';
1515
import { isNumber, pickBy } from 'lodash-es';
1616

1717
import { accessibleQuery } from '@/ability/ability.utils';
@@ -342,7 +342,18 @@ export class InstrumentRecordsService {
342342
const addedSubject: CreateSubjectDto = {
343343
id: subjectId
344344
};
345-
await this.subjectsService.create(addedSubject);
345+
try {
346+
await this.subjectsService.create(addedSubject);
347+
} catch (prismaError) {
348+
if (prismaError instanceof Prisma.PrismaClientKnownRequestError) {
349+
// The .code property can be accessed in a type-safe manner
350+
// eslint-disable-next-line max-depth
351+
if (prismaError.code === 'P2002') {
352+
console.error(prismaError);
353+
}
354+
}
355+
throw prismaError;
356+
}
346357
} else {
347358
throw exception;
348359
}

0 commit comments

Comments
 (0)