Skip to content

Commit

Permalink
Add a patient GET /:id route and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samau3 committed Sep 6, 2024
1 parent d3e7a23 commit 1c5e73b
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 0 deletions.
111 changes: 111 additions & 0 deletions server/routes/api/v1/patients/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { Role } from '../../../../models/user.js';
import { StatusCodes } from 'http-status-codes';

export default async function (fastify, _opts) {
fastify.get(
'/:id',
{
schema: {
params: {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
},
},
},
response: {
[StatusCodes.OK]: {
type: 'object',
properties: {
id: { type: 'string' },
firstName: { type: 'string' },
middleName: { type: 'string' },
lastName: { type: 'string' },
gender: { type: 'string' },
language: { type: 'string' },
dateOfBirth: { type: 'string', format: 'date' },
codeStatus: { type: 'string' },
emergencyContact: {
type: 'object',
properties: {
id: { type: 'string' },
firstName: { type: 'string' },
middleName: { type: 'string' },
lastName: { type: 'string' },
phone: { type: 'string' },
relationship: { type: 'string' },
},
},
allergies: { type: 'array' },
conditions: { type: 'array' },
medications: { type: 'array' },
hospital: {
type: 'object',
properties: {
id: { type: 'string' },
name: { type: 'string' },
address: { type: 'string' },
phone: { type: 'string' },
email: { type: 'string' },
},
},
physician: {
type: 'object',
properties: {
id: { type: 'string' },
firstName: { type: 'string' },
middleName: { type: 'string' },
lastName: { type: 'string' },
phone: { type: 'string' },
email: { type: 'string' },
},
},
updatedById: { type: 'string' },
},
},
[StatusCodes.NOT_FOUND]: {
type: 'object',
properties: {
message: { type: 'string' },
},
},
},
onRequest: fastify.requireUser([Role.ADMIN, Role.STAFF, Role.VOLUNTEER]),
},
async (request, reply) => {
const { id } = request.params;

try {
const patient = await fastify.prisma.patient.findUnique({
where: { id },
include: {
emergencyContact: true,
allergies: {
select: { allergy: true },
orderBy: { sortOrder: 'asc' },
},
medications: {
select: { medication: true },
orderBy: { sortOrder: 'asc' },
},
conditions: {
select: { condition: true },
orderBy: { sortOrder: 'asc' },
},
hospital: true,
physician: true,
},
});
if (!patient) throw new Error('Patient not found');

patient.dateOfBirth = patient.dateOfBirth.toISOString().split('T')[0];

return reply.code(StatusCodes.OK).send(patient);
} catch (error) {
return reply.status(StatusCodes.NOT_FOUND).send({
message: `Patient with ID ${id} does not exist in database.`,
});
}
},
);
}
101 changes: 101 additions & 0 deletions server/test/routes/api/v1/patients.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,64 @@ describe('/api/v1/patients', () => {
});
});

describe('GET /:id', () => {
it('should return a 403 error if not an ADMIN, STAFF or VOLUNTEER user', async (t) => {
const app = await build(t);
await t.loadFixtures();

let reply = await app
.inject()
.get('/api/v1/patients/27963f68-ebc1-408a-8bb5-8fbe54671064');
assert.deepStrictEqual(reply.statusCode, StatusCodes.UNAUTHORIZED);

let headers = await t.authenticate('[email protected]', 'test');
reply = await app
.inject()
.get('/api/v1/patients/27963f68-ebc1-408a-8bb5-8fbe54671064')
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.FORBIDDEN);
});

it('should allow ADMIN to retrieve a patient', async (t) => {
const app = await build(t);
await t.loadFixtures();
const headers = await t.authenticate('[email protected]', 'test');
const reply = await app
.inject()
.get('/api/v1/patients/27963f68-ebc1-408a-8bb5-8fbe54671064')
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.OK);
const response = JSON.parse(reply.body);
const { id, firstName, middleName, lastName, dateOfBirth } = response;

assert.deepStrictEqual(id, '27963f68-ebc1-408a-8bb5-8fbe54671064');
assert.deepStrictEqual(firstName, 'John');
assert.deepStrictEqual(middleName, 'A');
assert.deepStrictEqual(lastName, 'Doe');
assert.deepStrictEqual(dateOfBirth, '2000-10-05');
assert.deepStrictEqual(Object.keys(response).length, 22);
});

it('should throw a 404 error if a patient id does not exist', async (t) => {
const app = await build(t);
await t.loadFixtures();
const headers = await t.authenticate('[email protected]', 'test');
const reply = await app
.inject()
.get('/api/v1/patients/27963f68-ebc1-408a-8bb5-8fbe5467106a')
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.NOT_FOUND);
const result = JSON.parse(reply.body);
assert.deepStrictEqual(
result.message,
'Patient with ID 27963f68-ebc1-408a-8bb5-8fbe5467106a does not exist in database.',
);
});
});

describe('POST /', () => {
it('should return an error if not an ADMIN, STAFF or VOLUNTEER user', async (t) => {
const app = await build(t);
Expand Down Expand Up @@ -450,6 +508,49 @@ describe('/api/v1/patients', () => {
});
});

it('should trim user inputted text', async (t) => {
const app = await build(t);
await t.loadFixtures();
const headers = await t.authenticate('[email protected]', 'test');
const reply = await app
.inject()
.patch('/api/v1/patients/27963f68-ebc1-408a-8bb5-8fbe54671064')
.payload({
patientData: {
firstName: ' Jane ',
middleName: ' A ',
lastName: ' Doe ',
dateOfBirth: '1990-01-01',
language: 'RUSSIAN',
codeStatus: 'COMFORT',
},
contactData: {
firstName: ' Smith ',
lastName: 'Doe ',
phone: '(123)-456-7890',
relationship: 'Mother',
},
})
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.OK);
const { firstName, middleName, lastName, emergencyContact } = JSON.parse(
reply.body,
);
assert.deepStrictEqual(firstName, 'Jane');
assert.deepStrictEqual(middleName, 'A');
assert.deepStrictEqual(lastName, 'Doe');

assert.deepStrictEqual(emergencyContact, {
id: emergencyContact.id,
firstName: 'Smith',
middleName: '',
lastName: 'Doe',
phone: '(123)-456-7890',
relationship: 'Mother',
});
});

it('should allow ADMIN to update a patient with medical data', async (t) => {
const app = await build(t);
await t.loadFixtures();
Expand Down

0 comments on commit 1c5e73b

Please sign in to comment.