diff --git a/src/locales/en.json b/src/locales/en.json index e5575a14..d2937944 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -13,7 +13,7 @@ "Cycle Count": "Cycle Count", "eCom Store": "eCom Store", "Enter a SKU, or use the barcode scanner to search a product": "Enter a SKU, or use the barcode scanner to search a product", - "Enter product sku to search": "Enter product sku to search", + "Enter the product sku, upc, product name, etc. to search.": "Enter the product sku, upc, product name, etc. to search.", "Enter the amount of stock that has changed.": "Enter the amount of stock that has changed.", "Enter the count of stock on the shelf.": "Enter the count of stock on the shelf.", "Enter the stock count for the product": "Enter the stock count for the product", diff --git a/src/services/ProductService.ts b/src/services/ProductService.ts index 9731c5ff..45fb9448 100644 --- a/src/services/ProductService.ts +++ b/src/services/ProductService.ts @@ -3,9 +3,9 @@ import { hasError } from "@/utils"; const fetchProducts = async (query: any): Promise => { return api({ - url: "searchProducts", - method: "get", - params: query, + url: "solr-query", + method: "post", + data: query, cache: true }); } diff --git a/src/store/modules/product/actions.ts b/src/store/modules/product/actions.ts index 926755de..354473df 100644 --- a/src/store/modules/product/actions.ts +++ b/src/store/modules/product/actions.ts @@ -6,6 +6,7 @@ import * as types from './mutation-types' import { hasError, showToast } from '@/utils' import { translate } from '@/i18n' import emitter from '@/event-bus' +import { prepareProductQuery } from '@/utils/solrHelper' const actions: ActionTree = { @@ -19,11 +20,14 @@ const actions: ActionTree = { let resp; try { - resp = await ProductService.fetchProducts({ - "filters": [`sku: ${payload.queryString} OR upc: ${payload.queryString}`,'isVirtual: false'], - "viewSize": payload.viewSize, - "viewIndex": payload.viewIndex - }) + const params = { + ...payload, + filters: { + isVirtual: { value: 'false' }, + } + } + const productQueryPayload = prepareProductQuery(params) + resp = await ProductService.fetchProducts(productQueryPayload) // resp.data.response.numFound tells the number of items in the response if (resp.status === 200 && resp.data.response?.numFound > 0 && !hasError(resp)) { diff --git a/src/utils/solrHelper.ts b/src/utils/solrHelper.ts new file mode 100644 index 00000000..fb2432b3 --- /dev/null +++ b/src/utils/solrHelper.ts @@ -0,0 +1,45 @@ +const prepareProductQuery = (params: any) => { + const viewSize = params.viewSize ? params.viewSize : process.env.VUE_APP_VIEW_SIZE; + const viewIndex = params.viewIndex ? params.viewIndex : 0; + + const payload = { + "json": { + "params": { + "rows": viewSize, + "q.op": "AND", + "start": viewIndex * viewSize + }, + "query": "(*:*)", + "filter": [`docType: ${params.docType ? params.docType : 'PRODUCT'}`] + } + } as any + + if (params.queryString) { + payload.json.query = `*${params.queryString}* OR "${params.queryString}"^100` + payload.json.params['qf'] = params.queryFields ? params.queryFields : "sku^100 upc^100 productName^50 internalName^40 productId groupId groupName productCategoryNames" + payload.json.params['defType'] = "edismax" + } + + // checking that if the params has filters, and then adding the filter values in the payload filter + // for each key present in the params filters + if (params.filters) { + Object.keys(params.filters).map((key: any) => { + const filterValue = params.filters[key].value; + + if (Array.isArray(filterValue)) { + const filterOperator = params.filters[key].op ? params.filters[key].op : 'OR'; + payload.json.filter += ` AND ${key}: (${filterValue.join(' ' + filterOperator + ' ')})` + } else { + payload.json.filter += ` AND ${key}: ${filterValue}` + } + }) + } + + if (params.facet) { + payload.json['facet'] = params.facet + } + + return payload +} + +export { prepareProductQuery } diff --git a/src/views/Search.vue b/src/views/Search.vue index bfa9cd5e..073e9485 100644 --- a/src/views/Search.vue +++ b/src/views/Search.vue @@ -143,8 +143,7 @@ export default defineComponent({ this.fetchingProducts = true; const viewSize = vSize ? vSize : process.env.VUE_APP_VIEW_SIZE; const viewIndex = vIndex ? vIndex : 0; - const queryString = '*' + this.queryString + '*'; - await this.getProducts(viewSize, viewIndex, queryString); + await this.getProducts(viewSize, viewIndex, this.queryString); this.fetchingProducts = false; }, async getProducts(vSize?: any, vIndex?: any, queryString?: string) { @@ -156,7 +155,7 @@ export default defineComponent({ if (this.queryString) { await this.store.dispatch("product/findProduct", payload); } else { - showToast(translate("Enter product sku to search")) + showToast(translate("Enter the product sku, upc, product name, etc. to search.")) } } },