11import { Parameters } from 'fhir/r4' ;
22import logger from './logger' ;
3- import { translate } from './translate' ;
3+ import { translateExpression , translateLibrary } from './translate' ;
44import { CodeService , DateTime , Executor , Library , PatientSource } from '../..' ;
55import { toParameters } from './convert' ;
66
@@ -13,12 +13,44 @@ export interface ExpressionExecution {
1313 result : unknown ;
1414}
1515
16+ export interface FhirLibraryExecution {
17+ elm : unknown ;
18+ result : {
19+ patientResults : unknown ;
20+ unfilteredResults : unknown ;
21+ } ;
22+ }
23+
24+ interface FhirPatientSourceModule {
25+ PatientSource ?: {
26+ FHIRv401 : ( options ?: { requireProfileTagging ?: boolean } ) => {
27+ loadBundles : ( bundles : unknown [ ] ) => void ;
28+ } ;
29+ } ;
30+ }
31+
32+ function getFhirPatientSource ( ) {
33+ try {
34+ const module = require ( 'cql-exec-fhir' ) as FhirPatientSourceModule ;
35+ const factory = module . PatientSource ?. FHIRv401 ;
36+ if ( typeof factory !== 'function' ) {
37+ throw new Error ( "Module does not expose PatientSource.FHIRv401()" ) ;
38+ }
39+ return factory ( ) ;
40+ } catch ( err ) {
41+ const message = err instanceof Error ? err . message : String ( err ) ;
42+ throw new Error (
43+ `FHIR execution requires the optional 'cql-exec-fhir' dependency. Install test-server dependencies when your environment allows it. Details: ${ message } `
44+ ) ;
45+ }
46+ }
47+
1648export async function executeExpression ( expression : string ) : Promise < ExpressionExecution > {
1749 const id = counter ++ ;
1850 logger . debug ( `[${ id } ] Expression: ${ expression } ` ) ;
1951
2052 // 1: Translate CQL to ELM
21- const elm = await translate ( expression , USE_TRANSLATION_SERVICE ) ;
53+ const elm = await translateExpression ( expression , USE_TRANSLATION_SERVICE ) ;
2254
2355 // 2: Execute ELM
2456 const library = new Library ( elm ) ;
@@ -32,6 +64,31 @@ export async function executeExpression(expression: string): Promise<ExpressionE
3264 return { elm, result : result . unfilteredResults . expression } ;
3365}
3466
67+ export async function executeFhirLibrary ( cql : string , bundle : unknown ) : Promise < FhirLibraryExecution > {
68+ const id = counter ++ ;
69+ logger . debug ( `[${ id } ] FHIR CQL library received` ) ;
70+
71+ const elm = await translateLibrary ( cql , USE_TRANSLATION_SERVICE ) ;
72+ const library = new Library ( elm ) ;
73+ const codeService = new CodeService ( ) ;
74+ const patientSource = getFhirPatientSource ( ) ;
75+ patientSource . loadBundles ( [ bundle ] ) ;
76+
77+ const executionDateTime = DateTime . fromJSDate ( new Date ( ) , 0 ) ;
78+ const executor = new Executor ( library , codeService ) ;
79+ const result = await executor . exec ( patientSource as never , executionDateTime ) ;
80+ logger . debug ( `[${ id } ] Patient Results: ` , result . patientResults ) ;
81+ logger . debug ( `[${ id } ] Unfiltered Results: ` , result . unfilteredResults ) ;
82+
83+ return {
84+ elm,
85+ result : {
86+ patientResults : result . patientResults ,
87+ unfilteredResults : result . unfilteredResults
88+ }
89+ } ;
90+ }
91+
3592export async function $cql ( expression : string ) : Promise < Parameters > {
3693 const execution = await executeExpression ( expression ) ;
3794
0 commit comments