Skip to content

Commit

Permalink
Prevent creating a new contact if all fields are not filled out
Browse files Browse the repository at this point in the history
  • Loading branch information
samau3 committed Sep 11, 2024
1 parent 9080d0c commit abbcc2a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
41 changes: 21 additions & 20 deletions server/routes/api/v1/patients/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,9 @@ export default async function (fastify, _opts) {

for (const [key, value] of Object.entries(contactData)) {
if (value) newContactData[key] = value.trim();
if (
(key === 'middleName' && value.length === 0) ||
(key === 'email' && value.length === 0) ||
(key === 'phone' && value.length === 0)
) {
newContactData[key] = null;
}
if (value?.trim()?.length === 0) newContactData[key] = null;
if (key === 'relationship' && value === null)
newContactData[key] = value;
}

const existingContact = await tx.patient.findUnique({
Expand All @@ -250,21 +246,26 @@ export default async function (fastify, _opts) {
data: newContactData,
});
} else {
let contact = await tx.contact.create({
data: newContactData,
});
const nullFields = Object.entries(newContactData).filter(
([_, value]) => value === null,
);
if (nullFields.length !== Object.keys(newContactData).length) {
let contact = await tx.contact.create({
data: newContactData,
});

await tx.patient.update({
where: { id },
data: {
emergencyContact: {
connect: { id: contact.id },
},
updatedBy: {
connect: { id: userId },
await tx.patient.update({
where: { id },
data: {
emergencyContact: {
connect: { id: contact.id },
},
updatedBy: {
connect: { id: userId },
},
},
},
});
});
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions server/test/routes/api/v1/patients.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,30 @@ describe('/api/v1/patients', () => {
});
});

it('should not create a 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');
const 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 } = JSON.parse(reply.body);
assert.deepStrictEqual(emergencyContact, {});
});

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 abbcc2a

Please sign in to comment.