chore(order): preview removed items (#8680)
This commit is contained in:
committed by
GitHub
parent
430d9a38c4
commit
99eca64c20
@@ -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)
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
@@ -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 ?? {}),
|
||||
}
|
||||
|
||||
@@ -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 }) {
|
||||
|
||||
@@ -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 }) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user