Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved: closing purchase order item in sync and in batches instead of async (#386) #411

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 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,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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making synchronous calls, we can update items in batches. Let's say there are N items to update. We can process N−1 items in batches, and then update the final item synchronously after all batch processing is complete. This way PO status get updated correctly after all items are completed.

Later we can think of some api or csv data import mechanism to update order item status in bulk.

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;

Expand Down
Loading