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:
Riqwan Thamir
2024-10-15 13:05:14 +02:00
committed by GitHub
parent 827b32cffd
commit 537567b679
31 changed files with 2161 additions and 1772 deletions

View File

@@ -0,0 +1,18 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20241014142943 extends Migration {
async up(): Promise<void> {
this.addSql(
'alter table if exists "order_item" add column if not exists "compare_at_unit_price" numeric null, add column if not exists "raw_compare_at_unit_price" jsonb null;'
)
}
async down(): Promise<void> {
this.addSql(
'alter table if exists "order_item" drop column if exists "compare_at_unit_price";'
)
this.addSql(
'alter table if exists "order_item" drop column if exists "raw_compare_at_unit_price";'
)
}
}

View File

@@ -90,6 +90,12 @@ export default class OrderItem {
@Property({ columnType: "jsonb", nullable: true })
raw_unit_price: BigNumberRawValue | null = null
@MikroOrmBigNumberProperty({ nullable: true })
compare_at_unit_price: BigNumber | number | null = null
@Property({ columnType: "jsonb", nullable: true })
raw_compare_at_unit_price: BigNumberRawValue | null = null
@MikroOrmBigNumberProperty()
quantity: BigNumber | number

View File

@@ -10,6 +10,7 @@ describe("Order Exchange - Actions", function () {
id: "1",
quantity: 1,
unit_price: 10,
compare_at_unit_price: null,
order_id: "1",
detail: {
@@ -28,6 +29,7 @@ describe("Order Exchange - Actions", function () {
id: "2",
quantity: 2,
unit_price: 100,
compare_at_unit_price: null,
order_id: "1",
detail: {
@@ -46,6 +48,7 @@ describe("Order Exchange - Actions", function () {
id: "3",
quantity: 3,
unit_price: 20,
compare_at_unit_price: null,
order_id: "1",
detail: {
@@ -127,6 +130,7 @@ describe("Order Exchange - Actions", function () {
order_id: "1",
quantity: 1,
unit_price: 10,
compare_at_unit_price: null,
detail: {
quantity: 1,
order_id: "1",
@@ -144,6 +148,7 @@ describe("Order Exchange - Actions", function () {
order_id: "1",
quantity: 2,
unit_price: 100,
compare_at_unit_price: null,
detail: {
quantity: 2,
order_id: "1",
@@ -161,6 +166,7 @@ describe("Order Exchange - Actions", function () {
order_id: "1",
quantity: 3,
unit_price: 20,
compare_at_unit_price: null,
detail: {
quantity: 3,
order_id: "1",

View File

@@ -10,6 +10,7 @@ describe("Order Return - Actions", function () {
id: "1",
quantity: 1,
unit_price: 10,
compare_at_unit_price: null,
order_id: "1",
detail: {
@@ -28,6 +29,7 @@ describe("Order Return - Actions", function () {
id: "2",
quantity: 2,
unit_price: 100,
compare_at_unit_price: null,
order_id: "1",
detail: {
@@ -46,6 +48,7 @@ describe("Order Return - Actions", function () {
id: "3",
quantity: 3,
unit_price: 20,
compare_at_unit_price: null,
order_id: "1",
detail: {
@@ -144,6 +147,7 @@ describe("Order Return - Actions", function () {
order_id: "1",
quantity: 1,
unit_price: 10,
compare_at_unit_price: null,
detail: {
order_id: "1",
quantity: 1,
@@ -161,6 +165,7 @@ describe("Order Return - Actions", function () {
order_id: "1",
quantity: 2,
unit_price: 100,
compare_at_unit_price: null,
detail: {
order_id: "1",
quantity: 2,
@@ -178,6 +183,7 @@ describe("Order Return - Actions", function () {
order_id: "1",
quantity: 3,
unit_price: 20,
compare_at_unit_price: null,
detail: {
quantity: 3,
order_id: "1",
@@ -284,6 +290,7 @@ describe("Order Return - Actions", function () {
order_id: "1",
quantity: 1,
unit_price: 10,
compare_at_unit_price: null,
detail: {
quantity: 1,
order_id: "1",
@@ -301,6 +308,7 @@ describe("Order Return - Actions", function () {
order_id: "1",
quantity: 2,
unit_price: 100,
compare_at_unit_price: null,
detail: {
quantity: 2,
order_id: "1",
@@ -318,6 +326,7 @@ describe("Order Return - Actions", function () {
order_id: "1",
quantity: 3,
unit_price: 20,
compare_at_unit_price: null,
detail: {
quantity: 3,
order_id: "1",

View File

@@ -1994,7 +1994,10 @@ export default class OrderModuleService<
if (!isExistingItem) {
addedItems[item.id] = {
...item,
unit_price: item.detail?.unit_price ?? item.unit_price,
quantity: item.detail?.quantity ?? item.quantity,
unit_price: item.detail?.unit_price || item.unit_price,
compare_at_unit_price:
item.detail?.compare_at_unit_price || item.compare_at_unit_price,
}
}
}
@@ -2026,13 +2029,15 @@ export default class OrderModuleService<
const newItem = itemsToUpsert.find((d) => d.item_id === item.id)!
const unitPrice = newItem?.unit_price ?? item.unit_price
const compareAtUnitPrice =
newItem?.compare_at_unit_price ?? item.compare_at_unit_price
calculated.order.items[idx] = {
...lineItem,
actions,
quantity: newItem.quantity,
unit_price: unitPrice,
raw_unit_price: new BigNumber(unitPrice),
compare_at_unit_price: compareAtUnitPrice,
detail: {
...newItem,
...item,

View File

@@ -11,6 +11,7 @@ export type VirtualOrder = {
exchange_id?: string
unit_price: BigNumberInput
compare_at_unit_price: BigNumberInput | null
quantity: BigNumberInput
detail: {
@@ -22,6 +23,7 @@ export type VirtualOrder = {
item_id?: string
unit_price?: BigNumberInput
compare_at_unit_price?: BigNumberInput | null
quantity: BigNumberInput
shipped_quantity: BigNumberInput
fulfilled_quantity: BigNumberInput

View File

@@ -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]

View File

@@ -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 }) {

View File

@@ -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,

View File

@@ -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,