Skip to content

Commit

Permalink
Merge pull request openmrs#77 from METS-Programme/fix/requisitionComb…
Browse files Browse the repository at this point in the history
…oBox

(fix) update requistion combobox to display correct text and clean up base operation
  • Loading branch information
makombe authored Jan 18, 2024
2 parents b771f97 + a17cc70 commit 67fb8d1
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AddStockOperationProps> = (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<boolean>(props?.isEditing);
const [manageStockItems, setManageStockItems] = useState(props?.isEditing);
const [manageSubmitOrComplete, setManageSubmitOrComplete] = useState(
props.isEditing
Expand All @@ -42,7 +40,13 @@ const AddStockOperation: React.FC<AddStockOperationProps> = (props) => {
if (isLoading) return <AccordionSkeleton />;
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;
}

Expand Down
110 changes: 110 additions & 0 deletions src/stock-operations/add-stock-operation/add-stock-utils.ts
Original file line number Diff line number Diff line change
@@ -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<StockOperationDTO> = []
) {
// 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -62,24 +65,12 @@ const BaseOperationDetails: React.FC<BaseOperationDetailsProps> = ({
},
}) => {
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 {
Expand Down Expand Up @@ -108,69 +99,8 @@ const BaseOperationDetails: React.FC<BaseOperationDetailsProps> = ({
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 {
Expand All @@ -190,7 +120,6 @@ const BaseOperationDetails: React.FC<BaseOperationDetailsProps> = ({
<ComboBox
id="requisitionStockOperationUuid"
items={requisitionStockOperations}
{...field}
onChange={(data: { selectedItem: StockOperationDTO }) => {
field.onChange(data.selectedItem.uuid);
Object.assign(
Expand All @@ -201,9 +130,7 @@ const BaseOperationDetails: React.FC<BaseOperationDetailsProps> = ({
stockItems: data.selectedItem.stockOperationItems,
});
}}
itemToElement={(item) => {
return item?.operationNumber ?? "";
}}
itemToString={(item) => `${item?.operationNumber}` ?? ""}
titleText={t("requisitionStockOperation", "Requisition")}
invalid={!!errors.requisitionStockOperationUuid}
invalidText={errors.requisitionStockOperationUuid?.message}
Expand Down

0 comments on commit 67fb8d1

Please sign in to comment.