Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

36 track changes of case #37

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions data-serving/data-service/src/controllers/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ const fillEmpty = (caseData: any) => {
return caseData;
};

// For operations that change any of the value a new version needs to be created
const updatedRevisionMetadata = (
day0Case: CaseDocument,
curator: string,
note?: string,
) => {
return {
creationMetadata: day0Case.revisionMetadata.creationMetadata,
updateMetadata: {
curator: curator,
note: note,
},
revisionNumber: day0Case.revisionMetadata.revisionNumber + 1,
};
};

export class CasesController {
private csvHeaders: string[];
constructor(private readonly geocoders: Geocoder[]) {
Expand Down Expand Up @@ -481,7 +497,15 @@ export class CasesController {

try {
this.addGeoResolution(req);
const receivedCase = req.body as CaseDTO;
const receivedCase = {
...req.body,
revisionMetadata: {
revisionNumber: 0,
creationMetadata: {
curator: req.body.curator.email,
},
},
} as CaseDTO;

const c = fillEmpty(new Day0Case(await caseFromDTO(receivedCase)));

Expand Down Expand Up @@ -559,6 +583,11 @@ export class CasesController {
createdBy: c.curators.createdBy,
verifiedBy: verifier._id,
},
revisionMetadata: updatedRevisionMetadata(
c,
req.body.curator.email,
'Case Verification',
),
});
await c.save();
const responseCase = await Day0Case.find({
Expand Down Expand Up @@ -788,11 +817,17 @@ export class CasesController {
return;
}
const caseDetails = await caseFromDTO(req.body);
logger.info('Case details');
c.set(caseDetails);
logger.info('case set');

c.set({
...caseDetails,
revisionMetadata: updatedRevisionMetadata(
c,
req.body.curator.email,
'Case Update',
),
});
await c.save();
logger.info('Case save');

res.json(await dtoFromCase(c));
} catch (err) {
if (err instanceof Error) {
Expand Down
8 changes: 6 additions & 2 deletions data-serving/data-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ apiRouter.get('/cases', caseController.list);
apiRouter.get('/cases/symptoms', cases.listSymptoms);
apiRouter.get('/cases/placesOfTransmission', cases.listPlacesOfTransmission);
apiRouter.get('/cases/occupations', cases.listOccupations);
apiRouter.post('/cases/verify/:id(\\d+$)', caseController.verify);
apiRouter.post(
'/cases/verify/:id(\\d+$)',
createCaseRevision,
caseController.verify,
);
apiRouter.get('/cases/:id(\\d+$)', caseController.get);
apiRouter.post('/cases', caseController.create);
apiRouter.post('/cases/download', caseController.download);
Expand All @@ -141,7 +145,7 @@ apiRouter.post(
createBatchUpdateCaseRevisions,
caseController.batchUpdate,
);
apiRouter.put('/cases/:id', caseController.update);
apiRouter.put('/cases/:id', createCaseRevision, caseController.update);
apiRouter.delete(
'/cases',
batchDeleteCheckThreshold,
Expand Down
15 changes: 11 additions & 4 deletions data-serving/data-service/src/model/day0-case.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash';
import mongoose from 'mongoose';

import { CaseReferenceDocument, caseReferenceSchema } from './case-reference';
import {
demographicsAgeRange,
Expand All @@ -8,20 +9,24 @@ import {
demographicsSchema,
} from './demographics';
import { EventsDocument, EventsSchema } from './events';
import { LocationDocument, locationSchema } from './location';
import {
GenomeSequenceDocument,
genomeSequenceSchema,
} from './genome-sequence';
import { TravelHistoryDocument, travelHistorySchema } from './travel-history';
import { TransmissionDocument, transmissionSchema } from './transmission';
import { VaccineDocument, vaccineSchema } from './vaccine';
import { LocationDocument, locationSchema } from './location';
import {
PreexistingConditionsDocument,
preexistingConditionsSchema,
} from './preexisting-conditions';
import { TravelHistoryDocument, travelHistorySchema } from './travel-history';
import { TransmissionDocument, transmissionSchema } from './transmission';
import { VaccineDocument, vaccineSchema } from './vaccine';
import { CaseStatus } from '../types/enums';
import { IdCounter, COUNTER_DOCUMENT_ID } from '../model/id-counter';
import {
RevisionMetadataDocument,
revisionMetadataSchema,
} from './revision-metadata';

/*
* There are separate types for case for data storage (the mongoose document) and
Expand Down Expand Up @@ -99,6 +104,7 @@ export const caseSchema = new mongoose.Schema(
genomeSequences: genomeSequenceSchema,
vaccination: vaccineSchema,
curators: curatorsSchema,
revisionMetadata: revisionMetadataSchema,
},
{
toObject: {
Expand Down Expand Up @@ -194,6 +200,7 @@ export type ICase = {
genomeSequences: GenomeSequenceDocument;
vaccination: VaccineDocument;
curators: CuratorsDocument;
revisionMetadata: RevisionMetadataDocument;
};

export type CaseDTO = ICase & {
Expand Down
26 changes: 22 additions & 4 deletions data-serving/data-service/test/controllers/case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,26 @@ beforeAll(async () => {
minimalDay0CaseData = {
...minimalDay0CaseData,
...{ curators: { createdBy: curator._id } },
...{
revisionMetadata: {
revisionNumber: 0,
creationMetadata: {
curator: curatorUserEmail,
},
},
},
};
fullDay0CaseData = {
...fullCase,
...{ curators: { createdBy: curator._id } },
...{
revisionMetadata: {
revisionNumber: 0,
creationMetadata: {
curator: curatorUserEmail,
},
},
},
};
global.Date.now = jest.fn(() => new Date('2020-12-12T12:12:37Z').getTime());
});
Expand Down Expand Up @@ -1266,19 +1282,21 @@ describe('PUT', () => {
expect(res.body.demographics.ageRange.start).toEqual(6);
expect(res.body.demographics.ageRange.end).toEqual(10);
});
it.skip('update present item should create case revision', async () => {
it('update present item should create case revision', async () => {
const c = new Day0Case(minimalDay0CaseData);
await c.save();

const newNotes = 'abc';
const newComment = 'abc';
await request(app)
.put(`/api/cases/${c._id}`)
.send({ ...curatorMetadata, notes: newNotes })
.send({ ...curatorMetadata, comment: newComment })
.expect('Content-Type', /json/)
.expect(200);

expect(await CaseRevision.collection.countDocuments()).toEqual(1);
expect((await CaseRevision.find())[0].case).toMatchObject(c.toObject());
expect(
JSON.parse(JSON.stringify((await CaseRevision.find())[0].case)),
).toEqual(JSON.parse(JSON.stringify(c.toObject())));
Comment on lines +1297 to +1299
Copy link
Contributor

@jim-sheldon jim-sheldon Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 0th revision looks like the new case itself...should this test check for the first revision, i.e. the result of the PUT request?

Following the Mongo document versioning pattern this would mean checking the cases collection and revisions collection.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this test case we compare a case before update to the case revision that was crated during the update.
So at the beginning of this test we create a new case c. Then we send a request to update the content of this case, thus generating a new case revision based on case c. We then compare case c with case saved in case revision. The result from PUT request will contain an updated case.

The revision numbering might be a little odd. After creating case gets 0 as revision number, and after an update this number is incremented by 1.

});
it('invalid update present item should return 422', async () => {
const c = new Day0Case(minimalDay0CaseData);
Expand Down
23 changes: 23 additions & 0 deletions verification/curator-service/ui/src/api/models/Day0Case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ export interface Curators {
verifiedBy?: CuratorData;
}

export interface RevisionMetadata {
revisionNumber: number;
creationMetadata: {
curator: string;
};
updateMetadata: {
curator: string;
};
}

export interface Day0Case {
_id?: string;
caseStatus: CaseStatus | '';
Expand All @@ -190,6 +200,7 @@ export interface Day0Case {
vaccineSideEffects?: string[];
preexistingConditionsHelper?: string[];
curators?: Curators;
revisionMetadata?: RevisionMetadata;
[key: string]:
| CaseReference
| Demographics
Expand All @@ -201,6 +212,7 @@ export interface Day0Case {
| GenomeSequences
| Vaccination
| Curators
| RevisionMetadata
| string
| string[]
| number
Expand All @@ -222,6 +234,16 @@ interface VaccinationFormValues {
vaccineSideEffects?: string[];
}

interface RevisionMetadataValues {
revisionNumber: number;
creationMetadata: {
curator: string;
};
updateMetadata: {
curator: string;
};
}

// contains all the fields present in manual case entry form
export interface Day0CaseFormValues {
caseStatus: CaseStatus | '';
Expand Down Expand Up @@ -276,6 +298,7 @@ export interface Day0CaseFormValues {
preexistingConditionsHelper?: string[];
transmissionHelper?: string;
occupation?: string;
revisionMetadata?: RevisionMetadataValues;
// eslint-disable-next-line
[key: string]: any;
}
Loading