fix(medusa): Order edit missing transaction when consuming the inventory module (#4211)
* fix(medusa): Order edit missing transaction when consuming the inventory module * Create hot-cougars-speak.md * fix missing types
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/inventory": patch
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
"@medusajs/types": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(medusa): Order edit missing transaction when consuming the inventory module
|
||||||
@@ -441,7 +441,7 @@ export default class InventoryService implements IInventoryService {
|
|||||||
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
|
target.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.ISOLATED
|
||||||
)
|
)
|
||||||
async deleteReservationItemsByLineItem(
|
async deleteReservationItemsByLineItem(
|
||||||
lineItemId: string,
|
lineItemId: string | string[],
|
||||||
@MedusaContext() context: SharedContext = {}
|
@MedusaContext() context: SharedContext = {}
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
return await this.reservationItemService_.deleteByLineItem(
|
return await this.reservationItemService_.deleteByLineItem(
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ import {
|
|||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
InjectEntityManager,
|
InjectEntityManager,
|
||||||
|
isDefined,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
MedusaError,
|
MedusaError,
|
||||||
isDefined,
|
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { EntityManager, FindManyOptions } from "typeorm"
|
import { EntityManager, FindManyOptions, In } from "typeorm"
|
||||||
import { InventoryLevelService } from "."
|
import { InventoryLevelService } from "."
|
||||||
import { ReservationItem } from "../models"
|
import { ReservationItem } from "../models"
|
||||||
import { buildQuery } from "../utils/build-query"
|
import { buildQuery } from "../utils/build-query"
|
||||||
@@ -235,21 +235,24 @@ export default class ReservationItemService {
|
|||||||
*/
|
*/
|
||||||
@InjectEntityManager()
|
@InjectEntityManager()
|
||||||
async deleteByLineItem(
|
async deleteByLineItem(
|
||||||
lineItemId: string,
|
lineItemId: string | string[],
|
||||||
@MedusaContext() context: SharedContext = {}
|
@MedusaContext() context: SharedContext = {}
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const manager = context.transactionManager!
|
const manager = context.transactionManager!
|
||||||
const itemRepository = manager.getRepository(ReservationItem)
|
const itemRepository = manager.getRepository(ReservationItem)
|
||||||
|
|
||||||
|
const itemsIds = Array.isArray(lineItemId) ? lineItemId : [lineItemId]
|
||||||
|
|
||||||
const items = await this.list(
|
const items = await this.list(
|
||||||
{ line_item_id: lineItemId },
|
{ line_item_id: itemsIds },
|
||||||
undefined,
|
undefined,
|
||||||
context
|
context
|
||||||
)
|
)
|
||||||
|
|
||||||
const ops: Promise<unknown>[] = [
|
const ops: Promise<unknown>[] = [
|
||||||
itemRepository.softDelete({ line_item_id: lineItemId }),
|
itemRepository.softDelete({ line_item_id: In(itemsIds) }),
|
||||||
]
|
]
|
||||||
|
|
||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
ops.push(
|
ops.push(
|
||||||
this.inventoryLevelService_.adjustReservedQuantity(
|
this.inventoryLevelService_.adjustReservedQuantity(
|
||||||
@@ -260,6 +263,7 @@ export default class ReservationItemService {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(ops)
|
await Promise.all(ops)
|
||||||
|
|
||||||
await this.eventBusService_?.emit?.(ReservationItemService.Events.DELETED, {
|
await this.eventBusService_?.emit?.(ReservationItemService.Events.DELETED, {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import {
|
|||||||
TaxProviderService,
|
TaxProviderService,
|
||||||
TotalsService,
|
TotalsService,
|
||||||
} from "./index"
|
} from "./index"
|
||||||
import { MedusaError, isDefined } from "medusa-core-utils"
|
import { isDefined, MedusaError } from "medusa-core-utils"
|
||||||
import { buildQuery, isString } from "../utils"
|
import { buildQuery, isString } from "../utils"
|
||||||
|
|
||||||
import EventBusService from "./event-bus"
|
import EventBusService from "./event-bus"
|
||||||
@@ -767,13 +767,12 @@ export default class OrderEditService extends TransactionBaseService {
|
|||||||
orderEdit = await orderEditRepository.save(orderEdit)
|
orderEdit = await orderEditRepository.save(orderEdit)
|
||||||
|
|
||||||
if (this.inventoryService_) {
|
if (this.inventoryService_) {
|
||||||
await Promise.all(
|
const itemsIds = lineItems.map((i) => i.id)
|
||||||
lineItems.map(
|
await this.inventoryService_!.deleteReservationItemsByLineItem(
|
||||||
async (lineItem) =>
|
itemsIds,
|
||||||
await this.inventoryService_!.deleteReservationItemsByLineItem(
|
{
|
||||||
lineItem.id
|
transactionManager: manager,
|
||||||
)
|
}
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,14 +3,14 @@ import {
|
|||||||
ICacheService,
|
ICacheService,
|
||||||
IEventBusService,
|
IEventBusService,
|
||||||
IInventoryService,
|
IInventoryService,
|
||||||
IStockLocationService,
|
|
||||||
InventoryItemDTO,
|
InventoryItemDTO,
|
||||||
InventoryLevelDTO,
|
InventoryLevelDTO,
|
||||||
|
IStockLocationService,
|
||||||
ReservationItemDTO,
|
ReservationItemDTO,
|
||||||
ReserveQuantityContext,
|
ReserveQuantityContext,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { LineItem, Product, ProductVariant } from "../models"
|
import { LineItem, Product, ProductVariant } from "../models"
|
||||||
import { MedusaError, isDefined } from "@medusajs/utils"
|
import { isDefined, MedusaError } from "@medusajs/utils"
|
||||||
import { PricedProduct, PricedVariant } from "../types/pricing"
|
import { PricedProduct, PricedVariant } from "../types/pricing"
|
||||||
import { TransactionBaseService } from "../interfaces"
|
import { TransactionBaseService } from "../interfaces"
|
||||||
|
|
||||||
@@ -584,7 +584,7 @@ class ProductVariantInventoryService extends TransactionBaseService {
|
|||||||
* @param quantity quantity to release
|
* @param quantity quantity to release
|
||||||
*/
|
*/
|
||||||
async deleteReservationsByLineItem(
|
async deleteReservationsByLineItem(
|
||||||
lineItemId: string,
|
lineItemId: string | string[],
|
||||||
variantId: string,
|
variantId: string,
|
||||||
quantity: number
|
quantity: number
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
@@ -606,7 +606,10 @@ class ProductVariantInventoryService extends TransactionBaseService {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.inventoryService_.deleteReservationItemsByLineItem(lineItemId)
|
const itemIds = Array.isArray(lineItemId) ? lineItemId : [lineItemId]
|
||||||
|
await this.inventoryService_.deleteReservationItemsByLineItem(itemIds, {
|
||||||
|
transactionManager: this.activeManager_,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ export interface IInventoryService {
|
|||||||
): Promise<ReservationItemDTO>
|
): Promise<ReservationItemDTO>
|
||||||
|
|
||||||
deleteReservationItemsByLineItem(
|
deleteReservationItemsByLineItem(
|
||||||
lineItemId: string,
|
lineItemId: string | string[],
|
||||||
context?: SharedContext
|
context?: SharedContext
|
||||||
): Promise<void>
|
): Promise<void>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user