Skip to content

Commit

Permalink
Revert "Add pres-ave hook to populate case age buckets from age range #…
Browse files Browse the repository at this point in the history
…2670"

I can't use the mongoose hooks to migrate the age buckets over, as they won't run correctly in model tests.

This reverts commit ef87a6f.
  • Loading branch information
iamleeg committed Apr 25, 2022
1 parent 27439ff commit 8075002
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 168 deletions.
101 changes: 8 additions & 93 deletions data-serving/data-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions data-serving/data-service/src/controllers/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RestrictedCase,
} from '../model/case';
import { EventDocument } from '../model/event';
import { GenomeSequenceDocument } from '../model/genome-sequence';
import caseFields from '../model/fields.json';
import { Source } from '../model/source';
import {
Expand Down
2 changes: 1 addition & 1 deletion data-serving/data-service/src/model/age-bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const ageBucketSchema = new mongoose.Schema({

export type AgeBucketDocument = mongoose.Document & Range<number>;

export const AgeBucket = mongoose.model<AgeBucketDocument>(
export const AgeBuckets = mongoose.model<AgeBucketDocument>(
'AgeBuckets',
ageBucketSchema,
);
35 changes: 0 additions & 35 deletions data-serving/data-service/src/model/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import _ from 'lodash';
import mongoose from 'mongoose';
import { ExclusionDataDocument, exclusionDataSchema } from './exclusion-data';
import { dateFieldInfo } from './date';
import { AgeBucket } from './age-bucket';

const requiredDateField = {
...dateFieldInfo,
Expand Down Expand Up @@ -170,44 +169,10 @@ export function caseWithDenormalisedConfirmationDate(aCase: CaseDocument) {
return aCase;
}

/* Turn an age range in demographic info into age buckets */

async function bucketAgeRange(aCase: CaseDocument) {
// there should be a small number of these age ranges, so fetch all
const ageBuckets = await AgeBucket.find({});
if (aCase.demographics?.ageRange?.start && aCase.demographics?.ageRange?.end) {
const caseStart = aCase.demographics?.ageRange?.start;
const caseEnd = aCase.demographics?.ageRange?.end;
const matchingBuckets = ageBuckets.filter((b) => {
const bRangeWithinCaseRange = (b.start <= caseStart && b.end >= caseEnd);
const bContainsCaseStart = (b.start <= caseStart && b.end >= caseStart);
const bContainsCaseEnd = (b.start <= caseEnd && b.end >= caseEnd);
const bWithinCaseRange = (b.start >= caseStart && b.end <= caseEnd);
const matches = (
bRangeWithinCaseRange ||
bContainsCaseStart ||
bContainsCaseEnd ||
bWithinCaseRange
);
return matches;
});
aCase.demographics.ageBuckets = matchingBuckets.map((b) => (b._id));
}
}

async function caseWithBucketedAge(aCase: CaseDocument) {
await bucketAgeRange(aCase);
return aCase;
}

caseSchema.pre('save', async function(this: CaseDocument) {
denormaliseConfirmationDate(this);
});

caseSchema.pre('save', async function(this: CaseDocument) {
await bucketAgeRange(this);
});

caseSchema.pre('validate', async function(this: CaseDocument) {
denormaliseConfirmationDate(this);
});
Expand Down
2 changes: 1 addition & 1 deletion data-serving/data-service/src/model/demographics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const demographicsSchema = new mongoose.Schema(

export type DemographicsDocument = mongoose.Document & {
ageBuckets: ObjectId[];
ageRange?: Range<number>;
ageRange: Range<number>;
gender: string;
occupation: string;
nationalities: [string];
Expand Down
38 changes: 0 additions & 38 deletions data-serving/data-service/test/controllers/case.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AgeBucket } from '../../src/model/age-bucket';
import { Case, RestrictedCase } from '../../src/model/case';
import { CaseRevision } from '../../src/model/case-revision';
import { Demographics } from '../../src/model/demographics';
Expand Down Expand Up @@ -27,11 +26,6 @@ const minimalRequest = {
...curatorMetadata,
};

const fullRequest = {
...fullCase,
...curatorMetadata,
};

const invalidRequest = {
...minimalRequest,
demographics: { ageRange: { start: 400 } },
Expand All @@ -49,25 +43,10 @@ function stringParser(res: request.Response) {
});
}

async function createAgeBuckets() {
await new AgeBucket({
start: 0,
end: 0,
}).save();
for (let start = 1; start <= 116; start += 5) {
const end = start + 4;
await new AgeBucket({
start,
end,
}).save();
}
}

beforeAll(async () => {
mockLocationServer.listen();
mongoServer = new MongoMemoryServer();
global.Date.now = jest.fn(() => new Date('2020-12-12T12:12:37Z').getTime());
await createAgeBuckets();
});

beforeEach(async () => {
Expand All @@ -84,7 +63,6 @@ afterEach(() => {
afterAll(async () => {
mockLocationServer.close();
global.Date.now = realDate;
await AgeBucket.deleteMany({});
return mongoServer.stop();
});

Expand Down Expand Up @@ -538,22 +516,6 @@ describe('POST', () => {
expect(await Case.collection.countDocuments()).toEqual(0);
expect(res.body._id).not.toHaveLength(0);
});
it('create with valid input should write age buckets', async () => {
/*
* The idea is that the age bucketing done in #2670 should improve the privacy of the application
* without changing the API. You still tell us what the age range of the case is, and we map that
* onto the buckets supported in the application and store those.
*/
const res = await request(app)
.post('/api/cases')
.send(fullRequest)
.expect('Content-Type', /json/)
.expect(201);
expect(await Case.collection.countDocuments()).toEqual(1);
// case has age range 50-59, which matches our buckets 46-50, 51-55, 56-60
const theCase = await Case.findOne({});
expect(theCase?.demographics.ageBuckets.length).toEqual(3);
});
it('batch upsert with no body should return 415', () => {
return request(app).post('/api/cases/batchUpsert').expect(415);
});
Expand Down

0 comments on commit 8075002

Please sign in to comment.