diff --git a/src/components/RequestBox/RequestBox.jsx b/src/components/RequestBox/RequestBox.jsx index 4765b33..2b59035 100644 --- a/src/components/RequestBox/RequestBox.jsx +++ b/src/components/RequestBox/RequestBox.jsx @@ -6,14 +6,15 @@ import MuiAlert from '@mui/material/Alert'; import Snackbar from '@mui/material/Snackbar'; import { shortNameMap } from '../../util/data.js'; import { getAge, createMedicationDispenseFromMedicationRequest } from '../../util/fhir.js'; -import { retrieveLaunchContext } from '../../util/util.js'; +import { retrieveLaunchContext, prepPrefetch } from '../../util/util.js'; import './request.css'; const RequestBox = props => { const [state, setState] = useState({ gatherCount: 0, response: {}, - submittedRx: false + submittedRx: false, + prefetchCompleted: false }); const { @@ -29,52 +30,36 @@ const RequestBox = props => { smartAppUrl, client, pimsUrl, + prefetchCompleted, getRemsAdminUrl } = props; const emptyField = empty; - const prepPrefetch = () => { - const preppedResources = new Map(); - Object.keys(prefetchedResources).forEach(resourceKey => { - let resourceList = []; - if (Array.isArray(prefetchedResources[resourceKey])) { - resourceList = prefetchedResources[resourceKey].map(resource => { - return resource; - }); - } else { - resourceList = prefetchedResources[resourceKey]; - } - - preppedResources.set(resourceKey, resourceList); - }); - return preppedResources; - }; - const submitPatientView = () => { - submitInfo(prepPrefetch(), null, patient, 'patient-view'); + submitInfo(prepPrefetch(prefetchedResources), null, patient, 'patient-view'); }; const _submitOrderSelect = () => { if (!_.isEmpty(request)) { - submitInfo(prepPrefetch(), request, patient, 'order-select'); + submitInfo(prepPrefetch(prefetchedResources), request, patient, 'order-select'); } }; const submitOrderSign = request => { if (!_.isEmpty(request)) { - submitInfo(prepPrefetch(), request, patient, 'order-sign'); + submitInfo(prepPrefetch(prefetchedResources), request, patient, 'order-sign'); } }; useEffect(() => { // if prefetch completed - if (state.prefetchCompleted) { + if (props.prefetchCompleted) { // if the prefetch contains a medicationRequests bundle if (prefetchedResources.medicationRequests) { submitPatientView(); } } - }, [state.prefetchCompleted]); + }, [props.prefetchCompleted]); const renderPatientInfo = () => { if (Object.keys(patient).length === 0) { diff --git a/src/components/SMARTBox/PatientBox.jsx b/src/components/SMARTBox/PatientBox.jsx index d200735..885252c 100644 --- a/src/components/SMARTBox/PatientBox.jsx +++ b/src/components/SMARTBox/PatientBox.jsx @@ -278,6 +278,7 @@ const PatientBox = props => { response: '' })); callback('response', ''); + clearCallback(); // update prefetch request for the medication const request = JSON.parse(data); if ( diff --git a/src/containers/RequestBuilder.jsx b/src/containers/RequestBuilder.jsx index 60bdb5a..ca53471 100644 --- a/src/containers/RequestBuilder.jsx +++ b/src/containers/RequestBuilder.jsx @@ -8,7 +8,7 @@ import RequestBox from '../components/RequestBox/RequestBox.jsx'; import buildRequest from '../util/buildRequest.js'; import { types } from '../util/data.js'; import { createJwt } from '../util/auth.js'; -import { getMedicationSpecificRemsAdminUrl } from '../util/util.js'; +import { getMedicationSpecificRemsAdminUrl, prepPrefetch } from '../util/util.js'; import Accordion from '@mui/material/Accordion'; import AccordionSummary from '@mui/material/AccordionSummary'; @@ -40,7 +40,9 @@ const RequestBuilder = props => { token: null, client: client, medicationDispense: null, - lastCheckedMedicationTime: null + lastCheckedMedicationTime: null, + prefetchCompleted: false, + medicationRequests: {} }); const displayRequestBox = !!globalState.patient?.id; @@ -101,9 +103,56 @@ const RequestBuilder = props => { } }; + const getMedicationRequests = patientId => { + client + .request(`MedicationRequest?subject=Patient/${patientId}`, { + resolveReferences: ['subject', 'performer', 'medicationReference'], + graph: false, + flat: true + }) + .then(result => { + setState(prevState => ({ ...prevState, medicationRequests: result })); + }); + }; + + + useEffect(() => { + const hook = 'patient-view'; + + let remsAdminUrls = []; + // get all the remsAdminUrl for each MedicationRequest + state.medicationRequests?.data?.forEach(request => { + const code = request?.medicationCodeableConcept?.coding[0]?.code; + const remsAdminUrl = getMedicationSpecificRemsAdminUrl(request, globalState, hook); + if (remsAdminUrl) { + remsAdminUrls.push(remsAdminUrl); + } + //sendHook(prefetch, request, patient, hook, remsAdminUrl); + }); + const uniqueUrls = [... new Set(remsAdminUrls.map(item => item))]; + + uniqueUrls?.forEach(url => { + sendHook(prepPrefetch(state.prefetchedResources), null, globalState.patient, hook, url); + //TODO: still need to handle multiple sends and multiple cards coming back + }); + + }, [state.medicationRequests]); + const submitInfo = (prefetch, request, patient, hook) => { console.log('Initiating form submission ', types.info); - const remsAdminUrl = getMedicationSpecificRemsAdminUrl(request, globalState, hook); + let remsAdminUrl = null; + if (request) { + remsAdminUrl = getMedicationSpecificRemsAdminUrl(request, globalState, hook); + sendHook(prefetch, request, patient, hook, remsAdminUrl); + } else { + // grab all of the REMS admins for the medications for this patient + + // get all MedicationRequests for the patient, then continue + getMedicationRequests(patient.id); + } + }; + + const sendHook = (prefetch, request, patient, hook, remsAdminUrl) => { setState(prevState => ({ ...prevState, @@ -292,6 +341,7 @@ const RequestBuilder = props => { defaultUser={globalState.defaultUser} loading={state.loading} patientFhirQuery={globalState.patientFhirQuery} + prefetchCompleted={state.prefetchCompleted} getRemsAdminUrl={(request, hook) => getMedicationSpecificRemsAdminUrl(request, globalState, hook) } diff --git a/src/util/util.js b/src/util/util.js index 44b9286..f494e61 100644 --- a/src/util/util.js +++ b/src/util/util.js @@ -92,6 +92,10 @@ function standardsBasedGetEtasu(etasuUrl, body, responseCallback) { } const getMedicationSpecificRemsAdminUrl = (request, globalState, hook) => { + // if empty request, just return + if (Object.keys(request).length === 0) { + return undefined; + } const display = request.medicationCodeableConcept?.coding?.[0]?.display; const rxnorm = request.medicationCodeableConcept?.coding?.[0]?.code; @@ -117,4 +121,21 @@ const getMedicationSpecificRemsAdminUrl = (request, globalState, hook) => { return cdsUrl; }; -export { retrieveLaunchContext, standardsBasedGetEtasu, getMedicationSpecificRemsAdminUrl }; +const prepPrefetch = (prefetchedResources) => { + const preppedResources = new Map(); + Object.keys(prefetchedResources).forEach(resourceKey => { + let resourceList = []; + if (Array.isArray(prefetchedResources[resourceKey])) { + resourceList = prefetchedResources[resourceKey].map(resource => { + return resource; + }); + } else { + resourceList = prefetchedResources[resourceKey]; + } + + preppedResources.set(resourceKey, resourceList); + }); + return preppedResources; +}; + +export { retrieveLaunchContext, standardsBasedGetEtasu, getMedicationSpecificRemsAdminUrl, prepPrefetch };