feat(core-flows,medusa,utils,types): adds delivered_quantity to order (#9130)

what:

- adds delivered_quantity to order


https://github.com/user-attachments/assets/709b1727-08ed-4a88-ae29-38f13540e301
This commit is contained in:
Riqwan Thamir
2024-09-16 11:59:01 +02:00
committed by GitHub
parent 950cf9af79
commit 3e97a64b21
41 changed files with 794 additions and 25 deletions

View File

@@ -0,0 +1,67 @@
import { ChangeActionType, MathBN, MedusaError } from "@medusajs/utils"
import { OrderChangeProcessing } from "../calculate-order-change"
import { setActionReference } from "../set-action-reference"
OrderChangeProcessing.registerActionType(ChangeActionType.DELIVER_ITEM, {
operation({ action, currentOrder, options }) {
const item = currentOrder.items.find(
(item) => item.id === action.details.reference_id
)!
item.detail.delivered_quantity ??= 0
item.detail.delivered_quantity = MathBN.add(
item.detail.delivered_quantity,
action.details.quantity
)
setActionReference(item, action, options)
},
validate({ action, currentOrder }) {
const refId = action.details?.reference_id
if (refId == null) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Reference ID is required."
)
}
const item = currentOrder.items.find((item) => item.id === refId)
if (!item) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Item ID "${refId}" not found.`
)
}
const totalDeliverable = MathBN.convert(item.quantity)
const totalDelivered = MathBN.convert(item.detail?.delivered_quantity)
const newDelivered = MathBN.convert(action.details?.quantity ?? 0)
const newTotalDelivered = MathBN.sum(totalDelivered, newDelivered)
const totalFulfilled = MathBN.convert(item.detail?.fulfilled_quantity)
if (MathBN.lte(newDelivered, 0)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Quantity of item ${refId} must be greater than 0.`
)
}
if (MathBN.gt(newTotalDelivered, totalFulfilled)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Cannot deliver more items than what was fulfilled for item ${refId}.`
)
}
if (MathBN.gt(newTotalDelivered, totalDeliverable)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Cannot deliver more items than what was ordered for item ${refId}.`
)
}
},
})

View File

@@ -1,5 +1,6 @@
export * from "./cancel-item-fulfillment"
export * from "./cancel-return"
export * from "./deliver-item"
export * from "./fulfill-item"
export * from "./item-add"
export * from "./item-remove"

View File

@@ -58,6 +58,7 @@ export function applyChangesToOrder(
version,
quantity: orderItem.quantity,
fulfilled_quantity: orderItem.fulfilled_quantity ?? 0,
delivered_quantity: orderItem.delivered_quantity ?? 0,
shipped_quantity: orderItem.shipped_quantity ?? 0,
return_requested_quantity: orderItem.return_requested_quantity ?? 0,
return_received_quantity: orderItem.return_received_quantity ?? 0,

View File

@@ -148,6 +148,7 @@ export class OrderChangeProcessing {
isReplay = false
): BigNumberInput | void {
const definedType = OrderChangeProcessing.typeDefinition[action.action]
if (!isPresent(definedType)) {
throw new Error(`Action type ${action.action} is not defined`)
}