diff options
Diffstat (limited to 'src-migrate/services')
| -rw-r--r-- | src-migrate/services/product.ts | 58 |
1 files changed, 4 insertions, 54 deletions
diff --git a/src-migrate/services/product.ts b/src-migrate/services/product.ts index a5039ad9..fe415d11 100644 --- a/src-migrate/services/product.ts +++ b/src-migrate/services/product.ts @@ -2,7 +2,6 @@ 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; @@ -21,7 +20,6 @@ export const getProductById = async ( }; export interface GetProductSimilarProps { - source?: string; name: string; except?: { productId?: number; @@ -35,78 +33,30 @@ 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<GetProductSimilarRes> => { - 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=${ - source === 'bottom' ? '4' : '1' - }`, - operation: 'AND' - }) - - dataflashSale = dataFlash.response.products.map((product: { - qtySold: any; - stockTotal: any; - flashSale: any; - lowestPrice: any; - }) => ({ - ...product, - lowest_price: { - discount_percentage: product.lowestPrice.discountPercentage, - price: product.lowestPrice.price, - price_discount: product.lowestPrice.priceDiscount, - }, - flash_sale: product.flashSale, - stock_total: product.stockTotal, - qty_sold: product.qtySold, - })); - if (source === 'bottom') { - dataflashSale = dataflashSale.slice('2', '4') - } else { - dataflashSale = dataflashSale - } - } - - const regex = /\b(?![A-Z\s]+\b)[A-Za-z\s]+\b/g; - const matches = name?.match(regex); - const extractedName = matches ? matches.join(' ').trim() : ''; - console.log("extractedName",extractedName); const query = [ - `q=${extractedName}`, + `q=${name}`, 'page=1', 'orderBy=popular-weekly', '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('&')}`; - const fetchedData = await fetch(url) + return 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 ( |
