chore(order): item update quantity (#8513)

This commit is contained in:
Carlos R. L. Rodrigues
2024-08-08 14:12:16 -03:00
committed by GitHub
parent 6efdfbc9a6
commit 91f07e1a59
6 changed files with 239 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ export * from "./cancel-return"
export * from "./fulfill-item"
export * from "./item-add"
export * from "./item-remove"
export * from "./item-update"
export * from "./receive-damaged-return-item"
export * from "./receive-return-item"
export * from "./reinstate-item"

View File

@@ -0,0 +1,69 @@
import { ChangeActionType, MathBN, MedusaError } from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
import { setActionReference } from "../set-action-reference"
OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_UPDATE, {
operation({ action, currentOrder, options }) {
const existingIndex = currentOrder.items.findIndex(
(item) => item.id === action.details.reference_id
)
const existing = currentOrder.items[existingIndex]
existing.detail.quantity ??= 0
const quantityDiff = MathBN.sub(
action.details.quantity,
existing.detail.quantity
)
existing.quantity = action.details.quantity
existing.detail.quantity = action.details.quantity
setActionReference(existing, action, options)
if (MathBN.lte(existing.quantity, 0)) {
currentOrder.items.splice(existingIndex, 1)
}
return MathBN.mult(existing.unit_price, quantityDiff)
},
validate({ action, currentOrder }) {
const refId = action.details?.reference_id
if (refId == null) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"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 == null) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Quantity of item ${refId} is required.`
)
}
action.details.quantity ??= 0
const lower = MathBN.lt(
action.details.quantity,
existing.detail?.fulfilled_quantity
)
if (lower) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Item ${refId} has already been fulfilled and quantity cannot be lower than ${existing.detail?.fulfilled_quantity}.`
)
}
},
})