fix(order): ignore reservation when manage_inventory is false (#7594)
This commit is contained in:
@@ -94,7 +94,7 @@ function prepareFulfillmentData({
|
||||
const reservation = reservationItemMap.get(i.id)!
|
||||
return {
|
||||
line_item_id: i.id,
|
||||
inventory_item_id: reservation.inventory_item_id,
|
||||
inventory_item_id: reservation?.inventory_item_id,
|
||||
quantity: i.quantity,
|
||||
title: orderItem.variant_title ?? orderItem.title,
|
||||
sku: orderItem.variant_sku || "",
|
||||
@@ -114,6 +114,9 @@ function prepareFulfillmentData({
|
||||
)
|
||||
}
|
||||
|
||||
const shippingAddress = order.shipping_address ?? { id: undefined }
|
||||
delete shippingAddress.id
|
||||
|
||||
return {
|
||||
input: {
|
||||
location_id: locationId,
|
||||
@@ -121,18 +124,12 @@ function prepareFulfillmentData({
|
||||
shipping_option_id: shippingOption.id,
|
||||
items: fulfillmentItems,
|
||||
labels: [] as FulfillmentWorkflow.CreateFulfillmentLabelWorkflowDTO[], // TODO: shipping labels
|
||||
delivery_address: order.shipping_address ?? ({} as any),
|
||||
delivery_address: shippingAddress as any,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function prepareInventoryUpdate({ reservations, order, input }) {
|
||||
if (!reservations || !reservations.length) {
|
||||
throw new Error(
|
||||
`No stock reservation found for items ${input.items.map((i) => i.id)}`
|
||||
)
|
||||
}
|
||||
|
||||
const reservationMap = reservations.reduce((acc, reservation) => {
|
||||
acc[reservation.line_item_id as string] = reservation
|
||||
return acc
|
||||
@@ -157,6 +154,15 @@ function prepareInventoryUpdate({ reservations, order, input }) {
|
||||
|
||||
for (const item of order.items) {
|
||||
const reservation = reservationMap[item.id]
|
||||
if (!reservation) {
|
||||
if (item.manage_inventory) {
|
||||
throw new Error(
|
||||
`No stock reservation found for item ${item.id} - ${item.title} (${item.variant_title})`
|
||||
)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
const inputQuantity = inputItemsMap[item.id]?.quantity ?? item.quantity
|
||||
|
||||
const quantity = reservation.quantity - inputQuantity
|
||||
@@ -199,6 +205,7 @@ export const createOrderFulfillmentWorkflow = createWorkflow(
|
||||
"region_id",
|
||||
"currency_code",
|
||||
"items.*",
|
||||
"items.variant.manage_inventory",
|
||||
"shipping_address.*",
|
||||
"shipping_methods.shipping_option_id", // TODO: which shipping method to use when multiple?
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user