-
Notifications
You must be signed in to change notification settings - Fork 6
/
fhirConversionTools.js
387 lines (360 loc) · 15.1 KB
/
fhirConversionTools.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* Converts an object representing parsed FHIR Questionnaire JSON to the format expected by SurveyJS.
* See: https://surveyjs.io/Documentation/Library#objects
* @param {object} fhirJson - Object resulting from parsing the JSON of a FHIR Questionnaire resource
* @returns {object} surveyJson - The converted Object
*/
export function convertFromFhir(fhirJson, resolver=()=>{}) {
// Make sure this a Questionnaire resource
if (fhirJson.resourceType != 'Questionnaire') {
throw new Error('Only FHIR Questionnaire resources are supported.');
}
// Check to see whether this conforms to the SDC Questionnaire profile
let sdcRegExp = /^http:\/\/hl7\.org\/fhir\/uv\/sdc\/StructureDefinition\/sdc-questionnaire/;
// eslint-disable-next-line no-unused-vars
if (fhirJson.meta && fhirJson.meta.profile) {
let usingSdcProfile = fhirJson.meta.profile.map(profile => sdcRegExp.test(profile)).reduce((a,b) => a || b);
}
// Determine the entry mode of the Questionnaire
let entModRegExp = /^http:\/\/hl7\.org\/fhir\/uv\/sdc\/StructureDefinition\/sdc-questionnaire-entryMode/;
let entModExt = fhirJson.extension ? fhirJson.extension.filter(ext => entModRegExp.test(ext.url))[0] : null;
if (entModExt) {
if (!['sequential', 'prior-edit', 'random'].includes(entModExt.valueCode)) {
throw new Error('sdc-questionnaire-entryMode extension does not specify a supported entry mode.');
}
} else { // Default to 'prior-edit'
entModExt = {
valueCode: 'prior-edit'
};
}
// TODO: REPLACE REFERENCES TO CONTAINED RESOURCES WITH COPIES OF THE ACTUAL RESOURCES
// Initialize our converted SurveyJS JSON
let surveyJson = {};
// We assume that only one question is shown at a time, per the supported entry mode (prior-edit)
if (entModExt.valueCode === 'random') {
surveyJson.questions = []; // each `item` in FHIR Questionnaire corresponds to a `question` in SurveyJS
} else { // 'sequential' or 'prior-edit'
surveyJson.pages = []; // each `item` in FHIR Questionnaire corresponds to a `page` in SurveyJS
if (entModExt.valueCode === 'sequential') {
// with a 'sequential' edit mode you can't change answers or go back
surveyJson.goNextPageAutomatic = true;
surveyJson.showNavigationButtons = false;
}
}
surveyJson.calculatedValues = []; // calculatedValues are top-level elements in SurveyJS
// Loop over each of the items in the Questionnaire and convert them
fhirJson.item.forEach(item => {
let {converted, calculatedValues} = convertItem(item, resolver); // Recursive function call
if (entModExt.valueCode === 'random') {
// Wrap each converted item in an object, one question per page, and add it to `pages` array
surveyJson.questions.push(Object.assign({}, converted));
} else { // 'sequential' or 'prior-edit'
// Wrap each converted item in an object, one question per page, and add it to `pages` array
surveyJson.pages.push({questions: [Object.assign({}, converted)]});
}
if (Object.keys(calculatedValues).length > 0) {
surveyJson.calculatedValues.push(calculatedValues);
}
});
// Make sure everything is flat, otherwise things will be missed by SurveyJS.
surveyJson.calculatedValues = surveyJson.calculatedValues.flat(10);
return surveyJson;
}
/**
* Converts a single item element from a FHIR Questionnaire into a format acceptable to SurveyJS.
* @param {object} item - An item element from a FHIR Questionnaire
* @returns {object} Contains the item formatted for SurveyJS as well as any calculated value expressions.
*/
export function convertItem(item, resolver=()=>{}) {
// Basic properties
let converted = {
name: item.linkId,
type: typeMap(item.type, item.repeats),
inputType: inputTypeMap(item.type)
};
if (item.readOnly) converted.readOnly = true;
if (item.initial) converted.defaultValue = getDefaultValue(item.initial);
// Get the item's title, which may be calculated by an expression
let extendedText = getTextExtensions(item._text); // Note: https://www.hl7.org/fhir/json.html#primitive
let calculatedValues = [];
if (extendedText.calculated.length > 0) {
converted.title = "";
extendedText.calculated.forEach(ext => { // See: getTextExtensions()
converted.title = converted.title + ' ' + '{' + Object.keys(ext)[0] + '}';
calculatedValues.push({
name: Object.keys(ext)[0],
expression: Object.values(ext)[0]
});
});
} else {
converted.title = item.text;
}
// Consider HTML formatted text
if (extendedText.html) converted.html = extendedText.html;
// Consider visibility conditions
let visibility = extractVisibility(item);
if (visibility.conditions) converted.visibleIf = visibility.conditions;
if (Object.keys(visibility.expressions).length > 0) {
// element.calculatedValues contains referenced calculated value expressions, which must be
// bubbled up to the top of the converted JSON
calculatedValues.push(visibility.expressions);
}
if (item.required) {
if (visibility.conditions) {
converted.requiredIf = visibility.conditions;
} else {
converted.isRequired = true;
}
}
// Extract the answers for this item
let choices = extractAnswers(item, resolver);
if (choices) converted.choices = choices;
// Extract any expressions used in place for the response
let calcRegExp = /^http:\/\/hl7\.org\/fhir\/uv\/sdc\/StructureDefinition\/sdc-questionnaire-calculatedExpression/;
let calcExts = item.extension ? item.extension.filter(ext => calcRegExp.test(ext.url)) : null;
if (calcExts) {
calcExts.forEach((ext, idx) => {
if (idx > 0) {
throw new Error('Only one calculatedExpression allowed per item.')
}
let valExp = ext.valueExpression;
if (valExp && valExp.language == 'text/cql') {
converted.type = 'expression';
converted.expression = `evaluateExpression('${valExp.expression}')`;
} else {
throw new Error('sdc-questionnaire-calculatedExpression extension does not specify a supported language.');
}
});
}
// Process child items
if (item.item) {
converted.elements = [];
item.item.forEach(itm => {
let element = convertItem(itm); // Note recursion
// element.converted contains the converted child item
converted.elements.push(element.converted);
if (Object.keys(element.calculatedValues).length > 0) {
// element.calculatedValues contains referenced calculated value expressions, which must be
// bubbled up to the top of the converted JSON
calculatedValues.push(element.calculatedValues);
}
});
}
// Need to return the converted item and the calculated values separately, since SurveyJS expects
// all calculated values to be defined at the top level in the JSON.
return {
converted: converted,
calculatedValues: calculatedValues
};
}
function getDefaultValue(initial) {
if (Array.isArray(initial) == false) initial = [initial];
return initial.map(i => i?.valueString);
}
/**
* A function for mapping FHIR Questionnaire item types to what SurveyJS expects
* @param {string} valueType - The type of the FHIR item
* @param {boolean} fhirItemRepeats - A flag that when true indicates that an item can have multiple answers
* @returns {string|Error} - The corresponding type in SurveyJS
*/
export function typeMap(valueType, fhirItemRepeats = false) {
switch(valueType) {
case 'boolean': return 'boolean';
case 'choice': return fhirItemRepeats ? 'checkbox' : 'radiogroup';
case 'date': return 'text';
case 'dateTime': return 'text';
case 'decimal': return 'text';
case 'display': return 'html';
case 'group': return 'panel';
case 'integer': return 'text';
case 'string': return 'text';
case 'text': return 'comment';
case 'time': return 'text';
case 'url': return 'text';
default:
throw new Error('Unsupported item type.');
}
}
/**
*
* @param {string} valueType
* @returns {string|null}
*/
export function inputTypeMap(valueType) {
switch(valueType) {
case 'date': return 'date';
case 'dateTime': return 'datetime-local';
case 'decimal': return 'text';
case 'integer': return 'number';
case 'string': return 'text';
case 'time': return 'time';
case 'url': return 'url';
default: null
}
}
/**
* Converts the answers from a FHIR Questionnaire item to a format suitable for SurveyJS.
* @param {object} item - A FHIR Questionnaire item
* @returns {array} answers - The answers from the item converted to SurveyJS
*/
export function extractAnswers(item, resolver=()=>{}) {
if (item.answerValueSet) {
let valueSet = resolver(item.answerValueSet);
if (valueSet && valueSet.length > 0) valueSet = valueSet[0];
else throw new Error('Answer value set could not be resolved.');
const includedAnswers = valueSet?.compose?.include ?? [{}];
const answers = includedAnswers.reduce((acc,cv) => {
const system = cv?.system ?? '';
const concept = cv?.concept ?? [];
return [
...acc,
concept.reduce((cacc,ccv) => {
let val = ccv.display;
return [
...cacc,
{
value: val,
text: String(val),
ordinalValue: 0,
valueCodingCode: ccv.code ?? '',
valueCodingSystem: system ?? '',
valueCodingDisplay: ccv?.designation?.length > 0 && ccv.designation[0].value ? ccv.designation[0].value : '',
valueType: 'coding'
}
]
},[])
];
},[]).flat();
return answers;
} else if (item.answerOption) {
let answers = [];
item.answerOption.forEach(ans => {
let val = getAnswerValue(ans);
let type = getAnswerType(ans);
let extRegExp = /^http:\/\/hl7\.org\/fhir\/StructureDefinition\/ordinalValue/;
let ordValExt = ans.extension ?
ans.extension.filter(ext => extRegExp.test(ext.url)) :
[{valueDecimal: 0}];
answers.push({
value: val,
text: String(val),
ordinalValue: ordValExt[0].valueDecimal,
valueCodingCode: ans?.valueCoding?.code ?? '',
valueCodingSystem: ans?.valueCoding?.system ?? '',
valueCodingDisplay: ans?.valueCoding?.display ?? '',
valueType: type
});
});
return answers;
} else return null;
}
/**
* Takes an item.answerOption and returns the value[x] element.
* @param {object} ans - An answer from item.answerOption
* @returns{*} - The value[x] element from item.answerOption
*/
export function getAnswerValue(ans) {
if (ans.valueInteger) return ans.valueInteger;
else if (ans.valueDate) return ans.valueDate;
else if (ans.valueTime) return ans.valueTime;
else if (ans.valueString) return ans.valueString;
else if (ans.valueCoding) {
if (ans.valueCoding.display) return ans.valueCoding.display;
else if (ans.valueCoding.code) return ans.valueCoding.code;
else throw new Error('Answer valueCoding with no display or code property.');
}
else if (ans.valueRefernce) throw new Error('valueReferences not currently supported in answers.');
else throw new Error('Unsupported value[x] in an answerOption.');
}
/**
*
* @param {*} ans
* @returns
*/
export function getAnswerType(ans) {
if (ans.valueInteger) return 'integer';
else if (ans.valueDate) return 'date';
else if (ans.valueTime) return 'time';
else if (ans.valueString) return 'string';
else if (ans.valueCoding) return 'coding';
else if (ans.valueRefernce) throw new Error('valueReferences not currently supported in answers.');
else throw new Error('Unsupported value[x] in an answerOption.');
}
/**
* Takes a FHIR.Questionnaire.item.text object and formats any extensions so they can be used by SurveyJS.
* @param {object} text - May contain various extensions for FHIR Questionnaire.item.text element
* @returns {object} extendedText - May contain markup or dynamic text to be generated by a library
*/
export function getTextExtensions(text) {
let extendedText = {
html: new String(),
calculated: []
};
if (text === undefined) return extendedText;
// First look for any mark-up
let htmlRegExp = /^http:\/\/hl7\.org\/fhir\/StructureDefinition\/rendering-xhtml/;
let htmlExts = text.extension.filter(ext => htmlRegExp.test(ext.url));
htmlExts.forEach(ext => extendedText.html = extendedText.html + ext.valueString);
// Then look for calculated values
let calcRegExp = /^http:\/\/hl7\.org\/fhir\/StructureDefinition\/cqf-calculatedValue/;
let calcExts = text.extension.filter(ext => calcRegExp.test(ext.url));
calcExts.forEach(ext => {
let key = `${ext.valueString}`;
let value = `evaluateExpression('${ext.valueString}')`;
extendedText.calculated.push({[key]: value});
});
return extendedText;
}
/**
* Checks a FHIR Questionnaire.item for any enable (visibility) conditions and converts them to SurveyJS.
* @param {object} item - A FHIR Questionnaire.item
* @returns{string} visibleIf - Visibility conditions for item which have been formatted for SurveyJS
*/
export function extractVisibility(item) {
let visibleIf = {
conditions: '',
expressions: []
};
// Visibility controlled by enableWhen expressions
let visConds = item.enableWhen;
if (visConds) {
// Ensure visConds is an array
visConds = Array.isArray(visConds) ? visConds : [visConds];
// item.enableBehavior controls how multiple enableWhen conditions are combined
let behave = item.enableBehavior ? item.enableBehavior : 'all';
let behaviorOperator = behave == 'all' ? ' and ' : ' or '; // all->and & any->or
visibleIf.conditions = visConds.map(cond => {
let condAnswerKey = Object.keys(cond).filter(key => /^answer/.test(key))[0]; // extract answer[x]
let condAnswer = condAnswerKey == 'answerCoding' ? cond[condAnswerKey].display : cond[condAnswerKey];
if (cond.operator == 'exists') return `{${cond.question}} != undefined`;
else if (cond.operator == '=') return `{${cond.question}} contains '${condAnswer}'`;
else if (cond.operator == '!=') return `{${cond.question}} notcontains '${condAnswer}'`;
else return `{${cond.question}} ${cond.operator} '${condAnswer}'`;
}).reduce((c1,c2) => {
if (!c2) return c1;
else return c1 + behaviorOperator + c2
});
}
// Visibility controlled by enableWhenExpression
let calcRegExp = /^http:\/\/hl7\.org\/fhir\/uv\/sdc\/StructureDefinition\/sdc-questionnaire-enableWhenExpression/;
let expConds = item.extension ? item.extension.filter(ext => calcRegExp.test(ext.url)) : null;
if (expConds) {
expConds.forEach((ext, idx) => { // TODO: ADD SUPPORT FOR MULTIPLE EXPRESSIONS
if (idx > 0) {
throw new Error('Only one enableWhenExpression allowed per item.')
}
let valExp = ext.valueExpression;
if (valExp && valExp.language == 'text/cql') {
let enableExp = valExp.expression;
visibleIf.conditions = '{' + enableExp + '} == true'; // TODO: ADD SUPPORT FOR COMBINING WITH ENABLEWHEN CONDITIONS
visibleIf.expressions.push({
name: `${enableExp}`,
expression: `evaluateExpression('${enableExp}')`
});
} else {
throw new Error('sdc-questionnaire-enableWhenExpression extension does not specify a supported language.');
}
});
}
return visibleIf;
}