From dc9c23be34a836c2b90b7a38d55fccb2cac825dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Mon, 7 Oct 2024 19:43:30 +0200 Subject: [PATCH] fix(core-flows): remove reservations on order edit confirm (#9477) **What** - remove reservations from the old version of the order and create a new ones --- FIXES CC-566 --- .../order-edit/confirm-order-edit-request.ts | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/packages/core/core-flows/src/order/workflows/order-edit/confirm-order-edit-request.ts b/packages/core/core-flows/src/order/workflows/order-edit/confirm-order-edit-request.ts index 2365bb51c0..431f340710 100644 --- a/packages/core/core-flows/src/order/workflows/order-edit/confirm-order-edit-request.ts +++ b/packages/core/core-flows/src/order/workflows/order-edit/confirm-order-edit-request.ts @@ -25,6 +25,7 @@ import { throwIfOrderChangeIsNotActive, } from "../../utils/order-validation" import { createOrUpdateOrderPaymentCollectionWorkflow } from "../create-or-update-order-payment-collection" +import { deleteReservationsByLineItemsStep } from "../../../reservation" export type ConfirmOrderEditRequestWorkflowInput = { order_id: string @@ -132,6 +133,12 @@ export const confirmOrderEditRequestWorkflow = createWorkflow( throw_if_key_not_found: true, }).config({ name: "order-items-query" }) + const lineItemIds = transform({ orderItems }, (data) => + data.orderItems.items.map(({ id }) => id) + ) + + deleteReservationsByLineItemsStep(lineItemIds) + const { variants, items } = transform( { orderItems, orderPreview }, ({ orderItems, orderPreview }) => { @@ -152,27 +159,35 @@ export const confirmOrderEditRequestWorkflow = createWorkflow( return } - let quantity: BigNumberInput = - itemAction.raw_quantity ?? itemAction.quantity - - let unit_price: BigNumberInput = + const unitPrice: BigNumberInput = itemAction.raw_unit_price ?? itemAction.unit_price + const updateAction = itemAction.actions!.find( (a) => a.action === ChangeActionType.ITEM_UPDATE ) - if (updateAction) { - quantity = MathBN.sub(quantity, ordItem.raw_quantity) - if (MathBN.lte(quantity, 0)) { - return - } + + const quantity: BigNumberInput = + itemAction.raw_quantity ?? itemAction.quantity + + const newQuantity = updateAction + ? MathBN.sub(quantity, ordItem.raw_quantity) + : quantity + + if (MathBN.lte(newQuantity, 0)) { + return } + const reservationQuantity = MathBN.sub( + newQuantity, + ordItem.raw_fulfilled_quantity + ) + allItems.push({ id: ordItem.id, variant_id: ordItem.variant_id, - quantity, - unit_price, + quantity: reservationQuantity, + unit_price: unitPrice }) allVariants.push(ordItem.variant) })