Skip to content

Commit b09f545

Browse files
Changes as per review
LF-2100
1 parent 0d12999 commit b09f545

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

src/sdc-ig-supplements.js

+36-25
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,53 @@ engine.weight = function (coll) {
2121
const res = [];
2222

2323
const questionnaire = this.vars.questionnaire || this.processedVars.questionnaire?.data;
24-
coll.forEach((answer) => {
25-
if (answer?.data) {
26-
const valueCoding = answer.data.valueCoding;
24+
coll.forEach((item) => {
25+
if (item?.data) {
26+
const valueCoding = item.data.valueCoding;
2727
let value = valueCoding;
2828
if (!value) {
29-
const prop = Object.keys(answer.data).find(p => p.startsWith('value'));
30-
value = prop ? answer.data[prop] : null;
29+
const prop = Object.keys(item.data).find(p => p.length > 5 && p.startsWith('value'));
30+
// if we found a child value[x] property
31+
value = prop
32+
// we use it to get a score extension
33+
? item.data[prop]
34+
// otherwise, if the source item has a simple data type
35+
: item._data?.extension
36+
// we get the extension from the adjacent property starting with
37+
// an underscore
38+
? item._data
39+
// otherwise we get the extension from the source item
40+
// (e.g. 'item' is a Coding)
41+
: item.data;
3142
}
3243
const score = value?.extension?.find(checkExtUrl)?.valueDecimal;
3344
if (score !== undefined) {
3445
// if we have a score extension in the source item, use it.
3546
res.push(score);
36-
} else if (questionnaire) {
37-
if (valueCoding) {
38-
const qItem = getQItemByLinkIds(
39-
questionnaire, getLinkIds(answer.parentResNode)
40-
);
41-
const answerOption = qItem?.answerOption?.find(o =>
42-
o.valueCoding.code === valueCoding.code
43-
&& o.valueCoding.system === valueCoding.system
44-
);
45-
if (answerOption) {
46-
const score = answerOption.extension?.find(checkExtUrl)?.valueDecimal;
47-
if (score !== undefined) {
48-
// if we have a score extension for the answerOption, use it.
49-
res.push(score);
47+
} else if (valueCoding) {
48+
const linkIds = getLinkIds(item.parentResNode);
49+
if (linkIds.length) {
50+
if (questionnaire) {
51+
const qItem = getQItemByLinkIds(questionnaire, linkIds);
52+
const answerOption = qItem?.answerOption?.find(o =>
53+
o.valueCoding.code === valueCoding.code
54+
&& o.valueCoding.system === valueCoding.system
55+
);
56+
if (answerOption) {
57+
const score = answerOption.extension?.find(checkExtUrl)?.valueDecimal;
58+
if (score !== undefined) {
59+
// if we have a score extension for the answerOption, use it.
60+
res.push(score);
61+
}
62+
} else {
63+
throw new Error(
64+
'Questionnaire answerOption with this linkId was not found: ' +
65+
item.parentResNode.data.linkId + '.');
5066
}
5167
} else {
52-
throw new Error(
53-
'Questionnaire answerOption with this linkId was not found: ' +
54-
answer.parentResNode.data.linkId +
55-
'. Looking upon the underlying CodeSystem is not supported yet.');
68+
throw new Error('%questionnaire is needed but not specified.');
5669
}
5770
}
58-
} else {
59-
throw new Error('%questionnaire is needed but not specified.');
6071
}
6172
}
6273
});

test/supplements.test.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ const r4_model = require("../fhir-context/r4");
33
const _ = require("lodash");
44

55
const input = {
6-
get observation() {
6+
get observationExample1() {
7+
// Clone input file contents to avoid one test affecting another
8+
return _.cloneDeep(require("../test/resources/observation-example.json"));
9+
},
10+
get observationExample2() {
711
// Clone input file contents to avoid one test affecting another
812
return _.cloneDeep(require("../test/resources/observation-example-2.json"));
913
},
@@ -59,13 +63,29 @@ describe("supplements", () => {
5963
expect(res).toThrow('Questionnaire answerOption with this linkId was not found: /44250-9-unlinked-item.');
6064
});
6165

66+
it("should return an empty array when the Observation resource doesn't have a score", () => {
67+
const res = fhirpath.evaluate(
68+
input.observationExample1, `%context.${fnName}()`,
69+
{ scoreExt: 'http://someScoreExtension'}, r4_model
70+
);
71+
expect(res).toStrictEqual([]);
72+
});
73+
6274
it("should return the correct result when getting a score from the Observation resource", () => {
6375
const res = fhirpath.evaluate(
64-
input.observation, `%context.${fnName}()`,
76+
input.observationExample2, `%context.${fnName}()`,
6577
{ scoreExt: 'http://someScoreExtension'}, r4_model
6678
);
6779
expect(res).toStrictEqual([3]);
6880
});
81+
82+
it("should return the correct result when the source item has a score", () => {
83+
const res = fhirpath.evaluate(
84+
input.observationExample2, `%context.value.${fnName}()`,
85+
{}, r4_model
86+
);
87+
expect(res).toStrictEqual([4]);
88+
});
6989
});
7090
});
7191

0 commit comments

Comments
 (0)