Skip to content

Commit

Permalink
(feat) O3-4087 : enable input for set-based lab tests on lab results …
Browse files Browse the repository at this point in the history
…form and handle partial set results
  • Loading branch information
donaldkibet committed Oct 16, 2024
1 parent 1af01f7 commit f0ef332
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { mutate } from 'swr';
import { Button, ButtonSet, Form, InlineLoading, InlineNotification, Stack } from '@carbon/react';
import { type DefaultPatientWorkspaceProps, type Order } from '@openmrs/esm-patient-common-lib';
import { restBaseUrl, showSnackbar, useAbortController, useLayoutType } from '@openmrs/esm-framework';
import { useOrderConceptByUuid, updateOrderResult, useLabEncounter, useObservation } from './lab-results.resource';
import {
useOrderConceptByUuid,
updateOrderResult,
useLabEncounter,
useObservation,
createObservationPayload,
} from './lab-results.resource';
import ResultFormField from './lab-results-form-field.component';
import styles from './lab-results-form.scss';
import { useLabResultsFormSchema } from './useLabResultsFormSchema';
Expand Down Expand Up @@ -39,7 +45,7 @@ const LabResultsForm: React.FC<LabResultsFormProps> = ({
formState: { errors, isDirty, isSubmitting },
getValues,
handleSubmit,
} = useForm<{ testResult: any }>({
} = useForm<{ testResult: Record<string, unknown> }>({
defaultValues: {},
resolver: zodResolver(schema),
mode: 'all',
Expand Down Expand Up @@ -94,66 +100,15 @@ const LabResultsForm: React.FC<LabResultsFormProps> = ({
);
}

const saveLabResults = async (formData) => {
const formValues = getValues();

const saveLabResults = async (formValues: Record<string, unknown>) => {
const isEmptyForm = Object.values(formValues).every(
(value) => value === '' || value === null || value === undefined,
);

if (isEmptyForm) {
setShowEmptyFormErrorNotification(true);
return;
}

let obsValue = [];

if (concept.set && concept.setMembers.length > 0) {
let groupMembers = [];
concept.setMembers.forEach((member) => {
let value;
if (member.datatype.display === 'Numeric' || member.datatype.display === 'Text') {
value = getValues()[`${member.uuid}`];
} else if (member.datatype.display === 'Coded') {
value = {
uuid: getValues()[`${member.uuid}`],
};
}
const groupMember = {
concept: { uuid: member.uuid },
value: value,
status: 'FINAL',
order: { uuid: order.uuid },
};
groupMembers.push(groupMember);
});

obsValue.push({
concept: { uuid: order.concept.uuid },
status: 'FINAL',
order: { uuid: order.uuid },
groupMembers: groupMembers,
});
} else if (!concept.set && concept.setMembers.length === 0) {
let value;
if (concept.datatype.display === 'Numeric' || concept.datatype.display === 'Text') {
value = getValues()[`${concept.uuid}`];
} else if (concept.datatype.display === 'Coded') {
value = {
uuid: getValues()[`${concept.uuid}`],
};
}

obsValue.push({
concept: { uuid: order.concept.uuid },
status: 'FINAL',
order: { uuid: order.uuid },
value: value,
});
}

const obsPayload = { obs: obsValue };

const obsPayload = createObservationPayload(concept, order, formValues);
const orderDiscontinuationPayload = {
previousOrder: order.uuid,
type: 'testorder',
Expand All @@ -164,7 +119,6 @@ const LabResultsForm: React.FC<LabResultsFormProps> = ({
concept: order.concept.uuid,
orderer: order.orderer,
};

const resultsStatusPayload = {
fulfillerStatus: 'COMPLETED',
fulfillerComment: 'Test Results Entered',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import useSWR from 'swr';
import { type OpenmrsResource, openmrsFetch, restBaseUrl, type FetchResponse } from '@openmrs/esm-framework';
import { type Observation, type Encounter } from '../types/encounter';
import { type OrderDiscontinuationPayload } from '../types/order';
import { type Order } from '@openmrs/esm-patient-common-lib';

const labEncounterRepresentation =
'custom:(uuid,encounterDatetime,encounterType,location:(uuid,name),' +
Expand Down Expand Up @@ -158,3 +159,69 @@ export async function updateOrderResult(
}
throw new Error('Failed to update order');
}

export function createObservationPayload(concept: LabOrderConcept, order: Order, values: any) {
if (concept.set && concept.setMembers.length > 0) {
const groupMembers = concept.setMembers
.map((member) => createGroupMember(member, order, values))
.filter((member) => member?.value !== null && member?.value !== undefined);

if (groupMembers.length === 0) {
return { obs: [] };
}

return { obs: [createObservation(order, groupMembers)] };
}

if (!concept.set && concept.setMembers.length === 0) {
const value = getValue(concept, values);
if (value === null || value === undefined) {
return { obs: [] };
}
return { obs: [createObservation(order, null, value)] };
}

return { obs: [] };
}

function createGroupMember(member, order, values) {
const value = getValue(member, values);
if (value === null || value === undefined) {
return null;
}
return {
concept: { uuid: member.uuid },
value: value,
status: 'FINAL',
order: { uuid: order.uuid },
};
}

function createObservation(order, groupMembers = null, value = null) {
return {
concept: { uuid: order.concept.uuid },
status: 'FINAL',
order: { uuid: order.uuid },
...(groupMembers && groupMembers.length > 0 && { groupMembers }),
...(value !== null && value !== undefined && { value }),
};
}

function getValue(concept, values) {
const { datatype, uuid } = concept;
const value = values[uuid];

if (value === null || value === undefined) {
return null;
}

if (['Numeric', 'Text'].includes(datatype.display)) {
return value;
}

if (datatype.display === 'Coded') {
return { uuid: value };
}

return null;
}

0 comments on commit f0ef332

Please sign in to comment.