Skip to content

Commit

Permalink
(O3-3571): Modify results observation payload to send data as an enco…
Browse files Browse the repository at this point in the history
…unter. (#78)

* send results as encounter/ error handling

* imporve error handling

* update encounter type config description
  • Loading branch information
arodidev authored Jul 28, 2024
1 parent eb3f4b4 commit 7497f45
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 76 deletions.
6 changes: 6 additions & 0 deletions src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,5 +31,6 @@ export const configSchema = {

export type Config = {
laboratoryOrderTypeUuid: string;
encounterTypeUuid: string;
targetPatientDashboard: Object;
};
123 changes: 64 additions & 59 deletions src/results/result-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
useConfig,
useLayoutType,
usePatient,
useSession,
} from "@openmrs/esm-framework";
import {
useGetOrderConceptByUuid,
Expand All @@ -31,7 +32,8 @@ interface ResultFormProps {

const ResultForm: React.FC<ResultFormProps> = ({ order, patientUuid }) => {
const { t } = useTranslation();
const { laboratoryOrderTypeUuid } = useConfig<Config>();
const session = useSession();
const { laboratoryOrderTypeUuid, encounterTypeUuid } = useConfig<Config>();
const {
control,
register,
Expand Down Expand Up @@ -60,6 +62,8 @@ const ResultForm: React.FC<ResultFormProps> = ({ order, patientUuid }) => {
e.preventDefault();
let obsValue = [];

const submissionDatetime = new Date().toISOString();

if (concept.set && concept.setMembers.length > 0) {
let groupMembers = [];
concept.setMembers.forEach((member) => {
Expand Down Expand Up @@ -107,10 +111,14 @@ const ResultForm: React.FC<ResultFormProps> = ({ 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,
};

Expand All @@ -125,13 +133,9 @@ const ResultForm: React.FC<ResultFormProps> = ({ 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"),
Expand All @@ -141,56 +145,57 @@ const ResultForm: React.FC<ResultFormProps> = ({ 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 <Loader />;
Expand Down
51 changes: 34 additions & 17 deletions src/results/result-form.resource.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<FetchResponse<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}`, {
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
}
}

0 comments on commit 7497f45

Please sign in to comment.