Fix/adjust reservations correctly (#3474)

**What**
- Adjust reservations correctly according to the following heuristic: 

adjustment by addition:  (i.e. positive quantity adjustment passed to the adjustment method)
- if a reservation for the line-item in the location exists add quantity to that
- if not create a new reservation

adjustment by subtraction: 
- if a reservation with the exact quantity exists, delete it and return
- if a reservation with a greater quantity exists, subtract from it and return 
- otherwise delete from reservations until a reservation with greater quantity than the remaining is found and adjust that with the remaining quantity OR there are no more reservations

Fixes CORE-1247
This commit is contained in:
Philip Korsholm
2023-03-16 09:47:54 +00:00
committed by GitHub
parent 38c8d49f46
commit 02c77d7059
9 changed files with 392 additions and 102 deletions
+1 -1
View File
@@ -350,7 +350,7 @@ export default class InventoryService
* Deletes a reservation item
* @param reservationItemId - the id of the reservation item to delete
*/
async deleteReservationItem(reservationItemId: string): Promise<void> {
async deleteReservationItem(reservationItemId: string | string[]): Promise<void> {
return await this.reservationItemService_
.withTransaction(this.activeManager_)
.delete(reservationItemId)
@@ -277,21 +277,29 @@ export default class ReservationItemService extends TransactionBaseService {
* Deletes a reservation item by id.
* @param reservationItemId - the id of the reservation item to delete.
*/
async delete(reservationItemId: string): Promise<void> {
await this.atomicPhase_(async (manager) => {
async delete(reservationItemId: string | string[]): Promise<void> {
const ids = Array.isArray(reservationItemId)
? reservationItemId
: [reservationItemId]
return await this.atomicPhase_(async (manager) => {
const itemRepository = manager.getRepository(ReservationItem)
const item = await this.retrieve(reservationItemId)
await Promise.all([
itemRepository.softRemove({ id: reservationItemId }),
this.inventoryLevelService_
.withTransaction(manager)
.adjustReservedQuantity(
const items = await this.list({ id: ids })
await itemRepository.softRemove(items)
const inventoryServiceTx =
this.inventoryLevelService_.withTransaction(manager)
await Promise.all(
items.map(async (item) => {
return inventoryServiceTx.adjustReservedQuantity(
item.inventory_item_id,
item.location_id,
item.quantity * -1
),
])
)
})
)
await this.eventBusService_
.withTransaction(manager)