feat(providers): locking redis (#9544)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { IInventoryService } from "@medusajs/framework/types"
|
||||
import { MathBN, Modules } from "@medusajs/framework/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
|
||||
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
|
||||
import { BigNumberInput } from "@medusajs/types"
|
||||
|
||||
export interface ReserveVariantInventoryStepInput {
|
||||
@@ -22,34 +21,45 @@ export const reserveInventoryStepId = "reserve-inventory-step"
|
||||
export const reserveInventoryStep = createStep(
|
||||
reserveInventoryStepId,
|
||||
async (data: ReserveVariantInventoryStepInput, { container }) => {
|
||||
const inventoryService = container.resolve<IInventoryService>(
|
||||
Modules.INVENTORY
|
||||
)
|
||||
const inventoryService = container.resolve(Modules.INVENTORY)
|
||||
|
||||
const items = data.items.map((item) => ({
|
||||
line_item_id: item.id,
|
||||
inventory_item_id: item.inventory_item_id,
|
||||
quantity: MathBN.mult(item.required_quantity, item.quantity),
|
||||
allow_backorder: item.allow_backorder,
|
||||
location_id: item.location_ids[0],
|
||||
}))
|
||||
const locking = container.resolve(Modules.LOCKING)
|
||||
|
||||
const reservations = await inventoryService.createReservationItems(items)
|
||||
const inventoryItemIds: string[] = []
|
||||
|
||||
const items = data.items.map((item) => {
|
||||
inventoryItemIds.push(item.inventory_item_id)
|
||||
|
||||
return {
|
||||
line_item_id: item.id,
|
||||
inventory_item_id: item.inventory_item_id,
|
||||
quantity: MathBN.mult(item.required_quantity, item.quantity),
|
||||
allow_backorder: item.allow_backorder,
|
||||
location_id: item.location_ids[0],
|
||||
}
|
||||
})
|
||||
|
||||
const reservations = await locking.execute(inventoryItemIds, async () => {
|
||||
return await inventoryService.createReservationItems(items)
|
||||
})
|
||||
|
||||
return new StepResponse(reservations, {
|
||||
reservations: reservations.map((r) => r.id),
|
||||
inventoryItemIds,
|
||||
})
|
||||
},
|
||||
async (data, { container }) => {
|
||||
if (!data) {
|
||||
if (!data?.reservations?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const inventoryService = container.resolve<IInventoryService>(
|
||||
Modules.INVENTORY
|
||||
)
|
||||
const inventoryService = container.resolve(Modules.INVENTORY)
|
||||
const locking = container.resolve(Modules.LOCKING)
|
||||
|
||||
await inventoryService.deleteReservationItems(data.reservations)
|
||||
const inventoryItemIds = data.inventoryItemIds
|
||||
await locking.execute(inventoryItemIds, async () => {
|
||||
await inventoryService.deleteReservationItems(data.reservations)
|
||||
})
|
||||
|
||||
return new StepResponse()
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { IInventoryService, InventoryTypes } from "@medusajs/framework/types"
|
||||
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
|
||||
import { InventoryTypes } from "@medusajs/framework/types"
|
||||
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
|
||||
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
@@ -10,22 +10,31 @@ export const createReservationsStepId = "create-reservations-step"
|
||||
export const createReservationsStep = createStep(
|
||||
createReservationsStepId,
|
||||
async (data: InventoryTypes.CreateReservationItemInput[], { container }) => {
|
||||
const service = container.resolve<IInventoryService>(Modules.INVENTORY)
|
||||
const service = container.resolve(Modules.INVENTORY)
|
||||
const locking = container.resolve(Modules.LOCKING)
|
||||
|
||||
const created = await service.createReservationItems(data)
|
||||
const inventoryItemIds = data.map((item) => item.inventory_item_id)
|
||||
|
||||
return new StepResponse(
|
||||
created,
|
||||
created.map((reservation) => reservation.id)
|
||||
)
|
||||
const created = await locking.execute(inventoryItemIds, async () => {
|
||||
return await service.createReservationItems(data)
|
||||
})
|
||||
|
||||
return new StepResponse(created, {
|
||||
reservations: created.map((reservation) => reservation.id),
|
||||
inventoryItemIds: inventoryItemIds,
|
||||
})
|
||||
},
|
||||
async (createdIds, { container }) => {
|
||||
if (!createdIds?.length) {
|
||||
async (data, { container }) => {
|
||||
if (!data?.reservations?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IInventoryService>(Modules.INVENTORY)
|
||||
const service = container.resolve(Modules.INVENTORY)
|
||||
const locking = container.resolve(Modules.LOCKING)
|
||||
|
||||
await service.deleteReservationItems(createdIds)
|
||||
const inventoryItemIds = data.inventoryItemIds
|
||||
await locking.execute(inventoryItemIds, async () => {
|
||||
await service.deleteReservationItems(data.reservations)
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user