Skip to content

Commit d160326

Browse files
committed
added getConditions fn
1 parent dcb8c72 commit d160326

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/helpers/contextUtils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1+
const _ = require('lodash');
12
const logger = require('./logger');
23
const { getBundleEntriesByResourceType, getBundleResourcesByType } = require('./fhirUtils');
34

5+
6+
function getConditionsFromContext(mrn, context) {
7+
logger.debug('Getting conditions from context');
8+
const conditionsInContext = getBundleResourcesByType(context, 'Condition', {}, false);
9+
if (_.isEmpty(conditionsInContext)) {
10+
throw Error('Could not find conditions in context; ensure that a ConditionExtractor is used earlier in your extraction configuration');
11+
}
12+
logger.debug('Condition resources found in context.');
13+
return conditionsInContext;
14+
}
15+
416
function getPatientFromContext(mrn, context) {
517
logger.debug('Getting patient from context');
618
const patientInContext = getBundleResourcesByType(context, 'Patient', {}, true);
@@ -38,6 +50,7 @@ function getEncountersFromContext(context) {
3850

3951
module.exports = {
4052
getConditionEntriesFromContext,
53+
getConditionsFromContext,
4154
getEncountersFromContext,
4255
getPatientFromContext,
4356
};

src/index.js

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

6969
module.exports = {
7070
// CLI Related utilities
@@ -134,6 +134,7 @@ module.exports = {
134134
mEpochToDate,
135135
// Context operations
136136
getConditionEntriesFromContext,
137+
getConditionsFromContext,
137138
getEncountersFromContext,
138139
getPatientFromContext,
139140
};

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 { getConditionEntriesFromContext, getEncountersFromContext, getPatientFromContext } = require('../../src/helpers/contextUtils');
1+
const { getConditionEntriesFromContext, getConditionsFromContext, getEncountersFromContext, getPatientFromContext } = require('../../src/helpers/contextUtils');
22

33
const MOCK_PATIENT_MRN = '123';
44

@@ -27,6 +27,41 @@ describe('getPatientFromContext', () => {
2727
});
2828
});
2929

30+
describe('getConditionsFromContext', () => {
31+
const conditionResource = {
32+
resourceType: 'Condition',
33+
id: 'mCODEConditionExample01',
34+
};
35+
const conditionResource2 = {
36+
resourceType: 'Condition',
37+
id: 'mCODEConditionExample02',
38+
};
39+
const conditionContext = {
40+
resourceType: 'Bundle',
41+
type: 'collection',
42+
entry: [
43+
{
44+
fullUrl: 'context-url-1',
45+
resource: conditionResource,
46+
},
47+
{
48+
fullUrl: 'context-url-2',
49+
resource: conditionResource2,
50+
},
51+
],
52+
};
53+
test('Should return Patient resource in context', () => {
54+
const conditions = getConditionsFromContext(MOCK_PATIENT_MRN, conditionContext);
55+
expect(conditions).toContain(conditionResource);
56+
expect(conditions).toContain(conditionResource2);
57+
});
58+
59+
test('Should throw an error if there is no patient in context', () => {
60+
expect(() => getConditionsFromContext(MOCK_PATIENT_MRN, {}))
61+
.toThrow('Could not find conditions in context; ensure that a ConditionExtractor is used earlier in your extraction configuration');
62+
});
63+
});
64+
3065
describe('getConditionFromContext', () => {
3166
const conditionResource = {
3267
resourceType: 'Condition',

0 commit comments

Comments
 (0)