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:
committed by
GitHub
parent
4736d9e2dd
commit
2b2e2fbb3d
@@ -1,12 +1,15 @@
|
||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { ChangeActionType } from "../action-key"
|
||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||
import { setActionReference } from "../set-action-reference"
|
||||
import {
|
||||
setActionReference,
|
||||
unsetActionReference,
|
||||
} from "../set-action-reference"
|
||||
|
||||
OrderChangeProcessing.registerActionType(
|
||||
ChangeActionType.CANCEL_ITEM_FULFILLMENT,
|
||||
{
|
||||
operation({ action, currentOrder }) {
|
||||
operation({ action, currentOrder, options }) {
|
||||
const existing = currentOrder.items.find(
|
||||
(item) => item.id === action.details.reference_id
|
||||
)!
|
||||
@@ -18,7 +21,7 @@ OrderChangeProcessing.registerActionType(
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
setActionReference(existing, action)
|
||||
setActionReference(existing, action, options)
|
||||
},
|
||||
revert({ action, currentOrder }) {
|
||||
const existing = currentOrder.items.find(
|
||||
@@ -29,6 +32,8 @@ OrderChangeProcessing.registerActionType(
|
||||
existing.detail.fulfilled_quantity,
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
unsetActionReference(existing, action)
|
||||
},
|
||||
validate({ action, currentOrder }) {
|
||||
const refId = action.details?.reference_id
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { ChangeActionType } from "../action-key"
|
||||
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, {
|
||||
operation({ action, currentOrder }) {
|
||||
operation({ action, currentOrder, options }) {
|
||||
const existing = currentOrder.items.find(
|
||||
(item) => item.id === action.details.reference_id
|
||||
)!
|
||||
@@ -16,7 +19,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.CANCEL_RETURN_ITEM, {
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
setActionReference(existing, action)
|
||||
setActionReference(existing, action, options)
|
||||
|
||||
return action.details.unit_price * action.details.quantity
|
||||
},
|
||||
@@ -29,6 +32,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.CANCEL_RETURN_ITEM, {
|
||||
existing.detail.return_requested_quantity,
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
unsetActionReference(existing, action)
|
||||
},
|
||||
validate({ action, currentOrder }) {
|
||||
const refId = action.details?.reference_id
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { ChangeActionType } from "../action-key"
|
||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||
import { setActionReference } from "../set-action-reference"
|
||||
import {
|
||||
setActionReference,
|
||||
unsetActionReference,
|
||||
} from "../set-action-reference"
|
||||
|
||||
OrderChangeProcessing.registerActionType(ChangeActionType.FULFILL_ITEM, {
|
||||
operation({ action, currentOrder }) {
|
||||
operation({ action, currentOrder, options }) {
|
||||
const existing = currentOrder.items.find(
|
||||
(item) => item.id === action.details.reference_id
|
||||
)!
|
||||
@@ -16,7 +19,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.FULFILL_ITEM, {
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
setActionReference(existing, action)
|
||||
setActionReference(existing, action, options)
|
||||
},
|
||||
revert({ action, currentOrder }) {
|
||||
const existing = currentOrder.items.find(
|
||||
@@ -27,6 +30,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.FULFILL_ITEM, {
|
||||
existing.detail.fulfilled_quantity,
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
unsetActionReference(existing, action)
|
||||
},
|
||||
validate({ action, currentOrder }) {
|
||||
const refId = action.details?.reference_id
|
||||
|
||||
@@ -2,11 +2,14 @@ import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { VirtualOrder } from "@types"
|
||||
import { ChangeActionType } from "../action-key"
|
||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||
import { setActionReference } from "../set-action-reference"
|
||||
import {
|
||||
setActionReference,
|
||||
unsetActionReference,
|
||||
} from "../set-action-reference"
|
||||
|
||||
OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
||||
operation({ action, currentOrder }) {
|
||||
const existing = currentOrder.items.find(
|
||||
operation({ action, currentOrder, options }) {
|
||||
let existing = currentOrder.items.find(
|
||||
(item) => item.id === action.details.reference_id
|
||||
)
|
||||
|
||||
@@ -19,10 +22,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
||||
existing.detail.quantity,
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
setActionReference(existing, action)
|
||||
} else {
|
||||
currentOrder.items.push({
|
||||
existing = {
|
||||
id: action.details.reference_id!,
|
||||
order_id: currentOrder.id,
|
||||
return_id: action.details.return_id,
|
||||
@@ -31,9 +32,13 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
||||
|
||||
unit_price: action.details.unit_price,
|
||||
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)
|
||||
},
|
||||
revert({ action, currentOrder }) {
|
||||
@@ -52,6 +57,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
||||
if (MathBN.lte(existing.quantity, 0)) {
|
||||
currentOrder.items.splice(existingIndex, 1)
|
||||
}
|
||||
|
||||
unsetActionReference(existing, action)
|
||||
}
|
||||
},
|
||||
validate({ action }) {
|
||||
|
||||
@@ -2,11 +2,14 @@ import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { VirtualOrder } from "@types"
|
||||
import { ChangeActionType } from "../action-key"
|
||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||
import { setActionReference } from "../set-action-reference"
|
||||
import {
|
||||
setActionReference,
|
||||
unsetActionReference,
|
||||
} from "../set-action-reference"
|
||||
|
||||
OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, {
|
||||
isDeduction: true,
|
||||
operation({ action, currentOrder }) {
|
||||
operation({ action, currentOrder, options }) {
|
||||
const existingIndex = currentOrder.items.findIndex(
|
||||
(item) => item.id === action.details.reference_id
|
||||
)
|
||||
@@ -21,7 +24,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, {
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
setActionReference(existing, action)
|
||||
setActionReference(existing, action, options)
|
||||
|
||||
if (MathBN.lte(existing.quantity, 0)) {
|
||||
currentOrder.items.splice(existingIndex, 1)
|
||||
@@ -40,6 +43,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, {
|
||||
existing.detail.quantity,
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
unsetActionReference(existing, action)
|
||||
} else {
|
||||
currentOrder.items.push({
|
||||
id: action.details.reference_id!,
|
||||
|
||||
@@ -2,14 +2,17 @@ import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { EVENT_STATUS } from "@types"
|
||||
import { ChangeActionType } from "../action-key"
|
||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||
import { setActionReference } from "../set-action-reference"
|
||||
import {
|
||||
setActionReference,
|
||||
unsetActionReference,
|
||||
} from "../set-action-reference"
|
||||
|
||||
OrderChangeProcessing.registerActionType(
|
||||
ChangeActionType.RECEIVE_DAMAGED_RETURN_ITEM,
|
||||
{
|
||||
isDeduction: true,
|
||||
commitsAction: "return_item",
|
||||
operation({ action, currentOrder, previousEvents }) {
|
||||
operation({ action, currentOrder, previousEvents, options }) {
|
||||
const existing = currentOrder.items.find(
|
||||
(item) => item.id === action.details.reference_id
|
||||
)!
|
||||
@@ -28,7 +31,7 @@ OrderChangeProcessing.registerActionType(
|
||||
toReturn
|
||||
)
|
||||
|
||||
setActionReference(existing, action)
|
||||
setActionReference(existing, action, options)
|
||||
|
||||
if (previousEvents) {
|
||||
for (const previousEvent of previousEvents) {
|
||||
@@ -63,6 +66,8 @@ OrderChangeProcessing.registerActionType(
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
unsetActionReference(existing, action)
|
||||
|
||||
if (previousEvents) {
|
||||
for (const previousEvent of previousEvents) {
|
||||
if (!previousEvent.original_) {
|
||||
|
||||
@@ -7,12 +7,15 @@ import {
|
||||
import { EVENT_STATUS } from "@types"
|
||||
import { ChangeActionType } from "../action-key"
|
||||
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, {
|
||||
isDeduction: true,
|
||||
commitsAction: "return_item",
|
||||
operation({ action, currentOrder, previousEvents }) {
|
||||
operation({ action, currentOrder, previousEvents, options }) {
|
||||
const existing = currentOrder.items.find(
|
||||
(item) => item.id === action.details.reference_id
|
||||
)!
|
||||
@@ -31,7 +34,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RECEIVE_RETURN_ITEM, {
|
||||
toReturn
|
||||
)
|
||||
|
||||
setActionReference(existing, action)
|
||||
setActionReference(existing, action, options)
|
||||
|
||||
if (previousEvents) {
|
||||
for (const previousEvent of previousEvents) {
|
||||
@@ -67,6 +70,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RECEIVE_RETURN_ITEM, {
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
unsetActionReference(existing, action)
|
||||
|
||||
if (previousEvents) {
|
||||
for (const previousEvent of previousEvents) {
|
||||
if (!previousEvent.original_) {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { ChangeActionType } from "../action-key"
|
||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||
import { setActionReference } from "../set-action-reference"
|
||||
import {
|
||||
setActionReference,
|
||||
unsetActionReference,
|
||||
} from "../set-action-reference"
|
||||
|
||||
OrderChangeProcessing.registerActionType(ChangeActionType.REINSTATE_ITEM, {
|
||||
operation({ action, currentOrder }) {
|
||||
operation({ action, currentOrder, options }) {
|
||||
const existing = currentOrder.items.find(
|
||||
(item) => item.id === action.details.reference_id
|
||||
)!
|
||||
@@ -15,7 +18,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.REINSTATE_ITEM, {
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
setActionReference(existing, action)
|
||||
setActionReference(existing, action, options)
|
||||
},
|
||||
revert({ action, currentOrder }) {
|
||||
const existing = currentOrder.items.find(
|
||||
@@ -26,6 +29,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.REINSTATE_ITEM, {
|
||||
existing.detail.written_off_quantity,
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
unsetActionReference(existing, action)
|
||||
},
|
||||
validate({ action, currentOrder }) {
|
||||
const refId = action.details?.reference_id
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { ChangeActionType } from "../action-key"
|
||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||
import { setActionReference } from "../set-action-reference"
|
||||
import {
|
||||
setActionReference,
|
||||
unsetActionReference,
|
||||
} from "../set-action-reference"
|
||||
|
||||
OrderChangeProcessing.registerActionType(ChangeActionType.RETURN_ITEM, {
|
||||
isDeduction: true,
|
||||
awaitRequired: true,
|
||||
operation({ action, currentOrder }) {
|
||||
operation({ action, currentOrder, options }) {
|
||||
const existing = currentOrder.items.find(
|
||||
(item) => item.id === action.details.reference_id
|
||||
)!
|
||||
@@ -17,7 +20,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RETURN_ITEM, {
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
setActionReference(existing, action)
|
||||
setActionReference(existing, action, options)
|
||||
|
||||
return MathBN.mult(existing.unit_price, action.details.quantity)
|
||||
},
|
||||
@@ -30,6 +33,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RETURN_ITEM, {
|
||||
existing.detail.return_requested_quantity,
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
unsetActionReference(existing, action)
|
||||
},
|
||||
validate({ action, currentOrder }) {
|
||||
const refId = action.details?.reference_id
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { ChangeActionType } from "../action-key"
|
||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||
import { setActionReference } from "../set-action-reference"
|
||||
import {
|
||||
setActionReference,
|
||||
unsetActionReference,
|
||||
} from "../set-action-reference"
|
||||
|
||||
OrderChangeProcessing.registerActionType(ChangeActionType.SHIP_ITEM, {
|
||||
operation({ action, currentOrder }) {
|
||||
operation({ action, currentOrder, options }) {
|
||||
const existing = currentOrder.items.find(
|
||||
(item) => item.id === action.details.reference_id
|
||||
)!
|
||||
@@ -16,7 +19,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIP_ITEM, {
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
setActionReference(existing, action)
|
||||
setActionReference(existing, action, options)
|
||||
},
|
||||
revert({ action, currentOrder }) {
|
||||
const existing = currentOrder.items.find(
|
||||
@@ -27,6 +30,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIP_ITEM, {
|
||||
existing.detail.shipped_quantity,
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
unsetActionReference(existing, action)
|
||||
},
|
||||
validate({ action, currentOrder }) {
|
||||
const refId = action.details?.reference_id
|
||||
|
||||
@@ -4,26 +4,26 @@ import { OrderChangeProcessing } from "../calculate-order-change"
|
||||
import { setActionReference } from "../set-action-reference"
|
||||
|
||||
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, {
|
||||
operation({ action, currentOrder }) {
|
||||
operation({ action, currentOrder, options }) {
|
||||
const shipping = Array.isArray(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) {
|
||||
setActionReference(existing, action)
|
||||
} else {
|
||||
shipping.push({
|
||||
if (!existing) {
|
||||
existing = {
|
||||
id: action.reference_id!,
|
||||
order_id: currentOrder.id,
|
||||
return_id: action.return_id,
|
||||
claim_id: action.claim_id,
|
||||
exchange_id: action.exchange_id,
|
||||
price: action.amount as number,
|
||||
})
|
||||
}
|
||||
shipping.push(existing)
|
||||
}
|
||||
|
||||
setActionReference(existing, action, options)
|
||||
currentOrder.shipping_methods = shipping
|
||||
},
|
||||
revert({ action, currentOrder }) {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { ChangeActionType } from "../action-key"
|
||||
import { OrderChangeProcessing } from "../calculate-order-change"
|
||||
|
||||
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_REMOVE, {
|
||||
operation({ action, currentOrder }) {
|
||||
operation({ action, currentOrder, options }) {
|
||||
const shipping = Array.isArray(currentOrder.shipping_methods)
|
||||
? currentOrder.shipping_methods
|
||||
: [currentOrder.shipping_methods]
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { ChangeActionType } from "../action-key"
|
||||
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, {
|
||||
operation({ action, currentOrder }) {
|
||||
operation({ action, currentOrder, options }) {
|
||||
const existing = currentOrder.items.find(
|
||||
(item) => item.id === action.details.reference_id
|
||||
)!
|
||||
@@ -15,7 +18,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.WRITE_OFF_ITEM, {
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
setActionReference(existing, action)
|
||||
setActionReference(existing, action, options)
|
||||
},
|
||||
revert({ action, currentOrder }) {
|
||||
const existing = currentOrder.items.find(
|
||||
@@ -26,6 +29,8 @@ OrderChangeProcessing.registerActionType(ChangeActionType.WRITE_OFF_ITEM, {
|
||||
existing.detail.written_off_quantity,
|
||||
action.details.quantity
|
||||
)
|
||||
|
||||
unsetActionReference(existing, action)
|
||||
},
|
||||
validate({ action, currentOrder }) {
|
||||
const refId = action.details?.reference_id
|
||||
|
||||
Reference in New Issue
Block a user