From 9ada53aee9ff74a5eade9e5bd8864194d14d0853 Mon Sep 17 00:00:00 2001 From: amansinghbais Date: Wed, 13 Nov 2024 19:01:19 +0530 Subject: [PATCH] Improved: updating purchase order item status in batches to avoid parallel call (#386) --- src/components/ClosePurchaseOrderModal.vue | 54 ++++++++++++++++------ 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/src/components/ClosePurchaseOrderModal.vue b/src/components/ClosePurchaseOrderModal.vue index 36c6ba51..25cc125f 100644 --- a/src/components/ClosePurchaseOrderModal.vue +++ b/src/components/ClosePurchaseOrderModal.vue @@ -133,23 +133,51 @@ export default defineComponent({ const eligibleItems = this.order.items.filter((item: any) => item.isChecked && this.isPOItemStatusPending(item)) let hasFailedItems = false; let completedItems = [] as any; + let lastItem = {} as any; - for(const item of eligibleItems) { - try{ - const resp = await OrderService.updatePOItemStatus({ - orderId: item.orderId, - orderItemSeqId: item.orderItemSeqId, - statusId: "ITEM_COMPLETED" + if(eligibleItems.length > 1) { + const itemsToBatchUpdate = eligibleItems.slice(0, -1); + lastItem = eligibleItems[eligibleItems.length - 1]; + + const batchSize = 10; + while(itemsToBatchUpdate.length) { + const itemsToUpdate = itemsToBatchUpdate.splice(0, batchSize) + + const responses = await Promise.allSettled(itemsToUpdate.map(async(item: any) => { + await OrderService.updatePOItemStatus({ + orderId: item.orderId, + orderItemSeqId: item.orderItemSeqId, + statusId: "ITEM_COMPLETED" + }) + return item.orderItemSeqId + })) + + responses.map((response: any) => { + if(response.status === "fulfilled") { + completedItems.push(response.value) + } else { + hasFailedItems = true + } }) + } + } else { + lastItem = eligibleItems[0] + } - if(!hasError(resp)) { - completedItems.push(item.orderItemSeqId) - } else { - throw resp.data; - } - } catch(error: any) { - hasFailedItems = true; + try{ + const resp = await OrderService.updatePOItemStatus({ + orderId: lastItem.orderId, + orderItemSeqId: lastItem.orderItemSeqId, + statusId: "ITEM_COMPLETED" + }) + + if(!hasError(resp)) { + completedItems.push(lastItem.orderItemSeqId) + } else { + throw resp.data; } + } catch(error: any) { + hasFailedItems = true; } if(hasFailedItems){