-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a patient GET /:id route and update tests
- Loading branch information
Showing
2 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`, | ||
}); | ||
} | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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(); | ||
|