feat(core-flows): revisit fulfillment flow reservations and support inventory kit (#11790)

* feat: fulfillment flow with reservations and inventory kit

* fix: account for custom line items

* chore: cleanup, changeset

* fix: revert action check

* fix: deduplicate, test item qunatities, compute line item quantity from fulfillment items

* fix:

* fix: improve types

* fix: optimize fetched fileds in cancel fulfillment workflow

* fix: add a test case

* chore: update type
This commit is contained in:
Frane Polić
2025-03-19 13:15:24 +01:00
committed by GitHub
parent a73c9770fa
commit 8385a5e34d
5 changed files with 751 additions and 66 deletions

View File

@@ -0,0 +1,21 @@
import { ReservationItemDTO } from "@medusajs/types"
/**
* Builds a map of reservations by line item id.
*
* @param reservations - The reservations to build the map from.
* @returns A map of reservations by line item id.
*/
export function buildReservationsMap(reservations: ReservationItemDTO[]) {
const map = new Map<string, ReservationItemDTO[]>()
for (const reservation of reservations) {
if (map.has(reservation.line_item_id as string)) {
map.get(reservation.line_item_id as string)!.push(reservation)
} else {
map.set(reservation.line_item_id as string, [reservation])
}
}
return map
}

View File

@@ -2,10 +2,15 @@ import {
AdditionalData,
BigNumberInput,
FulfillmentDTO,
InventoryItemDTO,
OrderDTO,
OrderLineItemDTO,
OrderWorkflow,
ProductVariantDTO,
ReservationItemDTO,
} from "@medusajs/framework/types"
import {
MathBN,
MedusaError,
Modules,
OrderWorkflowEvents,
@@ -27,6 +32,19 @@ import {
throwIfItemsDoesNotExistsInOrder,
throwIfOrderIsCancelled,
} from "../utils/order-validation"
import { createReservationsStep } from "../../reservation"
import { updateReservationsStep } from "../../reservation"
type OrderItemWithVariantDTO = OrderLineItemDTO & {
variant?: ProductVariantDTO & {
inventory_items: {
inventory: InventoryItemDTO
variant_id: string
inventory_item_id: string
required_quantity: number
}[]
}
}
/**
* The data to validate the order fulfillment cancelation.
@@ -90,6 +108,13 @@ export const cancelOrderFulfillmentValidateOrder = createStep(
)
}
if (fulfillment.canceled_at) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"The fulfillment is already canceled"
)
}
if (fulfillment.shipped_at) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
@@ -114,14 +139,47 @@ function prepareCancelOrderFulfillmentData({
order: OrderDTO
fulfillment: FulfillmentDTO
}) {
const lineItemIds = new Array(
...new Set(fulfillment.items.map((i) => i.line_item_id))
)
return {
order_id: order.id,
reference: Modules.FULFILLMENT,
reference_id: fulfillment.id,
items: fulfillment.items!.map((i) => {
items: lineItemIds.map((lineItemId) => {
// find order item
const orderItem = order.items!.find(
(i) => i.id === lineItemId
) as OrderItemWithVariantDTO
// find inventory items
const iitems = orderItem!.variant?.inventory_items
// find fulfillment item
const fitem = fulfillment.items.find(
(i) => i.line_item_id === lineItemId
)!
let quantity: BigNumberInput = fitem.quantity
// NOTE: if the order item has an inventory kit or `required_qunatity` > 1, fulfillment items wont't match 1:1 with order items.
// - for each inventory item in the kit, a fulfillment item will be created i.e. one line item could have multiple fulfillment items
// - the quantity of the fulfillment item will be the quantity of the order item multiplied by the required quantity of the inventory item
//
// We need to take this into account when canceling the fulfillment to compute quantity of line items not being fulfilled based on fulfillment items and qunatities.
// NOTE: for now we only need to find one inventory item of a line item to compute this since when a fulfillment is created all inventory items are fulfilled together.
// If we allow to cancel partial fulfillments for an order item, we need to change this.
//
if (iitems?.length) {
const iitem = iitems.find(
(i) => i.inventory.id === fitem.inventory_item_id
)
quantity = MathBN.div(quantity, iitem!.required_quantity)
}
return {
id: i.line_item_id as string,
quantity: i.quantity,
id: lineItemId as string,
quantity,
}
}),
}
@@ -129,29 +187,74 @@ function prepareCancelOrderFulfillmentData({
function prepareInventoryUpdate({
fulfillment,
reservations,
order,
}: {
order: OrderDTO
fulfillment: FulfillmentDTO
reservations: ReservationItemDTO[]
order: any
}) {
const inventoryAdjustment: {
inventory_item_id: string
location_id: string
adjustment: BigNumberInput
}[] = []
for (const item of fulfillment.items) {
const toCreate: {
inventory_item_id: string
location_id: string
quantity: BigNumberInput
}[] = []
const toUpdate: {
id: string
quantity: BigNumberInput
}[] = []
const orderItemsMap = order.items!.reduce((acc, item) => {
acc[item.id] = item
return acc
}, {})
const reservationMap = reservations.reduce((acc, reservation) => {
acc[reservation.inventory_item_id as string] = reservation
return acc
}, {})
for (const fulfillmentItem of fulfillment.items) {
// if this is `null` this means that item is from variant that has `manage_inventory` false
if (!item.inventory_item_id) {
if (!fulfillmentItem.inventory_item_id) {
continue
}
const orderItem = orderItemsMap[fulfillmentItem.line_item_id as string]
orderItem?.variant?.inventory_items.forEach((iitem) => {
const reservation =
reservationMap[fulfillmentItem.inventory_item_id as string]
if (!reservation) {
toCreate.push({
inventory_item_id: iitem.inventory.id,
location_id: fulfillment.location_id,
quantity: fulfillmentItem.quantity, // <- this is the inventory quantity that is being fulfilled so it menas it does include the required quantity
})
} else {
toUpdate.push({
id: reservation.id,
quantity: reservation.quantity + fulfillmentItem.quantity,
})
}
})
inventoryAdjustment.push({
inventory_item_id: item.inventory_item_id as string,
inventory_item_id: fulfillmentItem.inventory_item_id as string,
location_id: fulfillment.location_id,
adjustment: item.quantity,
adjustment: fulfillmentItem.quantity,
})
}
return {
toCreate,
toUpdate,
inventoryAdjustment,
}
}
@@ -198,9 +301,17 @@ export const cancelOrderFulfillmentWorkflow = createWorkflow(
fields: [
"id",
"status",
"items.*",
"fulfillments.*",
"fulfillments.items.*",
"items.id",
"items.quantity",
"items.variant.manage_inventory",
"items.variant.inventory_items.inventory.id",
"items.variant.inventory_items.required_quantity",
"fulfillments.id",
"fulfillments.location_id",
"fulfillments.items.id",
"fulfillments.items.quantity",
"fulfillments.items.line_item_id",
"fulfillments.items.inventory_item_id",
],
variables: { id: input.order_id },
list: false,
@@ -213,16 +324,38 @@ export const cancelOrderFulfillmentWorkflow = createWorkflow(
return order.fulfillments.find((f) => f.id === input.fulfillment_id)!
})
const lineItemIds = transform({ fulfillment }, ({ fulfillment }) => {
return fulfillment.items.map((i) => i.line_item_id)
})
const reservations = useRemoteQueryStep({
entry_point: "reservations",
fields: [
"id",
"line_item_id",
"quantity",
"inventory_item_id",
"location_id",
],
variables: {
filter: {
line_item_id: lineItemIds,
},
},
}).config({ name: "get-reservations" })
const cancelOrderFulfillmentData = transform(
{ order, fulfillment },
prepareCancelOrderFulfillmentData
)
const { inventoryAdjustment } = transform(
{ order, fulfillment },
const { toCreate, toUpdate, inventoryAdjustment } = transform(
{ order, fulfillment, reservations },
prepareInventoryUpdate
)
adjustInventoryLevelsStep(inventoryAdjustment)
const eventData = transform({ order, fulfillment, input }, (data) => {
return {
order_id: data.order.id,
@@ -233,7 +366,8 @@ export const cancelOrderFulfillmentWorkflow = createWorkflow(
parallelize(
cancelOrderFulfillmentStep(cancelOrderFulfillmentData),
adjustInventoryLevelsStep(inventoryAdjustment),
createReservationsStep(toCreate),
updateReservationsStep(toUpdate),
emitEventStep({
eventName: OrderWorkflowEvents.FULFILLMENT_CANCELED,
data: eventData,

View File

@@ -2,10 +2,14 @@ import {
AdditionalData,
BigNumberInput,
FulfillmentWorkflow,
InventoryItemDTO,
OrderDTO,
OrderLineItemDTO,
OrderWorkflow,
ProductDTO,
ProductVariantDTO,
ReservationItemDTO,
ShippingProfileDTO,
} from "@medusajs/framework/types"
import {
MathBN,
@@ -39,6 +43,21 @@ import {
throwIfItemsDoesNotExistsInOrder,
throwIfOrderIsCancelled,
} from "../utils/order-validation"
import { buildReservationsMap } from "../utils/build-reservations-map"
type OrderItemWithVariantDTO = OrderLineItemDTO & {
variant?: ProductVariantDTO & {
product?: ProductDTO & {
shipping_profile?: ShippingProfileDTO
}
inventory_items: {
inventory: InventoryItemDTO
variant_id: string
inventory_item_id: string
required_quantity: number
}[]
}
}
/**
* The data to validate the order fulfillment creation.
@@ -143,9 +162,7 @@ function prepareFulfillmentData({
(itemsList ?? order.items)!.map((i) => [i.id, i])
)
const reservationItemMap = new Map<string, ReservationItemDTO>(
reservations.map((r) => [r.line_item_id as string, r])
)
const reservationItemMap = buildReservationsMap(reservations)
// Note: If any of the items require shipping, we enable fulfillment
// unless explicitly set to not require shipping by the item in the request
@@ -157,31 +174,59 @@ function prepareFulfillmentData({
})
: true
const fulfillmentItems = fulfillableItems.map((i) => {
const orderItem = orderItemsMap.get(i.id)!
const reservation = reservationItemMap.get(i.id)!
const fulfillmentItems = fulfillableItems
.map((i) => {
const orderItem = orderItemsMap.get(i.id)! as OrderItemWithVariantDTO
const reservations = reservationItemMap.get(i.id)
if (
orderItem.requires_shipping &&
(orderItem as any).variant?.product &&
(orderItem as any).variant?.product.shipping_profile?.id !==
shippingOption.shipping_profile_id
) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Shipping profile ${shippingOption.shipping_profile_id} does not match the shipping profile of the order item ${orderItem.id}`
)
}
if (
orderItem.requires_shipping &&
orderItem.variant?.product &&
orderItem.variant?.product.shipping_profile?.id !==
shippingOption.shipping_profile_id
) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Shipping profile ${shippingOption.shipping_profile_id} does not match the shipping profile of the order item ${orderItem.id}`
)
}
return {
line_item_id: i.id,
inventory_item_id: reservation?.inventory_item_id,
quantity: i.quantity,
title: orderItem.variant_title ?? orderItem.title,
sku: orderItem.variant_sku || "",
barcode: orderItem.variant_barcode || "",
} as FulfillmentWorkflow.CreateFulfillmentItemWorkflowDTO
})
if (!reservations?.length) {
return [
{
line_item_id: i.id,
inventory_item_id: undefined,
quantity: i.quantity,
title: orderItem.variant_title ?? orderItem.title,
sku: orderItem.variant_sku || "",
barcode: orderItem.variant_barcode || "",
},
] as FulfillmentWorkflow.CreateFulfillmentItemWorkflowDTO[]
}
// if line item is from a managed variant, create a fulfillment item for each reservation item
return reservations.map((r) => {
const iItem = orderItem?.variant?.inventory_items.find(
(ii) => ii.inventory.id === r.inventory_item_id
)
return {
line_item_id: i.id,
inventory_item_id: r.inventory_item_id,
quantity: MathBN.mult(
iItem?.required_quantity ?? 1,
i.quantity
) as BigNumberInput,
title:
iItem?.inventory.title ||
orderItem.variant_title ||
orderItem.title,
sku: iItem?.inventory.sku || orderItem.variant_sku || "",
barcode: orderItem.variant_barcode || "",
} as FulfillmentWorkflow.CreateFulfillmentItemWorkflowDTO
})
})
.flat()
let locationId: string | undefined | null = input.location_id
@@ -223,11 +268,6 @@ function prepareInventoryUpdate({
inputItemsMap,
itemsList,
}) {
const reservationMap = reservations.reduce((acc, reservation) => {
acc[reservation.line_item_id as string] = reservation
return acc
}, {})
const toDelete: string[] = []
const toUpdate: {
id: string
@@ -240,14 +280,21 @@ function prepareInventoryUpdate({
adjustment: BigNumberInput
}[] = []
const orderItemsMap = new Map<string, Required<OrderDTO>["items"][0]>(
(itemsList ?? order.items)!.map((i) => [i.id, i])
)
const reservationMap = buildReservationsMap(reservations)
const allItems = itemsList ?? order.items
const itemsToFulfill = allItems.filter((i) => i.id in inputItemsMap)
// iterate over items that are being fulfilled
for (const item of itemsToFulfill) {
const reservation = reservationMap[item.id]
const reservations = reservationMap.get(item.id)
const orderItem = orderItemsMap.get(item.id)! as OrderItemWithVariantDTO
if (!reservation) {
if (!reservations?.length) {
if (item.variant?.manage_inventory) {
throw new Error(
`No stock reservation found for item ${item.id} - ${item.title} (${item.variant_title})`
@@ -258,32 +305,45 @@ function prepareInventoryUpdate({
const inputQuantity = inputItemsMap[item.id]?.quantity ?? item.quantity
if (MathBN.gt(inputQuantity, reservation.quantity)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Quantity to fulfill exceeds the reserved quantity for the item: ${item.id}`
reservations.forEach((reservation) => {
if (MathBN.gt(inputQuantity, reservation.quantity)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Quantity to fulfill exceeds the reserved quantity for the item: ${item.id}`
)
}
const iItem = orderItem?.variant?.inventory_items.find(
(ii) => ii.inventory.id === reservation.inventory_item_id
)
}
const remainingReservationQuantity = reservation.quantity - inputQuantity
const adjustemntQuantity = MathBN.mult(
inputQuantity,
iItem?.required_quantity ?? 1
)
inventoryAdjustment.push({
inventory_item_id: reservation.inventory_item_id,
location_id: input.location_id ?? reservation.location_id,
adjustment: MathBN.mult(inputQuantity, -1),
})
const remainingReservationQuantity = MathBN.sub(
reservation.quantity,
adjustemntQuantity
)
if (remainingReservationQuantity === 0) {
toDelete.push(reservation.id)
} else {
toUpdate.push({
id: reservation.id,
quantity: remainingReservationQuantity,
inventoryAdjustment.push({
inventory_item_id: reservation.inventory_item_id,
location_id: input.location_id ?? reservation.location_id,
adjustment: MathBN.mult(adjustemntQuantity, -1),
})
}
}
if (MathBN.eq(remainingReservationQuantity, 0)) {
toDelete.push(reservation.id)
} else {
toUpdate.push({
id: reservation.id,
quantity: remainingReservationQuantity,
location_id: input.location_id ?? reservation.location_id,
})
}
})
}
return {
toDelete,
toUpdate,
@@ -350,6 +410,10 @@ export const createOrderFulfillmentWorkflow = createWorkflow(
"items.variant.width",
"items.variant.material",
"items.variant_title",
"items.variant.inventory_items.required_quantity",
"items.variant.inventory_items.inventory.id",
"items.variant.inventory_items.inventory.title",
"items.variant.inventory_items.inventory.sku",
"shipping_address.*",
"shipping_methods.id",
"shipping_methods.shipping_option_id",
@@ -406,6 +470,7 @@ export const createOrderFulfillmentWorkflow = createWorkflow(
.filter((i) => i in inputItemsMap)
}
)
const reservations = useRemoteQueryStep({
entry_point: "reservations",
fields: [