chore(order): preview removed items (#8680)
This commit is contained in:
@@ -298,21 +298,6 @@ medusaIntegrationTestRunner({
|
|||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
).data.shipping_option
|
).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", () => {
|
describe("Order Edits lifecycle", () => {
|
||||||
@@ -369,6 +354,22 @@ medusaIntegrationTestRunner({
|
|||||||
expect(result.summary.current_order_total).toEqual(134)
|
expect(result.summary.current_order_total).toEqual(134)
|
||||||
expect(result.summary.original_order_total).toEqual(60)
|
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 = (
|
result = (
|
||||||
await api.post(
|
await api.post(
|
||||||
`/admin/order-edits/${orderId}/confirm`,
|
`/admin/order-edits/${orderId}/confirm`,
|
||||||
@@ -380,7 +381,8 @@ medusaIntegrationTestRunner({
|
|||||||
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
||||||
.order
|
.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)
|
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 = {
|
const optionalFields = {
|
||||||
...(context.extraQuantityFields ?? {}),
|
...(context.extraQuantityFields ?? {}),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,10 +21,6 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, {
|
|||||||
|
|
||||||
setActionReference(existing, action, options)
|
setActionReference(existing, action, options)
|
||||||
|
|
||||||
if (MathBN.lte(existing.quantity, 0)) {
|
|
||||||
currentOrder.items.splice(existingIndex, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
return MathBN.mult(existing.unit_price, action.details.quantity)
|
return MathBN.mult(existing.unit_price, action.details.quantity)
|
||||||
},
|
},
|
||||||
validate({ action, currentOrder }) {
|
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 { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import { setActionReference } from "../set-action-reference"
|
||||||
|
|
||||||
@@ -17,15 +22,12 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_UPDATE, {
|
|||||||
existing.detail.quantity
|
existing.detail.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
existing.quantity = action.details.quantity
|
const quant = new BigNumber(action.details.quantity)
|
||||||
existing.detail.quantity = action.details.quantity
|
existing.quantity = quant
|
||||||
|
existing.detail.quantity = quant
|
||||||
|
|
||||||
setActionReference(existing, action, options)
|
setActionReference(existing, action, options)
|
||||||
|
|
||||||
if (MathBN.lte(existing.quantity, 0)) {
|
|
||||||
currentOrder.items.splice(existingIndex, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
return MathBN.mult(existing.unit_price, quantityDiff)
|
return MathBN.mult(existing.unit_price, quantityDiff)
|
||||||
},
|
},
|
||||||
validate({ action, currentOrder }) {
|
validate({ action, currentOrder }) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { OrderChangeActionDTO } from "@medusajs/types"
|
import { OrderChangeActionDTO } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
ChangeActionType,
|
ChangeActionType,
|
||||||
|
MathBN,
|
||||||
createRawPropertiesFromBigNumber,
|
createRawPropertiesFromBigNumber,
|
||||||
isDefined,
|
isDefined,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
@@ -42,6 +43,10 @@ export function applyChangesToOrder(
|
|||||||
const version = actionsMap[order.id]?.[0]?.version ?? order.version
|
const version = actionsMap[order.id]?.[0]?.version ?? order.version
|
||||||
|
|
||||||
for (const item of calculated.order.items) {
|
for (const item of calculated.order.items) {
|
||||||
|
if (MathBN.lte(item.quantity, 0)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
const isExistingItem = item.id === item.detail?.item_id
|
const isExistingItem = item.id === item.detail?.item_id
|
||||||
const orderItem = isExistingItem ? (item.detail as any) : item
|
const orderItem = isExistingItem ? (item.detail as any) : item
|
||||||
const itemId = isExistingItem ? orderItem.item_id : item.id
|
const itemId = isExistingItem ? orderItem.item_id : item.id
|
||||||
|
|||||||
@@ -64,8 +64,11 @@ export function getComputedActionsForBuyGet(
|
|||||||
)
|
)
|
||||||
.filter((item) => isPresent(item.subtotal) && isPresent(item.quantity))
|
.filter((item) => isPresent(item.subtotal) && isPresent(item.quantity))
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
const aPrice = MathBN.div(a.subtotal, a.quantity)
|
const divA = MathBN.eq(a.quantity, 0) ? 1 : a.quantity
|
||||||
const bPrice = MathBN.div(b.subtotal, b.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
|
return MathBN.lt(bPrice, aPrice) ? -1 : 1
|
||||||
})
|
})
|
||||||
@@ -75,10 +78,8 @@ export function getComputedActionsForBuyGet(
|
|||||||
for (const method of validItemsForTargetRules) {
|
for (const method of validItemsForTargetRules) {
|
||||||
const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0
|
const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0
|
||||||
const multiplier = MathBN.min(method.quantity, remainingQtyToApply)
|
const multiplier = MathBN.min(method.quantity, remainingQtyToApply)
|
||||||
const amount = MathBN.mult(
|
const div = MathBN.eq(method.quantity, 0) ? 1 : method.quantity
|
||||||
MathBN.div(method.subtotal, method.quantity),
|
const amount = MathBN.mult(MathBN.div(method.subtotal, div), multiplier)
|
||||||
multiplier
|
|
||||||
)
|
|
||||||
const newRemainingQtyToApply = MathBN.sub(remainingQtyToApply, multiplier)
|
const newRemainingQtyToApply = MathBN.sub(remainingQtyToApply, multiplier)
|
||||||
|
|
||||||
if (MathBN.lt(newRemainingQtyToApply, 0) || MathBN.lte(amount, 0)) {
|
if (MathBN.lt(newRemainingQtyToApply, 0) || MathBN.lte(amount, 0)) {
|
||||||
|
|||||||
@@ -126,11 +126,9 @@ export function applyPromotionToShippingMethods(
|
|||||||
const applicableTotal = method.subtotal
|
const applicableTotal = method.subtotal
|
||||||
const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0
|
const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0
|
||||||
|
|
||||||
|
const div = MathBN.eq(totalApplicableValue, 0) ? 1 : totalApplicableValue
|
||||||
let applicablePromotionValue = MathBN.sub(
|
let applicablePromotionValue = MathBN.sub(
|
||||||
MathBN.mult(
|
MathBN.mult(MathBN.div(applicableTotal, div), promotionValue),
|
||||||
MathBN.div(applicableTotal, totalApplicableValue),
|
|
||||||
promotionValue
|
|
||||||
),
|
|
||||||
appliedPromoValue
|
appliedPromoValue
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user