Skip to content

Commit

Permalink
locking inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-r-l-rodrigues committed Jan 9, 2025
1 parent feeed01 commit 1bd1a64
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,15 @@ async function createOrderFixture({ container, product, location }) {

const inventoryModule = container.resolve(Modules.INVENTORY)

const itemWithInventory = order.items!.find(
(o) => o.variant_sku === variantSkuWithInventory
)!
const reservation = await inventoryModule.createReservationItems([
{
line_item_id: order.items![0].id,
line_item_id: itemWithInventory.id,
inventory_item_id: inventoryItem.id,
location_id: location.id,
quantity: order.items![0].quantity,
quantity: itemWithInventory.quantity,
},
])

Expand Down Expand Up @@ -433,13 +436,13 @@ medusaIntegrationTestRunner({
await cancelOrderFulfillmentWorkflow(container).run({
input: cancelFulfillmentData,
})

const remoteQueryObjectFulfill = remoteQueryObjectFromString({
entryPoint: "order",
variables: {
id: order.id,
},
fields: [
"*",
"items.*",
"shipping_methods.*",
"total",
Expand All @@ -457,14 +460,15 @@ medusaIntegrationTestRunner({
)!

expect(orderFulfillAfterCancelled.fulfillments).toHaveLength(1)
expect(orderFulfillItemWithInventory.detail.fulfilled_quantity).toEqual(
0
)
expect(
orderFulfillItemWithInventory.detail.fulfilled_quantity.valueOf()
).toEqual(0)

const stockAvailabilityAfterCancelled =
await inventoryModule.retrieveStockedQuantity(inventoryItem.id, [
location.id,
])

expect(stockAvailabilityAfterCancelled.valueOf()).toEqual(2)
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IInventoryService, InventoryTypes } from "@medusajs/framework/types"
import { InventoryTypes } from "@medusajs/framework/types"
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"

import { MathBN, Modules } from "@medusajs/framework/utils"
Expand All @@ -13,20 +13,22 @@ export const adjustInventoryLevelsStep = createStep(
input: InventoryTypes.BulkAdjustInventoryLevelInput[],
{ container }
) => {
const inventoryService: IInventoryService = container.resolve(
Modules.INVENTORY
)
const inventoryService = container.resolve(Modules.INVENTORY)
const locking = container.resolve(Modules.LOCKING)
const inventoryItemIds = input.map((item) => item.inventory_item_id)

const adjustedLevels: InventoryTypes.InventoryLevelDTO[] =
await inventoryService.adjustInventory(
input.map((item) => {
return {
inventoryItemId: item.inventory_item_id,
locationId: item.location_id,
adjustment: item.adjustment,
}
})
)
await locking.execute(inventoryItemIds, async () => {
return await inventoryService.adjustInventory(
input.map((item) => {
return {
inventoryItemId: item.inventory_item_id,
locationId: item.location_id,
adjustment: item.adjustment,
}
})
)
})

return new StepResponse(
adjustedLevels,
Expand All @@ -39,26 +41,32 @@ export const adjustInventoryLevelsStep = createStep(
)
},
async (adjustedLevels, { container }) => {
if (!adjustedLevels) {
if (!adjustedLevels?.length) {
return
}

const inventoryService = container.resolve(Modules.INVENTORY)
const locking = container.resolve(Modules.LOCKING)
const inventoryItemIds = adjustedLevels.map(
(item) => item.inventory_item_id
)

/**
* @todo
* The method "adjustInventory" was broken, it was receiving the
* "inventoryItemId" and "locationId" as snake case, whereas
* the expected object needed these properties as camelCase
*/
await inventoryService.adjustInventory(
adjustedLevels.map((level) => {
return {
inventoryItemId: level.inventory_item_id,
locationId: level.location_id,
adjustment: level.adjustment,
}
})
)
await locking.execute(inventoryItemIds, async () => {
await inventoryService.adjustInventory(
adjustedLevels.map((level) => {
return {
inventoryItemId: level.inventory_item_id,
locationId: level.location_id,
adjustment: level.adjustment,
}
})
)
})
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ export const cancelOrderFulfillmentWorkflow = createWorkflow(
}
})

adjustInventoryLevelsStep(inventoryAdjustment)
parallelize(
cancelOrderFulfillmentStep(cancelOrderFulfillmentData),
adjustInventoryLevelsStep(inventoryAdjustment),
emitEventStep({
eventName: OrderWorkflowEvents.FULFILLMENT_CANCELED,
data: eventData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ export const confirmReturnReceiveWorkflow = createWorkflow(

confirmReceiveReturnValidationStep({ order, orderReturn, orderChange })

adjustInventoryLevelsStep(inventoryAdjustment)
parallelize(
updateReturnsStep([updateReturn]),
updateReturnItemsStep(updateReturnItem),
Expand All @@ -301,6 +300,7 @@ export const confirmReturnReceiveWorkflow = createWorkflow(
orderId: order.id,
confirmed_by: input.confirmed_by,
}),
adjustInventoryLevelsStep(inventoryAdjustment),
emitEventStep({
eventName: OrderWorkflowEvents.RETURN_RECEIVED,
data: {
Expand Down

0 comments on commit 1bd1a64

Please sign in to comment.