Skip to content

Commit

Permalink
Bug/#413 exception fail (#1131)
Browse files Browse the repository at this point in the history
* remove formatting data function

* add default properties in document for new saves

* make comment more clear

* cleanup

* add migration

---------

Co-authored-by: Ciaran Schutte <[email protected]>
  • Loading branch information
ciaranschutte and Ciaran Schutte authored Feb 9, 2024
1 parent 877d6e4 commit f16c900
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 41 deletions.
38 changes: 38 additions & 0 deletions migrations/20240209102734-413-add-default-exception-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* ensure that all property entity exceptions are defined with default values ie. []
*/
const defaults = {
follow_up: [],
treatment: [],
specimen: [],
};

module.exports = {
async up(db, client) {
try {
const exceptions = await db
.collection('entityexceptions')
.find()
.toArray();

exceptions.forEach((exception) => {
const _id = exception._id;
const update = { ...defaults, ...exception };

db.collection('entityexceptions').updateOne(
{ _id },
{
$set: update,
},
);
});
} catch (e) {
console.error('migration up failed', err);
throw err;
}
},

async down(db, client) {
// no action needed
},
};
32 changes: 1 addition & 31 deletions src/exception/property-exceptions/property-exception-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,6 @@ import {
validateRecords,
} from './validation';

/**
* creates exception object with tsv style records
* @param programId
* @param records
* @param schema
* @returns valid EntityException array
*/
const recordsToEntityException = ({
programId,
records,
schema,
}: {
programId: string;
records: ReadonlyArray<EntityExceptionRecord>;
schema: ClinicalEntitySchemaNames;
}) => {
const exception: OnlyRequired<EntityException, 'programId'> = { programId };

if (schema === ClinicalEntitySchemaNames.SPECIMEN) {
exception.specimen = records as SpecimenExceptionRecord[];
} else if (schema === ClinicalEntitySchemaNames.FOLLOW_UP) {
exception.follow_up = records as FollowUpExceptionRecord[];
} else if (schema === ClinicalEntitySchemaNames.TREATMENT) {
exception.treatment = records as TreatmentExceptionRecord[];
}

return exception;
};

/**
* normalize before schema validation
* tsv record values may contain different casing, validation needs normalized casing
Expand Down Expand Up @@ -190,8 +161,7 @@ export const createEntityException = async ({
throw new ValidationError(errors);
}

const exceptionToSave = recordsToEntityException({ programId, records, schema });
const doc = await entityExceptionRepository.save(exceptionToSave);
const doc = await entityExceptionRepository.save(programId, records, schema);
return success(doc);
};

Expand Down
28 changes: 19 additions & 9 deletions src/exception/property-exceptions/repo/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import {
BaseEntityExceptionRecord,
Entity,
EntityException,
EntityExceptionRecord,
ExceptionValue,
OnlyRequired,
} from '../types';
import { ClinicalEntitySchemaNames } from '../../../common-model/entities';

const L = loggerFor(__filename);

Expand Down Expand Up @@ -66,17 +67,26 @@ const EntityExceptionModel =
mongoose.model<EntityException>('EntityException', entityExceptionSchema);

const entityExceptionRepository = {
async save(exception: OnlyRequired<EntityException, 'programId'>): Promise<EntityException> {
L.debug(`Creating new donor exception with: ${JSON.stringify(exception)}`);
async save(
programId: string,
records: ReadonlyArray<EntityExceptionRecord>,
entity: ClinicalEntitySchemaNames,
): Promise<EntityException> {
const entities: Record<Entity, typeof records> = {
follow_up: [],
treatment: [],
specimen: [],
};
const update = { ...entities, ...{ [entity]: records } };

const update = { $set: exception };
L.debug(`Creating new donor exception for program: ${programId}, entity: ${entity}`);

try {
const doc = await EntityExceptionModel.findOneAndUpdate(
{ programId: exception.programId },
update,
{ upsert: true, new: true, returnDocument: 'after' },
).lean(true);
const doc = await EntityExceptionModel.findOneAndUpdate({ programId }, update, {
upsert: true,
new: true,
returnDocument: 'after',
}).lean(true);
return doc;
} catch (e) {
L.error('Failed to create entity exception: ', e);
Expand Down
2 changes: 1 addition & 1 deletion src/submission/exceptions/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const validateFieldValueWithExceptions = ({
.forEach((exception) => exceptions.push(exception));
break;
default:
// schema is neither specimen nor followup, do not filter for entity exceptions
// schema is NOT specimen || follow_up || treatment, do not filter for entity exceptions
break;
}

Expand Down

0 comments on commit f16c900

Please sign in to comment.