diff --git a/server/routes/api/v1/patients/create.js b/server/routes/api/v1/patients/create.js index 6d1810ee..b28bf473 100644 --- a/server/routes/api/v1/patients/create.js +++ b/server/routes/api/v1/patients/create.js @@ -1,8 +1,6 @@ import { Role } from '../../../../models/user.js'; import { StatusCodes } from 'http-status-codes'; - - export default async function (fastify, _opts) { fastify.post( '/', @@ -77,7 +75,6 @@ export default async function (fastify, _opts) { }); if (exists) { throw new Error( - `Patient with ID ${id} already exists in database.`, ); } diff --git a/server/routes/api/v1/patients/update.js b/server/routes/api/v1/patients/update.js index c983580c..be979f3d 100644 --- a/server/routes/api/v1/patients/update.js +++ b/server/routes/api/v1/patients/update.js @@ -233,6 +233,10 @@ export default async function (fastify, _opts) { newContactData[key] = value; } + const nullFields = Object.entries(newContactData).filter( + ([_, value]) => value === null, + ); + const existingContact = await tx.patient.findUnique({ where: { id }, include: { @@ -241,14 +245,25 @@ export default async function (fastify, _opts) { }); if (existingContact.emergencyContact) { - await tx.contact.update({ - where: { id: existingContact.emergencyContact.id }, - data: newContactData, - }); + if (nullFields.length !== Object.keys(newContactData).length) { + await tx.contact.update({ + where: { id: existingContact.emergencyContact.id }, + data: newContactData, + }); + } else { + await tx.patient.update({ + where: { id }, + data: { + emergencyContact: { + disconnect: true, + }, + updatedBy: { + connect: { id: userId }, + }, + }, + }); + } } else { - const nullFields = Object.entries(newContactData).filter( - ([_, value]) => value === null, - ); if (nullFields.length !== Object.keys(newContactData).length) { let contact = await tx.contact.create({ data: newContactData, diff --git a/server/test/routes/api/v1/patients.test.js b/server/test/routes/api/v1/patients.test.js index ca15c1f5..9da3f908 100644 --- a/server/test/routes/api/v1/patients.test.js +++ b/server/test/routes/api/v1/patients.test.js @@ -627,6 +627,58 @@ describe('/api/v1/patients', () => { assert.deepStrictEqual(emergencyContact, {}); }); + it('should disconnect an existing contact if all fields are empty strings and relationship is null', 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/27963f68-ebc1-408a-8bb5-8fbe54671064') + .payload({ + contactData: { + firstName: 'newName', + middleName: '', + lastName: '', + email: '', + phone: '', + relationship: null, + }, + }) + .headers(headers); + + assert.deepStrictEqual(reply.statusCode, StatusCodes.OK); + const { emergencyContact } = JSON.parse(reply.body); + assert.deepStrictEqual(emergencyContact, { + ...emergencyContact, + id: emergencyContact.id, + firstName: 'newName', + middleName: '', + lastName: '', + email: '', + phone: '', + relationship: '', + }); + + reply = await app + .inject() + .patch('/api/v1/patients/27963f68-ebc1-408a-8bb5-8fbe54671064') + .payload({ + contactData: { + firstName: '', + middleName: '', + lastName: '', + email: '', + phone: '', + relationship: null, + }, + }) + .headers(headers); + + assert.deepStrictEqual(reply.statusCode, StatusCodes.OK); + const { emergencyContact: updatedContact } = JSON.parse(reply.body); + assert.deepStrictEqual(updatedContact, {}); + }); + it('should allow ADMIN to update a patient with medical data', async (t) => { const app = await build(t); await t.loadFixtures();