Fix(inventory, stock-location): Remove orphaned location levels and reservations (#3460)
**What** - Remove related inventory levels and reservation items when a stock location is removed **How** - Add bulk deletion methods for both inventory levels and reservation items to the inventory service api - invoke both on location removal Fixes CORE-1232
This commit is contained in:
@@ -227,6 +227,24 @@ export default class InventoryLevelService extends TransactionBaseService {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes inventory levels by location ID.
|
||||
* @param locationId - The ID of the location to delete inventory levels for.
|
||||
*/
|
||||
async deleteByLocationId(locationId: string): Promise<void> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const levelRepository = manager.getRepository(InventoryLevel)
|
||||
|
||||
await levelRepository.delete({ location_id: locationId })
|
||||
|
||||
await this.eventBusService_
|
||||
.withTransaction(manager)
|
||||
.emit(InventoryLevelService.Events.DELETED, {
|
||||
location_id: locationId,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the total stocked quantity for a specific inventory item at multiple locations.
|
||||
* @param inventoryItemId - The ID of the inventory item.
|
||||
|
||||
@@ -254,6 +254,18 @@ export default class InventoryService
|
||||
.delete(inventoryItemId)
|
||||
}
|
||||
|
||||
async deleteInventoryItemLevelByLocationId(locationId: string): Promise<void> {
|
||||
return await this.inventoryLevelService_
|
||||
.withTransaction(this.activeManager_)
|
||||
.deleteByLocationId(locationId)
|
||||
}
|
||||
|
||||
async deleteReservationItemByLocationId(locationId: string): Promise<void> {
|
||||
return await this.reservationItemService_
|
||||
.withTransaction(this.activeManager_)
|
||||
.deleteByLocationId(locationId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes an inventory level
|
||||
* @param inventoryItemId - the id of the inventory item associated with the level
|
||||
|
||||
@@ -24,7 +24,6 @@ export default class ReservationItemService extends TransactionBaseService {
|
||||
CREATED: "reservation-item.created",
|
||||
UPDATED: "reservation-item.updated",
|
||||
DELETED: "reservation-item.deleted",
|
||||
DELETED_BY_LINE_ITEM: "reservation-item.deleted-by-line-item",
|
||||
}
|
||||
|
||||
protected readonly eventBusService_: IEventBusService
|
||||
@@ -95,7 +94,10 @@ export default class ReservationItemService extends TransactionBaseService {
|
||||
const manager = this.activeManager_
|
||||
const reservationItemRepository = manager.getRepository(ReservationItem)
|
||||
|
||||
const query = buildQuery({ id: reservationItemId }, config) as FindManyOptions
|
||||
const query = buildQuery(
|
||||
{ id: reservationItemId },
|
||||
config
|
||||
) as FindManyOptions
|
||||
const [reservationItem] = await reservationItemRepository.find(query)
|
||||
|
||||
if (!reservationItem) {
|
||||
@@ -165,8 +167,7 @@ export default class ReservationItemService extends TransactionBaseService {
|
||||
isDefined(data.quantity) && data.quantity !== item.quantity
|
||||
|
||||
const shouldUpdateLocation =
|
||||
isDefined(data.location_id) &&
|
||||
data.location_id !== item.location_id
|
||||
isDefined(data.location_id) && data.location_id !== item.location_id
|
||||
|
||||
const ops: Promise<unknown>[] = []
|
||||
|
||||
@@ -243,12 +244,35 @@ export default class ReservationItemService extends TransactionBaseService {
|
||||
|
||||
await this.eventBusService_
|
||||
.withTransaction(manager)
|
||||
.emit(ReservationItemService.Events.DELETED_BY_LINE_ITEM, {
|
||||
.emit(ReservationItemService.Events.DELETED, {
|
||||
line_item_id: lineItemId,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes reservation items by location ID.
|
||||
* @param locationId - The ID of the location to delete reservations for.
|
||||
*/
|
||||
async deleteByLocationId(locationId: string): Promise<void> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const itemRepository = manager.getRepository(ReservationItem)
|
||||
|
||||
await itemRepository
|
||||
.createQueryBuilder("reservation_item")
|
||||
.softDelete()
|
||||
.where("location_id = :locationId", { locationId })
|
||||
.andWhere("deleted_at IS NULL")
|
||||
.execute()
|
||||
|
||||
await this.eventBusService_
|
||||
.withTransaction(manager)
|
||||
.emit(ReservationItemService.Events.DELETED, {
|
||||
location_id: locationId,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a reservation item by id.
|
||||
* @param reservationItemId - the id of the reservation item to delete.
|
||||
@@ -272,8 +296,8 @@ export default class ReservationItemService extends TransactionBaseService {
|
||||
await this.eventBusService_
|
||||
.withTransaction(manager)
|
||||
.emit(ReservationItemService.Events.DELETED, {
|
||||
id: reservationItemId,
|
||||
})
|
||||
id: reservationItemId,
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user