From 5f0a1e7128083cc96c4fd7bb92bf1394a6af80d6 Mon Sep 17 00:00:00 2001 From: Lars Mitsem Selbekk Date: Wed, 12 Jun 2024 00:21:49 +0200 Subject: [PATCH] fix: register multiple standmatch items at a time Previously, there was a bug where handing out or in multiple items in one order would cause only one (the last one) to be registered in the StandMatch. That is now fixed. --- .../operations/place/order-place.operation.ts | 66 ++++++++++++------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/src/collections/order/operations/place/order-place.operation.ts b/src/collections/order/operations/place/order-place.operation.ts index 5556a985..0edca96b 100644 --- a/src/collections/order/operations/place/order-place.operation.ts +++ b/src/collections/order/operations/place/order-place.operation.ts @@ -295,7 +295,8 @@ export class OrderPlaceOperation implements Operation { (match) => match._variant === MatchVariant.StandMatch, ) as StandMatch[]; - // Register items as delivered + // Discover items to register delivery of + const matchToDeliveredItemsMap = new Map>(); for (const customerItem of returnCustomerItems) { const foundStandMatch = standMatches.find( (standMatch) => @@ -304,22 +305,33 @@ export class OrderPlaceOperation implements Operation { !standMatch.deliveredItems.includes(String(customerItem.item)), ); if (foundStandMatch) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - await this._matchStorage.update( + matchToDeliveredItemsMap.set( foundStandMatch.id, - { - deliveredItems: [ - ...foundStandMatch.deliveredItems, - customerItem.item as string, - ], - }, - new SystemUser(), + new Set([ + ...(matchToDeliveredItemsMap.get(foundStandMatch.id) ?? []), + ...foundStandMatch.deliveredItems, + customerItem.item as string, + ]), ); } } - // Register items as received + // Register delivery + for (const [ + standMatchId, + deliveredItems, + ] of matchToDeliveredItemsMap.entries()) { + await this._matchStorage.update( + standMatchId, + { + deliveredItems: Array.from(deliveredItems), + }, + new SystemUser(), + ); + } + + // Discover items to register receiving of + const matchToReceivedItemsMap = new Map>(); for (const customerItem of handoutCustomerItems) { const foundStandMatch = standMatches.find( (standMatch) => @@ -328,20 +340,30 @@ export class OrderPlaceOperation implements Operation { !standMatch.receivedItems.includes(String(customerItem.item)), ); if (foundStandMatch) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - await this._matchStorage.update( + matchToReceivedItemsMap.set( foundStandMatch.id, - { - receivedItems: [ - ...foundStandMatch.receivedItems, - customerItem.item as string, - ], - }, - new SystemUser(), + new Set([ + ...(matchToReceivedItemsMap.get(foundStandMatch.id) ?? []), + ...foundStandMatch.receivedItems, + customerItem.item as string, + ]), ); } } + + // Register receiving + for (const [ + standMatchId, + receivedItems, + ] of matchToReceivedItemsMap.entries()) { + await this._matchStorage.update( + standMatchId, + { + receivedItems: Array.from(receivedItems), + }, + new SystemUser(), + ); + } } public async run(