From 8fe8b37115c3d4e36d77e4d617b1f094aaca5582 Mon Sep 17 00:00:00 2001 From: amansinghbais Date: Mon, 28 Oct 2024 18:45:49 +0530 Subject: [PATCH 1/2] Improved: closing purchase order item in sync (#386) --- src/components/ClosePurchaseOrderModal.vue | 35 ++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/components/ClosePurchaseOrderModal.vue b/src/components/ClosePurchaseOrderModal.vue index 5424a71a..36c6ba51 100644 --- a/src/components/ClosePurchaseOrderModal.vue +++ b/src/components/ClosePurchaseOrderModal.vue @@ -67,6 +67,7 @@ import { mapGetters, useStore } from 'vuex' import { OrderService } from "@/services/OrderService"; import { DxpShopifyImg, translate, getProductIdentificationValue, useProductIdentificationStore } from '@hotwax/dxp-components'; import { useRouter } from 'vue-router'; +import { hasError } from '@/utils'; export default defineComponent({ name: "ClosePurchaseOrderModal", @@ -130,20 +131,30 @@ export default defineComponent({ } const eligibleItems = this.order.items.filter((item: any) => item.isChecked && this.isPOItemStatusPending(item)) - const responses = await Promise.allSettled(eligibleItems.map(async (item: any) => { - await OrderService.updatePOItemStatus({ - orderId: item.orderId, - orderItemSeqId: item.orderItemSeqId, - statusId: "ITEM_COMPLETED" - }) - return item.orderItemSeqId - })) - const failedItemsCount = responses.filter((response) => response.status === 'rejected').length - if(failedItemsCount){ - console.error('Failed to update the status of purchase order items.') + let hasFailedItems = false; + let completedItems = [] as any; + + for(const item of eligibleItems) { + try{ + const resp = await OrderService.updatePOItemStatus({ + orderId: item.orderId, + orderItemSeqId: item.orderItemSeqId, + statusId: "ITEM_COMPLETED" + }) + + if(!hasError(resp)) { + completedItems.push(item.orderItemSeqId) + } else { + throw resp.data; + } + } catch(error: any) { + hasFailedItems = true; + } } - const completedItems = responses.filter((response) => response.status === 'fulfilled')?.map((response: any) => response.value) + if(hasFailedItems){ + console.error('Failed to update the status of purchase order items.') + } if(!completedItems.length) return; From 9ada53aee9ff74a5eade9e5bd8864194d14d0853 Mon Sep 17 00:00:00 2001 From: amansinghbais Date: Wed, 13 Nov 2024 19:01:19 +0530 Subject: [PATCH 2/2] 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){