chore: add compare_at_unit_price when price list price is retrieved (#9564)
* chore: add compare_at_unit_price when price list price is retrieved * chore: add test for update item + more fixes along the way * chore: fix tests * chore: add refresh spec * Apply suggestions from code review Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com> * chore: use undefined checker * chore: switch to map --------- Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com>
This commit is contained in:
@@ -31,6 +31,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
||||
exchange_id: action.exchange_id,
|
||||
|
||||
unit_price: action.details.unit_price,
|
||||
compare_at_unit_price: action.details.compare_at_unit_price,
|
||||
quantity: action.details.quantity,
|
||||
} as VirtualOrder["items"][0]
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import {
|
||||
BigNumber,
|
||||
ChangeActionType,
|
||||
MathBN,
|
||||
MedusaError,
|
||||
@@ -13,36 +12,28 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_UPDATE, {
|
||||
(item) => item.id === action.details.reference_id
|
||||
)
|
||||
|
||||
const unitPrice = action.details.unit_price
|
||||
const existing = currentOrder.items[existingIndex]
|
||||
|
||||
existing.detail.quantity ??= 0
|
||||
|
||||
let quantityDiff = MathBN.sub(
|
||||
action.details.quantity,
|
||||
existing.detail.quantity
|
||||
const originalQuantity = MathBN.convert(
|
||||
existing.detail.quantity ?? existing.quantity
|
||||
)
|
||||
const originalUnitPrice = MathBN.convert(
|
||||
existing.detail.unit_price ?? existing.unit_price
|
||||
)
|
||||
|
||||
const quant = new BigNumber(action.details.quantity)
|
||||
existing.quantity = quant
|
||||
existing.detail.quantity = quant
|
||||
const currentQuantity = MathBN.convert(action.details.quantity)
|
||||
const quantityDiff = MathBN.sub(currentQuantity, originalQuantity)
|
||||
|
||||
if (unitPrice) {
|
||||
const currentUnitPriceBN = MathBN.convert(unitPrice)
|
||||
const originalUnitPriceBn = MathBN.convert(
|
||||
existing.detail.unit_price ?? existing.unit_price
|
||||
)
|
||||
existing.quantity = currentQuantity
|
||||
existing.detail.quantity = currentQuantity
|
||||
|
||||
const currentQuantityBn = MathBN.convert(action.details.quantity)
|
||||
const originalQuantityBn = MathBN.convert(
|
||||
existing.detail.quantity ?? existing.quantity
|
||||
)
|
||||
if (action.details.unit_price) {
|
||||
const currentUnitPrice = MathBN.convert(action.details.unit_price)
|
||||
const originalTotal = MathBN.mult(originalUnitPrice, originalQuantity)
|
||||
const currentTotal = MathBN.mult(currentUnitPrice, currentQuantity)
|
||||
|
||||
const originalTotal = MathBN.mult(originalUnitPriceBn, originalQuantityBn)
|
||||
const currentTotal = MathBN.mult(currentUnitPriceBN, currentQuantityBn)
|
||||
|
||||
existing.unit_price = currentUnitPriceBN
|
||||
existing.detail.unit_price = currentUnitPriceBN
|
||||
existing.unit_price = currentUnitPrice
|
||||
existing.detail.unit_price = currentUnitPrice
|
||||
|
||||
setActionReference(existing, action, options)
|
||||
|
||||
@@ -50,6 +41,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_UPDATE, {
|
||||
}
|
||||
|
||||
setActionReference(existing, action, options)
|
||||
|
||||
return MathBN.mult(existing.unit_price, quantityDiff)
|
||||
},
|
||||
validate({ action, currentOrder }) {
|
||||
|
||||
@@ -58,6 +58,8 @@ export function applyChangesToOrder(
|
||||
version,
|
||||
quantity: orderItem.quantity,
|
||||
unit_price: item.unit_price ?? orderItem.unit_price,
|
||||
compare_at_unit_price:
|
||||
item.compare_at_unit_price ?? orderItem.compare_at_unit_price,
|
||||
fulfilled_quantity: orderItem.fulfilled_quantity ?? 0,
|
||||
delivered_quantity: orderItem.delivered_quantity ?? 0,
|
||||
shipped_quantity: orderItem.shipped_quantity ?? 0,
|
||||
|
||||
@@ -48,6 +48,11 @@ export function formatOrder<T = any>(
|
||||
raw_quantity: detail.raw_quantity,
|
||||
unit_price: detail.unit_price ?? orderItem.item.unit_price,
|
||||
raw_unit_price: detail.raw_unit_price ?? orderItem.item.raw_unit_price,
|
||||
compare_at_unit_price:
|
||||
detail.compare_at_unit_price ?? orderItem.item.compare_at_unit_price,
|
||||
raw_compare_at_unit_price:
|
||||
detail.raw_compare_at_unit_price ??
|
||||
orderItem.item.raw_compare_at_unit_price,
|
||||
detail,
|
||||
}
|
||||
})
|
||||
@@ -254,6 +259,11 @@ export function mapRepositoryToOrderModel(config, isRelatedEntity = false) {
|
||||
delete conf.where.items.item.unit_price
|
||||
}
|
||||
|
||||
if (original.compare_at_unit_price) {
|
||||
conf.where.items.compare_at_unit_price = original.compare_at_unit_price
|
||||
delete conf.where.items.item.compare_at_unit_price
|
||||
}
|
||||
|
||||
if (original.detail) {
|
||||
conf.where.items = {
|
||||
...original.detail,
|
||||
|
||||
Reference in New Issue
Block a user