diff --git a/src/config-schema.ts b/src/config-schema.ts index 3ae27752..3265631c 100644 --- a/src/config-schema.ts +++ b/src/config-schema.ts @@ -6,6 +6,11 @@ export const configSchema = { _default: "52a447d3-a64a-11e3-9aeb-50e549534c5e", _description: "Uuid for orderType", }, + encounterTypeUuid: { + _type: Type.String, + _default: "39da3525-afe4-45ff-8977-c53b7b359158", + _description: "Orders encounter type uuid", + }, targetPatientDashboard: { redirectToResultsViewer: { _type: Type.String, @@ -26,5 +31,6 @@ export const configSchema = { export type Config = { laboratoryOrderTypeUuid: string; + encounterTypeUuid: string; targetPatientDashboard: Object; }; diff --git a/src/results/result-form.component.tsx b/src/results/result-form.component.tsx index ce944b13..e7a6b4f5 100644 --- a/src/results/result-form.component.tsx +++ b/src/results/result-form.component.tsx @@ -11,6 +11,7 @@ import { useConfig, useLayoutType, usePatient, + useSession, } from "@openmrs/esm-framework"; import { useGetOrderConceptByUuid, @@ -31,7 +32,8 @@ interface ResultFormProps { const ResultForm: React.FC = ({ order, patientUuid }) => { const { t } = useTranslation(); - const { laboratoryOrderTypeUuid } = useConfig(); + const session = useSession(); + const { laboratoryOrderTypeUuid, encounterTypeUuid } = useConfig(); const { control, register, @@ -60,6 +62,8 @@ const ResultForm: React.FC = ({ order, patientUuid }) => { e.preventDefault(); let obsValue = []; + const submissionDatetime = new Date().toISOString(); + if (concept.set && concept.setMembers.length > 0) { let groupMembers = []; concept.setMembers.forEach((member) => { @@ -107,10 +111,14 @@ const ResultForm: React.FC = ({ order, patientUuid }) => { status: "FINAL", order: { uuid: order.uuid }, value: value, + obsDatetime: submissionDatetime, }); } - - const obsPayload = { + const encounterPayload = { + encounterDatetime: submissionDatetime, + patient: patientUuid, + encounterType: encounterTypeUuid, + location: session.sessionLocation.uuid, obs: obsValue, }; @@ -125,13 +133,9 @@ const ResultForm: React.FC = ({ order, patientUuid }) => { orderer: order.orderer, }; - UpdateOrderResult( - order.encounter.uuid, - obsPayload, - orderDiscontinuationPayload - ) - .then( - (resp) => { + UpdateOrderResult(encounterPayload, orderDiscontinuationPayload).then( + (response) => { + if (response.ok) { showSnackbar({ isLowContrast: true, title: t("updateEncounter", "Update lab results"), @@ -141,56 +145,57 @@ const ResultForm: React.FC = ({ order, patientUuid }) => { "You have successfully updated test results" ), }); - return resp; - }, - (err) => { - showNotification({ - title: t( - `errorUpdatingEncounter', 'Error occurred while updating test results` - ), - kind: "error", - critical: true, - description: err?.message, - }); - } - ) - .then((resp) => { - const abortController = new AbortController(); - setFulfillerStatus(order.uuid, "COMPLETED", abortController).then( - () => { - showSnackbar({ - isLowContrast: true, - title: t("markOrderFulfillStatus", "Test order completed"), - kind: "success", - subtitle: t( - "testOrderCompletedSuccessfully", - "You have successfully completed the test order" - ), - }); - mutate( - (key) => - typeof key === "string" && - key.startsWith( - `${restBaseUrl}/order?orderTypes=${laboratoryOrderTypeUuid}` + + const abortController = new AbortController(); + setFulfillerStatus(order.uuid, "COMPLETED", abortController).then( + () => { + showSnackbar({ + isLowContrast: true, + title: t("markOrderFulfillStatus", "Test order completed"), + kind: "success", + subtitle: t( + "testOrderCompletedSuccessfully", + "You have successfully completed the test order" ), - undefined, - { revalidate: true } - ); - closeOverlay(); - }, - (err) => { - showNotification({ - title: t( - `errorMarkingOrderFulfillStatus`, - "Error occurred while marking order fulfill status" - ), - kind: "error", - critical: true, - description: err?.message, - }); - } - ); - }); + }); + mutate( + (key) => + typeof key === "string" && + key.startsWith( + `${restBaseUrl}/order?orderTypes=${laboratoryOrderTypeUuid}` + ), + undefined, + { revalidate: true } + ); + closeOverlay(); + }, + (err) => { + showNotification({ + title: t( + `errorMarkingOrderFulfillStatus`, + "Error occurred while marking order fulfill status" + ), + kind: "error", + critical: true, + description: err?.message, + }); + } + ); + + return response; + } + }, + (err) => { + showNotification({ + title: t( + `errorUpdatingEncounter', 'Error occurred while updating test results` + ), + kind: "error", + critical: true, + description: err?.message, + }); + } + ); }; if (isLoadingPatient || isLoadingConcepts) { return ; diff --git a/src/results/result-form.resource.ts b/src/results/result-form.resource.ts index 11740854..e1f79170 100644 --- a/src/results/result-form.resource.ts +++ b/src/results/result-form.resource.ts @@ -1,4 +1,9 @@ -import { openmrsFetch, restBaseUrl } from "@openmrs/esm-framework"; +import { + FetchResponse, + openmrsFetch, + restBaseUrl, + showNotification, +} from "@openmrs/esm-framework"; import useSWR from "swr"; export interface ConceptResponse { @@ -343,30 +348,42 @@ export async function UpdateEncounter(uuid: string, payload: any) { //TODO: the calls to update order and observations for results should be transactional to allow for rollback export async function UpdateOrderResult( - encounterUuid: string, - obsPayload: any, + encounterPayload: any, orderPayload: any -) { +): Promise> { const abortController = new AbortController(); - const updateOrderCall = await openmrsFetch(`${restBaseUrl}/order`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - signal: abortController.signal, - body: orderPayload, - }); - if (updateOrderCall.status === 201) { - return await openmrsFetch(`${restBaseUrl}/encounter/${encounterUuid}`, { + try { + const orderResponse = await openmrsFetch(`${restBaseUrl}/order`, { method: "POST", headers: { "Content-Type": "application/json", }, signal: abortController.signal, - body: obsPayload, + body: orderPayload, + }); + + if (orderResponse.status === 201) { + const encounterResponse = await openmrsFetch(`${restBaseUrl}/encounter`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + signal: abortController.signal, + body: encounterPayload, + }); + + return encounterResponse; + } else + throw new Error( + `Order update failed with status ${orderResponse.status}` + ); + } catch (error) { + showNotification({ + title: "Error", + kind: "error", + critical: true, + description: error?.message, }); - } else { - // handle errors } }