Skip to content

Commit

Permalink
Allow reading of x-fhir-query variables in the whole questionnaire
Browse files Browse the repository at this point in the history
  • Loading branch information
fongsean committed Nov 6, 2023
1 parent 5c69b5c commit f14a059
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
* limitations under the License.
*/

import type { Questionnaire } from 'fhir/r4';
import type { Extension } from 'fhir/r4';
import type { Extension, Questionnaire, QuestionnaireItem } from 'fhir/r4';
import type {
LaunchContext,
QuestionnaireLevelXFhirQueryVariable,
Expand Down Expand Up @@ -100,14 +99,43 @@ export function isXFhirQueryVariable(
*
* @author Sean Fong
*/
export function getQuestionnaireLevelXFhirQueryVariables(
export function getXFhirQueryVariables(
questionnaire: Questionnaire
): QuestionnaireLevelXFhirQueryVariable[] {
const xFhirQueryVariables: QuestionnaireLevelXFhirQueryVariable[] = [];
if (questionnaire.extension && questionnaire.extension.length > 0) {
return questionnaire.extension.filter((extension) =>
isXFhirQueryVariable(extension)
) as QuestionnaireLevelXFhirQueryVariable[];
xFhirQueryVariables.push(
...(questionnaire.extension.filter((extension) =>
isXFhirQueryVariable(extension)
) as QuestionnaireLevelXFhirQueryVariable[])
);
}

return [];
if (questionnaire.item && questionnaire.item.length > 0) {
for (const qItem of questionnaire.item) {
xFhirQueryVariables.push(
...(getXFhirQueryVariablesRecursive(qItem) as QuestionnaireLevelXFhirQueryVariable[])
);
}
}

return xFhirQueryVariables;
}

function getXFhirQueryVariablesRecursive(qItem: QuestionnaireItem) {
let xFhirQueryVariables: Extension[] = [];

if (qItem.item) {
for (const childItem of qItem.item) {
xFhirQueryVariables = xFhirQueryVariables.concat(getXFhirQueryVariablesRecursive(childItem));
}
}

if (qItem.extension) {
xFhirQueryVariables.push(
...qItem.extension.filter((extension) => isXFhirQueryVariable(extension))
);
}

return xFhirQueryVariables;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ import type {
Questionnaire,
QuestionnaireResponse
} from 'fhir/r4';
import {
getLaunchContexts,
getQuestionnaireLevelXFhirQueryVariables,
getSourceQueries
} from './getExtensions.ts';
import { getLaunchContexts, getSourceQueries, getXFhirQueryVariables } from './getExtensions.ts';
import type { IssuesParameter, ResponseParameter } from '@aehrc/sdc-populate';
import { isInputParameters } from '@aehrc/sdc-populate';
import type Client from 'fhirclient/lib/Client';
Expand Down Expand Up @@ -56,7 +52,7 @@ export async function populateQuestionnaire(
// Get launch contexts, source queries and questionnaire-level variables
const launchContexts = getLaunchContexts(questionnaire);
const sourceQueries = getSourceQueries(questionnaire);
const questionnaireLevelVariables = getQuestionnaireLevelXFhirQueryVariables(questionnaire);
const questionnaireLevelVariables = getXFhirQueryVariables(questionnaire);

if (
launchContexts.length === 0 &&
Expand Down

0 comments on commit f14a059

Please sign in to comment.