Skip to content

Commit

Permalink
Allow api to return empty name fields from database
Browse files Browse the repository at this point in the history
  • Loading branch information
samau3 committed Oct 27, 2024
1 parent e647d34 commit abad3f7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
54 changes: 40 additions & 14 deletions server/routes/api/v1/patients/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,62 @@ export default async function (fastify) {

const splitQuery = patient.trim().split(' ');

let whereClase = {};
let whereClause = {};
if (splitQuery.length > 1) {
whereClase = {
whereClause = {
AND: [
{
firstName: {
contains: splitQuery[0].trim(),
mode: 'insensitive',
},
OR: [
{
firstName: {
contains: splitQuery[0].trim(),
mode: 'insensitive',
},
},
{ firstName: null },
],
},
{
lastName: { contains: splitQuery[1].trim(), mode: 'insensitive' },
OR: [
{
lastName: {
contains: splitQuery[1].trim(),
mode: 'insensitive',
},
},
{ lastName: null },
],
},
],
};
} else {
whereClase = {
whereClause = {
OR: [
{ firstName: { contains: patient.trim(), mode: 'insensitive' } },
{ lastName: { contains: patient.trim(), mode: 'insensitive' } },
{
AND: [
{
firstName: {
contains: patient.trim(),
mode: 'insensitive',
},
OR: [
{
firstName: {
contains: patient.trim(),
mode: 'insensitive',
},
},
{ firstName: null },
],
},
{
lastName: { contains: patient.trim(), mode: 'insensitive' },
OR: [
{
lastName: {
contains: patient.trim(),
mode: 'insensitive',
},
},
{ lastName: null },
],
},
],
},
Expand All @@ -99,7 +125,7 @@ export default async function (fastify) {
page,
perPage,
orderBy: [{ firstName: 'asc' }, { lastName: 'asc' }],
where: whereClase,
where: whereClause,
include: {
createdBy: true,
updatedBy: true,
Expand Down
27 changes: 27 additions & 0 deletions server/test/routes/api/v1/patients.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,33 @@ describe('/api/v1/patients', () => {
'2022-10-05T00:00:00.000Z',
);
});

it('should return a patient even if it has null values', async (t) => {
const app = await build(t);
await t.loadFixtures();
const headers = await t.authenticate('[email protected]', 'test');
let reply = await app
.inject()
.post('/api/v1/patients')
.payload({
id: '0849219e-e2c6-409b-bea4-1a229c3df805',
firstName: '',
middleName: '',
lastName: '',
gender: 'MALE',
language: 'ENGLISH',
dateOfBirth: '1990-01-01',
})
.headers(headers);

reply = await app
.inject()
.get('/api/v1/patients?patient=')
.headers(headers);

assert.deepStrictEqual(reply.statusCode, StatusCodes.OK);
assert.deepStrictEqual(JSON.parse(reply.payload).length, 4);
});
});

describe('GET /generate', () => {
Expand Down

0 comments on commit abad3f7

Please sign in to comment.