@@ -11,6 +11,8 @@ import axios from 'axios';
1111import { ServicePrefetch } from '../rems-cds-hooks/resources/CdsService' ;
1212import { hydrate } from '../rems-cds-hooks/prefetch/PrefetchHydrator' ;
1313import { getServiceConnection } from './hookProxy' ;
14+ import { HookSession } from '../lib/schemas/HookSession' ;
15+ import * as env from 'env-var' ;
1416
1517export interface CardRule {
1618 links : Link [ ] ;
@@ -96,19 +98,48 @@ export async function handleHook(
9698 if ( contextRequest && contextRequest . resourceType === 'MedicationRequest' ) {
9799
98100 const forwardData = async ( hook : Hook , url : string ) => {
99- // remove the auth token before any forwarding occurs
100- delete hook . fhirAuthorization ;
101- const options = {
102- method : 'POST' ,
103- data : hook ,
104- timeout : 5000 ,
105- } ;
101+ // Store original EHR details before modifying the hook
102+ const originalFhirServer = hook . fhirServer ?. toString ( ) ;
106103
107104 try {
105+ // Create and save HookSession to MongoDB
106+ const session = await HookSession . createFromHook ( hook ) ;
107+
108+ console . log ( `\n Created HookSession in MongoDB:` ) ;
109+ console . log ( ` Session ID: ${ session . _id } ` ) ;
110+ console . log ( ` Patient: ${ session . patientId } ` ) ;
111+ console . log ( ` Hook Instance: ${ session . hookInstance } ` ) ;
112+ console . log ( ` EHR FHIR Server: ${ session . ehrFhirServer } ` ) ;
113+ console . log ( ` Authorization stored: ${ session . ehrAuthorization ?. access_token ? 'Yes' : 'No' } ` ) ;
114+
115+ // Get intermediary's FHIR base URL from environment
116+ const intermediaryFhirUrl = env . get ( 'INTERMEDIARY_FHIR_URL' ) . asString ( ) ||
117+ config . server . backendApiBase ||
118+ `http://localhost:${ config . server . port } ` ;
119+
120+ // Override the fhirServer URL to point to intermediary
121+ hook . fhirServer = new URL ( intermediaryFhirUrl ) ;
122+
123+ // Remove authorization before forwarding to REMS Admin
124+ delete hook . fhirAuthorization ;
125+
126+ console . log ( `\n Forwarding CDS Hook to REMS Admin:` ) ;
127+ console . log ( ` Original EHR: ${ originalFhirServer } ` ) ;
128+ console . log ( ` Overridden to: ${ hook . fhirServer } ` ) ;
129+ console . log ( ` REMS Admin will POST Communications to: ${ hook . fhirServer } Communication\n` ) ;
130+
131+ const options = {
132+ method : 'POST' ,
133+ data : hook ,
134+ timeout : 5000 ,
135+ } ;
136+
108137 const response = await axios ( url , options ) ;
109138 res . json ( response . data ) ;
110- } catch ( err ) {
111- console . log ( err ) ;
139+
140+ } catch ( err : any ) {
141+ console . error ( ' Error in forwardData:' , err . message ) ;
142+ console . error ( err . stack ) ;
112143 res . json ( { cards : [ ] } ) ; // Return fallback response
113144 }
114145 } ;
@@ -126,7 +157,7 @@ export async function handleHook(
126157 let serviceConnection = await getServiceConnection ( drugCode , hook . fhirServer ?. toString ( ) ) ;
127158 if ( serviceConnection ) {
128159 const url = serviceConnection . to + hook . hook ;
129- console . log ( 'rems-admin hook url : ' + url ) ;
160+ console . log ( 'REMS Admin hook URL : ' + url ) ;
130161 if ( hook . fhirAuthorization && hook . fhirServer && hook . fhirAuthorization . access_token ) {
131162 hydrate ( getFhirResource , hookPrefetch , hook ) . then ( async hydratedPrefetch => {
132163 if ( hydratedPrefetch ) {
@@ -227,15 +258,36 @@ export async function handleHook(
227258 return ;
228259 }
229260 uniqueUrls . forEach ( async ( url : string ) => {
230- // remove the auth token before any forwarding occurs
231- delete hook . fhirAuthorization ;
232-
233- const options = {
234- method : 'POST' ,
235- data : hook
236- } ;
261+ // Store original before modification
262+ const originalFhirServer = hook . fhirServer ?. toString ( ) ;
237263
238264 try {
265+ // Create and save HookSession to MongoDB
266+ const session = await HookSession . createFromHook ( hook ) ;
267+
268+ console . log ( `\n Created HookSession in MongoDB (${ hookType } ):` ) ;
269+ console . log ( ` Session ID: ${ session . _id } ` ) ;
270+ console . log ( ` Patient: ${ session . patientId } ` ) ;
271+
272+ // Get intermediary's FHIR base URL
273+ const intermediaryFhirUrl = env . get ( 'INTERMEDIARY_FHIR_URL' ) . asString ( ) ||
274+ config . server . backendApiBase ||
275+ `http://localhost:${ config . server . port } ` ;
276+
277+ // Override fhirServer to point to intermediary
278+ hook . fhirServer = new URL ( intermediaryFhirUrl ) ;
279+
280+ // Remove authorization
281+ delete hook . fhirAuthorization ;
282+
283+ console . log ( ` Original EHR: ${ originalFhirServer } ` ) ;
284+ console . log ( ` Overridden to: ${ hook . fhirServer } \n` ) ;
285+
286+ const options = {
287+ method : 'POST' ,
288+ data : hook
289+ } ;
290+
239291 const response = await axios ( url , options ) ;
240292 cards = [ ...cards , ...response . data . cards ] ;
241293
@@ -244,8 +296,8 @@ export async function handleHook(
244296 // return the final list of cards
245297 res . json ( { cards } ) ;
246298 }
247- } catch ( error ) {
248- console . error ( 'Error calling REMS Admin :' , error ) ;
299+ } catch ( error : any ) {
300+ console . error ( ' Error in processMedications :' , error . message ) ;
249301 urlCount -- ;
250302 if ( urlCount <= 0 ) {
251303 res . json ( { cards } ) ;
0 commit comments