feat(inventory): check stock to create reservation (#7209)

This commit is contained in:
Carlos R. L. Rodrigues
2024-05-05 10:24:52 +02:00
committed by GitHub
parent e97b0125b5
commit c32a31e139
6 changed files with 137 additions and 27 deletions
@@ -15,11 +15,12 @@ import {
InjectManager,
InjectTransactionManager,
InventoryEvents,
isDefined,
MedusaContext,
MedusaError,
ModulesSdkUtils,
isDefined,
partitionArray,
promiseAll,
} from "@medusajs/utils"
import { InventoryItem, InventoryLevel, ReservationItem } from "@models"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
@@ -170,6 +171,27 @@ export default class InventoryModuleService<
> {
const toCreate = Array.isArray(input) ? input : [input]
const checkLevels = toCreate.map(async (item) => {
if (!!item.allow_backorder) {
return
}
const available = await this.retrieveAvailableQuantity(
item.inventory_item_id,
[item.location_id],
context
)
if (available < item.quantity) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
`Not enough stock available for item ${item.inventory_item_id} at location ${item.location_id}`
)
}
})
await promiseAll(checkLevels)
const created = await this.createReservationItems_(toCreate, context)
context.messageAggregator?.saveRawMessageData(