Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Patient Service and Implement CRUD operations #45

Merged
merged 6 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"env": {
"browser": true,
"es2021": true,
"node": true,
"jest": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 13,
"sourceType": "module"
},
"rules": {
}
"env": {
"browser": true,
"es2021": true,
"node": true,
"jest": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 13,
"sourceType": "module"
},
"rules": {},
"ignorePatterns": ["ecqm-content-r4-2021"]
}
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage
coverage
ecqm-content-r4-2021
7 changes: 6 additions & 1 deletion src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { returnNDJsonContent } = require('../services/ndjson.service');
const { groupSearchById, groupSearch, groupCreate, groupUpdate } = require('../services/group.service');
const { uploadTransactionOrBatchBundle } = require('../services/bundle.service');
const { generateCapabilityStatement } = require('../services/metadata.service');

const { patientSearch, patientSearchById, patientCreate, patientUpdate } = require('../services/patient.service');
// set bodyLimit to 50mb
function build(opts) {
const app = fastify({ ...opts, bodyLimit: 50 * 1024 * 1024 });
Expand All @@ -26,6 +26,11 @@ function build(opts) {
app.post('/Group', groupCreate);
app.put('/Group/:groupId', groupUpdate);
app.post('/', uploadTransactionOrBatchBundle);
app.get('/Patient/:patientId', patientSearchById);
app.get('/Patient', patientSearch);
app.post('/Patient', patientCreate);
app.put('/Patient/:patientId', patientUpdate);

return app;
}

Expand Down
5 changes: 2 additions & 3 deletions src/services/group.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const groupSearchById = async (request, reply) => {

/**
* Result of sending a GET request to [base]/Group to find all available Groups.
* Searches for a Group resource with the passed in id
* @param {Object} request the request object passed in by the user
* @param {Object} reply the response object
*/
Expand All @@ -35,7 +34,7 @@ const groupSearch = async (request, reply) => {
};

/**
* Creates an object and generates an id for it regardless of the id passed in
* Creates a Group object and generates an id for it regardless of the id passed in.
* @param {Object} request the request object passed in by the user
* @param {Object} reply the response object
*/
Expand All @@ -48,7 +47,7 @@ const groupCreate = async (request, reply) => {

/**
* Updates the Group resource with the passed in id or creates a new document if
* no document with passed id is found
* no document with passed id is found.
* @param {Object} request the request object passed in by the user
* @param {Object} reply the response object
*/
Expand Down
67 changes: 67 additions & 0 deletions src/services/patient.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const {
findResourceById,
findResourcesWithQuery,
createResource,
updateResource
} = require('../util/mongo.controller');
const { v4: uuidv4 } = require('uuid');

/**
* Result of sending a GET request to [base]/Patient/[id].
* Searches for a Patient resource with the passed in id
* @param {Object} request the request object passed in by the user
* @param {Object} reply the response object
*/
const patientSearchById = async (request, reply) => {
const result = await findResourceById(request.params.patientId, 'Patient');
if (!result) {
reply.code(404).send(new Error(`The requested patient ${request.params.patientId} was not found.`));
}
return result;
};

/**
* Result of sending a GET request to [base]/Patient to find all available Patients.
* @param {Object} request the request object passed in by the user
* @param {Object} reply the response object
*/
const patientSearch = async (request, reply) => {
const result = await findResourcesWithQuery({}, 'Patient');
if (!result.length > 0) {
reply.code(404).send(new Error('No Patient resources were found on the server'));
}
return result;
};

/**
* Creates a Patient object and generates an id for it regardless of the id passed in.
* @param {Object} request the request object passed in by the user
* @param {Object} reply the response object
*/
const patientCreate = async (request, reply) => {
const data = request.body;
data['id'] = uuidv4();
reply.code(201);
return createResource(data, 'Patient');
};

/**
* Updates the Patient resource with the passed in id or creates a new document if
* no document with passed id is found.
* @param {Object} request the request object passed in by the user
* @param {Object} reply the response object
*/
const patientUpdate = async (request, reply) => {
const data = request.body;
if (data.id !== request.params.patientId) {
reply.code(400).send(new Error('Argument id must match request body id for PUT request'));
}
return updateResource(request.params.patientId, data, 'Patient');
};

module.exports = {
patientSearchById,
patientSearch,
patientCreate,
patientUpdate
};
5 changes: 5 additions & 0 deletions test/fixtures/updatedTestPatient.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"resourceType": "Patient",
"id": "testPatient",
"gender": "female"
}
73 changes: 73 additions & 0 deletions test/services/patient.service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const build = require('../../src/server/app');
const app = build();
const { client } = require('../../src/util/mongo');
const supertest = require('supertest');
const { cleanUpDb, createTestResource } = require('../populateTestData');
const testPatient = require('../fixtures/testPatient.json');
const updatedTestPatient = require('../fixtures/updatedTestPatient.json');
const queue = require('../../src/resources/exportQueue');

const TEST_PATIENT_ID = 'testPatient';
const INVALID_PATIENT_ID = 'INVALID';

describe('CRUD operations for Patient resource', () => {
beforeEach(async () => {
await client.connect();
await app.ready();
});
test('test create returns 201', async () => {
await supertest(app.server).post('/Patient').send(testPatient).expect(201);
});

test('test searchById should return 200 when patient is in db', async () => {
await createTestResource(testPatient, 'Patient');
await supertest(app.server)
.get(`/Patient/${TEST_PATIENT_ID}`)
.expect(200)
.then(response => {
expect(response.body.id).toEqual(TEST_PATIENT_ID);
});
});

test('test searchById should return 404 when patient is not in db', async () => {
await supertest(app.server)
.get(`/Patient/${INVALID_PATIENT_ID}`)
.expect(404)
.then(response => {
expect(JSON.parse(response.text).message).toEqual('The requested patient INVALID was not found.');
});
});

test('test search should return 200 when patients are in the db', async () => {
await createTestResource(testPatient, 'Patient');
await supertest(app.server)
.get(`/Patient`)
.expect(200)
.then(response => {
expect(JSON.parse(response.text).length).toEqual(1);
});
});

test('test search should return 404 if no patients are in the db', async () => {
await supertest(app.server)
.get(`/Patient`)
.expect(404)
.then(response => {
expect(JSON.parse(response.text).message).toEqual('No Patient resources were found on the server');
});
});

test('test update returns 200 when patient is in db', async () => {
await createTestResource(testPatient, 'Patient');
await supertest(app.server).put(`/Patient/${TEST_PATIENT_ID}`).send(updatedTestPatient).expect(200);
});

test('test update returns 201 when patient is not in db', async () => {
await supertest(app.server).put(`/Patient/${TEST_PATIENT_ID}`).send(updatedTestPatient).expect(200);
});

afterEach(async () => {
await cleanUpDb();
await queue.close();
});
});
Loading