import { productMappingSolr } from '@/utils/solrMapping'; import axios from 'axios'; import camelcaseObjectDeep from 'camelcase-object-deep'; import searchSuggestApi from '@/core/api/searchSuggestApi'; import { ECDH } from 'crypto'; export default async function handler(req, res) { const { q = '*', page = 1, brand = '', category = '', priceFrom = 0, priceTo = 0, orderBy = '', operation = 'AND', fq = '', limit = 30, source = '', } = req.query; let { stock = '' } = req.query; let paramOrderBy = ''; switch (orderBy) { case 'flashsale-discount-desc': paramOrderBy += 'flashsale_discount_f DESC'; break; case 'price-asc': paramOrderBy += 'price_tier1_v2_f ASC'; break; case 'price-desc': paramOrderBy += 'price_tier1_v2_f DESC'; break; case 'popular': paramOrderBy += 'product_rating_f DESC, search_rank_i DESC,'; break; case 'popular-weekly': paramOrderBy += 'search_rank_weekly_i DESC'; break; case 'stock': paramOrderBy += 'product_rating_f DESC, stock_total_f DESC'; break; case 'flashsale-price-asc': paramOrderBy += 'flashsale_price_f ASC'; break; default: paramOrderBy += ''; break; } let checkQ = q.trim().split(/[\s\+\-\!\(\)\{\}\[\]\^"~\*\?:\\\/]+/); let newQ = escapeSolrQuery(q); const formattedQuery = `(${newQ .split(' ') .map((term) => (term.length < 2 ? term : `${term}*`)) // Tambahkan '*' hanya jika panjang kata >= 2 .join(' ')})`; const mm = checkQ.length > 2 ? checkQ.length > 5 ? '55%' : '85%' : `${checkQ.length}`; const filterQueries = [ '-publish_b:false', 'product_rating_f:[8 TO *]', 'price_tier1_v2_f:[1 TO *]', ]; if (orderBy === 'stock') { filterQueries.push('stock_total_f:{1 TO *}&sort=stock_total_f desc'); // filterQueries.push(`stock_total_f DESC`) } if (fq && source != 'similar' && typeof fq != 'string') { // filterQueries.push(fq); fq.push(...filterQueries); } const fq_ = filterQueries.join(' AND '); let keywords = newQ; if (source === 'similar' || checkQ.length < 3) { if (checkQ.length < 2 || checkQ[1].length < 2) { keywords = newQ; } else { keywords = newQ + '*'; } } else { keywords = formattedQuery; } let offset = (page - 1) * limit; let parameter = [ // === Disjunctive facets: exclude brand & category filters saat hitung facet === 'facet.field={!ex=brand}manufacture_name_s', 'facet.field={!ex=cat}category_name', 'facet=true', 'indent=true', `facet.query=${escapeSolrQuery(q)}`, `q.op=OR`, `q=${keywords}`, `defType=edismax`, 'qf=name_s description_clean_t category_name manufacture_name_s variants_code_t variants_name_t category_id_ids default_code_s manufacture_id_i category_id_i ', `start=${parseInt(offset)}`, `rows=${limit}`, `sort=${paramOrderBy}`, `fq=${encodeURIComponent(fq_)}`, `mm=${encodeURIComponent(mm)}`, ]; if (priceFrom > 0 || priceTo > 0) { parameter.push( `fq=price_tier1_v2_f:[${priceFrom == '' ? '*' : priceFrom} TO ${ priceTo == '' ? '*' : priceTo }]` ); } let { auth } = req.cookies; if (auth) { auth = JSON.parse(auth); if (auth.feature.onlyReadyStock) stock = true; } if (brand) { // bentuk ekspresi sama seperti versi kamu, tapi dibungkus tag brand const brandExpr = brand .split(',') .map( (manufacturer) => `manufacture_name:"${encodeURIComponent(manufacturer)}"` ) .join(' OR '); parameter.push(`fq={!tag=brand}(${brandExpr})`); } if (category) { // sama: tag kategori const catExpr = category .split(',') .map((cat) => `category_name:"${encodeURIComponent(cat)}"`) .join(' OR '); parameter.push(`fq={!tag=cat}(${catExpr})`); } // if (category) parameter.push(`fq=category_name:${capitalizeFirstLetter(category.replace(/,/g, ' OR '))}`) if (stock) parameter.push(`fq=stock_total_f:{1 TO *}`); // Single fq in url params if (typeof fq === 'string') parameter.push(`fq=${encodeURIComponent(fq)}`); // Multi fq in url params if (Array.isArray(fq)) parameter = parameter.concat( fq.map((val) => `fq=${encodeURIComponent(val)}`) ); let result = await axios( process.env.SOLR_HOST + '/solr/product/select?' + parameter.join('&') ); try { result.data.response.products = productMappingSolr( result.data.response.docs, auth?.pricelist || false ); result.data.responseHeader.params.start = parseInt( result.data.responseHeader.params.start ); result.data.responseHeader.params.rows = parseInt( result.data.responseHeader.params.rows ); delete result.data.response.docs; result.data = camelcaseObjectDeep(result.data); res.status(200).json(result.data); } catch (error) { res.status(400).json({ error: error.message }); } } const escapeSolrQuery = (query) => { if (query == '*') return query; query = query.replace(/-/g, ' '); const specialChars = /([\+\!\(\)\{\}\[\]\^"~\*\?:\\\/])/g; const words = query.split(/\s+/); const escapedWords = words.map((word) => { if (specialChars.test(word)) { return word.replace(specialChars, '\\$1'); } return word; }); return escapedWords.join(' '); }; /*const productResponseMap = (products, pricelist) => { return products.map((product) => { let price = product.price_tier1_v2_f || 0 let priceDiscount = product.price_discount_f || 0 let discountPercentage = product.discount_f || 0 if (pricelist) { // const pricelistDiscount = product?.[`price_${pricelist}_f`] || false // const pricelistDiscountPerc = product?.[`discount_${pricelist}_f`] || false // if (pricelistDiscount && pricelistDiscount > 0) priceDiscount = pricelistDiscount // if (pricelistDiscountPerc && pricelistDiscountPerc > 0) // discountPercentage = pricelistDiscountPerc price = product?.[`price_${pricelist}_f`] || 0 } if (product?.flashsale_id_i > 0) { price = product?.flashsale_base_price_f || 0 priceDiscount = product?.flashsale_price_f || 0 discountPercentage = product?.flashsale_discount_f || 0 } let productMapped = { id: product.product_id_i || '', image: product.image_s || '', code: product.default_code_s || '', name: product.name_s || '', lowestPrice: { price, priceDiscount, discountPercentage }, variantTotal: product.variant_total_i || 0, stockTotal: product.stock_total_f || 0, weight: product.weight_f || 0, manufacture: {}, categories: [], flashSale: { id: product?.flashsale_id_i, name: product?.product?.flashsale_name_s, tag : product?.flashsale_tag_s || 'FLASH SALE' } } if (product.manufacture_id_i && product.manufacture_name_s) { productMapped.manufacture = { id: product.manufacture_id_i || '', name: product.manufacture_name_s || '' } } productMapped.categories = [ { id: product.category_id_i || '', name: product.category_name_s || '' } ] return productMapped }) }*/