Skip to content

Commit

Permalink
Set age buckets on batchUpsert #2670
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Apr 25, 2022
1 parent f7edeb4 commit 42aae47
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
11 changes: 6 additions & 5 deletions data-serving/data-service/src/controllers/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,9 @@ export class CasesController {
} = this.filterCasesBySourceRestricted(cases);
logger.info('batchUpsert: preparing bulk write');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const upsertLambda = (c: any) => {
const upsertLambda = async (c: any) => {
delete c.caseCount;
c = caseWithDenormalisedConfirmationDate(c as CaseDocument);
c = caseWithDenormalisedConfirmationDate(await caseFromDTO(c as CaseDTO));
if (
c.caseReference?.sourceId &&
c.caseReference?.sourceEntryId
Expand All @@ -564,11 +564,11 @@ export class CasesController {
}
};
const unrestrictedBulkWriteResult = await Case.bulkWrite(
unrestrictedCases.map(upsertLambda),
await Promise.all(unrestrictedCases.map(upsertLambda)),
{ ordered: false },
);
const restrictedBulkWriteResult = await RestrictedCase.bulkWrite(
restrictedCases.map(upsertLambda),
await Promise.all(restrictedCases.map(upsertLambda)),
{ ordered: false },
);
logger.info('batchUpsert: finished bulk write');
Expand All @@ -586,7 +586,8 @@ export class CasesController {
errors,
});
return;
} catch (err) {
} catch (e) {
const err = e as Error;
if (err.name === 'ValidationError') {
res.status(422).json(err);
return;
Expand Down
6 changes: 3 additions & 3 deletions data-serving/data-service/src/model/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { VariantDocument, variantSchema } from './variant';

import { ObjectId } from 'mongodb';
import _ from 'lodash';
import mongoose from 'mongoose';
import mongoose, { LeanDocument } from 'mongoose';
import { ExclusionDataDocument, exclusionDataSchema } from './exclusion-data';
import { dateFieldInfo } from './date';

Expand Down Expand Up @@ -172,14 +172,14 @@ export type CaseDocument = mongoose.Document & ICase & {

/* Denormalise the confirmation date before saving or updating any case object */

function denormaliseConfirmationDate(aCase: CaseDocument) {
function denormaliseConfirmationDate(aCase: CaseDocument | LeanDocument<CaseDocument>) {
const confirmationEvents = _.filter(aCase.events, (e) => e.name === 'confirmed');
if (confirmationEvents.length) {
aCase.confirmationDate = confirmationEvents[0].dateRange.start;
}
}

export function caseWithDenormalisedConfirmationDate(aCase: CaseDocument) {
export function caseWithDenormalisedConfirmationDate(aCase: CaseDocument | LeanDocument<CaseDocument>) {
denormaliseConfirmationDate(aCase);
return aCase;
}
Expand Down
23 changes: 23 additions & 0 deletions data-serving/data-service/test/controllers/case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '../mocks/handlers';
import fs from 'fs';
import { AgeBucket } from '../../src/model/age-bucket';
import { ObjectId } from 'mongodb';

let mongoServer: MongoMemoryServer;

Expand Down Expand Up @@ -705,6 +706,28 @@ describe('POST', () => {
expect(res.body.numCreated).toBe(1); // Case was created.
expect(res.body.numUpdated).toBe(0); // No case was updated.
});
it('batch upsert should set the age buckets', async () => {
const caseID = new ObjectId();

const res = await request(app)
.post('/api/cases/batchUpsert')
.send({
cases: [
{
_id: caseID,
...fullCase,
},
],
...curatorMetadata,
})
.expect(200);

expect(res.body.numCreated).toBe(1); // A new case was created.
expect(res.body.numUpdated).toBe(0); // No case was updated.

const updatedCaseInDb = await Case.findById(caseID);
expect(updatedCaseInDb?.demographics.ageBuckets).toHaveLength(3);
});
it('geocodes everything that is necessary', async () => {
seedFakeGeocodes('Canada', {
country: 'CA',
Expand Down

0 comments on commit 42aae47

Please sign in to comment.