chore(order): cancel order (#7586)

This commit is contained in:
Carlos R. L. Rodrigues
2024-06-03 12:31:33 -03:00
committed by GitHub
parent fdd9022376
commit 122186a78d
42 changed files with 945 additions and 116 deletions

View File

@@ -1,17 +1,8 @@
import {
Context,
CreateInventoryLevelInput,
DAL,
SharedContext,
} from "@medusajs/types"
import {
InjectTransactionManager,
MedusaContext,
ModulesSdkUtils,
} from "@medusajs/utils"
import { Context } from "@medusajs/types"
import { ModulesSdkUtils } from "@medusajs/utils"
import { InventoryLevel } from "../models/inventory-level"
import { InventoryLevelRepository } from "@repositories"
import { InventoryLevel } from "../models/inventory-level"
type InjectedDependencies = {
inventoryLevelRepository: InventoryLevelRepository

View File

@@ -15,11 +15,11 @@ import {
InjectManager,
InjectTransactionManager,
InventoryEvents,
isDefined,
isString,
MedusaContext,
MedusaError,
ModulesSdkUtils,
isDefined,
isString,
partitionArray,
promiseAll,
} from "@medusajs/utils"
@@ -781,6 +781,43 @@ export default class InventoryModuleService<
)
}
/**
* Deletes reservation items by line item
* @param lineItemId - the id of the line item associated with the reservation item
* @param context
*/
@InjectTransactionManager("baseRepository_")
@EmitEvents()
async restoreReservationItemsByLineItem(
lineItemId: string | string[],
@MedusaContext() context: Context = {}
): Promise<void> {
const reservations: InventoryNext.ReservationItemDTO[] =
await this.listReservationItems({ line_item_id: lineItemId }, {}, context)
await this.reservationItemService_.restore(
{ line_item_id: lineItemId },
context
)
await this.adjustInventoryLevelsForReservationsRestore(
reservations,
context
)
context.messageAggregator?.saveRawMessageData(
reservations.map((reservationItem) => ({
eventName: InventoryEvents.reservation_item_created,
service: this.constructor.name,
action: CommonEvents.CREATED,
object: "reservation-item",
context,
data: { id: reservationItem.id },
}))
)
}
/**
* Adjusts the inventory level for a given inventory item and location.
* @param inventoryItemId - the id of the inventory item
@@ -1040,6 +1077,30 @@ export default class InventoryModuleService<
reservations: ReservationItemDTO[],
context: Context
): Promise<void> {
await this.adjustInventoryLevelsForReservations_(
reservations,
true,
context
)
}
private async adjustInventoryLevelsForReservationsRestore(
reservations: ReservationItemDTO[],
context: Context
): Promise<void> {
await this.adjustInventoryLevelsForReservations_(
reservations,
false,
context
)
}
private async adjustInventoryLevelsForReservations_(
reservations: ReservationItemDTO[],
isDelete: boolean,
context: Context
): Promise<void> {
const multiplier = isDelete ? -1 : 1
const inventoryLevels = await this.ensureInventoryLevels(
reservations.map((r) => ({
inventory_item_id: r.inventory_item_id,
@@ -1055,8 +1116,8 @@ export default class InventoryModuleService<
const inventoryLevelMap = acc.get(curr.inventory_item_id) ?? new Map()
const adjustment = inventoryLevelMap.has(curr.location_id)
? inventoryLevelMap.get(curr.location_id) - curr.quantity
: -curr.quantity
? inventoryLevelMap.get(curr.location_id) + curr.quantity * multiplier
: curr.quantity * multiplier
inventoryLevelMap.set(curr.location_id, adjustment)
acc.set(curr.inventory_item_id, inventoryLevelMap)