feat(medusa): Remove reservations for all line items when an order edit is accepted (#3544)
**What** - Remove all allocations to line items once an order edit is confirmed **Why** - Since all line items of an order are discarded once an order edit is confirmed it will orphan the reservations causing inconsistencies with the stock
This commit is contained in:
1 parent
999aeb116c
commit
5e405be02c
6 files changed
+167
-41
No files matched your search
@@ -1,6 +1,4 @@
|
||||
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
|
||||
import { OrderEditItemChangeType, OrderEditStatus } from "../../models"
|
||||
import EventBusService from "../event-bus"
|
||||
import {
|
||||
LineItemService,
|
||||
NewTotalsService,
|
||||
@@ -10,15 +8,18 @@ import {
|
||||
TaxProviderService,
|
||||
TotalsService,
|
||||
} from "../index"
|
||||
import LineItemAdjustmentService from "../line-item-adjustment"
|
||||
import { OrderEditItemChangeType, OrderEditStatus } from "../../models"
|
||||
|
||||
import EventBusService from "../event-bus"
|
||||
import { EventBusServiceMock } from "../__mocks__/event-bus"
|
||||
import { LineItemServiceMock } from "../__mocks__/line-item"
|
||||
import LineItemAdjustmentService from "../line-item-adjustment"
|
||||
import { LineItemAdjustmentServiceMock } from "../__mocks__/line-item-adjustment"
|
||||
import { LineItemServiceMock } from "../__mocks__/line-item"
|
||||
import NewTotalsServiceMock from "../__mocks__/new-totals"
|
||||
import { OrderServiceMock } from "../__mocks__/order"
|
||||
import { TotalsServiceMock } from "../__mocks__/totals"
|
||||
import { orderEditItemChangeServiceMock } from "../__mocks__/order-edit-item-change"
|
||||
import { taxProviderServiceMock } from "../__mocks__/tax-provider"
|
||||
import { TotalsServiceMock } from "../__mocks__/totals"
|
||||
import NewTotalsServiceMock from "../__mocks__/new-totals"
|
||||
|
||||
const orderEditToUpdate = {
|
||||
id: IdMap.getId("order-edit-to-update"),
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
import { isDefined, MedusaError } from "medusa-core-utils"
|
||||
import {
|
||||
DeepPartial,
|
||||
EntityManager,
|
||||
FindOptionsWhere,
|
||||
ILike,
|
||||
IsNull,
|
||||
} from "typeorm"
|
||||
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
AddOrderEditLineItemInput,
|
||||
CreateOrderEditInput,
|
||||
} from "../types/order-edit"
|
||||
import {
|
||||
Cart,
|
||||
Order,
|
||||
@@ -15,14 +9,14 @@ import {
|
||||
OrderEditItemChangeType,
|
||||
OrderEditStatus,
|
||||
} from "../models"
|
||||
import { OrderEditRepository } from "../repositories/order-edit"
|
||||
import { FindConfig, Selector } from "../types/common"
|
||||
import {
|
||||
AddOrderEditLineItemInput,
|
||||
CreateOrderEditInput,
|
||||
} from "../types/order-edit"
|
||||
import { buildQuery, isString } from "../utils"
|
||||
import EventBusService from "./event-bus"
|
||||
DeepPartial,
|
||||
EntityManager,
|
||||
FindOptionsWhere,
|
||||
ILike,
|
||||
IsNull,
|
||||
} from "typeorm"
|
||||
import { FindConfig, Selector } from "../types/common"
|
||||
import {
|
||||
LineItemAdjustmentService,
|
||||
LineItemService,
|
||||
@@ -32,6 +26,13 @@ import {
|
||||
TaxProviderService,
|
||||
TotalsService,
|
||||
} from "./index"
|
||||
import { MedusaError, isDefined } from "medusa-core-utils"
|
||||
import { buildQuery, isString } from "../utils"
|
||||
|
||||
import EventBusService from "./event-bus"
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { OrderEditRepository } from "../repositories/order-edit"
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
|
||||
type InjectedDependencies = {
|
||||
manager: EntityManager
|
||||
@@ -45,6 +46,8 @@ type InjectedDependencies = {
|
||||
taxProviderService: TaxProviderService
|
||||
lineItemAdjustmentService: LineItemAdjustmentService
|
||||
orderEditItemChangeService: OrderEditItemChangeService
|
||||
|
||||
inventoryService?: IInventoryService
|
||||
}
|
||||
|
||||
export default class OrderEditService extends TransactionBaseService {
|
||||
@@ -67,6 +70,7 @@ export default class OrderEditService extends TransactionBaseService {
|
||||
protected readonly taxProviderService_: TaxProviderService
|
||||
protected readonly lineItemAdjustmentService_: LineItemAdjustmentService
|
||||
protected readonly orderEditItemChangeService_: OrderEditItemChangeService
|
||||
protected readonly inventoryService_: IInventoryService | undefined
|
||||
|
||||
constructor({
|
||||
orderEditRepository,
|
||||
@@ -78,6 +82,7 @@ export default class OrderEditService extends TransactionBaseService {
|
||||
orderEditItemChangeService,
|
||||
lineItemAdjustmentService,
|
||||
taxProviderService,
|
||||
inventoryService,
|
||||
}: InjectedDependencies) {
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(arguments[0])
|
||||
@@ -91,6 +96,7 @@ export default class OrderEditService extends TransactionBaseService {
|
||||
this.orderEditItemChangeService_ = orderEditItemChangeService
|
||||
this.lineItemAdjustmentService_ = lineItemAdjustmentService
|
||||
this.taxProviderService_ = taxProviderService
|
||||
this.inventoryService_ = inventoryService
|
||||
}
|
||||
|
||||
async retrieve(
|
||||
@@ -744,7 +750,7 @@ export default class OrderEditService extends TransactionBaseService {
|
||||
|
||||
const lineItemServiceTx = this.lineItemService_.withTransaction(manager)
|
||||
|
||||
await Promise.all([
|
||||
const [lineItems] = await Promise.all([
|
||||
lineItemServiceTx.update(
|
||||
{ order_id: orderEdit.order_id },
|
||||
{ order_id: null }
|
||||
@@ -760,6 +766,17 @@ export default class OrderEditService extends TransactionBaseService {
|
||||
|
||||
orderEdit = await orderEditRepository.save(orderEdit)
|
||||
|
||||
if (this.inventoryService_) {
|
||||
await Promise.all(
|
||||
lineItems.map(
|
||||
async (lineItem) =>
|
||||
await this.inventoryService_!.deleteReservationItemsByLineItem(
|
||||
lineItem.id
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
await this.eventBusService_
|
||||
.withTransaction(manager)
|
||||
.emit(OrderEditService.Events.CONFIRMED, { id: orderEditId })
|
||||
|
||||
Reference in new issue
Block a user