From 83af1126cfb2de29712c5f6363a8b1044271b46e Mon Sep 17 00:00:00 2001 From: reagan-meant Date: Thu, 18 Jul 2024 01:05:44 +0300 Subject: [PATCH 1/2] Add lab results to lab results encounter --- src/results/result-form.component.tsx | 23 +++++++++--- src/results/result-form.resource.ts | 51 +++++++++++++++++++-------- 2 files changed, 56 insertions(+), 18 deletions(-) diff --git a/src/results/result-form.component.tsx b/src/results/result-form.component.tsx index ce944b13..84cecdde 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 { laboratoryOrderTypeUuid, laboratoryResultEncounterTypeUuid } = + useConfig(); const { control, register, @@ -46,6 +48,8 @@ const ResultForm: React.FC = ({ order, patientUuid }) => { const { concept, isLoading: isLoadingConcepts } = useGetOrderConceptByUuid( order.concept.uuid ); + const userSession = useSession(); + const userLocation = userSession?.sessionLocation?.uuid; const bannerState = useMemo(() => { if (patient) { return { @@ -124,11 +128,22 @@ const ResultForm: React.FC = ({ order, patientUuid }) => { concept: order.concept.uuid, orderer: order.orderer, }; - + const orders: Array = []; + orders.push(order); + const encounterPostData = { + patient: patientUuid, + location: userLocation, + encounterType: laboratoryResultEncounterTypeUuid, + //visit: order.encounter activeVisit?.uuid, + obs: [], + //orders, + }; UpdateOrderResult( - order.encounter.uuid, + //order.encounter.uuid, + laboratoryResultEncounterTypeUuid, obsPayload, - orderDiscontinuationPayload + orderDiscontinuationPayload, + encounterPostData ) .then( (resp) => { diff --git a/src/results/result-form.resource.ts b/src/results/result-form.resource.ts index 11740854..355c71e0 100644 --- a/src/results/result-form.resource.ts +++ b/src/results/result-form.resource.ts @@ -345,28 +345,51 @@ export async function UpdateEncounter(uuid: string, payload: any) { export async function UpdateOrderResult( encounterUuid: string, obsPayload: any, - orderPayload: any + orderPayload: any, + encounterPostData: any ) { 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}`, { + const createOrderResultEncounterCall = await openmrsFetch( + `${restBaseUrl}/encounter`, + { method: "POST", headers: { "Content-Type": "application/json", }, signal: abortController.signal, - body: obsPayload, + body: encounterPostData, + } + ); + + if (createOrderResultEncounterCall.status === 201) { + const orderResultEncounterUuid = createOrderResultEncounterCall.data.uuid; + + orderPayload.encounter = orderResultEncounterUuid; + + const updateOrderCall = await openmrsFetch(`${restBaseUrl}/order`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + signal: abortController.signal, + body: orderPayload, }); - } else { - // handle errors + + if (updateOrderCall.status === 201) { + return await openmrsFetch( + `${restBaseUrl}/encounter/${orderResultEncounterUuid}`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + signal: abortController.signal, + body: obsPayload, + } + ); + } else { + // handle errors + } } } From 7c850177eab282270652a7d3c9e43b5e0133205a Mon Sep 17 00:00:00 2001 From: reagan-meant Date: Thu, 18 Jul 2024 22:25:48 +0300 Subject: [PATCH 2/2] Add results to exisiting visit --- src/config-schema.ts | 6 +++++ src/results/result-form.component.tsx | 7 +----- src/results/result-form.resource.ts | 32 ++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/config-schema.ts b/src/config-schema.ts index 3ae27752..a5544a66 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", }, + laboratoryResultEncounterTypeUuid: { + _type: Type.String, + _default: "3596fafb-6f6f-4396-8c87-6e63a0f1bd71", + _description: "Uuid for lab resulting encounter type", + }, targetPatientDashboard: { redirectToResultsViewer: { _type: Type.String, @@ -26,5 +31,6 @@ export const configSchema = { export type Config = { laboratoryOrderTypeUuid: string; + laboratoryResultEncounterTypeUuid: string; targetPatientDashboard: Object; }; diff --git a/src/results/result-form.component.tsx b/src/results/result-form.component.tsx index 84cecdde..0476ec74 100644 --- a/src/results/result-form.component.tsx +++ b/src/results/result-form.component.tsx @@ -128,19 +128,14 @@ const ResultForm: React.FC = ({ order, patientUuid }) => { concept: order.concept.uuid, orderer: order.orderer, }; - const orders: Array = []; - orders.push(order); + const encounterPostData = { patient: patientUuid, location: userLocation, encounterType: laboratoryResultEncounterTypeUuid, - //visit: order.encounter activeVisit?.uuid, obs: [], - //orders, }; UpdateOrderResult( - //order.encounter.uuid, - laboratoryResultEncounterTypeUuid, obsPayload, orderDiscontinuationPayload, encounterPostData diff --git a/src/results/result-form.resource.ts b/src/results/result-form.resource.ts index 355c71e0..d94401da 100644 --- a/src/results/result-form.resource.ts +++ b/src/results/result-form.resource.ts @@ -343,13 +343,43 @@ 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, orderPayload: any, encounterPostData: any ) { const abortController = new AbortController(); + const patientVisits = await openmrsFetch( + `${restBaseUrl}/visit?patient=${orderPayload.patient}&v=custom:(uuid,encounters:(uuid))`, + { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + signal: abortController.signal, + } + ); + + function findVisitUuidByOrderEncounterUuid(visits, orderEncounterUuid) { + for (const visit of visits) { + if ( + visit.encounters.some( + (encounter) => encounter.uuid === orderEncounterUuid + ) + ) { + return visit.uuid; + } + } + return null; + } + + const visitUuid = findVisitUuidByOrderEncounterUuid( + patientVisits.data.results, + orderPayload.encounter + ); + + encounterPostData.visit = visitUuid; + const createOrderResultEncounterCall = await openmrsFetch( `${restBaseUrl}/encounter`, {