From 99eca64c20e6b9adcbe6c9105b8b4ae10bd2a2fb Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Tue, 20 Aug 2024 14:53:39 -0300 Subject: [PATCH] chore(order): preview removed items (#8680) --- .../__tests__/order-edits/order-edits.spec.ts | 34 ++++++++++--------- .../core/utils/src/totals/line-item/index.ts | 4 ++- .../order/src/utils/actions/item-remove.ts | 4 --- .../order/src/utils/actions/item-update.ts | 16 +++++---- .../order/src/utils/apply-order-changes.ts | 5 +++ .../src/utils/compute-actions/buy-get.ts | 13 +++---- .../utils/compute-actions/shipping-methods.ts | 6 ++-- 7 files changed, 44 insertions(+), 38 deletions(-) diff --git a/integration-tests/http/__tests__/order-edits/order-edits.spec.ts b/integration-tests/http/__tests__/order-edits/order-edits.spec.ts index 96a1c144f9..bb188870b2 100644 --- a/integration-tests/http/__tests__/order-edits/order-edits.spec.ts +++ b/integration-tests/http/__tests__/order-edits/order-edits.spec.ts @@ -298,21 +298,6 @@ medusaIntegrationTestRunner({ adminHeaders ) ).data.shipping_option - - const item = order.items[0] - - await api.post( - `/admin/orders/${order.id}/fulfillments`, - { - items: [ - { - id: item.id, - quantity: 2, - }, - ], - }, - adminHeaders - ) }) describe("Order Edits lifecycle", () => { @@ -369,6 +354,22 @@ medusaIntegrationTestRunner({ expect(result.summary.current_order_total).toEqual(134) expect(result.summary.original_order_total).toEqual(60) + // Remove the item by setting the quantity to 0 + + result = ( + await api.post( + `/admin/order-edits/${orderId}/items/item/${item.id}`, + { + quantity: 0, + }, + adminHeaders + ) + ).data.order_preview + + expect(result.summary.current_order_total).toEqual(34) + expect(result.summary.original_order_total).toEqual(60) + expect(result.items.length).toEqual(2) + result = ( await api.post( `/admin/order-edits/${orderId}/confirm`, @@ -380,7 +381,8 @@ medusaIntegrationTestRunner({ result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data .order - expect(result.total).toEqual(134) + expect(result.total).toEqual(34) + expect(result.items.length).toEqual(1) }) }) }, diff --git a/packages/core/utils/src/totals/line-item/index.ts b/packages/core/utils/src/totals/line-item/index.ts index 1523508b97..249276db1d 100644 --- a/packages/core/utils/src/totals/line-item/index.ts +++ b/packages/core/utils/src/totals/line-item/index.ts @@ -182,7 +182,9 @@ function getLineItemTotals( totals.original_total = new BigNumber(originalTotal) } - const totalPerUnit = MathBN.div(totals.total, item.quantity) + const div = MathBN.eq(item.quantity, 0) ? 1 : item.quantity + const totalPerUnit = MathBN.div(totals.total, div) + const optionalFields = { ...(context.extraQuantityFields ?? {}), } diff --git a/packages/modules/order/src/utils/actions/item-remove.ts b/packages/modules/order/src/utils/actions/item-remove.ts index 0ba701ba04..d79f576c4c 100644 --- a/packages/modules/order/src/utils/actions/item-remove.ts +++ b/packages/modules/order/src/utils/actions/item-remove.ts @@ -21,10 +21,6 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, { setActionReference(existing, action, options) - if (MathBN.lte(existing.quantity, 0)) { - currentOrder.items.splice(existingIndex, 1) - } - return MathBN.mult(existing.unit_price, action.details.quantity) }, validate({ action, currentOrder }) { diff --git a/packages/modules/order/src/utils/actions/item-update.ts b/packages/modules/order/src/utils/actions/item-update.ts index d3aac24afe..ac144f4c5f 100644 --- a/packages/modules/order/src/utils/actions/item-update.ts +++ b/packages/modules/order/src/utils/actions/item-update.ts @@ -1,4 +1,9 @@ -import { ChangeActionType, MathBN, MedusaError } from "@medusajs/utils" +import { + BigNumber, + ChangeActionType, + MathBN, + MedusaError, +} from "@medusajs/utils" import { OrderChangeProcessing } from "../calculate-order-change" import { setActionReference } from "../set-action-reference" @@ -17,15 +22,12 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_UPDATE, { existing.detail.quantity ) - existing.quantity = action.details.quantity - existing.detail.quantity = action.details.quantity + const quant = new BigNumber(action.details.quantity) + existing.quantity = quant + existing.detail.quantity = quant setActionReference(existing, action, options) - if (MathBN.lte(existing.quantity, 0)) { - currentOrder.items.splice(existingIndex, 1) - } - return MathBN.mult(existing.unit_price, quantityDiff) }, validate({ action, currentOrder }) { diff --git a/packages/modules/order/src/utils/apply-order-changes.ts b/packages/modules/order/src/utils/apply-order-changes.ts index 51ba62b00e..da6f862c39 100644 --- a/packages/modules/order/src/utils/apply-order-changes.ts +++ b/packages/modules/order/src/utils/apply-order-changes.ts @@ -1,6 +1,7 @@ import { OrderChangeActionDTO } from "@medusajs/types" import { ChangeActionType, + MathBN, createRawPropertiesFromBigNumber, isDefined, } from "@medusajs/utils" @@ -42,6 +43,10 @@ export function applyChangesToOrder( const version = actionsMap[order.id]?.[0]?.version ?? order.version for (const item of calculated.order.items) { + if (MathBN.lte(item.quantity, 0)) { + continue + } + const isExistingItem = item.id === item.detail?.item_id const orderItem = isExistingItem ? (item.detail as any) : item const itemId = isExistingItem ? orderItem.item_id : item.id diff --git a/packages/modules/promotion/src/utils/compute-actions/buy-get.ts b/packages/modules/promotion/src/utils/compute-actions/buy-get.ts index 92f0eb4a70..ecad8b3998 100644 --- a/packages/modules/promotion/src/utils/compute-actions/buy-get.ts +++ b/packages/modules/promotion/src/utils/compute-actions/buy-get.ts @@ -64,8 +64,11 @@ export function getComputedActionsForBuyGet( ) .filter((item) => isPresent(item.subtotal) && isPresent(item.quantity)) .sort((a, b) => { - const aPrice = MathBN.div(a.subtotal, a.quantity) - const bPrice = MathBN.div(b.subtotal, b.quantity) + const divA = MathBN.eq(a.quantity, 0) ? 1 : a.quantity + const divB = MathBN.eq(b.quantity, 0) ? 1 : b.quantity + + const aPrice = MathBN.div(a.subtotal, divA) + const bPrice = MathBN.div(b.subtotal, divB) return MathBN.lt(bPrice, aPrice) ? -1 : 1 }) @@ -75,10 +78,8 @@ export function getComputedActionsForBuyGet( for (const method of validItemsForTargetRules) { const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0 const multiplier = MathBN.min(method.quantity, remainingQtyToApply) - const amount = MathBN.mult( - MathBN.div(method.subtotal, method.quantity), - multiplier - ) + const div = MathBN.eq(method.quantity, 0) ? 1 : method.quantity + const amount = MathBN.mult(MathBN.div(method.subtotal, div), multiplier) const newRemainingQtyToApply = MathBN.sub(remainingQtyToApply, multiplier) if (MathBN.lt(newRemainingQtyToApply, 0) || MathBN.lte(amount, 0)) { diff --git a/packages/modules/promotion/src/utils/compute-actions/shipping-methods.ts b/packages/modules/promotion/src/utils/compute-actions/shipping-methods.ts index 319043d168..ce9fecff22 100644 --- a/packages/modules/promotion/src/utils/compute-actions/shipping-methods.ts +++ b/packages/modules/promotion/src/utils/compute-actions/shipping-methods.ts @@ -126,11 +126,9 @@ export function applyPromotionToShippingMethods( const applicableTotal = method.subtotal const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0 + const div = MathBN.eq(totalApplicableValue, 0) ? 1 : totalApplicableValue let applicablePromotionValue = MathBN.sub( - MathBN.mult( - MathBN.div(applicableTotal, totalApplicableValue), - promotionValue - ), + MathBN.mult(MathBN.div(applicableTotal, div), promotionValue), appliedPromoValue )