diff --git a/server/routes/api/v1/patients/update.js b/server/routes/api/v1/patients/update.js index b5c601e1..7c038d7e 100644 --- a/server/routes/api/v1/patients/update.js +++ b/server/routes/api/v1/patients/update.js @@ -16,7 +16,7 @@ export default async function (fastify, _opts) { middleName: { type: 'string' }, lastName: { type: 'string' }, dateOfBirth: { type: 'string', format: 'date' }, - } + }, }, contactData: { type: 'object', @@ -79,7 +79,7 @@ export default async function (fastify, _opts) { [StatusCodes.OK]: { type: 'object', properties: { - id: { type: 'string'}, + id: { type: 'string' }, firstName: { type: 'string' }, middleName: { type: 'string' }, lastName: { type: 'string' }, @@ -107,19 +107,24 @@ export default async function (fastify, _opts) { }, async (request, reply) => { const { patientId } = request.params; - const { patientData, contactData, medicalData, healthcareChoices } = request.body; + const { patientData, contactData, medicalData, healthcareChoices } = + request.body; const userId = request.user.id; const updatedPatient = await fastify.prisma.$transaction(async (tx) => { if (patientData) { - const newPatientData = {} + const newPatientData = {}; + + if (patientData.firstName) + newPatientData.firstName = patientData.firstName; + if (patientData.middleName) + newPatientData.middleName = patientData.middleName; + if (patientData.lastName) + newPatientData.lastName = patientData.lastName; + if (patientData.dateOfBirth) + newPatientData.dateOfBirth = new Date(patientData.dateOfBirth); - if (patientData.firstName) newPatientData.firstName = patientData.firstName; - if (patientData.middleName) newPatientData.middleName = patientData.middleName; - if (patientData.lastName) newPatientData.lastName = patientData.lastName; - if (patientData.dateOfBirth) newPatientData.dateOfBirth = new Date(patientData.dateOfBirth); - await tx.patient.update({ where: { id: patientId, @@ -128,10 +133,12 @@ export default async function (fastify, _opts) { }); } if (contactData) { - const existingContact = (await tx.patient.findUnique({ - where: { id: patientId }, - include: { emergencyContact: true } - })).emergencyContact; + const existingContact = ( + await tx.patient.findUnique({ + where: { id: patientId }, + include: { emergencyContact: true }, + }) + ).emergencyContact; if (existingContact) { await tx.contact.update({ diff --git a/server/test/fixtures/db/Patient.yml b/server/test/fixtures/db/Patient.yml index 43f85647..627d1bc7 100644 --- a/server/test/fixtures/db/Patient.yml +++ b/server/test/fixtures/db/Patient.yml @@ -6,6 +6,6 @@ items: firstName: John middleName: A lastName: Doe - dateOfBirth: "2000-10-05T00:00:00.000Z" + dateOfBirth: '2000-10-05T00:00:00.000Z' createdBy: '@user1' - updatedBy: '@user1' \ No newline at end of file + updatedBy: '@user1' diff --git a/server/test/routes/api/v1/patients.test.js b/server/test/routes/api/v1/patients.test.js index b2a0ae5c..5c30988e 100644 --- a/server/test/routes/api/v1/patients.test.js +++ b/server/test/routes/api/v1/patients.test.js @@ -23,15 +23,12 @@ describe('/api/v1/patients', () => { const app = await build(t); await t.loadFixtures(); - let reply = await app - .inject() - .post('/api/v1/patients/register') - .payload({ - firstName: 'John', - middleName: 'A', - lastName: 'Doe', - dateOfBirth: '1990-01-01', - }); + let reply = await app.inject().post('/api/v1/patients/register').payload({ + firstName: 'John', + middleName: 'A', + lastName: 'Doe', + dateOfBirth: '1990-01-01', + }); assert.deepStrictEqual(reply.statusCode, StatusCodes.UNAUTHORIZED); @@ -143,9 +140,11 @@ describe('/api/v1/patients', () => { assert.deepStrictEqual(reply.statusCode, StatusCodes.BAD_REQUEST); const result = JSON.parse(reply.body); - assert.deepStrictEqual(result.message, "body must have required property 'firstName'"); + assert.deepStrictEqual( + result.message, + "body must have required property 'firstName'", + ); }); - }); describe('PATCH /update/:patientId', () => { @@ -196,8 +195,10 @@ describe('/api/v1/patients', () => { .headers(headers); assert.deepStrictEqual(reply.statusCode, StatusCodes.OK); - const { id, firstName, middleName, lastName, dateOfBirth } = JSON.parse(reply.body); - assert.deepStrictEqual(id, "27963f68-ebc1-408a-8bb5-8fbe54671064"); + const { id, firstName, middleName, lastName, dateOfBirth } = JSON.parse( + reply.body, + ); + assert.deepStrictEqual(id, '27963f68-ebc1-408a-8bb5-8fbe54671064'); assert.deepStrictEqual(firstName, 'Jane'); assert.deepStrictEqual(middleName, 'A'); assert.deepStrictEqual(lastName, 'Doe'); @@ -220,8 +221,10 @@ describe('/api/v1/patients', () => { .headers(headers); assert.deepStrictEqual(reply.statusCode, StatusCodes.OK); - const { id, firstName, middleName, lastName, dateOfBirth } = JSON.parse(reply.body); - assert.deepStrictEqual(id, "27963f68-ebc1-408a-8bb5-8fbe54671064"); + const { id, firstName, middleName, lastName, dateOfBirth } = JSON.parse( + reply.body, + ); + assert.deepStrictEqual(id, '27963f68-ebc1-408a-8bb5-8fbe54671064'); assert.deepStrictEqual(firstName, 'Jack'); assert.deepStrictEqual(middleName, 'A'); assert.deepStrictEqual(lastName, 'Doe'); @@ -244,8 +247,10 @@ describe('/api/v1/patients', () => { .headers(headers); assert.deepStrictEqual(reply.statusCode, StatusCodes.OK); - const { id, firstName, middleName, lastName, dateOfBirth } = JSON.parse(reply.body); - assert.deepStrictEqual(id, "27963f68-ebc1-408a-8bb5-8fbe54671064"); + const { id, firstName, middleName, lastName, dateOfBirth } = JSON.parse( + reply.body, + ); + assert.deepStrictEqual(id, '27963f68-ebc1-408a-8bb5-8fbe54671064'); assert.deepStrictEqual(firstName, 'Jill'); assert.deepStrictEqual(middleName, 'A'); assert.deepStrictEqual(lastName, 'Doe'); @@ -279,7 +284,6 @@ describe('/api/v1/patients', () => { phone: '123-456-7890', relationship: 'Mother', }); - }); it('should allow ADMIN to update a patient with medical data', async (t) => { @@ -311,16 +315,33 @@ describe('/api/v1/patients', () => { .headers(headers); assert.deepStrictEqual(reply.statusCode, StatusCodes.OK); - const { id, firstName, middleName, lastName, dateOfBirth, allergies, medications, conditions } = JSON.parse(reply.body); - assert.deepStrictEqual(id, "27963f68-ebc1-408a-8bb5-8fbe54671064"); + const { + id, + firstName, + middleName, + lastName, + dateOfBirth, + allergies, + medications, + conditions, + } = JSON.parse(reply.body); + 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(allergies[0].id, '5c057fc3-15d2-40fc-b664-707d04ba66c2'); - assert.deepStrictEqual(medications[0].id, '583c7775-9466-4dab-8a4d-edf1056f097f'); - assert.deepStrictEqual(conditions[0].id, '471c8529-81fc-4129-8ca0-f1b7406ed90c'); + assert.deepStrictEqual( + allergies[0].id, + '5c057fc3-15d2-40fc-b664-707d04ba66c2', + ); + assert.deepStrictEqual( + medications[0].id, + '583c7775-9466-4dab-8a4d-edf1056f097f', + ); + assert.deepStrictEqual( + conditions[0].id, + '471c8529-81fc-4129-8ca0-f1b7406ed90c', + ); }); - }); });