Skip to content

Commit 8aa354d

Browse files
authored
Merge pull request #84 from mcode/add-identifier-system
Add identifier helper and update ResearchSubject template
2 parents 2882a4a + 4b911fc commit 8aa354d

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

src/helpers/fhirUtils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ function mapFHIRVersions(resource, currentVersion, targetVersion) {
9595
return resource;
9696
}
9797

98+
function firstIdentifierEntry(resource) {
99+
if (resource.identifier && resource.identifier.length > 0) {
100+
return resource.identifier[0];
101+
}
102+
return null;
103+
}
104+
98105
// Utility function to get the resources of a type from our bundle
99106
// Optionally get only the first resource of that type via 'first' parameter
100107
const getBundleResourcesByType = (bundle, type, context = {}, first = false) => {
@@ -158,10 +165,12 @@ const logOperationOutcomeInfo = (operationOutcome) => {
158165

159166
const isValidFHIR = (resource) => validator.validate('FHIR', resource);
160167

168+
161169
module.exports = {
162170
allResourcesInBundle,
163171
determineVersion,
164172
firstEntryInBundle,
173+
firstIdentifierEntry,
165174
firstResourceInBundle,
166175
getBundleEntriesByResourceType,
167176
getBundleResourcesByType,

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const { getEthnicityDisplay,
4242
const {
4343
allResourcesInBundle,
4444
firstEntryInBundle,
45+
firstIdentifierEntry,
4546
firstResourceInBundle,
4647
getBundleEntriesByResourceType,
4748
getBundleResourcesByType,
@@ -104,6 +105,7 @@ module.exports = {
104105
// FHIR and resource helpers
105106
allResourcesInBundle,
106107
firstEntryInBundle,
108+
firstIdentifierEntry,
107109
firstResourceInBundle,
108110
formatDate,
109111
formatDateTime,

src/templates/ResearchSubjectTemplate.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const { reference, identifier, identifierArr } = require('./snippets');
22

3-
function studyTemplate(trialResearchID) {
3+
function studyTemplate(trialResearchID, trialResearchSystem) {
44
return {
55
study: {
66
...identifier({
7-
system: 'http://example.com/clinicaltrialids',
7+
system: trialResearchSystem,
88
value: trialResearchID,
99
}),
1010
},
@@ -37,6 +37,7 @@ function researchSubjectTemplate({
3737
trialSubjectID,
3838
trialResearchID,
3939
patientId,
40+
trialResearchSystem = 'http://example.com/clinicaltrialids',
4041
}) {
4142
if (!(id && enrollmentStatus && trialSubjectID && trialResearchID && patientId)) {
4243
throw Error('Trying to render a ResearchStudyTemplate, but a required argument is missing; ensure that id, trialStatus, trialResearchID, clinicalSiteID are all present');
@@ -46,7 +47,7 @@ function researchSubjectTemplate({
4647
resourceType: 'ResearchSubject',
4748
id,
4849
status: enrollmentStatus,
49-
...studyTemplate(trialResearchID),
50+
...studyTemplate(trialResearchID, trialResearchSystem),
5051
...individualTemplate(patientId),
5152
...researchSubjectIdentifiersTemplate(trialSubjectID),
5253
};

test/helpers/fhirUtils.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const {
22
getQuantityUnit,
33
isBundleEmpty,
44
firstEntryInBundle,
5+
firstIdentifierEntry,
56
firstResourceInBundle,
67
allResourcesInBundle,
78
quantityCodeToUnitLookup,
@@ -43,8 +44,8 @@ describe('isBundleEmpty', () => {
4344

4445
describe('firstEntryInBundle', () => {
4546
test('Empty bundle should return undefined', () => {
47+
expect(firstEntryInBundle(emptyBundle)).toBeUndefined();
4648
});
47-
expect(firstEntryInBundle(emptyBundle)).toBeUndefined();
4849

4950
test('Bundles with entries should always return the first', () => {
5051
expect(firstEntryInBundle(bundleWithOneEntry))
@@ -54,6 +55,28 @@ describe('firstEntryInBundle', () => {
5455
});
5556
});
5657

58+
describe('firstIdentifierEntry', () => {
59+
test('Resource with no identifier returns null', () => {
60+
expect(firstIdentifierEntry({})).toBeNull();
61+
});
62+
63+
test('Resource with identifier gives first value', () => {
64+
const id1 = {
65+
system: 'example',
66+
value: 'example-value',
67+
};
68+
const id2 = {
69+
system: 'example2',
70+
value: 'example-value2',
71+
};
72+
const resourceWithIdentifier = {
73+
identifier: [id1, id2],
74+
};
75+
76+
expect(firstIdentifierEntry(resourceWithIdentifier)).toEqual(id1);
77+
});
78+
});
79+
5780
describe('firstResourceInBundle', () => {
5881
test('Empty bundle should throw an error', () => {
5982
expect(() => firstResourceInBundle(emptyBundle))

0 commit comments

Comments
 (0)