feat(order): order change actions engine (#6467)

This commit is contained in:
Carlos R. L. Rodrigues
2024-02-29 16:22:14 -03:00
committed by GitHub
parent cdb01e073b
commit 03ca5c814e
70 changed files with 5727 additions and 992 deletions
@@ -0,0 +1,47 @@
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.written_off_quantity ??= 0
existing.written_off_quantity += action.details.quantity
},
revert({ action, currentOrder }) {
const existing = currentOrder.items.find(
(item) => item.id === action.details.reference_id
)!
existing.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.`
)
}
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."
)
}
},
})