Skip to content

Commit

Permalink
(fix) It should be possible to place lab orders (#1890)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibacher authored Jul 3, 2024
1 parent 762f6db commit e0442c9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ describe('AddLabOrder', () => {
instructions: 'plz do it thx',
labReferenceNumber: 'lba-000124',
testType: { label: 'CD4 COUNT', conceptUuid: 'test-lab-uuid-2' },
careSetting: '6f0c9a92-6f24-11e3-af88-005056821db0',
orderer: 'test-provider-uuid',
}),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useOrderBasket,
} from '@openmrs/esm-patient-common-lib';
import { translateFrom, useLayoutType, useSession, useConfig, ExtensionSlot } from '@openmrs/esm-framework';
import { careSettingUuid, prepLabOrderPostData, useOrderReasons } from '../api';
import { prepLabOrderPostData, useOrderReasons } from '../api';
import {
Button,
ButtonSet,
Expand Down Expand Up @@ -54,30 +54,39 @@ export function LabOrderForm({
const orderReasonRequired = (
config.labTestsWithOrderReasons?.find((c) => c.labTestUuid === initialOrder?.testType?.conceptUuid) || {}
).required;
const labOrderFormSchema = z.object({
instructions: z.string().optional(),
urgency: z.string().refine((value) => value !== '', {
message: translateFrom(moduleName, 'addLabOrderPriorityRequired', 'Priority is required'),
}),
labReferenceNumber: z.string().optional(),
testType: z.object(
{ label: z.string(), conceptUuid: z.string() },
{
required_error: translateFrom(moduleName, 'addLabOrderLabTestTypeRequired', 'Test type is required'),
invalid_type_error: translateFrom(moduleName, 'addLabOrderLabReferenceRequired', 'Test type is required'),
},
),
orderReason: orderReasonRequired
? z
.string({
required_error: translateFrom(moduleName, 'addLabOrderLabOrderReasonRequired', 'Order reason is required'),
})
.refine(
(value) => !!value,
translateFrom(moduleName, 'addLabOrderLabOrderReasonRequired', 'Order reason is required'),
)
: z.string().optional(),
});

const labOrderFormSchema = useMemo(
() =>
z.object({
instructions: z.string().optional(),
urgency: z.string().refine((value) => value !== '', {
message: translateFrom(moduleName, 'addLabOrderPriorityRequired', 'Priority is required'),
}),
labReferenceNumber: z.string().optional(),
testType: z.object(
{ label: z.string(), conceptUuid: z.string() },
{
required_error: translateFrom(moduleName, 'addLabOrderLabTestTypeRequired', 'Test type is required'),
invalid_type_error: translateFrom(moduleName, 'addLabOrderLabReferenceRequired', 'Test type is required'),
},
),
orderReason: orderReasonRequired
? z
.string({
required_error: translateFrom(
moduleName,
'addLabOrderLabOrderReasonRequired',
'Order reason is required',
),
})
.refine(
(value) => !!value,
translateFrom(moduleName, 'addLabOrderLabOrderReasonRequired', 'Order reason is required'),
)
: z.string().optional(),
}),
[orderReasonRequired],
);

const {
control,
Expand All @@ -90,9 +99,11 @@ export function LabOrderForm({
...initialOrder,
},
});

const orderReasonUuids =
(config.labTestsWithOrderReasons?.find((c) => c.labTestUuid === defaultValues?.testType?.conceptUuid) || {})
.orderReasons || [];

const { orderReasons } = useOrderReasons(orderReasonUuids);

const handleFormSubmission = useCallback(
Expand All @@ -101,19 +112,23 @@ export function LabOrderForm({
...initialOrder,
...data,
};
finalizedOrder.careSetting = careSettingUuid;
finalizedOrder.orderer = session.currentProvider.uuid;

const newOrders = [...orders];
const existingOrder = orders.find((order) => ordersEqual(order, finalizedOrder));

if (existingOrder) {
newOrders[orders.indexOf(existingOrder)] = {
...finalizedOrder,
// Incomplete orders should be marked completed on saving the form
isOrderIncomplete: false,
};
} else {
newOrders.push(finalizedOrder);
}

setOrders(newOrders);

closeWorkspaceWithSavedChanges({
onWorkspaceClose: () => launchPatientWorkspace('order-basket'),
});
Expand Down

0 comments on commit e0442c9

Please sign in to comment.