Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 59 additions & 35 deletions src/hooks/hookResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ export function buildErrorCard(reason: string) {
return cards;
}

export function getDrugCodeFromMedicationRequest(medicationRequest: MedicationRequest) {
export function getDrugCodesFromMedicationRequest(medicationRequest: MedicationRequest) {
if (medicationRequest) {
if (medicationRequest?.medicationCodeableConcept) {
console.log('Get Medication code from CodeableConcept');
return medicationRequest?.medicationCodeableConcept?.coding?.[0];
console.log('Get Medication codes from CodeableConcept');
return medicationRequest?.medicationCodeableConcept?.coding;
} else if (medicationRequest?.medicationReference) {
const reference = medicationRequest?.medicationReference;
let coding = null;
let codes = null;
medicationRequest?.contained?.every(e => {
if (e.resourceType + '/' + e.id === reference.reference) {
if (e.resourceType === 'Medication') {
console.log('Get Medication code from contained resource');
coding = e.code?.coding?.[0];
codes = e.code?.coding;
}
}
});
console.log('Found code: ' + JSON.stringify(coding));
return coding;
console.log('Found codes: ' + JSON.stringify(codes));
return codes;
}
}
return null;
Expand Down Expand Up @@ -94,7 +94,6 @@ export async function handleHook(
const hookType = req?.body?.hook;

if (contextRequest && contextRequest.resourceType === 'MedicationRequest') {
const drugCode = getDrugCodeFromMedicationRequest(contextRequest);

const forwardData = (hook: Hook, url: string) => {
// remove the auth token before any forwarding occurs
Expand All @@ -113,30 +112,48 @@ export async function handleHook(
res.json({ cards: [] }); // Return fallback response
});
};
if (drugCode) {
const hook: Hook = req.body;
const serviceConnection = await getServiceConnection(drugCode, hook.fhirServer?.toString());
if (serviceConnection) {
const url = serviceConnection.to + hook.hook;
console.log('rems-admin hook url: ' + url);
if (hook.fhirAuthorization && hook.fhirServer && hook.fhirAuthorization.access_token) {
hydrate(getFhirResource, hookPrefetch, hook).then(hydratedPrefetch => {
if (hydratedPrefetch) {
hook.prefetch = hydratedPrefetch;

let drugCodes = getDrugCodesFromMedicationRequest(contextRequest);
if (drugCodes) {
let found = false;

for (let i = 0; i < (drugCodes?.length ? drugCodes?.length : 0); i++) {
let drugCode = drugCodes?.[i];
console.log(' Processing drugCode: ' + JSON.stringify(drugCode));

if (drugCode) {
const hook: Hook = req.body;
let serviceConnection = await getServiceConnection(drugCode, hook.fhirServer?.toString());
if (serviceConnection) {
const url = serviceConnection.to + hook.hook;
console.log('rems-admin hook url: ' + url);
if (hook.fhirAuthorization && hook.fhirServer && hook.fhirAuthorization.access_token) {
hydrate(getFhirResource, hookPrefetch, hook).then(hydratedPrefetch => {
if (hydratedPrefetch) {
hook.prefetch = hydratedPrefetch;
}
forwardData(hook, url);
});
} else {
forwardData(hook, url);
}
forwardData(hook, url);
});
} else {
forwardData(hook, url);

found = true;
// if medication found and works, do not continue through medications
break;
}
}
} else {
}

if (!found) {
// unsupported drug code, send back empty card list
res.json({ cards: [] });
}
} else {
// drug code could not be extracted
res.json(createErrorCard('Could not extract drug code from request'));
res.json(createErrorCard('Could not extract drug codes from request'));
}

} else {
if (hookType === SupportedHooks.ORDER_SELECT || hookType === SupportedHooks.ORDER_SIGN) {
// context request is not a medicationRequest
Expand Down Expand Up @@ -178,17 +195,24 @@ export async function handleHook(
const urlList: string[] = [];
medicationRequests?.entry.forEach(async bundleEntry => {
if (bundleEntry?.resource?.resourceType == 'MedicationRequest') {
const drugCode = getDrugCodeFromMedicationRequest(bundleEntry?.resource);

if (drugCode) {
console.log(' medication: ' + drugCode?.display);
const serviceConnection = await getServiceConnection(
drugCode,
hook.fhirServer?.toString()
);
if (serviceConnection) {
const url = serviceConnection.to + hook.hook;
urlList.push(url);

const drugCodes = getDrugCodesFromMedicationRequest(bundleEntry?.resource);
if (drugCodes) {
for (let i = 0; i < (drugCodes?.length ? drugCodes?.length : 0); i++) {
let drugCode = drugCodes?.[i];
if (drugCode) {
console.log(' medication: ' + drugCode?.display);
const serviceConnection = await getServiceConnection(
drugCode,
hook.fhirServer?.toString()
);
if (serviceConnection) {
const url = serviceConnection.to + hook.hook;
urlList.push(url);
// if medication found and works, do not continue through medications
break;
}
}
}
}
}
Expand Down