Skip to content

Commit

Permalink
Disconnect existing contact if all fields are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
samau3 committed Sep 12, 2024
1 parent 8c48956 commit b3daa96
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
3 changes: 0 additions & 3 deletions server/routes/api/v1/patients/create.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Role } from '../../../../models/user.js';
import { StatusCodes } from 'http-status-codes';



export default async function (fastify, _opts) {
fastify.post(
'/',
Expand Down Expand Up @@ -77,7 +75,6 @@ export default async function (fastify, _opts) {
});
if (exists) {
throw new Error(

`Patient with ID ${id} already exists in database.`,
);
}
Expand Down
29 changes: 22 additions & 7 deletions server/routes/api/v1/patients/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
Expand Down
52 changes: 52 additions & 0 deletions server/test/routes/api/v1/patients.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]', '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();
Expand Down

0 comments on commit b3daa96

Please sign in to comment.