-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Patient Service and Implement CRUD operations (#45)
* Added /Patient endpoint with CRUD ops and tests * Fixed comments * Make errors not appear * restore main of testPatient.json * Add delete endpoints for patients and groups, update projection, change to response to a searchset. * Got rid of double projection set.
- Loading branch information
Showing
10 changed files
with
262 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
coverage | ||
coverage | ||
ecqm-content-r4-2021 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const { createSearchsetBundle } = require('../util/bundleUtils'); | ||
const { | ||
findResourceById, | ||
findResourcesWithQuery, | ||
createResource, | ||
updateResource, | ||
removeResource | ||
} = 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 createSearchsetBundle(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'); | ||
}; | ||
|
||
/** | ||
* Deletes the Patient resource with the passed in id. Sends 404 if Patient with id passed in not found. | ||
* @param {Object} request the request object passed in by the user | ||
* @param {Object} reply the response object | ||
*/ | ||
const patientRemove = async (request, reply) => { | ||
const found = await findResourceById(request.params.patientId, 'Patient'); | ||
if (!found) { | ||
reply.code(404).send(new Error(`The requested patient ${request.params.patientId} was not found.`)); | ||
} | ||
return removeResource(request.params.patientId, 'Patient'); | ||
}; | ||
|
||
module.exports = { | ||
patientSearchById, | ||
patientSearch, | ||
patientCreate, | ||
patientUpdate, | ||
patientRemove | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"resourceType": "Patient", | ||
"id": "testPatient", | ||
"gender": "female" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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(response.body.total).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); | ||
}); | ||
|
||
test('test delete returns 200 when patient in db', async () => { | ||
await createTestResource(testPatient, 'Patient'); | ||
await supertest(app.server).delete(`/Patient/${TEST_PATIENT_ID}`).expect(200); | ||
}); | ||
|
||
test('test delete returns 404 when patient is not in db', async () => { | ||
await supertest(app.server) | ||
.delete(`/Patient/${TEST_PATIENT_ID}`) | ||
.expect(404) | ||
.then(res => { | ||
expect(JSON.parse(res.text).message).toEqual(`The requested patient ${TEST_PATIENT_ID} was not found.`); | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
await cleanUpDb(); | ||
await queue.close(); | ||
}); | ||
}); |