Skip to content

Commit ffed1ad

Browse files
committed
Added context utilities to the base-MEF
1 parent 8c77023 commit ffed1ad

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/helpers/contextUtils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const logger = require('./logger');
2+
const { getBundleResourcesByType } = require('./fhirUtils');
3+
4+
async function getPatient(mrn, context) {
5+
const patientInContext = getBundleResourcesByType(context, 'Patient', {}, true);
6+
if (!patientInContext) {
7+
throw Error('Could not find a patient in context');
8+
}
9+
logger.debug('Patient resource found in context.');
10+
return patientInContext;
11+
}
12+
13+
module.exports = {
14+
getPatient,
15+
};

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const {
6161
} = require('./helpers/conditionUtils');
6262
const { getDiseaseStatusCode, getDiseaseStatusEvidenceCode, mEpochToDate } = require('./helpers/diseaseStatusUtils');
6363
const { formatDate, formatDateTime } = require('./helpers/dateUtils');
64+
const { getPatient } = require('./helpers/contextUtils');
6465

6566
module.exports = {
6667
// CLI Related utilities
@@ -125,4 +126,6 @@ module.exports = {
125126
isConditionCancer,
126127
logOperationOutcomeInfo,
127128
mEpochToDate,
129+
// Context operations
130+
getPatient,
128131
};

test/helpers/contextUtils.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { getPatient } = require('../../src/helpers/contextUtils');
2+
3+
const MOCK_PATIENT_MRN = '123';
4+
5+
describe('getPatient', () => {
6+
const patientResource = {
7+
resourceType: 'Patient',
8+
id: 'mCODEPatientExample01',
9+
};
10+
const patientContext = {
11+
resourceType: 'Bundle',
12+
type: 'collection',
13+
entry: [
14+
{
15+
fullUrl: 'context-url-1',
16+
resource: patientResource,
17+
},
18+
],
19+
};
20+
test('Should return Patient resource in context', async () => {
21+
const patient = await getPatient(MOCK_PATIENT_MRN, patientContext);
22+
expect(patient.id).toEqual(patientResource.id);
23+
});
24+
25+
test('Should throw an error if there is no patient in context', async () => {
26+
await expect(getPatient(MOCK_PATIENT_MRN, {})).rejects.toThrow('Could not find a patient in context');
27+
});
28+
});

0 commit comments

Comments
 (0)