summaryrefslogtreecommitdiff
path: root/src/pages/api/shop
diff options
context:
space:
mode:
authortrisusilo48 <tri.susilo@altama.co.id>2024-11-18 11:35:58 +0700
committertrisusilo48 <tri.susilo@altama.co.id>2024-11-18 11:35:58 +0700
commitfb58a58715a7f5530a60479487457e5e0a1a41ec (patch)
tree733f0332f127a4d6bc931330ec1cd103968dbb7d /src/pages/api/shop
parent0d4278bd482d2ec2563b29cb3597eb8c7227a2d7 (diff)
parentbde516b6b39cccfe8ac3248cd7f85592e6298d7a (diff)
Merge branch 'new-release' into feature/integrasi_biteship
Diffstat (limited to 'src/pages/api/shop')
-rw-r--r--src/pages/api/shop/brands.js30
-rw-r--r--src/pages/api/shop/preferredBrand.js61
-rw-r--r--src/pages/api/shop/search.js34
3 files changed, 104 insertions, 21 deletions
diff --git a/src/pages/api/shop/brands.js b/src/pages/api/shop/brands.js
index 9c2824b3..d56e4b13 100644
--- a/src/pages/api/shop/brands.js
+++ b/src/pages/api/shop/brands.js
@@ -1,8 +1,20 @@
import axios from 'axios';
+import { createClient } from 'redis';
const SOLR_HOST = process.env.SOLR_HOST;
+const client = createClient();
+
+client.on('error', (err) => console.error('Redis Client Error', err));
+
+const connectRedis = async () => {
+ if (!client.isOpen) {
+ await client.connect();
+ }
+};
export default async function handler(req, res) {
+ await connectRedis();
+
try {
let params = '*:*';
let sort =
@@ -11,12 +23,12 @@ export default async function handler(req, res) {
if (req.query.params) {
rows = 100;
- switch (req?.query?.params) {
+ switch (req.query.params) {
case 'level_s':
params = 'level_s:prioritas';
break;
case 'search':
- params = `name_s:"${req?.query?.q.toLowerCase()}"`;
+ params = `name_s:"${req.query.q.toLowerCase()}"`;
sort = '';
rows = 1;
break;
@@ -24,11 +36,11 @@ export default async function handler(req, res) {
params = `name_s:${req.query.params}`.toLowerCase();
}
}
- if(req.query.rows) rows = req.query.rows;
-
+ if (req.query.rows) rows = req.query.rows;
+
const url = `${SOLR_HOST}/solr/brands/select?q=${params}&q.op=OR&indent=true&rows=${rows}&${sort}`;
- let brands = await axios(url);
- let dataBrands = responseMap(brands.data.response.docs);
+ const brands = await axios(url);
+ const dataBrands = responseMap(brands.data.response.docs);
res.status(200).json(dataBrands);
} catch (error) {
@@ -39,13 +51,11 @@ export default async function handler(req, res) {
const responseMap = (brands) => {
return brands.map((brand) => {
- let brandMapping = {
+ return {
id: brand.id,
name: brand.display_name_s,
logo: brand.image_s || '',
- sequance: brand.sequence_i || '',
+ sequence: brand.sequence_i || '',
};
-
- return brandMapping;
});
};
diff --git a/src/pages/api/shop/preferredBrand.js b/src/pages/api/shop/preferredBrand.js
new file mode 100644
index 00000000..4cb35c84
--- /dev/null
+++ b/src/pages/api/shop/preferredBrand.js
@@ -0,0 +1,61 @@
+import axios from 'axios';
+import { createClient } from 'redis';
+
+const SOLR_HOST = process.env.SOLR_HOST;
+const client = createClient();
+
+client.on('error', (err) => console.error('Redis Client Error', err));
+
+const connectRedis = async () => {
+ if (!client.isOpen) {
+ await client.connect();
+ }
+};
+
+export default async function handler(req, res) {
+ await connectRedis();
+
+ try {
+ let params = '*:*';
+ let sort =
+ 'sort=if(exists(sequence_i),0,1) asc,sequence_i asc, if(exists(image_s),0,1) asc ';
+ let rows = 20;
+
+ if (req.query.params) {
+ rows = 20;
+ switch (req.query.params) {
+ case 'level_s':
+ params = 'level_s:prioritas';
+ break;
+ case 'search':
+ params = `name_s:"${req.query.q.toLowerCase()}"`;
+ sort = '';
+ rows = 1;
+ break;
+ default:
+ params = `name_s:${req.query.params}`.toLowerCase();
+ }
+ }
+ if (req.query.rows) rows = req.query.rows;
+
+ const url = `${SOLR_HOST}/solr/brands/select?q=${params}&q.op=OR&indent=true&rows=${rows}&${sort}`;
+ const brands = await axios(url);
+ const dataBrands = responseMap(brands.data.response.docs);
+
+ res.status(200).json(dataBrands);
+ } catch (error) {
+ console.error('Error fetching data from Solr:', error);
+ res.status(500).json({ error: 'Internal Server Error' });
+ }
+}
+
+const responseMap = (brands) => {
+ return brands.map((brand) => {
+ return {
+ id: brand.id,
+ name: brand.display_name_s,
+ logo: brand.image_s || '',
+ sequence: brand.sequence_i || '',
+ };
+ });
+};
diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js
index ace281f7..65927bbc 100644
--- a/src/pages/api/shop/search.js
+++ b/src/pages/api/shop/search.js
@@ -19,6 +19,10 @@ export default async function handler(req, res) {
source = '',
} = req.query;
+
+
+ console.log('fq new', fq);
+
let { stock = '' } = req.query;
let paramOrderBy = '';
@@ -73,8 +77,9 @@ export default async function handler(req, res) {
const formattedQuery = `(${newQ
.split(' ')
- .map((term) => `${term}*`)
+ .map((term) => (term.length < 2 ? term : `${term}*`)) // Tambahkan '*' hanya jika panjang kata >= 2
.join(' ')})`;
+
const mm =
checkQ.length > 2
? checkQ.length > 5
@@ -88,11 +93,24 @@ export default async function handler(req, res) {
'price_tier1_v2_f:[1 TO *]',
];
- if (fq && source != 'similar') {
- filterQueries.push(fq);
+
+ 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 = [
'facet.field=manufacture_name_s',
@@ -101,13 +119,7 @@ export default async function handler(req, res) {
'indent=true',
`facet.query=${escapeSolrQuery(q)}`,
`q.op=OR`,
- `q=${
- source == 'similar' || checkQ.length < 3
- ? checkQ.length < 2
- ? newQ
- : newQ + '*'
- : formattedQuery
- }`,
+ `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)}`,
@@ -152,7 +164,7 @@ export default async function handler(req, res) {
if (stock) parameter.push(`fq=stock_total_f:{1 TO *}`);
// Single fq in url params
- // if (typeof fq === 'string') parameter.push(`fq=${encodeURIComponent(fq)}`);
+ if (typeof fq === 'string') parameter.push(`fq=${encodeURIComponent(fq)}`);
// Multi fq in url params
if (Array.isArray(fq))
parameter = parameter.concat(