From a17cc703b61f932b16fa726e70e0f737cad422c6 Mon Sep 17 00:00:00 2001 From: Donald Kibet Date: Thu, 18 Jan 2024 13:10:12 +0300 Subject: [PATCH] (fix) update requistion combobox to display correct text and clean up base operation --- .../add-stock-operation.component.tsx | 14 ++- .../add-stock-operation/add-stock-utils.ts | 110 ++++++++++++++++++ .../base-operation-details.component.tsx | 91 ++------------- 3 files changed, 128 insertions(+), 87 deletions(-) create mode 100644 src/stock-operations/add-stock-operation/add-stock-utils.ts diff --git a/src/stock-operations/add-stock-operation/add-stock-operation.component.tsx b/src/stock-operations/add-stock-operation/add-stock-operation.component.tsx index cd172a55..b159af2e 100644 --- a/src/stock-operations/add-stock-operation/add-stock-operation.component.tsx +++ b/src/stock-operations/add-stock-operation/add-stock-operation.component.tsx @@ -22,14 +22,12 @@ import StockOperationApproveDispatchButton from "../stock-operations-dialog/stoc import StockOperationCompleteDispatchButton from "../stock-operations-dialog/stock-operations-completed-dispatch-button.component"; import StockOperationIssueStockButton from "../stock-operations-dialog/stock-operations-issue-stock-button.component"; import { StockOperation } from "./stock-operation-context/useStockOperationContext"; +import { showToast } from "@openmrs/esm-framework"; const AddStockOperation: React.FC = (props) => { const { t } = useTranslation(); + const { isEditing, canEdit, canPrint } = props; const { isLoading, isError, result } = useInitializeStockOperations(props); - const [canPrint, setCanPrint] = useState(props?.canPrint); - const [canEdit, setCanEdit] = useState(props?.canEdit); - - const [isEditing, setIsEditing] = useState(props?.isEditing); const [manageStockItems, setManageStockItems] = useState(props?.isEditing); const [manageSubmitOrComplete, setManageSubmitOrComplete] = useState( props.isEditing @@ -42,7 +40,13 @@ const AddStockOperation: React.FC = (props) => { if (isLoading) return ; if (isError) { closeOverlay(); - // TODO: Show an error + showToast({ + kind: "error", + title: t("error", "Error"), + description: t("errorLoadingStockOperation", "Error loading stock item"), + millis: 5000, + critical: true, + }); return; } diff --git a/src/stock-operations/add-stock-operation/add-stock-utils.ts b/src/stock-operations/add-stock-operation/add-stock-utils.ts new file mode 100644 index 00000000..d78bd00c --- /dev/null +++ b/src/stock-operations/add-stock-operation/add-stock-utils.ts @@ -0,0 +1,110 @@ +import { StockOperationDTO } from "../../core/api/types/stockOperation/StockOperationDTO"; +import { OperationType } from "../../core/api/types/stockOperation/StockOperationType"; + +const OPERATION_TYPES_FOR_DESTINATION_NAME_DELETION = [ + OperationType.ADJUSTMENT_OPERATION_TYPE, + OperationType.RECEIPT_OPERATION_TYPE, + OperationType.STOCK_ISSUE_OPERATION_TYPE, + OperationType.STOCK_TAKE_OPERATION_TYPE, + OperationType.RETURN_OPERATION_TYPE, + OperationType.DISPOSED_OPERATION_TYPE, + OperationType.OPENING_STOCK_OPERATION_TYPE, + OperationType.TRANSFER_OUT_OPERATION_TYPE, +]; + +const OPERATION_TYPES_FOR_DESTINATION_UUID_DELETION = [ + OperationType.ADJUSTMENT_OPERATION_TYPE, + OperationType.DISPOSED_OPERATION_TYPE, + OperationType.STOCK_TAKE_OPERATION_TYPE, + OperationType.OPENING_STOCK_OPERATION_TYPE, +]; + +export function getRequisitionStockOperations( + items: Array = [] +) { + // Extract stock issued requisition UUIDs + const stockIssuedRequisitionUuids = + items + ?.filter( + (item) => + item.operationType === OperationType.STOCK_ISSUE_OPERATION_TYPE + ) + .map((item) => item.requisitionStockOperationUuid) ?? []; + + // Filter requisition stock operations + const requisitionStockOperations = + items?.filter( + (item) => + item.operationType === OperationType.REQUISITION_OPERATION_TYPE && + !stockIssuedRequisitionUuids.includes(item.uuid) + ) ?? []; + + return requisitionStockOperations; +} + +function deleteProperties(req, properties) { + properties.forEach((prop) => { + delete req[prop]; + }); +} + +function shouldDeleteDestinationName(operationType) { + return OPERATION_TYPES_FOR_DESTINATION_NAME_DELETION.includes(operationType); +} + +function shouldDeleteDestinationUuid(operationType) { + return OPERATION_TYPES_FOR_DESTINATION_UUID_DELETION.includes(operationType); +} + +export function createBaseOperationPayload(model, item, operationType) { + const req = Object.assign(model, item); + + const propertiesToDelete = [ + "submitted", + "cancelledByFamilyName", + "atLocationName", + "completedByGivenName", + "cancelledBy", + "submittedByFamilyName", + "operationOrder", + "dispatchedByGivenName", + "submittedByGivenName", + "returnedByGivenName", + "operationNumber", + "responsiblePersonFamilyName", + "returnReason", + "atLocationUuid", + "cancelReason", + "rejectedByGivenName", + "reasonName", + "submittedBy", + "creator", + "completedByFamilyName", + "operationTypeName", + "rejectedByFamilyName", + "responsiblePerson", + "creatorFamilyName", + "returnedByFamilyName", + "cancelledByGivenName", + "operationType", + "responsiblePersonGivenName", + "sourceName", + "rejectionReason", + "completedBy", + "creatorGivenName", + "dispatchedByFamilyName", + "uuid", + ]; + + deleteProperties(req, propertiesToDelete); + + if (shouldDeleteDestinationName(operationType)) { + delete req.destinationName; + } + + if (shouldDeleteDestinationUuid(operationType)) { + delete req.destinationUuid; + } + + return req; +} diff --git a/src/stock-operations/add-stock-operation/base-operation-details.component.tsx b/src/stock-operations/add-stock-operation/base-operation-details.component.tsx index 3f5965d8..35625c04 100644 --- a/src/stock-operations/add-stock-operation/base-operation-details.component.tsx +++ b/src/stock-operations/add-stock-operation/base-operation-details.component.tsx @@ -35,8 +35,11 @@ import { InitializeResult } from "./types"; import rootStyles from "../../root.scss"; import { ResourceRepresentation } from "../../core/api/api"; import { useStockOperationPages } from "../stock-operations-table.resource"; -import { StockOperationItemDTO } from "../../core/api/types/stockOperation/StockOperationItemDTO"; import { useStockOperationContext } from "./stock-operation-context/useStockOperationContext"; +import { + createBaseOperationPayload, + getRequisitionStockOperations, +} from "./add-stock-utils"; interface BaseOperationDetailsProps { isEditing?: boolean; @@ -62,24 +65,12 @@ const BaseOperationDetails: React.FC = ({ }, }) => { const { t } = useTranslation(); - const { formContext, setFormContext } = useStockOperationContext(); + const { setFormContext } = useStockOperationContext(); const { isLoading, items } = useStockOperationPages({ v: ResourceRepresentation.Full, totalCount: true, }); - const stockIssuedRequisitionUuids = - items - ?.filter( - (item) => - item.operationType === OperationType.STOCK_ISSUE_OPERATION_TYPE - ) - .map((item) => item.requisitionStockOperationUuid) ?? []; - const requisitionStockOperations = - items?.filter( - (item) => - item.operationType === OperationType.REQUISITION_OPERATION_TYPE && - !stockIssuedRequisitionUuids.includes(item.uuid) - ) ?? []; + const requisitionStockOperations = getRequisitionStockOperations(items); const operationType = operationFromString(operation.operationType); const { @@ -108,69 +99,8 @@ const BaseOperationDetails: React.FC = ({ const handleSave = async (item: StockOperationDTO) => { try { setIsSaving(true); - - // Restore uuid - const req = Object.assign(model, item); - delete req.submitted; - delete req.cancelledByFamilyName; - delete req.atLocationName; - delete req.completedByGivenName; - delete req.cancelledBy; - delete req.submittedByFamilyName; - delete req.operationOrder; - delete req.dispatchedByGivenName; - delete req.submittedByGivenName; - delete req.returnedByGivenName; - delete req.operationNumber; - delete req.responsiblePersonFamilyName; - delete req.returnReason; - delete req.atLocationUuid; - delete req.cancelReason; - delete req.rejectedByGivenName; - delete req.reasonName; - delete req.submittedBy; - delete req.creator; - delete req.completedByFamilyName; - delete req.operationTypeName; - delete req.rejectedByFamilyName; - delete req.responsiblePerson; - delete req.creatorFamilyName; - delete req.returnedByFamilyName; - delete req.cancelledByGivenName; - delete req.operationType; - delete req.responsiblePersonGivenName; - delete req.sourceName; - delete req.rejectionReason; - delete req.completedBy; - delete req.creatorGivenName; - delete req.dispatchedByFamilyName; - delete req.uuid; - if ( - [ - OperationType.ADJUSTMENT_OPERATION_TYPE, - OperationType.RECEIPT_OPERATION_TYPE, - OperationType.STOCK_ISSUE_OPERATION_TYPE, - OperationType.STOCK_TAKE_OPERATION_TYPE, - OperationType.RETURN_OPERATION_TYPE, - OperationType.DISPOSED_OPERATION_TYPE, - OperationType.OPENING_STOCK_OPERATION_TYPE, - OperationType.TRANSFER_OUT_OPERATION_TYPE, - ].includes(operationType) - ) { - delete req.destinationName; - } - - if ( - [ - OperationType.ADJUSTMENT_OPERATION_TYPE, - OperationType.DISPOSED_OPERATION_TYPE, - OperationType.STOCK_TAKE_OPERATION_TYPE, - OperationType.OPENING_STOCK_OPERATION_TYPE, - ].includes(operationType) - ) { - delete req.destinationUuid; - } - await onSave(req); + const payload = createBaseOperationPayload(model, item, operationType); + await onSave(payload); } catch (e) { // Show notification } finally { @@ -190,7 +120,6 @@ const BaseOperationDetails: React.FC = ({ { field.onChange(data.selectedItem.uuid); Object.assign( @@ -201,9 +130,7 @@ const BaseOperationDetails: React.FC = ({ stockItems: data.selectedItem.stockOperationItems, }); }} - itemToElement={(item) => { - return item?.operationNumber ?? ""; - }} + itemToString={(item) => `${item?.operationNumber}` ?? ""} titleText={t("requisitionStockOperation", "Requisition")} invalid={!!errors.requisitionStockOperationUuid} invalidText={errors.requisitionStockOperationUuid?.message}