Skip to content

Commit

Permalink
Merge pull request #411 from amansinghbais/#386
Browse files Browse the repository at this point in the history
Improved: closing purchase order item in sync and in batches instead of async (#386)
  • Loading branch information
ymaheshwari1 authored Nov 14, 2024
2 parents 6b19892 + 68085b1 commit 8e5a9d0
Showing 1 changed file with 49 additions and 10 deletions.
59 changes: 49 additions & 10 deletions src/components/ClosePurchaseOrderModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -130,20 +131,58 @@ 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,
let hasFailedItems = false;
let completedItems = [] as any;
let lastItem = {} as any;
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]
}
try{
const resp = await OrderService.updatePOItemStatus({
orderId: lastItem.orderId,
orderItemSeqId: lastItem.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.')
if(!hasError(resp)) {
completedItems.push(lastItem.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;
Expand Down

0 comments on commit 8e5a9d0

Please sign in to comment.