Skip to content

Commit

Permalink
update create fulfillment flow
Browse files Browse the repository at this point in the history
  • Loading branch information
pKorsholm committed Feb 3, 2023
1 parent 2c6c327 commit 34f3239
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
68 changes: 41 additions & 27 deletions packages/medusa/src/api/routes/admin/orders/create-fulfillment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import { defaultAdminOrdersFields, defaultAdminOrdersRelations } from "."

import { EntityManager } from "typeorm"
import {
FulfillmentService,
OrderService,
ProductVariantInventoryService,
} from "../../../../services"
import { validator } from "../../../../utils/validator"
import { optionalBooleanMapper } from "../../../../utils/validators/is-boolean"
import { Fulfillment, LineItem } from "../../../../models"
import logger from "../../../../loaders/logger"

/**
* @oas [post] /orders/{id}/fulfillment
Expand Down Expand Up @@ -115,7 +117,7 @@ export default async (req, res) => {
existingFulfillments.map((fulfillment) => [fulfillment.id, fulfillment])
)

const { fulfillments } = await orderService
await orderService
.withTransaction(transactionManager)
.createFulfillment(id, validated.items, {
metadata: validated.metadata,
Expand All @@ -126,6 +128,16 @@ export default async (req, res) => {
pvInventoryService.withTransaction(transactionManager)

if (validated.location_id) {
const { fulfillments } = await orderService
.withTransaction(transactionManager)
.retrieve(id, {
relations: [
"fulfillments",
"fulfillments.items",
"fulfillments.items.item",
],
})

await updateInventoryAndReservations(
fulfillments.filter((f) => !existingFulfillmentMap[f.id]),
{
Expand Down Expand Up @@ -153,33 +165,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(
items.map(async ({ item, quantity }) => {
if (!item.variant_id) {
return
}
await Promise.all(
fulfillments.map(async ({ items }) => {
await inventoryService.validateInventoryAtLocation(
items.map(({ item, quantity }) => ({ ...item, quantity } as LineItem)),
locationId
)

await inventoryService.adjustReservationsQuantityByLineItem(
item.id,
item.variant_id,
locationId,
-quantity
)

await inventoryService.adjustInventory(
item.variant_id,
locationId,
-quantity
)
})
)
})
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.adjustInventory(
item.variant_id,
locationId,
-quantity
)
})
)
})
)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/medusa/src/services/product-variant-inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,11 +509,11 @@ class ProductVariantInventoryService extends TransactionBaseService {
)

for (const inventoryLevel of inventoryLevels) {
const pvInventoryItem = pviMap[inventoryLevel.inventory_item_id]
const pvInventoryItem = pviMap.get(inventoryLevel.inventory_item_id)

if (
!pvInventoryItem ||
pvInventoryItem.quantity * item.quantity >
pvInventoryItem.required_quantity * item.quantity >
inventoryLevel.stocked_quantity
) {
throw new MedusaError(
Expand Down

0 comments on commit 34f3239

Please sign in to comment.