Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) add a function that combines calls to update an order and the associated observation results #34

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
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
23 changes: 19 additions & 4 deletions src/results/result-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
usePatient,
} from "@openmrs/esm-framework";
import {
UpdateEncounter,
useGetOrderConceptByUuid,
UpdateOrderResult,
} from "./result-form.resource";
import { Result } from "../work-list/work-list.resource";
import ResultFormField from "./result-form-field.component";
Expand Down Expand Up @@ -135,12 +135,27 @@ const ResultForm: React.FC<ResultFormProps> = ({ order, patientUuid }) => {
});
}

const payload = {
const obsPayload = {
obs: obsValue,
};

const orderDiscontinuationPayload = {
previousOrder: order.uuid,
type: "testorder",
action: "DISCONTINUE",
careSetting: order.careSetting.uuid,
encounter: order.encounter.uuid,
patient: order.patient.uuid,
concept: order.concept.uuid,
orderer: order.orderer,
};

setIsSubmitting(true);
// update encounter
UpdateEncounter(order.encounter.uuid, payload).then(
UpdateOrderResult(
order.encounter.uuid,
obsPayload,
orderDiscontinuationPayload
).then(
() => {
setIsSubmitting(false);
showToast({
Expand Down
30 changes: 30 additions & 0 deletions src/results/result-form.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,33 @@ export async function UpdateEncounter(uuid: string, payload: any) {
body: payload,
});
}

//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
) {
const abortController = new AbortController();
const updateOrderCall = await openmrsFetch(`/ws/rest/v1/order`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
signal: abortController.signal,
body: orderPayload,
});

if (updateOrderCall.status === 201) {
return await openmrsFetch(`/ws/rest/v1/encounter/${encounterUuid}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
signal: abortController.signal,
body: obsPayload,
});
} else {
// handle errors
}
}
Loading