Skip to content

Commit

Permalink
Compute the age range from the buckets when returning a case #2670
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Apr 21, 2022
1 parent 9b98106 commit 59b166c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
21 changes: 20 additions & 1 deletion data-serving/data-service/src/controllers/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ const caseFromDTO = async (receivedCase: CaseDTO) => {
return aCase;
}

const dtoFromCase = async (storedCase: LeanDocument<CaseDocument>) => {
const dto = storedCase as unknown as CaseDTO;
if (storedCase.demographics.ageBuckets) {
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,
}
}
}
return dto;
}

export class CasesController {
private csvHeaders: string[];
constructor(private readonly geocoders: Geocoder[]) {
Expand Down Expand Up @@ -96,7 +115,7 @@ export class CasesController {
delete aCase.restrictedNotes;
});

res.json(c);
res.json(await Promise.all(c.map(aCase => dtoFromCase(aCase))));
};

/**
Expand Down
16 changes: 16 additions & 0 deletions data-serving/data-service/test/controllers/case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,22 @@ 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 () => {
const theCase = await request(app)
.post('/api/cases')
.send(minimalRequest)
.expect('Content-Type', /json/)
.expect(201);

const res = await request(app)
.get(`/api/cases/${theCase.body._id}`)
.expect('Content-Type', /json/)
.expect(200);
expect(res.body[0].demographics.ageRange).toEqual({
start: 36,
end: 50,
});
});
it('create many cases with valid input should return 201 OK', async () => {
const res = await request(app)
.post('/api/cases?num_cases=3')
Expand Down

0 comments on commit 59b166c

Please sign in to comment.