From f7227485f1a6a096633d9375d2564bcebd7c8210 Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Thu, 6 Jul 2023 16:12:28 +0530 Subject: [PATCH] Implemented: logic to get the preorder and backorder dynamically --- .gitignore | 5 +- src/i18n/locales/en.json | 4 ++ src/services/ProductService.ts | 22 +++++++- src/store/modules/product/ProductState.ts | 5 +- src/store/modules/product/actions.ts | 61 +++++++++++++++++++++ src/store/modules/product/index.ts | 3 +- src/store/modules/product/mutation-types.ts | 3 +- src/store/modules/product/mutations.ts | 3 + src/views/catalog-product-details.vue | 41 ++++++++++---- 9 files changed, 131 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index ef312bccc..9d4e0b408 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,7 @@ npm-debug.log* /www .firebase/ -.env \ No newline at end of file +.env +.history +.env.* +!.env.example \ No newline at end of file diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index 5b96c397a..01b065b9d 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -52,6 +52,7 @@ "Excluded ATP": "Excluded ATP", "from date": "from date", "Failed to update configuration": "Failed to update configuration", + "Failed to get pre-order/backorder categories": "Failed to get pre-order/backorder categories", "Go to OMS": "Go to OMS", "History": "History", "Hold pre-order physical inventory": "Hold pre-order physical inventory", @@ -83,12 +84,15 @@ "Loyalty status": "Loyalty status", "Never in any category": "Never in any category", "No": "No", + "No backorder category found": "No backorder category found", "No job found": "No job found", "No jobs found": "No jobs found", "No jobs have run yet": "No jobs have run yet", "No listing data": "No listing data", "No products found": "No products found", + "No pre-order category found": "No pre-order category found", "No results found": "No results found", + "No selected store found.": "No selected store found.", "No time zone found": "No time zone found", "No warehouses found": "No warehouses found", "No shop listings found": "No shop listings found", diff --git a/src/services/ProductService.ts b/src/services/ProductService.ts index 8b54052ab..519af9417 100644 --- a/src/services/ProductService.ts +++ b/src/services/ProductService.ts @@ -31,9 +31,29 @@ const getCatalogProducts = async (payload: any): Promise => { }) } +const getProductStoreCatalog = async (payload: any): Promise => { + return api({ + url: "performFind", + method: "get", + params: payload, + cache: true + }); +} + +const getCatalogCategories = async (payload: any): Promise => { + return api({ + url: "performFind", + method: "get", + params: payload, + cache: true + }); +} + export const ProductService = { fetchProducts, findOrder, fetchCurrentList, - getCatalogProducts + getCatalogCategories, + getCatalogProducts, + getProductStoreCatalog } \ No newline at end of file diff --git a/src/store/modules/product/ProductState.ts b/src/store/modules/product/ProductState.ts index 81377f896..804c65721 100644 --- a/src/store/modules/product/ProductState.ts +++ b/src/store/modules/product/ProductState.ts @@ -15,6 +15,7 @@ export default interface ProductState { catalogProducts: { items: any[], total: number - }, - currentCatalogProduct: any + }; + currentCatalogProduct: any; + productStoreCategory: any; } \ No newline at end of file diff --git a/src/store/modules/product/actions.ts b/src/store/modules/product/actions.ts index 437a1336c..094fd7f37 100644 --- a/src/store/modules/product/actions.ts +++ b/src/store/modules/product/actions.ts @@ -272,5 +272,66 @@ const actions: ActionTree = { total: 0 }); }, + /** + * Fetch catalog products + */ + async getPreOrderBackorderCategory({ commit, state }, payload) { + let productStoreCategories = state.productStoreCategory[payload.productStoreId]; + + if (productStoreCategories) { + return productStoreCategories; + } + + let resp; + try { + productStoreCategories = {}; + let productStoreCatalogId; + resp = await ProductService.getProductStoreCatalog({ + "inputFields": { + "productStoreId": payload.productStoreId, + }, + "fieldList": ["productStoreId", "prodCatalogId"], + "entityName": "ProductStoreCatalog", + "distinct": "Y", + "noConditionFind": "Y", + "filterByDate": "Y", + "viewSize": 1, + }) + + if (!hasError(resp)) { + productStoreCatalogId = resp.data.docs[0].prodCatalogId + } else { + throw resp.data; + } + + resp = await ProductService.getCatalogCategories({ + "inputFields": { + "productStoreId": payload.productStoreId, + "prodCatalogCategoryTypeId": ["PCCT_PREORDR", "PCCT_BACKORDER"], + "prodCatalogCategoryTypeId_op": "in" + }, + "fieldList": ["productCategoryId", "prodCatalogCategoryTypeId"], + "entityName": "ProdCatalogCategory", + "distinct": "Y", + "noConditionFind": "Y", + "filterByDate": "Y", + "viewSize": 10, // view size should be 2, considered if there are duplicate records it should not affect + }) + if (!hasError(resp)) { + resp.data.docs.reduce((productStoreCategories: any, category: any) => { + productStoreCategories[category.prodCatalogCategoryTypeId] = category.productCategoryId; + return productStoreCategories; + }, productStoreCategories); + } else { + throw resp.data; + } + state.productStoreCategory[payload.productStoreId] = productStoreCategories; + commit(types.PRODUCT_STR_CAT_UPDATED, state.productStoreCategory) + } catch (error) { + console.error(error) + return Promise.reject(error); + } + return productStoreCategories; + }, } export default actions; \ No newline at end of file diff --git a/src/store/modules/product/index.ts b/src/store/modules/product/index.ts index fc1c544ed..4d79a14d5 100644 --- a/src/store/modules/product/index.ts +++ b/src/store/modules/product/index.ts @@ -25,7 +25,8 @@ const productModule: Module = { items: [], total: 0 }, - currentCatalogProduct: {} + currentCatalogProduct: {}, + productStoreCategory: {} }, getters, actions, diff --git a/src/store/modules/product/mutation-types.ts b/src/store/modules/product/mutation-types.ts index 8bde1d89e..89fbaf97c 100644 --- a/src/store/modules/product/mutation-types.ts +++ b/src/store/modules/product/mutation-types.ts @@ -7,4 +7,5 @@ export const PRODUCT_CURRENT_LIST_UPDATED = SN_PRODUCT + '/CURRENT_LIST_UPDATED' export const PRODUCT_CURRENT_TOTAL_PRE_ORDERS_UPDATED = SN_PRODUCT + '/CURRENT_TOTAL_PRE_ORDERS_UPDATED' export const PRODUCT_CACHED_UPDATED = SN_PRODUCT + '/CACHED_UPDATED' export const PRODUCT_CATALOG_UPDATED = SN_PRODUCT + '/CATALOG_UPDATED' -export const PRODUCT_CURRENT_CTLGPRDCT_UPDATED = SN_PRODUCT + '/CURRENT_CTLGPRDCT_UPDATED' \ No newline at end of file +export const PRODUCT_CURRENT_CTLGPRDCT_UPDATED = SN_PRODUCT + '/CURRENT_CTLGPRDCT_UPDATED' +export const PRODUCT_STR_CAT_UPDATED = SN_PRODUCT + '/STR_CAT_UPDATED' \ No newline at end of file diff --git a/src/store/modules/product/mutations.ts b/src/store/modules/product/mutations.ts index 054a5053f..3374cc529 100644 --- a/src/store/modules/product/mutations.ts +++ b/src/store/modules/product/mutations.ts @@ -37,6 +37,9 @@ const mutations: MutationTree = { }, [types.PRODUCT_CURRENT_CTLGPRDCT_UPDATED] (state, payload) { state.currentCatalogProduct = payload + }, + [types.PRODUCT_STR_CAT_UPDATED] (state, payload) { + state.productStoreCategory = payload } } export default mutations; \ No newline at end of file diff --git a/src/views/catalog-product-details.vue b/src/views/catalog-product-details.vue index b9bb1346b..b4be3edcd 100644 --- a/src/views/catalog-product-details.vue +++ b/src/views/catalog-product-details.vue @@ -97,7 +97,7 @@ {{ $t("Category") }} - {{ poSummary.categoryId === 'PREORDER_CAT' ? $t('Pre-order') : poSummary.categoryId === 'BACKORDER_CAT' ? $t('Back-order') : $t('None') }} + {{ poSummary.categoryId === preOrderCategoryId ? $t('Pre-order') : poSummary.categoryId === backorderCategoryId ? $t('Back-order') : $t('None') }} @@ -455,7 +455,9 @@ export default defineComponent({ poSummary: {} as any, shopListings: [] as any, configsByStores: [] as any, - listingJobRunTime: 0 + listingJobRunTime: 0, + backorderCategoryId: '', + preOrderCategoryId: '' } }, computed: { @@ -469,6 +471,26 @@ export default defineComponent({ async ionViewWillEnter() { (this as any).productId = this.$route.params.productId; (this as any).variantId = this.$route.query.variantId; + const productStoreId = this.currentEComStore.productStoreId; + // TODO Handle it better + if (!productStoreId) { + showToast(translate("No selected store found.")) + return; + } + try { + // TODO Handle error case + const productStoreCategories = await this.store.dispatch('product/getPreOrderBackorderCategory', { productStoreId }) + this.preOrderCategoryId = productStoreCategories["PCCT_PREORDR"]; + this.backorderCategoryId = productStoreCategories["PCCT_BACKORDER"]; + if (!this.preOrderCategoryId) { + showToast(translate("No pre-order category found")) + } + if (!this.backorderCategoryId) { + showToast(translate("No backorder category found")) + } + } catch (error) { + showToast(translate("Failed to get pre-order/backorder categories")) + } await this.getShopifyConfigsByStore() await this.getVariantDetails() await this.getCtgryAndBrkrngJobs() @@ -585,15 +607,14 @@ export default defineComponent({ showToast(this.$t("Something went wrong, could not fetch", { data: 'total ATP' })) } - // TODO Make categoryIds dynamic - const hasPreOrderCategory = productCategories?.includes("PREORDER_CAT"); - const hasBackorderCategory = productCategories?.includes("BACKORDER_CAT"); + const hasPreOrderCategory = productCategories?.includes(this.preOrderCategoryId); + const hasBackorderCategory = productCategories?.includes(this.backorderCategoryId); if (hasPreOrderCategory || hasBackorderCategory) { payload = { "inputFields": { "productId": variantId, - "productCategoryId": hasPreOrderCategory ? "PREORDER_CAT" : "BACKORDER_CAT", + "productCategoryId": hasPreOrderCategory ? this.preOrderCategoryId : this.backorderCategoryId, "productCategoryId_op": "equals" }, "entityName": "ProductCategoryDcsnRsn", @@ -794,15 +815,15 @@ export default defineComponent({ }, hasCategory() { // TODO make categoryIds dynamic - return this.currentVariant.productCategories?.includes("PREORDER_CAT") || this.currentVariant.productCategories?.includes("BACKORDER_CAT"); + return this.currentVariant.productCategories?.includes(this.preOrderCategoryId) || this.currentVariant.productCategories?.includes(this.backorderCategoryId); }, async preparePoSummary() { this.poSummary.eligible = this.poAndAtpDetails.totalPoAtp > 0 && this.atpCalcDetails.onlineAtp === 0; this.poSummary.isActivePo = (this.poAndAtpDetails.activePo && Object.keys(this.poAndAtpDetails?.activePo).length) && this.poAndAtpDetails.onlineAtp > 0 // TODO Remove code for lastActivePoId // this.poSummary.isLastActivePo = this.poAndAtpDetails.lastActivePoId && Object.keys(this.poAndAtpDetails?.activePo).length - this.poSummary.categoryId = this.currentVariant.productCategories?.includes("PREORDER_CAT") ? "PREORDER_CAT" : this.currentVariant.productCategories?.includes("BACKORDER_CAT") ? "BACKORDER_CAT" : "" - // const category = this.poSummary.categoryId === 'PREORDER_CAT' ? 'pre-order' : 'back-order' + this.poSummary.categoryId = this.currentVariant.productCategories?.includes(this.preOrderCategoryId) ? this.preOrderCategoryId : this.currentVariant.productCategories?.includes(this.backorderCategoryId) ? this.backorderCategoryId : "" + // const category = this.poSummary.categoryId === this.preOrderCategoryId ? 'pre-order' : 'back-order' // Currently we are only having one shop listing condition // will improve the logic as the listing conditions increase this.poSummary.listedCount = this.shopListings.reduce((count: number, listData: any) => @@ -862,7 +883,7 @@ export default defineComponent({ } }, isCategoryValid() { - const poCategoryId = this.poAndAtpDetails.activePo?.isNewProduct === "Y" ? "PREORDER_CAT" : "BACKORDER_CAT"; + const poCategoryId = this.poAndAtpDetails.activePo?.isNewProduct === "Y" ? this.preOrderCategoryId : this.backorderCategoryId; return (this.poSummary.eligible && this.poSummary.categoryId && (!this.poAndAtpDetails.activePo || poCategoryId === this.poSummary.categoryId) ) || (!this.poSummary.eligible && !this.poSummary.categoryId) }, isShopifyListingValid() {