feat(inventory): check stock to create reservation (#7209)
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user