Skip to content

Commit

Permalink
feat(annotation-server): patch medications
Browse files Browse the repository at this point in the history
  • Loading branch information
jannis-baum committed Jul 5, 2022
1 parent 5d839e2 commit be59002
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
9 changes: 9 additions & 0 deletions annotation-server/src/common/api/bodies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiBody } from '@nestjs/swagger';

export function ApiBodyPatch(resourceName: string): MethodDecorator {
return ApiBody({
description: `An array of partial ${resourceName} entities. Note that the ID is required.`,
required: true,
isArray: true,
});
}
4 changes: 4 additions & 0 deletions annotation-server/src/common/dtos/patch-body.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { BaseEntity } from '../entities/base.entity';

export type PatchBodyDto<T extends BaseEntity> = (Pick<T, 'id'> &
Partial<Omit<T, 'id'>>)[];
2 changes: 1 addition & 1 deletion annotation-server/src/guidelines/guidelines.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class GuidelinesController {
@ApiOperation({
summary: 'Supplement matching guideline data from a Google Sheet',
})
@Patch()
@Patch('sheet')
async supplementSheetData(): Promise<void> {
return this.guidelinesService.supplementSheetData();
}
Expand Down
15 changes: 13 additions & 2 deletions annotation-server/src/medications/medications.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Controller, Get, Param, Patch, Query } from '@nestjs/common';
import { Body, Controller, Get, Param, Patch, Query } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

import { ApiBodyPatch } from '../common/api/bodies';
import { ApiParamGetById } from '../common/api/params';
import { PatchBodyDto } from '../common/dtos/patch-body.dto';
import {
ApiFindMedicationsQueries,
ApiFindMedicationQueries,
Expand All @@ -24,11 +26,20 @@ export class MedicationsController {
@ApiOperation({
summary: 'Supplement matching medication data from a Google Sheet',
})
@Patch()
@Patch('sheet')
async supplementSheetData(): Promise<void> {
return this.medicationsService.supplementSheetData();
}

@ApiOperation({ summary: 'Patch medications' })
@ApiBodyPatch('medication')
@Patch()
async patchMedications(
@Body() patch: PatchBodyDto<Medication>,
): Promise<void> {
return this.medicationsService.patch(patch);
}

@ApiOperation({ summary: 'Fetch all medications' })
@ApiFindMedicationsQueries()
@Get()
Expand Down
9 changes: 9 additions & 0 deletions annotation-server/src/medications/medications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
FindOptionsOrderValue,
} from 'typeorm';

import { PatchBodyDto } from '../common/dtos/patch-body.dto';
import { fetchSpreadsheetCells } from '../common/utils/google-sheets';
import { FetchTarget } from '../fetch-dates/fetch-date.entity';
import { FetchDatesService } from '../fetch-dates/fetch-dates.service';
Expand Down Expand Up @@ -162,6 +163,14 @@ export class MedicationsService {
return (await this.medicationRepository.count()) > 0;
}

async patch(patch: PatchBodyDto<Medication>): Promise<void> {
await Promise.all(
patch.map(({ id, ...update }) =>
this.medicationRepository.update(id, update),
),
);
}

getJSONfromZip(): Promise<string> {
const jsonPath = path.join(os.tmpdir(), 'drugbank-data.json');
const proc = getOsSpecificPyProcess(this.configService);
Expand Down
24 changes: 22 additions & 2 deletions annotation-server/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ describe('App (e2e)', () => {
describe('Add sheet data', () => {
it('should supplement medication data', async () => {
const patchResponse = await request(app.getHttpServer()).patch(
`/medications`,
`/medications/sheet`,
);
expect(patchResponse.status).toEqual(200);
}, 30000);

it('should supplement guideline data', async () => {
const patchResponse = await request(app.getHttpServer()).patch(
`/guidelines`,
`/guidelines/sheet`,
);
expect(patchResponse.status).toEqual(200);
}, 30000);
Expand Down Expand Up @@ -211,6 +211,26 @@ describe('App (e2e)', () => {
});
});

describe('Modify data', () => {
it('should patch details of one medication', async () => {
const patchResponse = await request(app.getHttpServer())
.patch('/medications/')
.send([{ id: codeineId, drugclass: 'Not a pain killer' }]);
expect(patchResponse.status).toEqual(200);
});
});

describe('Verify modified data', () => {
it('should verify details for one medication', async () => {
const getResponse = await request(app.getHttpServer()).get(
'/medications/' + codeineId,
);
expect(getResponse.status).toEqual(200);
expect(getResponse.body.drugclass).toEqual('Not a pain killer');
expect(getResponse.body.indication).toEqual('Codeine/indication');
});
});

afterAll(async () => {
await medicationService.clearAllMedicationData();
await app.close();
Expand Down

0 comments on commit be59002

Please sign in to comment.