fix(medusa): update create fulfillment flow (#3172)
* update create fulfillment flow * move transaction service creation close to where it's used * integration tests * fix feedback * use transformBody * add changeset
This commit is contained in:
@@ -27,6 +27,7 @@ import regionRoutes from "./regions"
|
||||
import reservationRoutes from "./reservations"
|
||||
import returnReasonRoutes from "./return-reasons"
|
||||
import returnRoutes from "./returns"
|
||||
import reservationRoutes from "./reservations"
|
||||
import salesChannelRoutes from "./sales-channels"
|
||||
import shippingOptionRoutes from "./shipping-options"
|
||||
import shippingProfileRoutes from "./shipping-profiles"
|
||||
@@ -101,6 +102,7 @@ export default (app, container, config) => {
|
||||
reservationRoutes(route)
|
||||
returnReasonRoutes(route)
|
||||
returnRoutes(route)
|
||||
reservationRoutes(route)
|
||||
salesChannelRoutes(route)
|
||||
shippingOptionRoutes(route, featureFlagRouter)
|
||||
shippingProfileRoutes(route)
|
||||
|
||||
@@ -97,39 +97,49 @@ import { FindParams } from "../../../../types/common"
|
||||
export default async (req, res) => {
|
||||
const { id } = req.params
|
||||
|
||||
const validated = req.validatedBody
|
||||
const { validatedBody } = req as {
|
||||
validatedBody: AdminPostOrdersOrderFulfillmentsReq
|
||||
}
|
||||
|
||||
const orderService: OrderService = req.scope.resolve("orderService")
|
||||
const pvInventoryService: ProductVariantInventoryService = req.scope.resolve(
|
||||
"productVariantInventoryService"
|
||||
)
|
||||
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
const { fulfillments: existingFulfillments } = await orderService
|
||||
.withTransaction(transactionManager)
|
||||
.retrieve(id, {
|
||||
const orderServiceTx = orderService.withTransaction(transactionManager)
|
||||
|
||||
const { fulfillments: existingFulfillments } =
|
||||
await orderServiceTx.retrieve(id, {
|
||||
relations: ["fulfillments"],
|
||||
})
|
||||
const existingFulfillmentMap = new Map(
|
||||
existingFulfillments.map((fulfillment) => [fulfillment.id, fulfillment])
|
||||
)
|
||||
|
||||
const { fulfillments } = await orderService
|
||||
.withTransaction(transactionManager)
|
||||
.createFulfillment(id, validated.items, {
|
||||
metadata: validated.metadata,
|
||||
no_notification: validated.no_notification,
|
||||
await orderServiceTx.createFulfillment(id, validatedBody.items, {
|
||||
metadata: validatedBody.metadata,
|
||||
no_notification: validatedBody.no_notification,
|
||||
})
|
||||
|
||||
if (validatedBody.location_id) {
|
||||
const { fulfillments } = await orderServiceTx.retrieve(id, {
|
||||
relations: [
|
||||
"fulfillments",
|
||||
"fulfillments.items",
|
||||
"fulfillments.items.item",
|
||||
],
|
||||
})
|
||||
|
||||
const pvInventoryServiceTx =
|
||||
pvInventoryService.withTransaction(transactionManager)
|
||||
const pvInventoryServiceTx =
|
||||
pvInventoryService.withTransaction(transactionManager)
|
||||
|
||||
if (validated.location_id) {
|
||||
await updateInventoryAndReservations(
|
||||
fulfillments.filter((f) => !existingFulfillmentMap[f.id]),
|
||||
{
|
||||
inventoryService: pvInventoryServiceTx,
|
||||
locationId: validated.location_id,
|
||||
locationId: validatedBody.location_id,
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -151,33 +161,35 @@ const updateInventoryAndReservations = async (
|
||||
) => {
|
||||
const { inventoryService, locationId } = context
|
||||
|
||||
fulfillments.map(async ({ items }) => {
|
||||
await inventoryService.validateInventoryAtLocation(
|
||||
items.map(({ item, quantity }) => ({ ...item, quantity } as LineItem)),
|
||||
locationId
|
||||
)
|
||||
await Promise.all(
|
||||
fulfillments.map(async ({ items }) => {
|
||||
await inventoryService.validateInventoryAtLocation(
|
||||
items.map(({ item, quantity }) => ({ ...item, quantity } as LineItem)),
|
||||
locationId
|
||||
)
|
||||
|
||||
await Promise.all(
|
||||
items.map(async ({ item, quantity }) => {
|
||||
if (!item.variant_id) {
|
||||
return
|
||||
}
|
||||
await Promise.all(
|
||||
items.map(async ({ item, quantity }) => {
|
||||
if (!item.variant_id) {
|
||||
return
|
||||
}
|
||||
|
||||
await inventoryService.adjustReservationsQuantityByLineItem(
|
||||
item.id,
|
||||
item.variant_id,
|
||||
locationId,
|
||||
-quantity
|
||||
)
|
||||
await inventoryService.adjustReservationsQuantityByLineItem(
|
||||
item.id,
|
||||
item.variant_id,
|
||||
locationId,
|
||||
-quantity
|
||||
)
|
||||
|
||||
await inventoryService.adjustInventory(
|
||||
item.variant_id,
|
||||
locationId,
|
||||
-quantity
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
await inventoryService.adjustInventory(
|
||||
item.variant_id,
|
||||
locationId,
|
||||
-quantity
|
||||
)
|
||||
})
|
||||
)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user