Skip to content

Commit 2882a4a

Browse files
authored
Merge pull request #81 from mcode/condition-context-util
Conditions from Context Util
2 parents 4be4ced + 2d49276 commit 2882a4a

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/helpers/contextUtils.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const logger = require('./logger');
2-
const { getBundleResourcesByType } = require('./fhirUtils');
2+
const { getBundleEntriesByResourceType, getBundleResourcesByType } = require('./fhirUtils');
33

44
function getPatientFromContext(mrn, context) {
55
logger.debug('Getting patient from context');
@@ -11,6 +11,17 @@ function getPatientFromContext(mrn, context) {
1111
return patientInContext;
1212
}
1313

14+
function getConditionEntriesFromContext(mrn, context) {
15+
logger.debug('Getting conditions from context');
16+
const conditionsInContext = getBundleEntriesByResourceType(context, 'Condition', {}, false);
17+
if (conditionsInContext.length === 0) {
18+
throw Error('Could not find any conditions in context; ensure that a ConditionExtractor is used earlier in your extraction configuration');
19+
}
20+
logger.debug(`Condition resources found in context. Found ${conditionsInContext.length} condition resources.`);
21+
return conditionsInContext;
22+
}
23+
1424
module.exports = {
25+
getConditionEntriesFromContext,
1526
getPatientFromContext,
1627
};

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const {
6262
} = require('./helpers/conditionUtils');
6363
const { getDiseaseStatusCode, getDiseaseStatusEvidenceCode, mEpochToDate } = require('./helpers/diseaseStatusUtils');
6464
const { formatDate, formatDateTime } = require('./helpers/dateUtils');
65-
const { getPatientFromContext } = require('./helpers/contextUtils');
65+
const { getConditionEntriesFromContext, getPatientFromContext } = require('./helpers/contextUtils');
6666

6767
module.exports = {
6868
// CLI Related utilities
@@ -129,5 +129,6 @@ module.exports = {
129129
logOperationOutcomeInfo,
130130
mEpochToDate,
131131
// Context operations
132+
getConditionEntriesFromContext,
132133
getPatientFromContext,
133134
};

test/helpers/contextUtils.test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getPatientFromContext } = require('../../src/helpers/contextUtils');
1+
const { getConditionEntriesFromContext, getPatientFromContext } = require('../../src/helpers/contextUtils');
22

33
const MOCK_PATIENT_MRN = '123';
44

@@ -26,3 +26,38 @@ describe('getPatientFromContext', () => {
2626
expect(() => getPatientFromContext(MOCK_PATIENT_MRN, {})).toThrow('Could not find a patient in context; ensure that a PatientExtractor is used earlier in your extraction configuration');
2727
});
2828
});
29+
30+
describe('getConditionFromContext', () => {
31+
const conditionResource = {
32+
resourceType: 'Condition',
33+
id: 'mCODEConditionExample01',
34+
};
35+
const conditionContext = {
36+
resourceType: 'Bundle',
37+
type: 'collection',
38+
entry: [
39+
{
40+
fullUrl: 'context-url-1',
41+
resource: conditionResource,
42+
},
43+
{
44+
fullUrl: 'context-url-2',
45+
resource: { ...conditionResource, id: 'mCODEConditionExample02' },
46+
},
47+
],
48+
};
49+
50+
test('Should return all Condition resources in context', () => {
51+
const conditions = getConditionEntriesFromContext(MOCK_PATIENT_MRN, conditionContext);
52+
expect(conditions).toHaveLength(2);
53+
expect(conditions[0]).toEqual({
54+
fullUrl: 'context-url-1',
55+
resource: conditionResource,
56+
});
57+
});
58+
59+
test('Should throw an error if there are no conditions in context', () => {
60+
expect(() => getConditionEntriesFromContext(MOCK_PATIENT_MRN, {}))
61+
.toThrow('Could not find any conditions in context; ensure that a ConditionExtractor is used earlier in your extraction configuration');
62+
});
63+
});

0 commit comments

Comments
 (0)