This is a PR to keep them relatively small. Very likely changes, validations and other features will be added. What: Basic methods to cancel, confirm or decline order changes Apply order changes to modify and create a new version of an order Things related to calculation, Order and Item totals are not covered in this PR. Properties won't match with definition, etc. Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { MedusaError, isDefined } from "@medusajs/utils"
|
|
import { ChangeActionType } from "../action-key"
|
|
import { OrderChangeProcessing } from "../calculate-order-change"
|
|
|
|
OrderChangeProcessing.registerActionType(ChangeActionType.WRITE_OFF_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 += action.details.quantity
|
|
},
|
|
revert({ action, currentOrder }) {
|
|
const existing = currentOrder.items.find(
|
|
(item) => item.id === action.details.reference_id
|
|
)!
|
|
|
|
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,
|
|
`Reference ID "${refId}" not found.`
|
|
)
|
|
}
|
|
|
|
if (!action.details?.quantity) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
`Quantity to write-off item ${refId} is required.`
|
|
)
|
|
}
|
|
|
|
const quantityAvailable = existing!.quantity ?? 0
|
|
if (action.details?.quantity > quantityAvailable) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_DATA,
|
|
"Cannot claim more items than what was ordered."
|
|
)
|
|
}
|
|
},
|
|
})
|