chore(order): cancel return (#7881)

This commit is contained in:
Carlos R. L. Rodrigues
2024-07-02 06:52:58 -03:00
committed by GitHub
parent b3236ff31c
commit 07715e6b50
46 changed files with 1339 additions and 121 deletions

View File

@@ -3,7 +3,7 @@ import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
import { setActionReference } from "../set-action-reference"
OrderChangeProcessing.registerActionType(ChangeActionType.CANCEL_RETURN, {
OrderChangeProcessing.registerActionType(ChangeActionType.CANCEL_RETURN_ITEM, {
operation({ action, currentOrder }) {
const existing = currentOrder.items.find(
(item) => item.id === action.details.reference_id

View File

@@ -9,4 +9,5 @@ export * from "./receive-return-item"
export * from "./return-item"
export * from "./ship-item"
export * from "./shipping-add"
export * from "./shipping-remove"
export * from "./write-off-item"

View File

@@ -0,0 +1,64 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
import { setActionReference } from "../set-action-reference"
OrderChangeProcessing.registerActionType(ChangeActionType.REINSTATE_ITEM, {
operation({ action, currentOrder }) {
const existing = currentOrder.items.find(
(item) => item.id === action.details.reference_id
)!
existing.detail.written_off_quantity ??= 0
existing.detail.written_off_quantity = MathBN.sub(
existing.detail.written_off_quantity,
action.details.quantity
)
setActionReference(existing, action)
},
revert({ action, currentOrder }) {
const existing = currentOrder.items.find(
(item) => item.id === action.details.reference_id
)!
existing.detail.written_off_quantity = MathBN.add(
existing.detail.written_off_quantity,
action.details.quantity
)
},
validate({ action, currentOrder }) {
const refId = action.details?.reference_id
if (!isDefined(refId)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Details reference ID is required."
)
}
const existing = currentOrder.items.find((item) => item.id === refId)
if (!existing) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Item ID "${refId}" not found.`
)
}
if (!action.details?.quantity) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Quantity to reinstate item ${refId} is required.`
)
}
const quantityAvailable = existing!.quantity ?? 0
const greater = MathBN.gt(action.details?.quantity, quantityAvailable)
if (greater) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Cannot unclaim more items than what was ordered."
)
}
},
})

View File

@@ -57,7 +57,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RETURN_ITEM, {
}
const quantityAvailable = MathBN.sub(
existing!.detail?.shipped_quantity ?? 0,
existing!.detail?.fulfilled_quantity ?? 0,
existing!.detail?.return_requested_quantity ?? 0
)
@@ -65,7 +65,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RETURN_ITEM, {
if (greater) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Cannot request to return more items than what was shipped for item ${refId}.`
`Cannot request to return more items than what was fulfilled for item ${refId}.`
)
}
},

View File

@@ -1,6 +1,7 @@
import { MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
import { setActionReference } from "../set-action-reference"
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, {
operation({ action, currentOrder }) {
@@ -8,15 +9,20 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, {
? currentOrder.shipping_methods
: [currentOrder.shipping_methods]
shipping.push({
shipping_method_id: action.reference_id!,
order_id: currentOrder.id,
return_id: action.return_id,
claim_id: action.claim_id,
exchange_id: action.exchange_id,
const existing = shipping.find((sh) => sh.id === action.reference_id)
price: action.amount as number,
})
if (existing) {
setActionReference(existing, action)
} else {
shipping.push({
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,
})
}
currentOrder.shipping_methods = shipping
},
@@ -26,7 +32,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, {
: [currentOrder.shipping_methods]
const existingIndex = shipping.findIndex(
(item) => item.shipping_method_id === action.reference_id
(item) => item.id === action.reference_id
)
if (existingIndex > -1) {

View File

@@ -0,0 +1,56 @@
import { MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_REMOVE, {
operation({ action, currentOrder }) {
const shipping = Array.isArray(currentOrder.shipping_methods)
? currentOrder.shipping_methods
: [currentOrder.shipping_methods]
const existingIndex = shipping.findIndex(
(item) => item.id === action.reference_id
)
if (existingIndex > -1) {
shipping.splice(existingIndex, 1)
}
currentOrder.shipping_methods = shipping
},
revert({ action, currentOrder }) {
const shipping = Array.isArray(currentOrder.shipping_methods)
? currentOrder.shipping_methods
: [currentOrder.shipping_methods]
const existingIndex = shipping.findIndex(
(item) => item.id === action.reference_id
)
if (existingIndex > -1) {
shipping.push({
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,
})
}
},
validate({ action }) {
if (!action.reference_id) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Reference ID is required."
)
}
if (!isDefined(action.amount)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Amount is required."
)
}
},
})