From 89c996632f5e0b839b69b5552d1fb892a92f59d2 Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Tue, 2 Apr 2024 18:15:12 +0530 Subject: [PATCH] Improved: support to upload csv file when updating inventory(#279) Added: support to parse json to csv before upload Added check for error message in api and display a toast message in both inventory and po upload case --- src/services/UploadService.ts | 10 ++++++++++ src/utils/index.ts | 2 +- src/views/InventoryReview.vue | 33 ++++++++++++++++++++++--------- src/views/PurchaseOrderReview.vue | 7 ++++++- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/services/UploadService.ts b/src/services/UploadService.ts index 79ed5f14..2b786504 100644 --- a/src/services/UploadService.ts +++ b/src/services/UploadService.ts @@ -8,6 +8,15 @@ const uploadJsonFile = async (payload: any): Promise => { ...payload }); } + +const uploadAndImportFile = async (payload: any): Promise => { + return api({ + url: "uploadAndImportFile", + method: "post", + ...payload + }); +} + const prepareUploadJsonPayload = (request: UploadRequest) => { const blob = new Blob([JSON.stringify(request.uploadData)], { type: 'application/json'}); const formData = new FormData(); @@ -28,5 +37,6 @@ const prepareUploadJsonPayload = (request: UploadRequest) => { export const UploadService = { prepareUploadJsonPayload, + uploadAndImportFile, uploadJsonFile } \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts index 5a4296ea..ed364066 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -118,7 +118,7 @@ const jsonToCsv = (file: any, options: JsonToCsvOption = {}) => { ...options.encode }; const blob = new Blob([csv], { - type: "application/csvcharset=" + encoding + type: "application/csv;charset=" + encoding }); if (options.download) { saveAs(blob, options.name ? options.name : "default.csv"); diff --git a/src/views/InventoryReview.vue b/src/views/InventoryReview.vue index d969b87e..5861c97c 100644 --- a/src/views/InventoryReview.vue +++ b/src/views/InventoryReview.vue @@ -101,9 +101,10 @@ import MissingFacilitiesModal from '@/components/MissingFacilitiesModal.vue' import { defineComponent } from 'vue'; import { mapGetters, useStore } from "vuex"; import { useRouter } from 'vue-router'; -import { showToast } from '@/utils'; +import { jsonToCsv, showToast } from '@/utils'; import { IonCard, IonCardContent, IonPage, IonHeader, IonToolbar, IonBackButton, IonContent, IonItem, IonThumbnail, IonLabel, IonChip, IonIcon, IonButton, IonButtons, popoverController, IonFab, IonFabButton, modalController, alertController, IonNote, IonSpinner, IonTitle } from '@ionic/vue' import { businessOutline, calculatorOutline, chevronForwardOutline, ellipsisVerticalOutline, locationOutline, shirtOutline, checkboxOutline, cloudUploadOutline, arrowUndoOutline, warningOutline } from 'ionicons/icons' +import { hasError } from "@/adapter"; export default defineComponent({ name: 'InventoryDetail', @@ -223,7 +224,6 @@ export default defineComponent({ this.store.dispatch('stock/updateStockItems', this.stockItems); }, async save(){ - const fileName = this.fileName.replace(".csv", ".json"); const uploadData = this.stockItems.parsed.map((item: any) => { return { "facilityId": item.facilityId, @@ -232,12 +232,12 @@ export default defineComponent({ "idType": item.identificationTypeId, "locationSeqId": item.locationSeqId, "availableQty": item.quantity, - "comments": `Inventory was modified via the Import App by ${this.userProfile.partyName} using the ${fileName} file.` + "comments": `Inventory was modified via the Import App by ${this.userProfile.partyName} using the ${this.fileName} file.` }; }) const params = { "configId": "RESET_INVENTORY" - } + } as any const alert = await alertController.create({ header: translate("Reset inventory"), message: translate("Make sure all the data you have entered is correct."), @@ -249,11 +249,26 @@ export default defineComponent({ { text: translate("Upload"), handler: () => { - UploadService.uploadJsonFile(UploadService.prepareUploadJsonPayload({ - uploadData, - fileName, - params - })).then(() => { + const data = jsonToCsv(uploadData) + const formData = new FormData(); + formData.append("uploadedFile", data, this.fileName); + + if(Object.keys(params)) { + for(const key in params) { + formData.append(key, params[key]); + } + } + + UploadService.uploadAndImportFile({ + data: formData, + headers: { + 'Content-Type': 'multipart/form-data;' + } + }).then((resp: any) => { + if(hasError(resp)) { + throw resp.data + } + this.isCsvUploadedSuccessfully = true; showToast(translate("The inventory has been updated successfully"), [{ text: translate('View'), diff --git a/src/views/PurchaseOrderReview.vue b/src/views/PurchaseOrderReview.vue index 22f88b57..b21fbb6b 100644 --- a/src/views/PurchaseOrderReview.vue +++ b/src/views/PurchaseOrderReview.vue @@ -105,6 +105,7 @@ import MissingSkuModal from "@/components/MissingSkuModal.vue" import { UploadService } from "@/services/UploadService"; import { showToast } from '@/utils'; import { translate } from "@hotwax/dxp-components"; +import { hasError } from '@/adapter'; export default defineComponent({ name: 'PurchaseOrderReview', @@ -277,7 +278,11 @@ export default defineComponent({ uploadData, fileName, params - })).then(() => { + })).then((resp: any) => { + if(hasError(resp)) { + throw resp.data + } + this.isPOUploadedSuccessfully = true; showToast(translate("The PO has been uploaded successfully"), [{ text: translate('View'),