From bd62835db7dc521a17d6b0aea81badaae6ade95c Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 2 Aug 2024 16:47:42 +0700 Subject: update template switch --- src-migrate/services/product.ts | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'src-migrate/services') diff --git a/src-migrate/services/product.ts b/src-migrate/services/product.ts index fe415d11..51667eb9 100644 --- a/src-migrate/services/product.ts +++ b/src-migrate/services/product.ts @@ -2,6 +2,7 @@ import { IProduct, IProductDetail } from '~/types/product'; import snakeCase from 'snakecase-keys'; import odooApi from '~/libs/odooApi'; import { ICategoryBreadcrumb } from '~/types/category'; +import productSearchApi from '../../src/lib/product/api/productSearchApi' const SELF_HOST = process.env.NEXT_PUBLIC_SELF_HOST; @@ -40,6 +41,27 @@ export const getProductSimilar = async ({ except, limit = 30, }: GetProductSimilarProps): Promise => { + let dataflashSale = null + const flashSale = await odooApi('GET', '/api/v1/flashsale/header') + if (flashSale && flashSale.length > 0) { + const dataFlash = await productSearchApi({ + query: `fq=flashsale_id_i:${flashSale[0].pricelist_id}&fq=flashsale_price_f:[1 TO *]&limit=4`, + operation: 'AND' + }) + + dataflashSale = dataFlash.response.products.map((product: { + qtySold: any; + stockTotal: any; + flashSale: any; lowestPrice: any; +}) => ({ + ...product, + lowest_price: product.lowestPrice, + flash_sale: product.flashSale, + stock_total: product.stockTotal, + qty_sold: product.qtySold, + lowestPrice: undefined + })); + } const query = [ `q=${name}`, 'page=1', @@ -47,16 +69,25 @@ export const getProductSimilar = async ({ 'operation=OR', 'priceFrom=1', ]; - if (except?.productId) query.push(`fq=-product_id_i:${except.productId}`); if (except?.manufactureId) query.push(`fq=-manufacture_id_i:${except.manufactureId}`); - const url = `${SELF_HOST}/api/shop/search?${query.join('&')}`; - return await fetch(url) + const fetchedData = await fetch(url) .then((res) => res.json()) .then((res) => snakeCase(res.response)); + + if (dataflashSale) { + fetchedData.products = [ + ...dataflashSale, + ...fetchedData.products + ] + } else { + fetchedData.products = [...fetchedData.products] + } + + return fetchedData; }; export const getProductCategoryBreadcrumb = async ( -- cgit v1.2.3 From 616d8ffc973eba673069243ec8981b5234234fe5 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Thu, 8 Aug 2024 17:09:56 +0700 Subject: add similar product template --- src-migrate/services/product.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src-migrate/services') diff --git a/src-migrate/services/product.ts b/src-migrate/services/product.ts index 51667eb9..ec5435cd 100644 --- a/src-migrate/services/product.ts +++ b/src-migrate/services/product.ts @@ -21,6 +21,7 @@ export const getProductById = async ( }; export interface GetProductSimilarProps { + source?: string; name: string; except?: { productId?: number; @@ -34,18 +35,22 @@ export interface GetProductSimilarRes { num_found: number; num_found_exact: boolean; start: number; + source: string; } export const getProductSimilar = async ({ name, except, limit = 30, + source, }: GetProductSimilarProps): Promise => { let dataflashSale = null const flashSale = await odooApi('GET', '/api/v1/flashsale/header') if (flashSale && flashSale.length > 0) { const dataFlash = await productSearchApi({ - query: `fq=flashsale_id_i:${flashSale[0].pricelist_id}&fq=flashsale_price_f:[1 TO *]&limit=4`, + query: `fq=flashsale_id_i:${flashSale[0].pricelist_id}&fq=flashsale_price_f:[1 TO *]&limit=${ + source === 'bottom' ? '4' : '1' + }`, operation: 'AND' }) @@ -61,6 +66,12 @@ export const getProductSimilar = async ({ qty_sold: product.qtySold, lowestPrice: undefined })); + if (source === 'bottom') { + dataflashSale = dataflashSale.slice('2', '4') + } else { + dataflashSale = dataflashSale + } + console.log("dataflashSale",dataflashSale) } const query = [ `q=${name}`, -- cgit v1.2.3