Skip to content

Commit f26cc8a

Browse files
authored
Merge pull request #82 from mcode/epic-staging-ext-context
Creates a getConditionsFromContext function for parsing Condition resources off Context
2 parents dcb8c72 + 3a1a2bd commit f26cc8a

File tree

3 files changed

+82
-19
lines changed

3 files changed

+82
-19
lines changed

src/helpers/contextUtils.js

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

5+
/**
6+
* Parses context a Patient resource
7+
* @param {Object} context - Context object consisting of a FHIR Bundle
8+
* @return {Object} The first Patient resource found in the bundle
9+
*/
410
function getPatientFromContext(mrn, context) {
511
logger.debug('Getting patient from context');
6-
const patientInContext = getBundleResourcesByType(context, 'Patient', {}, true);
7-
if (!patientInContext) {
12+
const patientResourceInContext = getBundleResourcesByType(context, 'Patient', {}, true);
13+
if (!patientResourceInContext) {
814
throw Error('Could not find a patient in context; ensure that a PatientExtractor is used earlier in your extraction configuration');
915
}
1016
logger.debug('Patient resource found in context.');
11-
return patientInContext;
17+
return patientResourceInContext;
1218
}
1319

14-
function getConditionEntriesFromContext(mrn, context) {
15-
logger.debug('Getting conditions from context');
16-
const conditionsInContext = getBundleEntriesByResourceType(context, 'Condition', {}, false);
17-
if (conditionsInContext.length === 0) {
20+
/**
21+
* Parses context for Condition entries, which themselves contain resources
22+
* @param {Object} context - Context object consisting of a FHIR Bundle
23+
* @return {Array} All the conditions entries found in context
24+
*/
25+
function getConditionEntriesFromContext(context) {
26+
logger.debug('Getting condition entries from context');
27+
const conditionEntriesInContext = getBundleEntriesByResourceType(context, 'Condition', {}, false);
28+
if (conditionEntriesInContext.length === 0) {
29+
throw Error('Could not find any conditions in context; ensure that a ConditionExtractor is used earlier in your extraction configuration');
30+
}
31+
logger.debug(`Condition entries found in context. Found ${conditionEntriesInContext.length} condition resources.`);
32+
return conditionEntriesInContext;
33+
}
34+
35+
/**
36+
* Parses context for Condition resources
37+
* @param {Object} context - Context object consisting of a FHIR Bundle
38+
* @return {Array} All the conditions resources found in context
39+
*/
40+
function getConditionsFromContext(context) {
41+
logger.debug('Getting condition resources from context');
42+
const conditionsResourcesInContext = getBundleResourcesByType(context, 'Condition', {}, false);
43+
if (_.isEmpty(conditionsResourcesInContext)) {
1844
throw Error('Could not find any conditions in context; ensure that a ConditionExtractor is used earlier in your extraction configuration');
1945
}
20-
logger.debug(`Condition resources found in context. Found ${conditionsInContext.length} condition resources.`);
21-
return conditionsInContext;
46+
logger.debug(`Condition resources found in context. Found ${conditionsResourcesInContext.length} condition resources.`);
47+
return conditionsResourcesInContext;
2248
}
2349

2450
/**
@@ -28,16 +54,17 @@ function getConditionEntriesFromContext(mrn, context) {
2854
*/
2955
function getEncountersFromContext(context) {
3056
logger.debug('Getting encounter resources from context');
31-
const encountersInContext = getBundleResourcesByType(context, 'Encounter');
32-
if (encountersInContext.length === 0) {
57+
const encounterResourcesInContext = getBundleResourcesByType(context, 'Encounter');
58+
if (encounterResourcesInContext.length === 0) {
3359
throw Error('Could not find any encounter resources in context; ensure that an EncounterExtractor is used earlier in your extraction configuration');
3460
}
35-
logger.debug(`Condition resources found in context. Found ${encountersInContext.length} condition resources.`);
36-
return encountersInContext;
61+
logger.debug(`Condition resources found in context. Found ${encounterResourcesInContext.length} condition resources.`);
62+
return encounterResourcesInContext;
3763
}
3864

3965
module.exports = {
4066
getConditionEntriesFromContext,
67+
getConditionsFromContext,
4168
getEncountersFromContext,
4269
getPatientFromContext,
4370
};

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: 40 additions & 5 deletions
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,7 +27,42 @@ describe('getPatientFromContext', () => {
2727
});
2828
});
2929

30-
describe('getConditionFromContext', () => {
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 Condition resource in context', () => {
54+
const conditions = getConditionsFromContext(conditionContext);
55+
expect(conditions).toContain(conditionResource);
56+
expect(conditions).toContain(conditionResource2);
57+
});
58+
59+
test('Should throw an error if there is no Condition resource in context', () => {
60+
expect(() => getConditionsFromContext({}))
61+
.toThrow('Could not find any conditions in context; ensure that a ConditionExtractor is used earlier in your extraction configuration');
62+
});
63+
});
64+
65+
describe('getConditionEntriesFromContext', () => {
3166
const conditionResource = {
3267
resourceType: 'Condition',
3368
id: 'mCODEConditionExample01',
@@ -47,8 +82,8 @@ describe('getConditionFromContext', () => {
4782
],
4883
};
4984

50-
test('Should return all Condition resources in context', () => {
51-
const conditions = getConditionEntriesFromContext(MOCK_PATIENT_MRN, conditionContext);
85+
test('Should return all Condition entries in context', () => {
86+
const conditions = getConditionEntriesFromContext(conditionContext);
5287
expect(conditions).toHaveLength(2);
5388
expect(conditions[0]).toEqual({
5489
fullUrl: 'context-url-1',
@@ -57,7 +92,7 @@ describe('getConditionFromContext', () => {
5792
});
5893

5994
test('Should throw an error if there are no conditions in context', () => {
60-
expect(() => getConditionEntriesFromContext(MOCK_PATIENT_MRN, {}))
95+
expect(() => getConditionEntriesFromContext({}))
6196
.toThrow('Could not find any conditions in context; ensure that a ConditionExtractor is used earlier in your extraction configuration');
6297
});
6398
});

0 commit comments

Comments
 (0)