chore(order): preview order change (#8025)
What: - new method `previewOrderChange` - Calculate all the actions related to an order change. - Return the preview of the final Order, with all the calculated values. - Associate actions with items and shipping_methods they modified. FIXES: CORE-2509
This commit is contained in:
@@ -835,6 +835,10 @@ export interface OrderDTO {
|
|||||||
* The version of the order.
|
* The version of the order.
|
||||||
*/
|
*/
|
||||||
version: number
|
version: number
|
||||||
|
/**
|
||||||
|
* The active order change, if any.
|
||||||
|
*/
|
||||||
|
order_change?: OrderChangeDTO
|
||||||
/**
|
/**
|
||||||
* The status of the order.
|
* The status of the order.
|
||||||
*/
|
*/
|
||||||
@@ -1191,16 +1195,54 @@ export interface OrderChangeDTO {
|
|||||||
* The ID of the order change
|
* The ID of the order change
|
||||||
*/
|
*/
|
||||||
id: string
|
id: string
|
||||||
|
/**
|
||||||
|
* The version of the order change
|
||||||
|
*/
|
||||||
|
version: string
|
||||||
|
/**
|
||||||
|
* The type of the order change
|
||||||
|
*/
|
||||||
|
change_type?: "return" | "exchange" | "claim" | "edit"
|
||||||
/**
|
/**
|
||||||
* The ID of the associated order
|
* The ID of the associated order
|
||||||
*/
|
*/
|
||||||
order_id: string
|
order_id: string
|
||||||
|
/**
|
||||||
|
* The ID of the associated return order
|
||||||
|
*/
|
||||||
|
return_id: string
|
||||||
|
/**
|
||||||
|
* The ID of the associated exchange order
|
||||||
|
*/
|
||||||
|
exchange_id: string
|
||||||
|
/**
|
||||||
|
* The ID of the associated claim order
|
||||||
|
*/
|
||||||
|
claim_id: string
|
||||||
/**
|
/**
|
||||||
* The associated order
|
* The associated order
|
||||||
*
|
*
|
||||||
* @expandable
|
* @expandable
|
||||||
*/
|
*/
|
||||||
order: OrderDTO
|
order: OrderDTO
|
||||||
|
/**
|
||||||
|
* The associated return order
|
||||||
|
*
|
||||||
|
* @expandable
|
||||||
|
*/
|
||||||
|
return_order: ReturnDTO
|
||||||
|
/**
|
||||||
|
* The associated exchange order
|
||||||
|
*
|
||||||
|
* @expandable
|
||||||
|
*/
|
||||||
|
exchange: OrderExchangeDTO
|
||||||
|
/**
|
||||||
|
* The associated claim order
|
||||||
|
*
|
||||||
|
* @expandable
|
||||||
|
*/
|
||||||
|
claim: OrderClaimDTO
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The actions of the order change
|
* The actions of the order change
|
||||||
|
|||||||
@@ -1304,6 +1304,11 @@ export interface IOrderModuleService extends IModuleService {
|
|||||||
*/
|
*/
|
||||||
cancelOrderChange(orderId: string[], sharedContext?: Context): Promise<void>
|
cancelOrderChange(orderId: string[], sharedContext?: Context): Promise<void>
|
||||||
|
|
||||||
|
previewOrderChange(
|
||||||
|
orderId: string,
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<OrderDTO>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method Represents the completion of an asynchronous operation
|
* This method Represents the completion of an asynchronous operation
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { BigNumber as BigNumberJS } from "bignumber.js"
|
||||||
import { isDefined, trimZeros } from "../common"
|
import { isDefined, trimZeros } from "../common"
|
||||||
import { BigNumber } from "./big-number"
|
import { BigNumber } from "./big-number"
|
||||||
|
|
||||||
@@ -11,6 +12,14 @@ export function createRawPropertiesFromBigNumber(
|
|||||||
exclude?: string[]
|
exclude?: string[]
|
||||||
} = {}
|
} = {}
|
||||||
) {
|
) {
|
||||||
|
const isBigNumber = (value) => {
|
||||||
|
return (
|
||||||
|
typeof value === "object" &&
|
||||||
|
isDefined(value.raw_) &&
|
||||||
|
isDefined(value.numeric_)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const stack = [{ current: obj, path: "" }]
|
const stack = [{ current: obj, path: "" }]
|
||||||
|
|
||||||
while (stack.length > 0) {
|
while (stack.length > 0) {
|
||||||
@@ -19,7 +28,8 @@ export function createRawPropertiesFromBigNumber(
|
|||||||
if (
|
if (
|
||||||
current == null ||
|
current == null ||
|
||||||
typeof current !== "object" ||
|
typeof current !== "object" ||
|
||||||
current instanceof BigNumber
|
isBigNumber(current) ||
|
||||||
|
path.includes("." + prefix)
|
||||||
) {
|
) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -30,16 +40,17 @@ export function createRawPropertiesFromBigNumber(
|
|||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
for (const key of Object.keys(current)) {
|
for (const key of Object.keys(current)) {
|
||||||
const value = current[key]
|
let value = current[key]
|
||||||
const currentPath = path ? `${path}.${key}` : key
|
const currentPath = path ? `${path}.${key}` : key
|
||||||
|
|
||||||
if (value != null && !exclude.includes(currentPath)) {
|
if (value != null && !exclude.includes(currentPath)) {
|
||||||
const isBigNumber =
|
const isBigNumberJS = BigNumberJS.isBigNumber(value)
|
||||||
typeof value === "object" &&
|
if (isBigNumberJS) {
|
||||||
isDefined(value.raw_) &&
|
current[key] = new BigNumber(current[key])
|
||||||
isDefined(value.numeric_)
|
value = current[key]
|
||||||
|
}
|
||||||
|
|
||||||
if (isBigNumber) {
|
if (isBigNumber(value)) {
|
||||||
const newKey = prefix + key
|
const newKey = prefix + key
|
||||||
const newPath = path ? `${path}.${newKey}` : newKey
|
const newPath = path ? `${path}.${newKey}` : newKey
|
||||||
if (!exclude.includes(newPath)) {
|
if (!exclude.includes(newPath)) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { CreateOrderDTO, IOrderModuleService } from "@medusajs/types"
|
import { CreateOrderDTO, IOrderModuleService } from "@medusajs/types"
|
||||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
|
||||||
import { Modules } from "@medusajs/utils"
|
import { Modules } from "@medusajs/utils"
|
||||||
|
import { SuiteOptions, moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||||
|
|
||||||
jest.setTimeout(100000)
|
jest.setTimeout(100000)
|
||||||
|
|
||||||
|
|||||||
@@ -92,6 +92,9 @@ describe("Order Exchange - Actions", function () {
|
|||||||
const changes = calculateOrderChange({
|
const changes = calculateOrderChange({
|
||||||
order: originalOrder,
|
order: originalOrder,
|
||||||
actions: actions,
|
actions: actions,
|
||||||
|
options: {
|
||||||
|
addActionReferenceToObject: true,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const sumToJSON = JSON.parse(JSON.stringify(changes.summary))
|
const sumToJSON = JSON.parse(JSON.stringify(changes.summary))
|
||||||
@@ -151,11 +154,65 @@ describe("Order Exchange - Actions", function () {
|
|||||||
return_dismissed_quantity: 0,
|
return_dismissed_quantity: 0,
|
||||||
written_off_quantity: 0,
|
written_off_quantity: 0,
|
||||||
},
|
},
|
||||||
|
actions: [
|
||||||
|
{
|
||||||
|
action: "RETURN_ITEM",
|
||||||
|
reference_id: "return_123",
|
||||||
|
details: {
|
||||||
|
reference_id: "3",
|
||||||
|
quantity: 1,
|
||||||
|
},
|
||||||
|
amount: "20",
|
||||||
|
},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "item_555",
|
id: "item_555",
|
||||||
unit_price: 50,
|
unit_price: 50,
|
||||||
quantity: 1,
|
quantity: 1,
|
||||||
|
detail: {},
|
||||||
|
actions: [
|
||||||
|
{
|
||||||
|
action: "ITEM_ADD",
|
||||||
|
details: {
|
||||||
|
reference_id: "item_555",
|
||||||
|
unit_price: 50,
|
||||||
|
quantity: 1,
|
||||||
|
},
|
||||||
|
amount: "50",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(changes.order.shipping_methods).toEqual([
|
||||||
|
{
|
||||||
|
id: "ship_123",
|
||||||
|
price: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "shipping_345",
|
||||||
|
price: 5,
|
||||||
|
detail: {},
|
||||||
|
actions: [
|
||||||
|
{
|
||||||
|
action: "SHIPPING_ADD",
|
||||||
|
reference_id: "shipping_345",
|
||||||
|
amount: 5,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "return_shipping_345",
|
||||||
|
price: 7.5,
|
||||||
|
detail: {},
|
||||||
|
actions: [
|
||||||
|
{
|
||||||
|
action: "SHIPPING_ADD",
|
||||||
|
reference_id: "return_shipping_345",
|
||||||
|
amount: 7.5,
|
||||||
|
},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -174,6 +174,7 @@ async function processAdditionalItems(
|
|||||||
reference_id: item.id,
|
reference_id: item.id,
|
||||||
claim_id: claimReference.id,
|
claim_id: claimReference.id,
|
||||||
quantity: addedItem.quantity,
|
quantity: addedItem.quantity,
|
||||||
|
unit_price: item.unit_price,
|
||||||
metadata: addedItem.metadata,
|
metadata: addedItem.metadata,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -129,7 +129,9 @@ async function processAdditionalItems(
|
|||||||
|
|
||||||
createItems.forEach((item, index) => {
|
createItems.forEach((item, index) => {
|
||||||
const addedItem = itemsToAdd[index]
|
const addedItem = itemsToAdd[index]
|
||||||
|
|
||||||
additionalNewItems[index].item_id = item.id
|
additionalNewItems[index].item_id = item.id
|
||||||
|
|
||||||
actions.push({
|
actions.push({
|
||||||
action: ChangeActionType.ITEM_ADD,
|
action: ChangeActionType.ITEM_ADD,
|
||||||
exchange_id: exchangeReference.id,
|
exchange_id: exchangeReference.id,
|
||||||
@@ -140,6 +142,7 @@ async function processAdditionalItems(
|
|||||||
reference_id: item.id,
|
reference_id: item.id,
|
||||||
exchange_id: exchangeReference.id,
|
exchange_id: exchangeReference.id,
|
||||||
quantity: addedItem.quantity,
|
quantity: addedItem.quantity,
|
||||||
|
unit_price: item.unit_price,
|
||||||
metadata: addedItem.metadata,
|
metadata: addedItem.metadata,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
BigNumber,
|
BigNumber,
|
||||||
createRawPropertiesFromBigNumber,
|
createRawPropertiesFromBigNumber,
|
||||||
|
DecorateCartLikeInputDTO,
|
||||||
decorateCartTotals,
|
decorateCartTotals,
|
||||||
deduplicate,
|
deduplicate,
|
||||||
InjectManager,
|
InjectManager,
|
||||||
@@ -307,6 +308,14 @@ export default class OrderModuleService<
|
|||||||
|
|
||||||
const order = await super.retrieveOrder(id, config, sharedContext)
|
const order = await super.retrieveOrder(id, config, sharedContext)
|
||||||
|
|
||||||
|
const orderChange = await this.getActiveOrderChange_(
|
||||||
|
order.id,
|
||||||
|
false,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
order.order_change = orderChange
|
||||||
|
|
||||||
return formatOrder(order, {
|
return formatOrder(order, {
|
||||||
entity: Order,
|
entity: Order,
|
||||||
includeTotals,
|
includeTotals,
|
||||||
@@ -1795,6 +1804,58 @@ export default class OrderModuleService<
|
|||||||
return await this.orderChangeService_.create(input, sharedContext)
|
return await this.orderChangeService_.create(input, sharedContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async previewOrderChange(orderChangeId: string, sharedContext?: Context) {
|
||||||
|
const orderChange = await super.retrieveOrderChange(
|
||||||
|
orderChangeId,
|
||||||
|
{ relations: ["actions"] },
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
orderChange.actions = orderChange.actions.map((action) => {
|
||||||
|
return {
|
||||||
|
...action,
|
||||||
|
version: orderChange.version,
|
||||||
|
order_id: orderChange.order_id,
|
||||||
|
return_id: orderChange.return_id,
|
||||||
|
claim_id: orderChange.claim_id,
|
||||||
|
exchange_id: orderChange.exchange_id,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const order = await this.retrieveOrder(
|
||||||
|
orderChange.order_id,
|
||||||
|
{
|
||||||
|
select: ["id", "version", "items.detail", "summary", "total"],
|
||||||
|
relations: [
|
||||||
|
"transactions",
|
||||||
|
"items",
|
||||||
|
"items.detail",
|
||||||
|
"shipping_methods",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
const calculated = calculateOrderChange({
|
||||||
|
order: order as any,
|
||||||
|
actions: orderChange.actions,
|
||||||
|
transactions: order.transactions ?? [],
|
||||||
|
options: {
|
||||||
|
addActionReferenceToObject: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
createRawPropertiesFromBigNumber(calculated)
|
||||||
|
|
||||||
|
const calcOrder = calculated.order as any
|
||||||
|
decorateCartTotals(calcOrder as DecorateCartLikeInputDTO)
|
||||||
|
calcOrder.summary = calculated.summary
|
||||||
|
|
||||||
|
createRawPropertiesFromBigNumber(calcOrder)
|
||||||
|
|
||||||
|
return calcOrder
|
||||||
|
}
|
||||||
|
|
||||||
async cancelOrderChange(
|
async cancelOrderChange(
|
||||||
orderId: string,
|
orderId: string,
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
@@ -2123,6 +2184,50 @@ export default class OrderModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async getActiveOrderChange_(
|
||||||
|
orderId: string,
|
||||||
|
includeActions: boolean,
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<any> {
|
||||||
|
const options = {
|
||||||
|
select: [
|
||||||
|
"id",
|
||||||
|
"change_type",
|
||||||
|
"order_id",
|
||||||
|
"return_id",
|
||||||
|
"claim_id",
|
||||||
|
"exchange_id",
|
||||||
|
"version",
|
||||||
|
"requested_at",
|
||||||
|
"requested_by",
|
||||||
|
"status",
|
||||||
|
],
|
||||||
|
relations: [] as string[],
|
||||||
|
order: {},
|
||||||
|
}
|
||||||
|
|
||||||
|
if (includeActions) {
|
||||||
|
options.select.push("actions")
|
||||||
|
options.relations.push("actions")
|
||||||
|
options.order = {
|
||||||
|
actions: {
|
||||||
|
ordering: "ASC",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const [orderChange] = await this.listOrderChanges(
|
||||||
|
{
|
||||||
|
order_id: orderId,
|
||||||
|
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
|
||||||
|
},
|
||||||
|
options,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
return orderChange
|
||||||
|
}
|
||||||
|
|
||||||
private async getAndValidateOrderChange_(
|
private async getAndValidateOrderChange_(
|
||||||
orderChangeIds: string[],
|
orderChangeIds: string[],
|
||||||
includeActions: boolean,
|
includeActions: boolean,
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ export type VirtualOrder = {
|
|||||||
claim_id?: string
|
claim_id?: string
|
||||||
exchange_id?: string
|
exchange_id?: string
|
||||||
|
|
||||||
|
item_id?: string
|
||||||
quantity: BigNumberInput
|
quantity: BigNumberInput
|
||||||
shipped_quantity: BigNumberInput
|
shipped_quantity: BigNumberInput
|
||||||
fulfilled_quantity: BigNumberInput
|
fulfilled_quantity: BigNumberInput
|
||||||
@@ -115,6 +116,10 @@ export type OrderReferences = {
|
|||||||
transactions: OrderTransaction[]
|
transactions: OrderTransaction[]
|
||||||
type: ActionTypeDefinition
|
type: ActionTypeDefinition
|
||||||
actions: InternalOrderChangeEvent[]
|
actions: InternalOrderChangeEvent[]
|
||||||
|
options?: {
|
||||||
|
addActionReferenceToObject?: boolean
|
||||||
|
[key: string]: unknown
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ActionTypeDefinition {
|
export interface ActionTypeDefinition {
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||||
import { ChangeActionType } from "../action-key"
|
import { ChangeActionType } from "../action-key"
|
||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import {
|
||||||
|
setActionReference,
|
||||||
|
unsetActionReference,
|
||||||
|
} from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(
|
OrderChangeProcessing.registerActionType(
|
||||||
ChangeActionType.CANCEL_ITEM_FULFILLMENT,
|
ChangeActionType.CANCEL_ITEM_FULFILLMENT,
|
||||||
{
|
{
|
||||||
operation({ action, currentOrder }) {
|
operation({ action, currentOrder, options }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
(item) => item.id === action.details.reference_id
|
(item) => item.id === action.details.reference_id
|
||||||
)!
|
)!
|
||||||
@@ -18,7 +21,7 @@ OrderChangeProcessing.registerActionType(
|
|||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
setActionReference(existing, action)
|
setActionReference(existing, action, options)
|
||||||
},
|
},
|
||||||
revert({ action, currentOrder }) {
|
revert({ action, currentOrder }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
@@ -29,6 +32,8 @@ OrderChangeProcessing.registerActionType(
|
|||||||
existing.detail.fulfilled_quantity,
|
existing.detail.fulfilled_quantity,
|
||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
|
unsetActionReference(existing, action)
|
||||||
},
|
},
|
||||||
validate({ action, currentOrder }) {
|
validate({ action, currentOrder }) {
|
||||||
const refId = action.details?.reference_id
|
const refId = action.details?.reference_id
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||||
import { ChangeActionType } from "../action-key"
|
import { ChangeActionType } from "../action-key"
|
||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import {
|
||||||
|
setActionReference,
|
||||||
|
unsetActionReference,
|
||||||
|
} from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(ChangeActionType.CANCEL_RETURN_ITEM, {
|
OrderChangeProcessing.registerActionType(ChangeActionType.CANCEL_RETURN_ITEM, {
|
||||||
operation({ action, currentOrder }) {
|
operation({ action, currentOrder, options }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
(item) => item.id === action.details.reference_id
|
(item) => item.id === action.details.reference_id
|
||||||
)!
|
)!
|
||||||
@@ -16,7 +19,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.CANCEL_RETURN_ITEM, {
|
|||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
setActionReference(existing, action)
|
setActionReference(existing, action, options)
|
||||||
|
|
||||||
return action.details.unit_price * action.details.quantity
|
return action.details.unit_price * action.details.quantity
|
||||||
},
|
},
|
||||||
@@ -29,6 +32,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.CANCEL_RETURN_ITEM, {
|
|||||||
existing.detail.return_requested_quantity,
|
existing.detail.return_requested_quantity,
|
||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
|
unsetActionReference(existing, action)
|
||||||
},
|
},
|
||||||
validate({ action, currentOrder }) {
|
validate({ action, currentOrder }) {
|
||||||
const refId = action.details?.reference_id
|
const refId = action.details?.reference_id
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||||
import { ChangeActionType } from "../action-key"
|
import { ChangeActionType } from "../action-key"
|
||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import {
|
||||||
|
setActionReference,
|
||||||
|
unsetActionReference,
|
||||||
|
} from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(ChangeActionType.FULFILL_ITEM, {
|
OrderChangeProcessing.registerActionType(ChangeActionType.FULFILL_ITEM, {
|
||||||
operation({ action, currentOrder }) {
|
operation({ action, currentOrder, options }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
(item) => item.id === action.details.reference_id
|
(item) => item.id === action.details.reference_id
|
||||||
)!
|
)!
|
||||||
@@ -16,7 +19,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.FULFILL_ITEM, {
|
|||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
setActionReference(existing, action)
|
setActionReference(existing, action, options)
|
||||||
},
|
},
|
||||||
revert({ action, currentOrder }) {
|
revert({ action, currentOrder }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
@@ -27,6 +30,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.FULFILL_ITEM, {
|
|||||||
existing.detail.fulfilled_quantity,
|
existing.detail.fulfilled_quantity,
|
||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
|
unsetActionReference(existing, action)
|
||||||
},
|
},
|
||||||
validate({ action, currentOrder }) {
|
validate({ action, currentOrder }) {
|
||||||
const refId = action.details?.reference_id
|
const refId = action.details?.reference_id
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
|||||||
import { VirtualOrder } from "@types"
|
import { VirtualOrder } from "@types"
|
||||||
import { ChangeActionType } from "../action-key"
|
import { ChangeActionType } from "../action-key"
|
||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import {
|
||||||
|
setActionReference,
|
||||||
|
unsetActionReference,
|
||||||
|
} from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
||||||
operation({ action, currentOrder }) {
|
operation({ action, currentOrder, options }) {
|
||||||
const existing = currentOrder.items.find(
|
let existing = currentOrder.items.find(
|
||||||
(item) => item.id === action.details.reference_id
|
(item) => item.id === action.details.reference_id
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,10 +22,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
|||||||
existing.detail.quantity,
|
existing.detail.quantity,
|
||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
setActionReference(existing, action)
|
|
||||||
} else {
|
} else {
|
||||||
currentOrder.items.push({
|
existing = {
|
||||||
id: action.details.reference_id!,
|
id: action.details.reference_id!,
|
||||||
order_id: currentOrder.id,
|
order_id: currentOrder.id,
|
||||||
return_id: action.details.return_id,
|
return_id: action.details.return_id,
|
||||||
@@ -31,9 +32,13 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
|||||||
|
|
||||||
unit_price: action.details.unit_price,
|
unit_price: action.details.unit_price,
|
||||||
quantity: action.details.quantity,
|
quantity: action.details.quantity,
|
||||||
} as VirtualOrder["items"][0])
|
} as VirtualOrder["items"][0]
|
||||||
|
|
||||||
|
currentOrder.items.push(existing)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setActionReference(existing, action, options)
|
||||||
|
|
||||||
return MathBN.mult(action.details.unit_price, action.details.quantity)
|
return MathBN.mult(action.details.unit_price, action.details.quantity)
|
||||||
},
|
},
|
||||||
revert({ action, currentOrder }) {
|
revert({ action, currentOrder }) {
|
||||||
@@ -52,6 +57,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
|||||||
if (MathBN.lte(existing.quantity, 0)) {
|
if (MathBN.lte(existing.quantity, 0)) {
|
||||||
currentOrder.items.splice(existingIndex, 1)
|
currentOrder.items.splice(existingIndex, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsetActionReference(existing, action)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
validate({ action }) {
|
validate({ action }) {
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
|||||||
import { VirtualOrder } from "@types"
|
import { VirtualOrder } from "@types"
|
||||||
import { ChangeActionType } from "../action-key"
|
import { ChangeActionType } from "../action-key"
|
||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import {
|
||||||
|
setActionReference,
|
||||||
|
unsetActionReference,
|
||||||
|
} from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, {
|
OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, {
|
||||||
isDeduction: true,
|
isDeduction: true,
|
||||||
operation({ action, currentOrder }) {
|
operation({ action, currentOrder, options }) {
|
||||||
const existingIndex = currentOrder.items.findIndex(
|
const existingIndex = currentOrder.items.findIndex(
|
||||||
(item) => item.id === action.details.reference_id
|
(item) => item.id === action.details.reference_id
|
||||||
)
|
)
|
||||||
@@ -21,7 +24,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, {
|
|||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
setActionReference(existing, action)
|
setActionReference(existing, action, options)
|
||||||
|
|
||||||
if (MathBN.lte(existing.quantity, 0)) {
|
if (MathBN.lte(existing.quantity, 0)) {
|
||||||
currentOrder.items.splice(existingIndex, 1)
|
currentOrder.items.splice(existingIndex, 1)
|
||||||
@@ -40,6 +43,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, {
|
|||||||
existing.detail.quantity,
|
existing.detail.quantity,
|
||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
|
unsetActionReference(existing, action)
|
||||||
} else {
|
} else {
|
||||||
currentOrder.items.push({
|
currentOrder.items.push({
|
||||||
id: action.details.reference_id!,
|
id: action.details.reference_id!,
|
||||||
|
|||||||
@@ -2,14 +2,17 @@ import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
|||||||
import { EVENT_STATUS } from "@types"
|
import { EVENT_STATUS } from "@types"
|
||||||
import { ChangeActionType } from "../action-key"
|
import { ChangeActionType } from "../action-key"
|
||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import {
|
||||||
|
setActionReference,
|
||||||
|
unsetActionReference,
|
||||||
|
} from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(
|
OrderChangeProcessing.registerActionType(
|
||||||
ChangeActionType.RECEIVE_DAMAGED_RETURN_ITEM,
|
ChangeActionType.RECEIVE_DAMAGED_RETURN_ITEM,
|
||||||
{
|
{
|
||||||
isDeduction: true,
|
isDeduction: true,
|
||||||
commitsAction: "return_item",
|
commitsAction: "return_item",
|
||||||
operation({ action, currentOrder, previousEvents }) {
|
operation({ action, currentOrder, previousEvents, options }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
(item) => item.id === action.details.reference_id
|
(item) => item.id === action.details.reference_id
|
||||||
)!
|
)!
|
||||||
@@ -28,7 +31,7 @@ OrderChangeProcessing.registerActionType(
|
|||||||
toReturn
|
toReturn
|
||||||
)
|
)
|
||||||
|
|
||||||
setActionReference(existing, action)
|
setActionReference(existing, action, options)
|
||||||
|
|
||||||
if (previousEvents) {
|
if (previousEvents) {
|
||||||
for (const previousEvent of previousEvents) {
|
for (const previousEvent of previousEvents) {
|
||||||
@@ -63,6 +66,8 @@ OrderChangeProcessing.registerActionType(
|
|||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
|
unsetActionReference(existing, action)
|
||||||
|
|
||||||
if (previousEvents) {
|
if (previousEvents) {
|
||||||
for (const previousEvent of previousEvents) {
|
for (const previousEvent of previousEvents) {
|
||||||
if (!previousEvent.original_) {
|
if (!previousEvent.original_) {
|
||||||
|
|||||||
@@ -7,12 +7,15 @@ import {
|
|||||||
import { EVENT_STATUS } from "@types"
|
import { EVENT_STATUS } from "@types"
|
||||||
import { ChangeActionType } from "../action-key"
|
import { ChangeActionType } from "../action-key"
|
||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import {
|
||||||
|
setActionReference,
|
||||||
|
unsetActionReference,
|
||||||
|
} from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(ChangeActionType.RECEIVE_RETURN_ITEM, {
|
OrderChangeProcessing.registerActionType(ChangeActionType.RECEIVE_RETURN_ITEM, {
|
||||||
isDeduction: true,
|
isDeduction: true,
|
||||||
commitsAction: "return_item",
|
commitsAction: "return_item",
|
||||||
operation({ action, currentOrder, previousEvents }) {
|
operation({ action, currentOrder, previousEvents, options }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
(item) => item.id === action.details.reference_id
|
(item) => item.id === action.details.reference_id
|
||||||
)!
|
)!
|
||||||
@@ -31,7 +34,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RECEIVE_RETURN_ITEM, {
|
|||||||
toReturn
|
toReturn
|
||||||
)
|
)
|
||||||
|
|
||||||
setActionReference(existing, action)
|
setActionReference(existing, action, options)
|
||||||
|
|
||||||
if (previousEvents) {
|
if (previousEvents) {
|
||||||
for (const previousEvent of previousEvents) {
|
for (const previousEvent of previousEvents) {
|
||||||
@@ -67,6 +70,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RECEIVE_RETURN_ITEM, {
|
|||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
|
unsetActionReference(existing, action)
|
||||||
|
|
||||||
if (previousEvents) {
|
if (previousEvents) {
|
||||||
for (const previousEvent of previousEvents) {
|
for (const previousEvent of previousEvents) {
|
||||||
if (!previousEvent.original_) {
|
if (!previousEvent.original_) {
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||||
import { ChangeActionType } from "../action-key"
|
import { ChangeActionType } from "../action-key"
|
||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import {
|
||||||
|
setActionReference,
|
||||||
|
unsetActionReference,
|
||||||
|
} from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(ChangeActionType.REINSTATE_ITEM, {
|
OrderChangeProcessing.registerActionType(ChangeActionType.REINSTATE_ITEM, {
|
||||||
operation({ action, currentOrder }) {
|
operation({ action, currentOrder, options }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
(item) => item.id === action.details.reference_id
|
(item) => item.id === action.details.reference_id
|
||||||
)!
|
)!
|
||||||
@@ -15,7 +18,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.REINSTATE_ITEM, {
|
|||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
setActionReference(existing, action)
|
setActionReference(existing, action, options)
|
||||||
},
|
},
|
||||||
revert({ action, currentOrder }) {
|
revert({ action, currentOrder }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
@@ -26,6 +29,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.REINSTATE_ITEM, {
|
|||||||
existing.detail.written_off_quantity,
|
existing.detail.written_off_quantity,
|
||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
|
unsetActionReference(existing, action)
|
||||||
},
|
},
|
||||||
validate({ action, currentOrder }) {
|
validate({ action, currentOrder }) {
|
||||||
const refId = action.details?.reference_id
|
const refId = action.details?.reference_id
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||||
import { ChangeActionType } from "../action-key"
|
import { ChangeActionType } from "../action-key"
|
||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import {
|
||||||
|
setActionReference,
|
||||||
|
unsetActionReference,
|
||||||
|
} from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(ChangeActionType.RETURN_ITEM, {
|
OrderChangeProcessing.registerActionType(ChangeActionType.RETURN_ITEM, {
|
||||||
isDeduction: true,
|
isDeduction: true,
|
||||||
awaitRequired: true,
|
awaitRequired: true,
|
||||||
operation({ action, currentOrder }) {
|
operation({ action, currentOrder, options }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
(item) => item.id === action.details.reference_id
|
(item) => item.id === action.details.reference_id
|
||||||
)!
|
)!
|
||||||
@@ -17,7 +20,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RETURN_ITEM, {
|
|||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
setActionReference(existing, action)
|
setActionReference(existing, action, options)
|
||||||
|
|
||||||
return MathBN.mult(existing.unit_price, action.details.quantity)
|
return MathBN.mult(existing.unit_price, action.details.quantity)
|
||||||
},
|
},
|
||||||
@@ -30,6 +33,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RETURN_ITEM, {
|
|||||||
existing.detail.return_requested_quantity,
|
existing.detail.return_requested_quantity,
|
||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
|
unsetActionReference(existing, action)
|
||||||
},
|
},
|
||||||
validate({ action, currentOrder }) {
|
validate({ action, currentOrder }) {
|
||||||
const refId = action.details?.reference_id
|
const refId = action.details?.reference_id
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||||
import { ChangeActionType } from "../action-key"
|
import { ChangeActionType } from "../action-key"
|
||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import {
|
||||||
|
setActionReference,
|
||||||
|
unsetActionReference,
|
||||||
|
} from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(ChangeActionType.SHIP_ITEM, {
|
OrderChangeProcessing.registerActionType(ChangeActionType.SHIP_ITEM, {
|
||||||
operation({ action, currentOrder }) {
|
operation({ action, currentOrder, options }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
(item) => item.id === action.details.reference_id
|
(item) => item.id === action.details.reference_id
|
||||||
)!
|
)!
|
||||||
@@ -16,7 +19,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIP_ITEM, {
|
|||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
setActionReference(existing, action)
|
setActionReference(existing, action, options)
|
||||||
},
|
},
|
||||||
revert({ action, currentOrder }) {
|
revert({ action, currentOrder }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
@@ -27,6 +30,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIP_ITEM, {
|
|||||||
existing.detail.shipped_quantity,
|
existing.detail.shipped_quantity,
|
||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
|
unsetActionReference(existing, action)
|
||||||
},
|
},
|
||||||
validate({ action, currentOrder }) {
|
validate({ action, currentOrder }) {
|
||||||
const refId = action.details?.reference_id
|
const refId = action.details?.reference_id
|
||||||
|
|||||||
@@ -4,26 +4,26 @@ import { OrderChangeProcessing } from "../calculate-order-change"
|
|||||||
import { setActionReference } from "../set-action-reference"
|
import { setActionReference } from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, {
|
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, {
|
||||||
operation({ action, currentOrder }) {
|
operation({ action, currentOrder, options }) {
|
||||||
const shipping = Array.isArray(currentOrder.shipping_methods)
|
const shipping = Array.isArray(currentOrder.shipping_methods)
|
||||||
? currentOrder.shipping_methods
|
? currentOrder.shipping_methods
|
||||||
: [currentOrder.shipping_methods]
|
: [currentOrder.shipping_methods]
|
||||||
|
|
||||||
const existing = shipping.find((sh) => sh.id === action.reference_id)
|
let existing = shipping.find((sh) => sh.id === action.reference_id)
|
||||||
|
|
||||||
if (existing) {
|
if (!existing) {
|
||||||
setActionReference(existing, action)
|
existing = {
|
||||||
} else {
|
|
||||||
shipping.push({
|
|
||||||
id: action.reference_id!,
|
id: action.reference_id!,
|
||||||
order_id: currentOrder.id,
|
order_id: currentOrder.id,
|
||||||
return_id: action.return_id,
|
return_id: action.return_id,
|
||||||
claim_id: action.claim_id,
|
claim_id: action.claim_id,
|
||||||
exchange_id: action.exchange_id,
|
exchange_id: action.exchange_id,
|
||||||
price: action.amount as number,
|
price: action.amount as number,
|
||||||
})
|
}
|
||||||
|
shipping.push(existing)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setActionReference(existing, action, options)
|
||||||
currentOrder.shipping_methods = shipping
|
currentOrder.shipping_methods = shipping
|
||||||
},
|
},
|
||||||
revert({ action, currentOrder }) {
|
revert({ action, currentOrder }) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { ChangeActionType } from "../action-key"
|
|||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_REMOVE, {
|
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_REMOVE, {
|
||||||
operation({ action, currentOrder }) {
|
operation({ action, currentOrder, options }) {
|
||||||
const shipping = Array.isArray(currentOrder.shipping_methods)
|
const shipping = Array.isArray(currentOrder.shipping_methods)
|
||||||
? currentOrder.shipping_methods
|
? currentOrder.shipping_methods
|
||||||
: [currentOrder.shipping_methods]
|
: [currentOrder.shipping_methods]
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||||
import { ChangeActionType } from "../action-key"
|
import { ChangeActionType } from "../action-key"
|
||||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||||
import { setActionReference } from "../set-action-reference"
|
import {
|
||||||
|
setActionReference,
|
||||||
|
unsetActionReference,
|
||||||
|
} from "../set-action-reference"
|
||||||
|
|
||||||
OrderChangeProcessing.registerActionType(ChangeActionType.WRITE_OFF_ITEM, {
|
OrderChangeProcessing.registerActionType(ChangeActionType.WRITE_OFF_ITEM, {
|
||||||
operation({ action, currentOrder }) {
|
operation({ action, currentOrder, options }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
(item) => item.id === action.details.reference_id
|
(item) => item.id === action.details.reference_id
|
||||||
)!
|
)!
|
||||||
@@ -15,7 +18,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.WRITE_OFF_ITEM, {
|
|||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
setActionReference(existing, action)
|
setActionReference(existing, action, options)
|
||||||
},
|
},
|
||||||
revert({ action, currentOrder }) {
|
revert({ action, currentOrder }) {
|
||||||
const existing = currentOrder.items.find(
|
const existing = currentOrder.items.find(
|
||||||
@@ -26,6 +29,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.WRITE_OFF_ITEM, {
|
|||||||
existing.detail.written_off_quantity,
|
existing.detail.written_off_quantity,
|
||||||
action.details.quantity
|
action.details.quantity
|
||||||
)
|
)
|
||||||
|
|
||||||
|
unsetActionReference(existing, action)
|
||||||
},
|
},
|
||||||
validate({ action, currentOrder }) {
|
validate({ action, currentOrder }) {
|
||||||
const refId = action.details?.reference_id
|
const refId = action.details?.reference_id
|
||||||
|
|||||||
@@ -31,8 +31,9 @@ export function applyChangesToOrder(
|
|||||||
const version = actionsMap[order.id][0].version ?? 1
|
const version = actionsMap[order.id][0].version ?? 1
|
||||||
|
|
||||||
for (const item of calculated.order.items) {
|
for (const item of calculated.order.items) {
|
||||||
const orderItem = (item.detail as any) ?? item
|
const isExistingItem = item.id === item.detail?.item_id
|
||||||
const itemId = item.detail ? orderItem.item_id : item.id
|
const orderItem = isExistingItem ? (item.detail as any) : item
|
||||||
|
const itemId = isExistingItem ? orderItem.item_id : item.id
|
||||||
|
|
||||||
itemsToUpsert.push({
|
itemsToUpsert.push({
|
||||||
id: orderItem.version === version ? orderItem.id : undefined,
|
id: orderItem.version === version ? orderItem.id : undefined,
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ import {
|
|||||||
interface InternalOrderSummary extends OrderSummaryCalculated {
|
interface InternalOrderSummary extends OrderSummaryCalculated {
|
||||||
future_temporary_sum: BigNumberInput
|
future_temporary_sum: BigNumberInput
|
||||||
}
|
}
|
||||||
|
interface ProcessOptions {
|
||||||
|
addActionReferenceToObject?: boolean
|
||||||
|
[key: string]: any
|
||||||
|
}
|
||||||
|
|
||||||
export class OrderChangeProcessing {
|
export class OrderChangeProcessing {
|
||||||
private static typeDefinition: { [key: string]: ActionTypeDefinition } = {}
|
private static typeDefinition: { [key: string]: ActionTypeDefinition } = {}
|
||||||
@@ -30,6 +34,7 @@ export class OrderChangeProcessing {
|
|||||||
private order: VirtualOrder
|
private order: VirtualOrder
|
||||||
private transactions: OrderTransaction[]
|
private transactions: OrderTransaction[]
|
||||||
private actions: InternalOrderChangeEvent[]
|
private actions: InternalOrderChangeEvent[]
|
||||||
|
private options: ProcessOptions = {}
|
||||||
|
|
||||||
private actionsProcessed: { [key: string]: InternalOrderChangeEvent[] } = {}
|
private actionsProcessed: { [key: string]: InternalOrderChangeEvent[] } = {}
|
||||||
private groupTotal: Record<string, BigNumberInput> = {}
|
private groupTotal: Record<string, BigNumberInput> = {}
|
||||||
@@ -43,14 +48,17 @@ export class OrderChangeProcessing {
|
|||||||
order,
|
order,
|
||||||
transactions,
|
transactions,
|
||||||
actions,
|
actions,
|
||||||
|
options,
|
||||||
}: {
|
}: {
|
||||||
order: VirtualOrder
|
order: VirtualOrder
|
||||||
transactions: OrderTransaction[]
|
transactions: OrderTransaction[]
|
||||||
actions: InternalOrderChangeEvent[]
|
actions: InternalOrderChangeEvent[]
|
||||||
|
options: ProcessOptions
|
||||||
}) {
|
}) {
|
||||||
this.order = JSON.parse(JSON.stringify(order))
|
this.order = JSON.parse(JSON.stringify(order))
|
||||||
this.transactions = JSON.parse(JSON.stringify(transactions ?? []))
|
this.transactions = JSON.parse(JSON.stringify(transactions ?? []))
|
||||||
this.actions = JSON.parse(JSON.stringify(actions ?? []))
|
this.actions = JSON.parse(JSON.stringify(actions ?? []))
|
||||||
|
this.options = options
|
||||||
|
|
||||||
let paid = MathBN.convert(0)
|
let paid = MathBN.convert(0)
|
||||||
let refunded = MathBN.convert(0)
|
let refunded = MathBN.convert(0)
|
||||||
@@ -218,6 +226,7 @@ export class OrderChangeProcessing {
|
|||||||
summary: this.summary,
|
summary: this.summary,
|
||||||
transactions: this.transactions,
|
transactions: this.transactions,
|
||||||
type,
|
type,
|
||||||
|
options: this.options,
|
||||||
}
|
}
|
||||||
if (typeof type.validate === "function") {
|
if (typeof type.validate === "function") {
|
||||||
type.validate(params)
|
type.validate(params)
|
||||||
@@ -393,12 +402,19 @@ export function calculateOrderChange({
|
|||||||
order,
|
order,
|
||||||
transactions = [],
|
transactions = [],
|
||||||
actions = [],
|
actions = [],
|
||||||
|
options = {},
|
||||||
}: {
|
}: {
|
||||||
order: VirtualOrder
|
order: VirtualOrder
|
||||||
transactions?: OrderTransaction[]
|
transactions?: OrderTransaction[]
|
||||||
actions?: OrderChangeEvent[]
|
actions?: OrderChangeEvent[]
|
||||||
|
options?: ProcessOptions
|
||||||
}) {
|
}) {
|
||||||
const calc = new OrderChangeProcessing({ order, transactions, actions })
|
const calc = new OrderChangeProcessing({
|
||||||
|
order,
|
||||||
|
transactions,
|
||||||
|
actions,
|
||||||
|
options,
|
||||||
|
})
|
||||||
calc.processActions()
|
calc.processActions()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -1,6 +1,19 @@
|
|||||||
export function setActionReference(existing, action) {
|
export function setActionReference(existing, action, options) {
|
||||||
|
existing.detail ??= {}
|
||||||
|
|
||||||
existing.detail.order_id ??= action.order_id
|
existing.detail.order_id ??= action.order_id
|
||||||
existing.detail.return_id ??= action.return_id
|
existing.detail.return_id ??= action.return_id
|
||||||
existing.detail.claim_id ??= action.claim_id
|
existing.detail.claim_id ??= action.claim_id
|
||||||
existing.detail.exchange_id ??= action.exchange_id
|
existing.detail.exchange_id ??= action.exchange_id
|
||||||
|
|
||||||
|
if (options?.addActionReferenceToObject) {
|
||||||
|
existing.actions ??= []
|
||||||
|
existing.actions.push(action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function unsetActionReference(existing, action) {
|
||||||
|
if (Array.isArray(existing?.actions)) {
|
||||||
|
existing.actions = existing.actions.filter((a) => a.id !== action.id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user