@@ -31,24 +31,24 @@ export function buildErrorCard(reason: string) {
3131 return cards ;
3232}
3333
34- export function getDrugCodeFromMedicationRequest ( medicationRequest : MedicationRequest ) {
34+ export function getDrugCodesFromMedicationRequest ( medicationRequest : MedicationRequest ) {
3535 if ( medicationRequest ) {
3636 if ( medicationRequest ?. medicationCodeableConcept ) {
37- console . log ( 'Get Medication code from CodeableConcept' ) ;
38- return medicationRequest ?. medicationCodeableConcept ?. coding ?. [ 0 ] ;
37+ console . log ( 'Get Medication codes from CodeableConcept' ) ;
38+ return medicationRequest ?. medicationCodeableConcept ?. coding ;
3939 } else if ( medicationRequest ?. medicationReference ) {
4040 const reference = medicationRequest ?. medicationReference ;
41- let coding = null ;
41+ let codes = null ;
4242 medicationRequest ?. contained ?. every ( e => {
4343 if ( e . resourceType + '/' + e . id === reference . reference ) {
4444 if ( e . resourceType === 'Medication' ) {
4545 console . log ( 'Get Medication code from contained resource' ) ;
46- coding = e . code ?. coding ?. [ 0 ] ;
46+ codes = e . code ?. coding ;
4747 }
4848 }
4949 } ) ;
50- console . log ( 'Found code : ' + JSON . stringify ( coding ) ) ;
51- return coding ;
50+ console . log ( 'Found codes : ' + JSON . stringify ( codes ) ) ;
51+ return codes ;
5252 }
5353 }
5454 return null ;
@@ -94,7 +94,6 @@ export async function handleHook(
9494 const hookType = req ?. body ?. hook ;
9595
9696 if ( contextRequest && contextRequest . resourceType === 'MedicationRequest' ) {
97- const drugCode = getDrugCodeFromMedicationRequest ( contextRequest ) ;
9897
9998 const forwardData = ( hook : Hook , url : string ) => {
10099 // remove the auth token before any forwarding occurs
@@ -113,30 +112,48 @@ export async function handleHook(
113112 res . json ( { cards : [ ] } ) ; // Return fallback response
114113 } ) ;
115114 } ;
116- if ( drugCode ) {
117- const hook : Hook = req . body ;
118- const serviceConnection = await getServiceConnection ( drugCode , hook . fhirServer ?. toString ( ) ) ;
119- if ( serviceConnection ) {
120- const url = serviceConnection . to + hook . hook ;
121- console . log ( 'rems-admin hook url: ' + url ) ;
122- if ( hook . fhirAuthorization && hook . fhirServer && hook . fhirAuthorization . access_token ) {
123- hydrate ( getFhirResource , hookPrefetch , hook ) . then ( hydratedPrefetch => {
124- if ( hydratedPrefetch ) {
125- hook . prefetch = hydratedPrefetch ;
115+
116+ let drugCodes = getDrugCodesFromMedicationRequest ( contextRequest ) ;
117+ if ( drugCodes ) {
118+ let found = false ;
119+
120+ for ( let i = 0 ; i < ( drugCodes ?. length ? drugCodes ?. length : 0 ) ; i ++ ) {
121+ let drugCode = drugCodes ?. [ i ] ;
122+ console . log ( ' Processing drugCode: ' + JSON . stringify ( drugCode ) ) ;
123+
124+ if ( drugCode ) {
125+ const hook : Hook = req . body ;
126+ let serviceConnection = await getServiceConnection ( drugCode , hook . fhirServer ?. toString ( ) ) ;
127+ if ( serviceConnection ) {
128+ const url = serviceConnection . to + hook . hook ;
129+ console . log ( 'rems-admin hook url: ' + url ) ;
130+ if ( hook . fhirAuthorization && hook . fhirServer && hook . fhirAuthorization . access_token ) {
131+ hydrate ( getFhirResource , hookPrefetch , hook ) . then ( hydratedPrefetch => {
132+ if ( hydratedPrefetch ) {
133+ hook . prefetch = hydratedPrefetch ;
134+ }
135+ forwardData ( hook , url ) ;
136+ } ) ;
137+ } else {
138+ forwardData ( hook , url ) ;
126139 }
127- forwardData ( hook , url ) ;
128- } ) ;
129- } else {
130- forwardData ( hook , url ) ;
140+
141+ found = true ;
142+ // if medication found and works, do not continue through medications
143+ break ;
144+ }
131145 }
132- } else {
146+ }
147+
148+ if ( ! found ) {
133149 // unsupported drug code, send back empty card list
134150 res . json ( { cards : [ ] } ) ;
135151 }
136152 } else {
137153 // drug code could not be extracted
138- res . json ( createErrorCard ( 'Could not extract drug code from request' ) ) ;
154+ res . json ( createErrorCard ( 'Could not extract drug codes from request' ) ) ;
139155 }
156+
140157 } else {
141158 if ( hookType === SupportedHooks . ORDER_SELECT || hookType === SupportedHooks . ORDER_SIGN ) {
142159 // context request is not a medicationRequest
@@ -178,17 +195,24 @@ export async function handleHook(
178195 const urlList : string [ ] = [ ] ;
179196 medicationRequests ?. entry . forEach ( async bundleEntry => {
180197 if ( bundleEntry ?. resource ?. resourceType == 'MedicationRequest' ) {
181- const drugCode = getDrugCodeFromMedicationRequest ( bundleEntry ?. resource ) ;
182-
183- if ( drugCode ) {
184- console . log ( ' medication: ' + drugCode ?. display ) ;
185- const serviceConnection = await getServiceConnection (
186- drugCode ,
187- hook . fhirServer ?. toString ( )
188- ) ;
189- if ( serviceConnection ) {
190- const url = serviceConnection . to + hook . hook ;
191- urlList . push ( url ) ;
198+
199+ const drugCodes = getDrugCodesFromMedicationRequest ( bundleEntry ?. resource ) ;
200+ if ( drugCodes ) {
201+ for ( let i = 0 ; i < ( drugCodes ?. length ? drugCodes ?. length : 0 ) ; i ++ ) {
202+ let drugCode = drugCodes ?. [ i ] ;
203+ if ( drugCode ) {
204+ console . log ( ' medication: ' + drugCode ?. display ) ;
205+ const serviceConnection = await getServiceConnection (
206+ drugCode ,
207+ hook . fhirServer ?. toString ( )
208+ ) ;
209+ if ( serviceConnection ) {
210+ const url = serviceConnection . to + hook . hook ;
211+ urlList . push ( url ) ;
212+ // if medication found and works, do not continue through medications
213+ break ;
214+ }
215+ }
192216 }
193217 }
194218 }
0 commit comments