Skip to content

Commit

Permalink
Add error handling when medical data or healthcare choices ids cannot…
Browse files Browse the repository at this point in the history
… be found
  • Loading branch information
samau3 committed Aug 19, 2024
1 parent 9cd9a19 commit 6fedc76
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
35 changes: 34 additions & 1 deletion server/routes/api/v1/patients/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,27 @@ export default async function (fastify, _opts) {

// Only update the medical data if the value is truthy
for (const [key, value] of Object.entries(medicalData)) {
if (value)
if (value) {
const models = {
allergies: 'allergy',
medications: 'medication',
conditions: 'condition',
};

for (const item of value) {
const exists = await tx[models[key]].findUnique({
where: { id: item.id },
});
if (!exists)
throw fastify.httpErrors.notFound(
`${key} with ID ${item.id} does not exist in database.`,
);
}
medicalUpdates[key] = {
set: [],
connect: value.map(({ id }) => ({ id })),
};
}
}

if (Object.keys(medicalUpdates).length > 0) {
Expand All @@ -231,6 +247,23 @@ export default async function (fastify, _opts) {
}

if (healthcareChoices) {
// Validate hospital and physician IDs
const hospital = await tx.hospital.findUnique({
where: { id: healthcareChoices.hospitalId },
});
if (!hospital)
throw new fastify.httpErrors.notFound(
`Hospital with ID ${healthcareChoices.hospitalId} does not exist in database.`,
);

const physician = await tx.physician.findUnique({
where: { id: healthcareChoices.physicianId },
});
if (!physician)
throw new fastify.httpErrors.notFound(
`Physician with ID ${healthcareChoices.physicianId} does not exist in database.`,
);

await tx.patient.update({
where: {
id: patientId,
Expand Down
73 changes: 73 additions & 0 deletions server/test/routes/api/v1/patients.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,56 @@ describe('/api/v1/patients', () => {
assert.deepStrictEqual(conditions.length, 0);
});

it('should throw an error if a medical data item does not exist in the database', async (t) => {
const app = await build(t);
await t.loadFixtures();
const headers = await t.authenticate('admin.user@test.com', 'test');
let reply = await app
.inject()
.patch('/api/v1/patients/update/27963f68-ebc1-408a-8bb5-8fbe54671064')
.payload({
medicalData: {
allergies: [
{
id: '5c057fc3-15d2-40fc-b664-707d04ba66c1',
},
],
},
})
.headers(headers);
assert.deepStrictEqual(reply.statusCode, StatusCodes.NOT_FOUND);
let result = JSON.parse(reply.body);
assert.deepStrictEqual(
result.message,
'allergies with ID 5c057fc3-15d2-40fc-b664-707d04ba66c1 does not exist in database.',
);

reply = await app
.inject()
.patch('/api/v1/patients/update/27963f68-ebc1-408a-8bb5-8fbe54671064')
.payload({
medicalData: {
medications: [
{
id: '583c7775-9466-4dab-8a4d-edf1056f097f',
},
],
conditions: [
{
id: '471c8529-81fc-4129-8ca0-f1b7406ed90a',
},
],
},
})
.headers(headers);
assert.deepStrictEqual(reply.statusCode, StatusCodes.NOT_FOUND);
result = JSON.parse(reply.body);
assert.deepStrictEqual(
result.message,
'conditions with ID 471c8529-81fc-4129-8ca0-f1b7406ed90a does not exist in database.',
);
});

it('should allow ADMIN to update a patient with healthcare choices', async (t) => {
const app = await build(t);
await t.loadFixtures();
Expand Down Expand Up @@ -599,5 +649,28 @@ describe('/api/v1/patients', () => {
'bbbf7f99-36cc-40b5-a26c-cd95daae04b5',
);
});

it('should throw an error if a healthcare choices item does not exist in the database', async (t) => {
const app = await build(t);
await t.loadFixtures();
const headers = await t.authenticate('admin.user@test.com', 'test');
const reply = await app
.inject()
.patch('/api/v1/patients/update/27963f68-ebc1-408a-8bb5-8fbe54671064')
.payload({
healthcareChoices: {
hospitalId: 'a50538cd-1e10-42a3-8d6b-f9ae1e48a022',
physicianId: '1ef50c4c-92cb-4298-ab0a-ce7644513bfb',
},
})
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.NOT_FOUND);
const result = JSON.parse(reply.body);
assert.deepStrictEqual(
result.message,
'Hospital with ID a50538cd-1e10-42a3-8d6b-f9ae1e48a022 does not exist in database.',
);
});
});
});

0 comments on commit 6fedc76

Please sign in to comment.