@@ -20,6 +20,7 @@ import {
2020
2121import {
2222 AnswerValue ,
23+ EvaluateFhirpath ,
2324 FHIRAnswerValue ,
2425 FormAnswerItems ,
2526 FormGroupItems ,
@@ -536,6 +537,7 @@ interface IsQuestionEnabledArgs {
536537 parentPath : string [ ] ;
537538 values : FormItems ;
538539 context : ItemContext ;
540+ evaluateFhirpath ?: EvaluateFhirpath ;
539541}
540542function isQuestionEnabled ( args : IsQuestionEnabledArgs ) {
541543 const { enableWhen, enableBehavior, enableWhenExpression, linkId } = args . qItem ;
@@ -559,6 +561,7 @@ function isQuestionEnabled(args: IsQuestionEnabledArgs) {
559561 enableWhenExpression ,
560562 args . context ,
561563 `${ linkId } .enableWhenExpression` ,
564+ args . evaluateFhirpath ,
562565 ) [ 0 ] ;
563566
564567 if ( typeof expressionResult !== 'boolean' ) {
@@ -601,13 +604,15 @@ export function removeDisabledAnswers(
601604 questionnaire : FCEQuestionnaire ,
602605 values : FormItems ,
603606 context : ItemContext ,
607+ evaluateFhirpath ?: EvaluateFhirpath ,
604608) : FormItems {
605609 return removeDisabledAnswersRecursive ( {
606610 questionnaireItems : questionnaire . item ?? [ ] ,
607611 parentPath : [ ] ,
608612 answersItems : values ,
609613 initialValues : { } ,
610614 context,
615+ evaluateFhirpath,
611616 } ) ;
612617}
613618
@@ -617,6 +622,7 @@ interface RemoveDisabledAnswersRecursiveArgs {
617622 answersItems : FormItems ;
618623 initialValues : FormItems ;
619624 context : ItemContext ;
625+ evaluateFhirpath ?: EvaluateFhirpath ;
620626}
621627function removeDisabledAnswersRecursive ( args : RemoveDisabledAnswersRecursiveArgs ) : FormItems {
622628 return args . questionnaireItems . reduce ( ( acc , questionnaireItem ) => {
@@ -635,6 +641,7 @@ function removeDisabledAnswersRecursive(args: RemoveDisabledAnswersRecursiveArgs
635641 parentPath : args . parentPath ,
636642 values,
637643 context : args . context ,
644+ evaluateFhirpath : args . evaluateFhirpath ,
638645 } )
639646 ) {
640647 return acc ;
@@ -657,6 +664,7 @@ function removeDisabledAnswersRecursive(args: RemoveDisabledAnswersRecursiveArgs
657664 answersItems : group ,
658665 initialValues : values ,
659666 context : args . context ,
667+ evaluateFhirpath : args . evaluateFhirpath ,
660668 } ) ,
661669 ) ,
662670 } ,
@@ -672,6 +680,7 @@ function removeDisabledAnswersRecursive(args: RemoveDisabledAnswersRecursiveArgs
672680 answersItems : answers . items ,
673681 initialValues : values ,
674682 context : args . context ,
683+ evaluateFhirpath : args . evaluateFhirpath ,
675684 } ) ,
676685 } ,
677686 } ;
@@ -688,6 +697,7 @@ function removeDisabledAnswersRecursive(args: RemoveDisabledAnswersRecursiveArgs
688697 answersItems : answer . items ,
689698 initialValues : { ...values , [ linkId ! ] : [ ...answersAcc , { ...answer , items : [ ] } ] } ,
690699 context : args . context ,
700+ evaluateFhirpath : args . evaluateFhirpath ,
691701 } )
692702 : { } ;
693703
@@ -702,6 +712,7 @@ export function getEnabledQuestions(
702712 parentPath : string [ ] ,
703713 values : FormItems ,
704714 context : ItemContext ,
715+ evaluateFhirpath ?: EvaluateFhirpath ,
705716) {
706717 return _ . filter ( questionnaireItems , ( qItem ) => {
707718 const { linkId } = qItem ;
@@ -711,7 +722,7 @@ export function getEnabledQuestions(
711722 return false ;
712723 }
713724
714- return isQuestionEnabled ( { qItem, parentPath, values, context } ) ;
725+ return isQuestionEnabled ( { qItem, parentPath, values, context, evaluateFhirpath } ) ;
715726 } ) ;
716727}
717728
@@ -750,18 +761,21 @@ export function resolveTemplateExpr(
750761 context : ItemContext ,
751762 path : string ,
752763 returnNullIfUnresolved ?: false ,
764+ evaluateFhirpath ?: EvaluateFhirpath ,
753765) : string ;
754766export function resolveTemplateExpr (
755767 str : string ,
756768 context : ItemContext ,
757769 path : string ,
758770 returnNullIfUnresolved : true ,
771+ evaluateFhirpath ?: EvaluateFhirpath ,
759772) : string | null ;
760773export function resolveTemplateExpr (
761774 str : string ,
762775 context : ItemContext ,
763776 path : string ,
764777 returnNullIfUnresolved : boolean = false ,
778+ evaluateFhirpath ?: EvaluateFhirpath ,
765779) : string | null {
766780 const matches = str . match ( / { { [ ^ } ] + } } / g) ;
767781
@@ -783,6 +797,7 @@ export function resolveTemplateExpr(
783797 } ,
784798 context ,
785799 path ,
800+ evaluateFhirpath ,
786801 ) ;
787802
788803 if ( resolvedVar ?. length ) {
@@ -797,10 +812,14 @@ export function resolveTemplateExpr(
797812 } , str ) ;
798813}
799814
815+ const defaultFhirpathEvaluate : EvaluateFhirpath = ( context , path , env ) =>
816+ fhirpath . evaluate ( context , path , env , fhirpathR4BModel , { async : false } ) ;
817+
800818export function parseFhirQueryExpression (
801819 expression : string ,
802820 context : ItemContext ,
803821 path : string = 'unknown' ,
822+ evaluateFhirpath ?: EvaluateFhirpath ,
804823) : [ string | undefined , Record < string , any > ] {
805824 const [ resourceType , paramsQS ] = expression . split ( '?' , 2 ) ;
806825 const searchParams = Object . fromEntries (
@@ -812,8 +831,8 @@ export function parseFhirQueryExpression(
812831 return [
813832 key ,
814833 isArray ( value )
815- ? value . map ( ( arrValue ) => resolveTemplateExpr ( arrValue ! , context , path ) )
816- : resolveTemplateExpr ( value , context , path ) ,
834+ ? value . map ( ( arrValue ) => resolveTemplateExpr ( arrValue ! , context , path , false , evaluateFhirpath ) )
835+ : resolveTemplateExpr ( value , context , path , false , evaluateFhirpath ) ,
817836 ] ;
818837 } ) ,
819838 ) ;
@@ -825,6 +844,7 @@ export function evaluateFHIRPathExpression(
825844 expression : Expression | undefined ,
826845 context : ItemContext ,
827846 path : string = 'unknown' ,
847+ evaluateFhirpath ?: EvaluateFhirpath ,
828848) {
829849 if ( ! expression ) {
830850 return [ ] ;
@@ -835,10 +855,10 @@ export function evaluateFHIRPathExpression(
835855 return [ ] ;
836856 }
837857
858+ const evaluator : EvaluateFhirpath = evaluateFhirpath ?? defaultFhirpathEvaluate ;
859+
838860 try {
839- return fhirpath . evaluate ( context . context ?? { } , expression . expression ! , context , fhirpathR4BModel , {
840- async : false ,
841- } ) ;
861+ return evaluator ( context . context ?? { } , expression . expression ! , context ) ;
842862 } catch ( err : unknown ) {
843863 throw Error ( `FHIRPath expression evaluation failure for ${ path } : ${ err } ` ) ;
844864 }
0 commit comments