Skip to content

Commit

Permalink
Fix enableWhenExpression to query against FHIR format
Browse files Browse the repository at this point in the history
  • Loading branch information
ir4y committed Sep 17, 2024
1 parent 7e13466 commit 5403edc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sdc-qrf/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
QuestionnaireResponseItemAnswer,
} from 'shared/src/contrib/aidbox';

import { fromFirstClassExtension } from './converter';
import {
AnswerValue,
FormAnswerItems,
Expand Down Expand Up @@ -502,8 +503,9 @@ function isQuestionEnabled(args: IsQuestionEnabledArgs) {

if (enableWhenExpression && enableWhenExpression.language === 'text/fhirpath') {
const expressionResult = fhirpath.evaluate(
args.context.resource,
fromFirstClassExtension(args.context.resource),
enableWhenExpression.expression!,
//TODO convert context
args.context ?? {},
)[0];

Expand Down
98 changes: 98 additions & 0 deletions sdc-qrf/tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Questionnaire as FHIRQuestionnaire } from 'fhir/r4b';

import { Questionnaire, QuestionnaireResponse } from 'shared/src/contrib/aidbox';

import { allergiesQuestionnaire } from './resources/questionnaire';
Expand All @@ -7,6 +9,7 @@ import {
mapFormToResponse,
mapResponseToForm,
removeDisabledAnswers,
toFirstClassExtension,
} from '../src';

test('Transform nested repeatable-groups from new resource to new resource', () => {
Expand Down Expand Up @@ -765,3 +768,98 @@ describe('isValueEmpty method test', () => {
expect(isValueEmpty(valueType.value)).toEqual(valueType.expect);
});
});

describe('enableWhenExpression', () => {
const fhirQuestionnaire: FHIRQuestionnaire = {
resourceType: 'Questionnaire',
status: 'active',
meta: {
profile: ['https://beda.software/beda-emr-questionnaire'],
},
item: [
{
type: 'string',
linkId: 'patient-id',
},
{
text: 'Patient',
type: 'reference',
linkId: 'patient-reference',
extension: [
{
url: 'http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-enableWhenExpression',
valueExpression: {
language: 'text/fhirpath',
expression:
"QuestionnaireResponse.repeat(item).where(linkId='patient-id').answer.valueString.empty()",
},
},
],
},
],
};
const questionnaire = toFirstClassExtension(fhirQuestionnaire);

test('Empty answer check', () => {
const qr: QuestionnaireResponse = {
resourceType: 'QuestionnaireResponse',
status: 'completed',
item: [
{
linkId: 'patient-id',
},
],
};
const formItems = mapResponseToForm(qr, questionnaire);
const enabledFormItems = getEnabledQuestions(questionnaire.item!, [], formItems, {
questionnaire,
resource: qr,
context: qr,
});
expect(enabledFormItems).toEqual([
{
linkId: 'patient-id',
type: 'string',
},
{
enableWhenExpression: {
expression:
"QuestionnaireResponse.repeat(item).where(linkId='patient-id').answer.valueString.empty()",
language: 'text/fhirpath',
},
linkId: 'patient-reference',
text: 'Patient',
type: 'reference',
},
]);
});

test('Non empty answer check', () => {
const qr: QuestionnaireResponse = {
resourceType: 'QuestionnaireResponse',
status: 'completed',
item: [
{
linkId: 'patient-id',
answer: [
{
value: { string: '123' },
},
],
},
],
};
const formItems = mapResponseToForm(qr, questionnaire);
const enabledFormItems = getEnabledQuestions(questionnaire.item!, [], formItems, {
questionnaire,
resource: qr,
context: qr,
});
expect(enabledFormItems).toEqual([
{
linkId: 'patient-id',
type: 'string',
},
]);
});
});

0 comments on commit 5403edc

Please sign in to comment.