Skip to content

Commit

Permalink
Improved: updating purchase order item status in batches to avoid par…
Browse files Browse the repository at this point in the history
…allel call (#386)
  • Loading branch information
amansinghbais committed Nov 13, 2024
1 parent 8fe8b37 commit 9ada53a
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions src/components/ClosePurchaseOrderModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down

0 comments on commit 9ada53a

Please sign in to comment.