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) It should be possible to place lab orders #1890

Merged
merged 1 commit into from
Jul 3, 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
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