Skip to content

Commit

Permalink
Bucket ages on put #2670
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Apr 22, 2022
1 parent 68eb088 commit fefa7b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
26 changes: 15 additions & 11 deletions data-serving/data-service/src/controllers/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class InvalidParamError extends Error {}
type BatchValidationErrors = { index: number; message: string }[];

const caseFromDTO = async (receivedCase: CaseDTO) => {
const aCase = new Case(receivedCase);
const aCase = receivedCase as unknown as LeanDocument<CaseDocument>;
if (receivedCase.demographics?.ageRange) {
// won't be many age buckets, so fetch all of them.
const allBuckets = await AgeBucket.find({});
Expand All @@ -57,18 +57,21 @@ const caseFromDTO = async (receivedCase: CaseDTO) => {
}

const dtoFromCase = async (storedCase: LeanDocument<CaseDocument>) => {
const dto = storedCase as unknown as CaseDTO;
if (storedCase.demographics.ageBuckets) {
let dto = storedCase as unknown as CaseDTO;
if (storedCase.demographics.ageBuckets && storedCase.demographics.ageBuckets.length > 0) {
const ageBuckets = await Promise.all(storedCase.demographics.ageBuckets.map((bucketId) => {
return AgeBucket.findById(bucketId).lean()
}));
const minimumAge = Math.min(...(ageBuckets.map(b => b!.start)));
const maximumAge = Math.max(...(ageBuckets.map(b => b!.end)));
dto.demographics = {
...dto.demographics!,
ageRange: {
start: minimumAge,
end: maximumAge,
dto = {
...dto,
demographics: {
...dto.demographics!,
ageRange: {
start: minimumAge,
end: maximumAge,
}
}
}
}
Expand Down Expand Up @@ -375,7 +378,7 @@ export class CasesController {
try {
await this.geocode(req);
const receivedCase = req.body as CaseDTO;
let c = await caseFromDTO(receivedCase);
let c = new Case(await caseFromDTO(receivedCase));
let restrictedCases = false;
if (c.caseReference.sourceId) {
const s = await Source.find({ _id: c.caseReference.sourceId });
Expand Down Expand Up @@ -608,9 +611,10 @@ export class CasesController {
});
return;
}
c.set(req.body);
const caseDetails = await caseFromDTO(req.body);
c.set(caseDetails);
await c.save();
res.json(c);
res.json(await dtoFromCase(c));
} catch (err) {
if (err.name === 'ValidationError') {
res.status(422).json(err);
Expand Down
23 changes: 22 additions & 1 deletion data-serving/data-service/test/controllers/case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ describe('POST', () => {
// case has range 40-50, should be bucketed into 36-40, 41-45, 46-50
expect(theCase!.demographics.ageBuckets).toHaveLength(3);
})
it('GETting the PUT case should return an age range', async () => {
it('GETting the POSTed case should return an age range', async () => {
const theCase = await request(app)
.post('/api/cases')
.send(minimalRequest)
Expand Down Expand Up @@ -1391,6 +1391,27 @@ describe('PUT', () => {

expect(res.body.notes).toEqual(newNotes);
});
it('update present item with new age range should change the age buckets', async () => {
const c = new Case(minimalCase);
await c.save();

const newAgeRange = {
start: 6,
end: 7,
};
const res = await request(app)
.put(`/api/cases/${c._id}`)
.send({
...curatorMetadata,
demographics: {
ageRange: newAgeRange,
},
})
.expect('Content-Type', /json/)
.expect(200);
expect(res.body.demographics.ageRange.start).toEqual(6);
expect(res.body.demographics.ageRange.end).toEqual(10);
});
it('update present item with unknown travel locations should be 200 OK', async () => {
const c = new Case(minimalCase);
await c.save();
Expand Down

0 comments on commit fefa7b2

Please sign in to comment.