fix(core-flows): order partial fulfillment (#8047)
This commit is contained in:
@@ -7,11 +7,11 @@ import {
|
|||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { MedusaError, Modules } from "@medusajs/utils"
|
import { MedusaError, Modules } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
|
WorkflowData,
|
||||||
createStep,
|
createStep,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
parallelize,
|
parallelize,
|
||||||
transform,
|
transform,
|
||||||
WorkflowData,
|
|
||||||
} from "@medusajs/workflows-sdk"
|
} from "@medusajs/workflows-sdk"
|
||||||
import { createRemoteLinkStep, useRemoteQueryStep } from "../../common"
|
import { createRemoteLinkStep, useRemoteQueryStep } from "../../common"
|
||||||
import { createFulfillmentWorkflow } from "../../fulfillment"
|
import { createFulfillmentWorkflow } from "../../fulfillment"
|
||||||
@@ -47,10 +47,7 @@ function prepareRegisterOrderFulfillmentData({
|
|||||||
order,
|
order,
|
||||||
fulfillment,
|
fulfillment,
|
||||||
input,
|
input,
|
||||||
}: {
|
inputItemsMap,
|
||||||
order: OrderDTO
|
|
||||||
fulfillment: FulfillmentDTO
|
|
||||||
input: OrderWorkflow.CreateOrderFulfillmentWorkflowInput
|
|
||||||
}) {
|
}) {
|
||||||
return {
|
return {
|
||||||
order_id: order.id,
|
order_id: order.id,
|
||||||
@@ -58,9 +55,10 @@ function prepareRegisterOrderFulfillmentData({
|
|||||||
reference_id: fulfillment.id,
|
reference_id: fulfillment.id,
|
||||||
created_by: input.created_by,
|
created_by: input.created_by,
|
||||||
items: order.items!.map((i) => {
|
items: order.items!.map((i) => {
|
||||||
|
const inputQuantity = inputItemsMap[i.id]?.quantity
|
||||||
return {
|
return {
|
||||||
id: i.id,
|
id: i.id,
|
||||||
quantity: i.quantity,
|
quantity: inputQuantity ?? i.quantity,
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
@@ -129,17 +127,12 @@ function prepareFulfillmentData({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function prepareInventoryUpdate({ reservations, order, input }) {
|
function prepareInventoryUpdate({ reservations, order, input, inputItemsMap }) {
|
||||||
const reservationMap = reservations.reduce((acc, reservation) => {
|
const reservationMap = reservations.reduce((acc, reservation) => {
|
||||||
acc[reservation.line_item_id as string] = reservation
|
acc[reservation.line_item_id as string] = reservation
|
||||||
return acc
|
return acc
|
||||||
}, {})
|
}, {})
|
||||||
|
|
||||||
const inputItemsMap = input.items.reduce((acc, item) => {
|
|
||||||
acc[item.id] = item
|
|
||||||
return acc
|
|
||||||
}, {})
|
|
||||||
|
|
||||||
const toDelete: string[] = []
|
const toDelete: string[] = []
|
||||||
const toUpdate: {
|
const toUpdate: {
|
||||||
id: string
|
id: string
|
||||||
@@ -216,6 +209,13 @@ export const createOrderFulfillmentWorkflow = createWorkflow(
|
|||||||
|
|
||||||
validateOrder({ order, inputItems: input.items })
|
validateOrder({ order, inputItems: input.items })
|
||||||
|
|
||||||
|
const inputItemsMap = transform(input, ({ items }) => {
|
||||||
|
return items.reduce((acc, item) => {
|
||||||
|
acc[item.id] = item
|
||||||
|
return acc
|
||||||
|
}, {})
|
||||||
|
})
|
||||||
|
|
||||||
const shippingOptionId = transform(order, (data) => {
|
const shippingOptionId = transform(order, (data) => {
|
||||||
return data.shipping_methods?.[0]?.shipping_option_id
|
return data.shipping_methods?.[0]?.shipping_option_id
|
||||||
})
|
})
|
||||||
@@ -257,7 +257,7 @@ export const createOrderFulfillmentWorkflow = createWorkflow(
|
|||||||
const fulfillment = createFulfillmentWorkflow.runAsStep(fulfillmentData)
|
const fulfillment = createFulfillmentWorkflow.runAsStep(fulfillmentData)
|
||||||
|
|
||||||
const registerOrderFulfillmentData = transform(
|
const registerOrderFulfillmentData = transform(
|
||||||
{ order, fulfillment, input },
|
{ order, fulfillment, input, inputItemsMap },
|
||||||
prepareRegisterOrderFulfillmentData
|
prepareRegisterOrderFulfillmentData
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -274,7 +274,7 @@ export const createOrderFulfillmentWorkflow = createWorkflow(
|
|||||||
)
|
)
|
||||||
|
|
||||||
const { toDelete, toUpdate, inventoryAdjustment } = transform(
|
const { toDelete, toUpdate, inventoryAdjustment } = transform(
|
||||||
{ order, reservations, input },
|
{ order, reservations, input, inputItemsMap },
|
||||||
prepareInventoryUpdate
|
prepareInventoryUpdate
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user