summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFIN-IT_AndriFP <andrifebriyadiputra@gmail.com>2025-12-16 14:35:33 +0700
committerFIN-IT_AndriFP <andrifebriyadiputra@gmail.com>2025-12-16 14:35:33 +0700
commit31853dc731c6e4105c9cf9bd373c63e6e989caa4 (patch)
tree9d8ae27723cdc4d29c231f2456f9bd26966ca5a0 /src
parent13b30c5da917264cb63f41d058f2dc66f28affcc (diff)
(andri) try compare with data
Diffstat (limited to 'src')
-rw-r--r--src/pages/api/shop/search.js94
1 files changed, 85 insertions, 9 deletions
diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js
index 7d4adfcb..90841e09 100644
--- a/src/pages/api/shop/search.js
+++ b/src/pages/api/shop/search.js
@@ -2,6 +2,14 @@ import { productMappingSolr } from '@/utils/solrMapping';
import axios from 'axios';
import camelcaseObjectDeep from 'camelcase-object-deep';
+const escapeSolrQuery = (query) => {
+ if (query == '*') return query;
+ query = query.replace(/-/g, ' ');
+ const specialChars = /([\+\!\(\)\{\}\[\]\^"~\*\?:\\\/])/g;
+ const words = query.split(/\s+/);
+ return words.map((word) => specialChars.test(word) ? word.replace(specialChars, '\\$1') : word).join(' ');
+};
+
export default async function handler(req, res) {
const {
q = '*',
@@ -20,6 +28,82 @@ export default async function handler(req, res) {
let { stock = '' } = req.query;
// ============================================================
+ // [BARU] 1. LOGIC KHUSUS COMPARE (Wajib ditaruh paling atas)
+ // ============================================================
+ if (source === 'compare') {
+ try {
+ let qCompare = q === '*' ? '*:*' : q;
+
+ // Sanitasi Query
+ if (qCompare !== '*:*') {
+ const escaped = escapeSolrQuery(qCompare);
+ qCompare = `*${escaped}*`;
+ }
+
+ // Susun Parameter Solr
+ const parameter = [
+ `q=${encodeURIComponent(qCompare)}`,
+ `rows=${limit}`,
+ 'wt=json',
+ 'indent=true',
+ 'defType=edismax',
+
+ // Grouping agar varian tidak banjir (per template)
+ 'group=true',
+ 'group.field=template_id_i',
+ 'group.limit=1',
+ 'group.main=true',
+
+ // Field Wajib (Perhatikan: kita butuh product_id_i/default_code_s)
+ 'fl=id,display_name_s,default_code_s,image_s,price_tier1_v2_f,attribute_set_id_i,attribute_set_name_s,template_id_i,product_id_i',
+
+ // Filter Dasar
+ 'fq=-publish_b:false',
+ 'fq=price_tier1_v2_f:[1 TO *]'
+ ];
+
+ // Logic Locking (Filter Attribute Set ID dari Frontend)
+ // Frontend akan mengirim fq="attribute_set_id_i:9"
+ if (fq) {
+ if (Array.isArray(fq)) {
+ fq.forEach(f => parameter.push(`fq=${encodeURIComponent(f)}`));
+ } else {
+ parameter.push(`fq=${encodeURIComponent(fq)}`);
+ }
+ }
+
+ // Target Core: VARIANTS (Karena compare butuh data spesifik)
+ const solrUrl = process.env.SOLR_HOST + '/solr/variants/select?' + parameter.join('&');
+
+ const result = await axios(solrUrl);
+
+ // Mapping Result
+ const mappedProducts = productMappingSolr(
+ result.data.response.docs,
+ false
+ );
+
+ const finalResponse = {
+ ...result.data,
+ response: {
+ ...result.data.response,
+ products: mappedProducts
+ }
+ };
+
+ delete finalResponse.response.docs;
+ const camelCasedData = camelcaseObjectDeep(finalResponse);
+
+ return res.status(200).json(camelCasedData);
+
+ } catch (e) {
+ console.error('[COMPARE SEARCH ERROR]', e.message);
+ // Return JSON valid meski kosong, agar frontend tidak error syntax
+ return res.status(200).json({ response: { products: [], numFound: 0 } });
+ }
+ }
+
+ // ============================================================
// LOGIC KHUSUS UPSELL (Simple & Direct)
// ============================================================
if (source === 'upsell') {
@@ -219,12 +303,4 @@ export default async function handler(req, res) {
} 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+/);
- return words.map((word) => specialChars.test(word) ? word.replace(specialChars, '\\$1') : word).join(' ');
-}; \ No newline at end of file
+} \ No newline at end of file