Skip to content

Commit 5b18107

Browse files
authored
Merge pull request #78 from mcode/cds-needs-context
Added context utilities to the base-MEF
2 parents 8c77023 + 1b689a5 commit 5b18107

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/helpers/contextUtils.js

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

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 { getPatientFromContext } = 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+
getPatientFromContext,
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 { getPatientFromContext } = require('../../src/helpers/contextUtils');
2+
3+
const MOCK_PATIENT_MRN = '123';
4+
5+
describe('getPatientFromContext', () => {
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 getPatientFromContext(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(getPatientFromContext(MOCK_PATIENT_MRN, {})).rejects.toThrow('Could not find a patient in context; ensure that a PatientExtractor is used earlier in your extraction configuration');
27+
});
28+
});

0 commit comments

Comments
 (0)