Chore(core-flows,order): exchange/claim add item (#8126)
This commit is contained in:
committed by
GitHub
parent
5bb870948a
commit
8d530aa7f2
@@ -171,7 +171,6 @@ describe("Order Exchange - Actions", function () {
|
||||
id: "item_555",
|
||||
unit_price: 50,
|
||||
quantity: 1,
|
||||
detail: {},
|
||||
actions: [
|
||||
{
|
||||
action: "ITEM_ADD",
|
||||
@@ -194,7 +193,6 @@ describe("Order Exchange - Actions", function () {
|
||||
{
|
||||
id: "shipping_345",
|
||||
price: 5,
|
||||
detail: {},
|
||||
actions: [
|
||||
{
|
||||
action: "SHIPPING_ADD",
|
||||
@@ -206,7 +204,6 @@ describe("Order Exchange - Actions", function () {
|
||||
{
|
||||
id: "return_shipping_345",
|
||||
price: 7.5,
|
||||
detail: {},
|
||||
actions: [
|
||||
{
|
||||
action: "SHIPPING_ADD",
|
||||
|
||||
@@ -1969,18 +1969,57 @@ export default class OrderModuleService<
|
||||
}
|
||||
})
|
||||
|
||||
const calculated = calculateOrderChange({
|
||||
order: order as any,
|
||||
actions: orderChange.actions,
|
||||
transactions: order.transactions ?? [],
|
||||
options: {
|
||||
addActionReferenceToObject: true,
|
||||
},
|
||||
})
|
||||
const { itemsToUpsert, shippingMethodsToUpsert, calculatedOrders } =
|
||||
applyChangesToOrder(
|
||||
[order],
|
||||
{ [order.id]: orderChange.actions },
|
||||
{ addActionReferenceToObject: true }
|
||||
)
|
||||
|
||||
createRawPropertiesFromBigNumber(calculated)
|
||||
const calculated = calculatedOrders[order.id]
|
||||
|
||||
const addedItems = {}
|
||||
for (const item of calculated.order.items) {
|
||||
const isExistingItem = item.id === item.detail?.item_id
|
||||
|
||||
if (!isExistingItem) {
|
||||
addedItems[item.id] = item
|
||||
}
|
||||
}
|
||||
if (Object.keys(addedItems).length > 0) {
|
||||
const addedItemDetails = await this.listLineItems(
|
||||
{ id: Object.keys(addedItems) },
|
||||
{
|
||||
relations: ["adjustments", "tax_lines"],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
calculated.order.items.forEach((item, idx) => {
|
||||
if (addedItems[item.id]) {
|
||||
const lineItem = addedItemDetails.find((d) => d.id === item.id) as any
|
||||
|
||||
const actions = item.actions
|
||||
delete item.actions
|
||||
|
||||
const newItem = itemsToUpsert.find((d) => d.item_id === item.id)!
|
||||
calculated.order.items[idx] = {
|
||||
...lineItem,
|
||||
actions,
|
||||
quantity: newItem.quantity,
|
||||
detail: {
|
||||
...newItem,
|
||||
...item,
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TODO - add new shipping methods
|
||||
|
||||
const calcOrder = calculated.order
|
||||
|
||||
const calcOrder = calculated.order as any
|
||||
decorateCartTotals(calcOrder as DecorateCartLikeInputDTO)
|
||||
calcOrder.summary = calculated.summary
|
||||
|
||||
|
||||
@@ -30,9 +30,9 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
||||
existing = {
|
||||
id: action.details.reference_id!,
|
||||
order_id: currentOrder.id,
|
||||
return_id: action.details.return_id,
|
||||
claim_id: action.details.claim_id,
|
||||
exchange_id: action.details.exchange_id,
|
||||
return_id: action.return_id,
|
||||
claim_id: action.claim_id,
|
||||
exchange_id: action.exchange_id,
|
||||
|
||||
unit_price: action.details.unit_price,
|
||||
quantity: action.details.quantity,
|
||||
|
||||
@@ -12,22 +12,29 @@ export interface ApplyOrderChangeDTO extends OrderChangeActionDTO {
|
||||
|
||||
export function applyChangesToOrder(
|
||||
orders: any[],
|
||||
actionsMap: Record<string, any[]>
|
||||
actionsMap: Record<string, any[]>,
|
||||
options?: {
|
||||
addActionReferenceToObject?: boolean
|
||||
}
|
||||
) {
|
||||
const itemsToUpsert: OrderItem[] = []
|
||||
const shippingMethodsToUpsert: OrderShippingMethod[] = []
|
||||
const summariesToUpsert: any[] = []
|
||||
const orderToUpdate: any[] = []
|
||||
|
||||
const calculatedOrders = {}
|
||||
for (const order of orders) {
|
||||
const calculated = calculateOrderChange({
|
||||
order: order as any,
|
||||
actions: actionsMap[order.id],
|
||||
transactions: order.transactions ?? [],
|
||||
options,
|
||||
})
|
||||
|
||||
createRawPropertiesFromBigNumber(calculated)
|
||||
|
||||
calculatedOrders[order.id] = calculated
|
||||
|
||||
const version = actionsMap[order.id][0].version ?? 1
|
||||
|
||||
for (const item of calculated.order.items) {
|
||||
@@ -41,11 +48,11 @@ export function applyChangesToOrder(
|
||||
order_id: order.id,
|
||||
version,
|
||||
quantity: orderItem.quantity,
|
||||
fulfilled_quantity: orderItem.fulfilled_quantity,
|
||||
shipped_quantity: orderItem.shipped_quantity,
|
||||
return_requested_quantity: orderItem.return_requested_quantity,
|
||||
return_received_quantity: orderItem.return_received_quantity,
|
||||
return_dismissed_quantity: orderItem.return_dismissed_quantity,
|
||||
fulfilled_quantity: orderItem.fulfilled_quantity ?? 0,
|
||||
shipped_quantity: orderItem.shipped_quantity ?? 0,
|
||||
return_requested_quantity: orderItem.return_requested_quantity ?? 0,
|
||||
return_received_quantity: orderItem.return_received_quantity ?? 0,
|
||||
return_dismissed_quantity: orderItem.return_dismissed_quantity ?? 0,
|
||||
written_off_quantity: orderItem.written_off_quantity,
|
||||
metadata: orderItem.metadata,
|
||||
} as OrderItem)
|
||||
@@ -90,5 +97,6 @@ export function applyChangesToOrder(
|
||||
shippingMethodsToUpsert,
|
||||
summariesToUpsert,
|
||||
orderToUpdate,
|
||||
calculatedOrders,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,4 @@
|
||||
export function setActionReference(existing, action, options) {
|
||||
existing.detail ??= {}
|
||||
|
||||
existing.detail.order_id ??= action.order_id
|
||||
existing.detail.return_id ??= action.return_id
|
||||
existing.detail.claim_id ??= action.claim_id
|
||||
existing.detail.exchange_id ??= action.exchange_id
|
||||
|
||||
if (options?.addActionReferenceToObject) {
|
||||
existing.actions ??= []
|
||||
existing.actions.push(action)
|
||||
|
||||
@@ -33,6 +33,11 @@ export function formatOrder(
|
||||
}
|
||||
|
||||
mainOrder.items = mainOrder.items?.map((orderItem) => {
|
||||
const isFormatted = isDefined(orderItem.detail?.fulfilled_quantity)
|
||||
if (isFormatted) {
|
||||
return orderItem
|
||||
}
|
||||
|
||||
const detail = { ...orderItem }
|
||||
delete detail.order
|
||||
delete detail.item
|
||||
@@ -62,6 +67,10 @@ export function formatOrder(
|
||||
|
||||
if (order.shipping_methods) {
|
||||
order.shipping_methods = order.shipping_methods?.map((shippingMethod) => {
|
||||
if (shippingMethod.detail) {
|
||||
return shippingMethod
|
||||
}
|
||||
|
||||
const sm = { ...shippingMethod.shipping_method }
|
||||
|
||||
delete shippingMethod.shipping_method
|
||||
|
||||
Reference in New Issue
Block a user