Skip to content

Commit

Permalink
Fix initialisation of answerExpressions
Browse files Browse the repository at this point in the history
  • Loading branch information
fongsean committed Aug 27, 2024
1 parent a0c31d3 commit 361dfdc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
13 changes: 11 additions & 2 deletions packages/smart-forms-renderer/src/utils/answerExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from 'fhir/r4';
import cloneDeep from 'lodash.clonedeep';
import { emptyResponse } from './emptyResource';
import { createFhirPathContext } from './fhirpath';
import { createFhirPathContext, evaluateTerminologyLinkIdVariables } from './fhirpath';

interface EvaluateInitialAnswerExpressionsParams {
initialResponse: QuestionnaireResponse;
Expand Down Expand Up @@ -66,13 +66,22 @@ export async function evaluateInitialAnswerExpressions(
...answerExpressions
};

const updatedFhirPathContext = await createFhirPathContext(
let updatedFhirPathContext = await createFhirPathContext(
initialResponse,
initialResponseItemMap,
variablesFhirPath,
existingFhirPathContext
);

// Perform initial evaluation of terminology variables
for (const linkId in variablesFhirPath) {
updatedFhirPathContext = await evaluateTerminologyLinkIdVariables(
linkId,
variablesFhirPath,
updatedFhirPathContext
);
}

for (const linkId in initialAnswerExpressions) {
const answerExpression = initialAnswerExpressions[linkId];

Expand Down
20 changes: 6 additions & 14 deletions packages/smart-forms-renderer/src/utils/fhirpath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,6 @@ export async function createFhirPathContext(
fhirPathContext
);

// Initialise fhirPathContext by using empty array as variable values
for (const linkId in variablesFhirPath) {
fhirPathContext = addEmptyLinkIdVariables(linkId, variablesFhirPath, fhirPathContext);
}

for (const linkId in variablesFhirPath) {
fhirPathContext = await evaluateTerminologyLinkIdVariables(
linkId,
variablesFhirPath,
fhirPathContext
);
}

// Add variables of items that exist in questionnaireResponseItemMap into fhirPathContext
for (const linkId in questionnaireResponseItemMap) {
// For non-repeat groups, the same linkId will have only one item
Expand All @@ -164,6 +151,11 @@ export async function createFhirPathContext(
}
}

// Items don't exist in questionnaireResponseItemMap, but we still have to add them into the fhirPathContext as empty arrays
for (const linkId in variablesFhirPath) {
fhirPathContext = addEmptyLinkIdVariables(linkId, variablesFhirPath, fhirPathContext);
}

return fhirPathContext;
}

Expand Down Expand Up @@ -235,7 +227,7 @@ export async function evaluateTerminologyLinkIdVariables(
if (variable.expression && variable.name && variable.expression.includes('%terminologies')) {
const initialResult = fhirPathContext[`${variable.name}`];
const terminologyPromises: Record<string, Promise<any[]>> = {};
if (Array.isArray(initialResult) && initialResult.length === 0) {
if (Array.isArray(initialResult)) {
terminologyPromises[variable.name] = evaluateFhirpathAsync(
{} as unknown as DomainResource,
variable.expression,
Expand Down

0 comments on commit 361dfdc

Please sign in to comment.