From 09dd51b37b5c315691cc4681b75248bc52c0580f Mon Sep 17 00:00:00 2001 From: amansinghbais Date: Wed, 11 Oct 2023 10:49:01 +0530 Subject: [PATCH] Improved: logic to call changeOrderItemStatus api and show Toast accordingly (#212) --- src/locales/en.json | 1 + src/views/ClosePurchaseOrderModal.vue | 49 +++++++++++++++------------ src/views/PurchaseOrderDetail.vue | 9 +++-- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/locales/en.json b/src/locales/en.json index f4f995f7..14563656 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -110,6 +110,7 @@ "Something went wrong": "Something went wrong", "Something went wrong while login. Please contact administrator": "Something went wrong while login. Please contact administrator.", "Sorry, your username or password is incorrect. Please try again.": "Sorry, your username or password is incorrect. Please try again.", + "Some purchase order items were not successfully updated, Please retry.": "Some purchase order items were not successfully updated, Please retry.", "Specify which facility you want to operate from. Order, inventory and other configuration data will be specific to the facility you select.": "Specify which facility you want to operate from. Order, inventory and other configuration data will be specific to the facility you select.", "store name": "store name", "Store": "Store", diff --git a/src/views/ClosePurchaseOrderModal.vue b/src/views/ClosePurchaseOrderModal.vue index 9da4b6ab..487b6bc6 100644 --- a/src/views/ClosePurchaseOrderModal.vue +++ b/src/views/ClosePurchaseOrderModal.vue @@ -72,9 +72,10 @@ import { productHelpers, showToast } from '@/utils'; import { ShopifyImg } from '@hotwax/dxp-components'; import { translate } from '@/i18n' import emitter from "@/event-bus" +import { useRouter } from 'vue-router'; export default defineComponent({ - name: "ClosePurchaseOrder", + name: "ClosePurchaseOrderModal", components: { IonBadge, IonButton, @@ -122,6 +123,8 @@ export default defineComponent({ emitter.emit('create-shipment') } await this.updatePOItemStatus() + modalController.dismiss() + this.router.push('/purchase-orders') } }] }); @@ -129,31 +132,33 @@ export default defineComponent({ }, async updatePOItemStatus() { const eligibleItems = this.order.items.filter((item: any) => item.isChecked) - const areAllItemsSelected = this.areAllItemsSelected(eligibleItems) - - eligibleItems.forEach(async (item:any) => { - const selectedItem = { - orderId: item.orderId, - orderItemSeqId: item.orderItemSeqId - } as any - if(!areAllItemsSelected) { - selectedItem.statusId = "ITEM_COMPLETED" + await Promise.allSettled(eligibleItems.map(async (item:any) => { + const selectedItemDetails = { + orderId: item.orderId, + orderItemSeqId: item.orderItemSeqId, + statusId: "ITEM_COMPLETED" } - - try{ - await OrderService.updatePOItemStatus({orderId: item.orderId, orderItemSeqId: item.orderItemSeqId}) - .then(() => { - showToast(translate('Purchase order updated successfully.')) - }) + + try { + await OrderService.updatePOItemStatus(selectedItemDetails) + item.statusUpdated = true } catch(err) { + item.statusUpdated = false console.error(err); - showToast(translate("Purchase order update failed.")) } - }); - }, - areAllItemsSelected(eligibleItems: any) { - return eligibleItems.length === this.order.items.filter((item:any) => item.orderItemStatusId != "ITEM_COMPLETED" || item.orderItemStatusId != "ITEM_REJECTED").length + })) + + const failedItemsCount = eligibleItems.filter((item: any) => item.statusUpdated === false).length + + if(failedItemsCount === 0){ + showToast(translate('Purchase order updated successfully.')) + } else if(failedItemsCount === eligibleItems.length){ + showToast(translate("Purchase order update failed.")) + } else { + showToast(translate("Some purchase order items were not successfully updated, Please retry.")) + } + }, isEligibleToClosePOItems() { return this.order.items.some((item: any) => item.isChecked) @@ -167,6 +172,7 @@ export default defineComponent({ } }, setup() { + const router = useRouter() return { arrowBackOutline, Actions, @@ -175,6 +181,7 @@ export default defineComponent({ hasPermission, OrderService, productHelpers, + router, saveOutline }; } diff --git a/src/views/PurchaseOrderDetail.vue b/src/views/PurchaseOrderDetail.vue index 141429e9..0cbc37bf 100644 --- a/src/views/PurchaseOrderDetail.vue +++ b/src/views/PurchaseOrderDetail.vue @@ -235,6 +235,9 @@ export default defineComponent({ productIdentificationPref: 'user/getProductIdentificationPref' }) }, + mounted() { + emitter.on('create-shipment', this.createShipment) + }, methods: { getRcvdToOrderedFraction(item: any){ return (parseInt(item.quantityAccepted) + this.getPOItemAccepted(item.productId))/(item.quantity) @@ -313,8 +316,6 @@ export default defineComponent({ } }) - emitter.on('create-shipment', this.createShipment) - return modal.present(); }, async createShipment() { @@ -323,7 +324,6 @@ export default defineComponent({ if (resp.status === 200 && !hasError(resp)) { this.router.push('/purchase-orders') } - emitter.off('create-shipment', this.createShipment) }, isEligibileForCreatingShipment() { return this.order.items.some((item: any) => item.quantityAccepted > 0) @@ -344,6 +344,9 @@ export default defineComponent({ this.store.dispatch('order/getPOHistory', { orderId: this.order.orderId }) }) }, + unmounted() { + emitter.off('create-shipment', this.createShipment) + }, setup() { const store = useStore(); const router = useRouter();