From 0fc1ee076d3a59b406f79716c343f15301c1066a Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Wed, 7 Aug 2024 17:06:58 +0700 Subject: update all promotion card flashSale --- src/pages/api/shop/search.js | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 29471f7b..8dc72559 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -3,6 +3,7 @@ import axios from 'axios'; import camelcaseObjectDeep from 'camelcase-object-deep'; export default async function handler(req, res) { + const { q = '*', page = 1, @@ -20,6 +21,9 @@ export default async function handler(req, res) { let paramOrderBy = ''; switch (orderBy) { + case 'flashsale-discount-desc': + paramOrderBy += 'flashsale_discount_f DESC'; + break; case 'price-asc': paramOrderBy += 'price_tier1_v2_f ASC'; break; @@ -59,6 +63,7 @@ export default async function handler(req, res) { `fq=-publish_b:false, product_rating_f:[13 TO *], discount_tier1_v2_f:[1 TO *]`, ]; + if (priceFrom > 0 || priceTo > 0) { parameter.push( `fq=price_tier1_v2_f:[${priceFrom == '' ? '*' : priceFrom} TO ${ @@ -99,6 +104,7 @@ export default async function handler(req, res) { let result = await axios( process.env.SOLR_HOST + '/solr/product/select?' + parameter.join('&') ); + console.log("result",result) try { result.data.response.products = productMappingSolr( result.data.response.docs, -- cgit v1.2.3 From f2e11c985f467cfe28b6f4c8c9ae9c7fabf85d94 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Wed, 7 Aug 2024 17:07:59 +0700 Subject: delete console log --- src/pages/api/shop/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 8dc72559..87d7c580 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -104,7 +104,7 @@ export default async function handler(req, res) { let result = await axios( process.env.SOLR_HOST + '/solr/product/select?' + parameter.join('&') ); - console.log("result",result) + try { result.data.response.products = productMappingSolr( result.data.response.docs, -- cgit v1.2.3 From f0d03727f69a84daff484ea9a7944d9c29daa5c3 Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Thu, 24 Oct 2024 15:02:04 +0700 Subject: done page category brand --- src/pages/api/shop/search.js | 9 ++++++--- src/pages/api/shop/url-category_brand.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 src/pages/api/shop/url-category_brand.js (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 49a5fe69..bc1c014b 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -88,7 +88,10 @@ export default async function handler(req, res) { 'price_tier1_v2_f:[1 TO *]', ]; - const fq_ = filterQueries.join('AND '); + if(fq){ + filterQueries.push(fq); + } + const fq_ = filterQueries.join(' AND '); let offset = (page - 1) * limit; let parameter = [ @@ -106,7 +109,7 @@ export default async function handler(req, res) { : formattedQuery }`, `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', + '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}`, @@ -149,7 +152,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( diff --git a/src/pages/api/shop/url-category_brand.js b/src/pages/api/shop/url-category_brand.js new file mode 100644 index 00000000..cfcc7d73 --- /dev/null +++ b/src/pages/api/shop/url-category_brand.js @@ -0,0 +1,17 @@ +import axios from 'axios'; + +export default async function handler(req, res) { + const { url = '' } = req.query; + + const params = [`q.op=AND`, `q=url_s:"${url}"`, `indent=true`]; + try { + let result = await axios( + process.env.SOLR_HOST + + `/solr/url_category_brand/select?` + + params.join('&') + ); + res.status(200).json(result.data); + } catch (error) { + res.status(500).json({ error: 'Internal Server Error' }); + } +} -- cgit v1.2.3 From bb2fc00e1f399be919d7b777dd0ae17edd6b2a2e Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Fri, 25 Oct 2024 10:04:34 +0700 Subject: sitemap categories brand --- src/pages/api/shop/url-category_brand.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/url-category_brand.js b/src/pages/api/shop/url-category_brand.js index cfcc7d73..160aa166 100644 --- a/src/pages/api/shop/url-category_brand.js +++ b/src/pages/api/shop/url-category_brand.js @@ -1,9 +1,12 @@ import axios from 'axios'; export default async function handler(req, res) { - const { url = '' } = req.query; + const { url = '', page = 1, limit = 30 } = req.query; + + let offset = (page - 1) * limit; - const params = [`q.op=AND`, `q=url_s:"${url}"`, `indent=true`]; + const params = [`q.op=AND`, `q=${url ? `"${url}"` : '*'}`, `indent=true`, `rows=${limit}`, `start=${offset}`]; + try { let result = await axios( process.env.SOLR_HOST + -- cgit v1.2.3 From 2d30aea4e9d225298594ee92f474a38225b91206 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Mon, 28 Oct 2024 12:07:09 +0700 Subject: update informasi product varian --- src/pages/api/shop/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index bc1c014b..ace281f7 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -88,7 +88,7 @@ export default async function handler(req, res) { 'price_tier1_v2_f:[1 TO *]', ]; - if(fq){ + if (fq && source != 'similar') { filterQueries.push(fq); } const fq_ = filterQueries.join(' AND '); -- cgit v1.2.3 From 4a5391def4c5d2e8991643287d40e3c3b53980be Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Tue, 5 Nov 2024 15:49:45 +0700 Subject: add redis --- src/pages/api/banner-section.js | 44 +++++++++++++++++++ src/pages/api/category-management.js | 85 ++++++++++++++++++++++++++++++++++++ src/pages/api/hero-banner.js | 45 +++++++++++++++++++ src/pages/api/shop/brands.js | 42 +++++++++++++----- 4 files changed, 206 insertions(+), 10 deletions(-) create mode 100644 src/pages/api/banner-section.js create mode 100644 src/pages/api/category-management.js create mode 100644 src/pages/api/hero-banner.js (limited to 'src/pages/api') diff --git a/src/pages/api/banner-section.js b/src/pages/api/banner-section.js new file mode 100644 index 00000000..7d7040c0 --- /dev/null +++ b/src/pages/api/banner-section.js @@ -0,0 +1,44 @@ +import odooApi from '@/core/api/odooApi'; +import { createClient } from 'redis'; + +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) { + try { + await connectRedis(); + const cacheKey = 'hero-banner'; + // await client.del(cacheKey); + let cachedData = await client.get(cacheKey); + + if (cachedData) { + const data = JSON.parse(cachedData); + return res.status(200).json({ data }); + } else { + const dataBannerSections = await odooApi( + 'GET', + '/api/v1/banner?type=home-banner' + ); + + // Simpan hasil fetch ke Redis dengan masa kadaluarsa 3 hari (259200 detik) + await client.set( + cacheKey, + JSON.stringify(dataBannerSections), + 'EX', + 259200 + ); + + return res.status(200).json({ data: dataBannerSections }); + } + } catch (error) { + console.error('Error interacting with Redis or fetching data:', error); + return res.status(500).json({ error: 'Internal Server Error' }); + } +} diff --git a/src/pages/api/category-management.js b/src/pages/api/category-management.js new file mode 100644 index 00000000..f05d8644 --- /dev/null +++ b/src/pages/api/category-management.js @@ -0,0 +1,85 @@ +import { createClient } from 'redis'; +// import { fetchCategoryManagementSolr } from '../../lib/home/api/categoryManagementApi'; +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) { + try { + await connectRedis(); + // await client.del('homepage_categoryDynamic'); + + let cachedData; + if (req.method === 'GET') { + cachedData = await client.get('homepage_categoryDynamic'); + + if (!cachedData) { + const items = await fetchCategoryManagementSolr(); + await client.set( + 'homepage_categoryDynamic', + JSON.stringify(items), + 'EX', + 259200 // Expiry 3 hari + ); + cachedData = await client.get('homepage_categoryDynamic'); + } + const data = cachedData ? JSON.parse(cachedData) : null; + res.status(200).json({ data }); + } else { + res.setHeader('Allow', ['GET']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } + } catch (error) { + console.error('Error interacting with Redis:', error); + res.status(500).json({ error: 'Error interacting with Redis' }); + } +} + +const fetchCategoryManagementSolr = async () => { + let sort = 'sort=sequence_i asc'; + try { + const response = await fetch( + `http://34.101.189.218:8983/solr/category_management/query?q=*:*&q.op=OR&indent=true&${sort}&&rows=20` + ); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); + const promotions = await map(data.response.docs); + return promotions; + } catch (error) { + console.error('Error fetching promotion data:', error); + return []; + } +}; +const map = async (promotions) => { + return promotions.map((promotion) => { + let parsedCategories = promotion.categories.map((category) => { + // Parse string JSON utama + let parsedCategory = JSON.parse(category); + + // Parse setiap elemen di child_frontend_id_i jika ada + if (parsedCategory.child_frontend_id_i) { + parsedCategory.child_frontend_id_i = + parsedCategory.child_frontend_id_i.map((child) => JSON.parse(child)); + } + + return parsedCategory; + }); + let productMapped = { + id: promotion.id, + name: promotion.name_s, + image: promotion.image_s, + sequence: promotion.sequence_i, + numFound: promotion.numFound_i, + categories: parsedCategories, + category_id: promotion.category_id_i, + }; + return productMapped; + }); +}; diff --git a/src/pages/api/hero-banner.js b/src/pages/api/hero-banner.js new file mode 100644 index 00000000..b57f86e0 --- /dev/null +++ b/src/pages/api/hero-banner.js @@ -0,0 +1,45 @@ +import odooApi from '@/core/api/odooApi'; +import { createClient } from 'redis'; + +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) { + const { type } = req.query; + try { + await connectRedis(); + const cacheKey = `homepage_bannerSection_${type}`; + // await client.del(cacheKey); + let cachedData = await client.get(cacheKey); + + if (cachedData) { + const data = JSON.parse(cachedData); + return res.status(200).json({ data }); + } else { + const dataBannerSections = await odooApi( + 'GET', + `/api/v1/banner?type=${type}` + ); + + // Simpan hasil fetch ke Redis dengan masa kadaluarsa 3 hari (259200 detik) + await client.set( + cacheKey, + JSON.stringify(dataBannerSections), + 'EX', + 259200 + ); + + return res.status(200).json({ data: dataBannerSections }); + } + } catch (error) { + console.error('Error interacting with Redis or fetching data:', error); + return res.status(500).json({ error: 'Internal Server Error' }); + } +} diff --git a/src/pages/api/shop/brands.js b/src/pages/api/shop/brands.js index 9c2824b3..380b3369 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,23 @@ 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 cacheKey = `brands_home`; + let cachedData = await client.get(cacheKey); + + if (cachedData) { + console.log('Retrieving data from Redis cache'); + return res.status(200).json(JSON.parse(cachedData)); + } + + // Fetch data from Solr 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); + + // Store fetched data in Redis with 3-day expiration + await client.set(cacheKey, JSON.stringify(dataBrands), 'EX', 259200); res.status(200).json(dataBrands); } catch (error) { @@ -39,13 +63,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; }); }; -- cgit v1.2.3 From ccc224e8a6261e40e2ea1db08e21a49d280fd1c4 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Thu, 7 Nov 2024 15:23:42 +0700 Subject: update fetch data logic --- src/pages/api/shop/variant-detail.js | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/variant-detail.js b/src/pages/api/shop/variant-detail.js index 08ce75b8..af3525b3 100644 --- a/src/pages/api/shop/variant-detail.js +++ b/src/pages/api/shop/variant-detail.js @@ -1,21 +1,28 @@ -import { productMappingSolr, variantsMappingSolr } from '@/utils/solrMapping' -import axios from 'axios' +import { productMappingSolr, variantsMappingSolr } from '@/utils/solrMapping'; +import axios from 'axios'; export default async function handler(req, res) { try { let productVariants = await axios( process.env.SOLR_HOST + `/solr/variants/select?q=id:${req.query.id}&q.op=OR&indent=true` - ) - let auth = req.query.auth === 'false' ? JSON.parse(req.query.auth) : req.query.auth + ); + let template_id = productVariants.data.response.docs[0].template_id_i; + let auth = + req.query.auth === 'false' ? JSON.parse(req.query.auth) : req.query.auth; let productTemplate = await axios( - process.env.SOLR_HOST + `/solr/product/select?q=id:${req.query.id}&q.op=OR&indent=true` - ) - let result = variantsMappingSolr(productTemplate.data.response.docs, productVariants.data.response.docs, auth || false) - - res.status(200).json(result) + process.env.SOLR_HOST + + `/solr/product/select?q=id:${template_id}&q.op=OR&indent=true` + ); + let result = variantsMappingSolr( + productTemplate.data.response.docs, + productVariants.data.response.docs, + auth || false + ); + + res.status(200).json(result); } catch (error) { - console.error('Error fetching data from Solr:', error) - res.status(500).json({ error: 'Internal Server Error' }) + console.error('Error fetching data from Solr:', error); + res.status(500).json({ error: 'Internal Server Error' }); } -} \ No newline at end of file +} -- cgit v1.2.3 From 0d3c0cf6a00ef81bfdb944490e48f16af41fc029 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Wed, 13 Nov 2024 10:08:16 +0700 Subject: add radis --- src/pages/api/hero-banner.js | 4 ++-- src/pages/api/page-content.js | 44 +++++++++++++++++++++++++++++++++++++++++++ src/pages/api/shop/brands.js | 3 --- 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 src/pages/api/page-content.js (limited to 'src/pages/api') diff --git a/src/pages/api/hero-banner.js b/src/pages/api/hero-banner.js index b57f86e0..7a348cfa 100644 --- a/src/pages/api/hero-banner.js +++ b/src/pages/api/hero-banner.js @@ -35,8 +35,8 @@ export default async function handler(req, res) { 'EX', 259200 ); - - return res.status(200).json({ data: dataBannerSections }); + cachedData = await client.get(cacheKey); + return res.status(200).json({ data: cachedData }); } } catch (error) { console.error('Error interacting with Redis or fetching data:', error); diff --git a/src/pages/api/page-content.js b/src/pages/api/page-content.js new file mode 100644 index 00000000..3cb8a237 --- /dev/null +++ b/src/pages/api/page-content.js @@ -0,0 +1,44 @@ +import { createClient } from 'redis'; +import { getPageContent } from '~/services/pageContent'; +// import { fetchCategoryManagementSolr } from '../../lib/home/api/categoryManagementApi'; +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) { + const { path } = req.query; + try { + await connectRedis(); + // await client.del('onbording-popup'); + + let cachedData; + if (req.method === 'GET') { + cachedData = await client.get(`page-content:${path}`); + + if (!cachedData) { + const items = await getPageContent({ path }); + console.log('items', items); + await client.set( + `page-content:${path}`, + JSON.stringify(items), + 'EX', + 604800 // Expiry 1 minggu + ); + cachedData = await client.get(`page-content:${path}`); + } + const data = cachedData ? JSON.parse(cachedData) : null; + res.status(200).json({ data }); + } else { + res.setHeader('Allow', ['GET']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } + } catch (error) { + console.error('Error interacting with Redis:', error); + res.status(500).json({ error: 'Error interacting with Redis' }); + } +} diff --git a/src/pages/api/shop/brands.js b/src/pages/api/shop/brands.js index 380b3369..219f2cb0 100644 --- a/src/pages/api/shop/brands.js +++ b/src/pages/api/shop/brands.js @@ -42,16 +42,13 @@ export default async function handler(req, res) { let cachedData = await client.get(cacheKey); if (cachedData) { - console.log('Retrieving data from Redis cache'); return res.status(200).json(JSON.parse(cachedData)); } - // Fetch data from Solr 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); - // Store fetched data in Redis with 3-day expiration await client.set(cacheKey, JSON.stringify(dataBrands), 'EX', 259200); res.status(200).json(dataBrands); -- cgit v1.2.3 From 90eb9e8ea858be3b38a6481050a029a32980f417 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Thu, 14 Nov 2024 11:57:01 +0700 Subject: update bug no flash sale --- src/pages/api/shop/search.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index ace281f7..1f0ff504 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -89,7 +89,8 @@ export default async function handler(req, res) { ]; if (fq && source != 'similar') { - filterQueries.push(fq); + // filterQueries.push(fq); + fq.push(...filterQueries); } const fq_ = filterQueries.join(' AND '); -- cgit v1.2.3 From c69b71c16ff7cc7a347394710d069ef711db08bb Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Fri, 15 Nov 2024 14:55:36 +0700 Subject: cr logic generate keywords --- src/pages/api/shop/search.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 1f0ff504..a789b469 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -73,8 +73,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 @@ -94,6 +95,17 @@ export default async function handler(req, res) { } 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', @@ -102,13 +114,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)}`, -- cgit v1.2.3 From d50e637383087cf03d2ad94ad6d076f30f3490c5 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 15 Nov 2024 15:36:38 +0700 Subject: delete cached --- src/pages/api/shop/brands.js | 9 --------- 1 file changed, 9 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/brands.js b/src/pages/api/shop/brands.js index 219f2cb0..d56e4b13 100644 --- a/src/pages/api/shop/brands.js +++ b/src/pages/api/shop/brands.js @@ -38,19 +38,10 @@ export default async function handler(req, res) { } if (req.query.rows) rows = req.query.rows; - const cacheKey = `brands_home`; - let cachedData = await client.get(cacheKey); - - if (cachedData) { - return res.status(200).json(JSON.parse(cachedData)); - } - 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); - await client.set(cacheKey, JSON.stringify(dataBrands), 'EX', 259200); - res.status(200).json(dataBrands); } catch (error) { console.error('Error fetching data from Solr:', error); -- cgit v1.2.3 From 48441ffd9a92827ffedf5e0b0f4e490132e02c82 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 15 Nov 2024 15:59:52 +0700 Subject: update radis error code preferredBrand --- src/pages/api/shop/preferredBrand.js | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/pages/api/shop/preferredBrand.js (limited to 'src/pages/api') diff --git a/src/pages/api/shop/preferredBrand.js b/src/pages/api/shop/preferredBrand.js new file mode 100644 index 00000000..9480c63b --- /dev/null +++ b/src/pages/api/shop/preferredBrand.js @@ -0,0 +1,46 @@ +import odooApi from '@/core/api/odooApi'; +import { createClient } from 'redis'; + +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) { + try { + await connectRedis(); + // await client.del('preferredBrand'); + + let cachedData; + if (req.method === 'GET') { + cachedData = await client.get('preferredBrand'); + + if (!cachedData) { + const items = await odooApi( + 'GET', + '/api/v1/manufacture?level=prioritas' + ); + await client.set( + 'preferredBrand', + JSON.stringify(items), + 'EX', + 259200 // Expiry 3 hari + ); + cachedData = await client.get('preferredBrand'); + } + const data = cachedData ? JSON.parse(cachedData) : null; + res.status(200).json({ data }); + } else { + res.setHeader('Allow', ['GET']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } + } catch (error) { + console.error('Error interacting with Redis:', error); + res.status(500).json({ error: 'Error interacting with Redis' }); + } +} -- cgit v1.2.3 From 607ddd5050b5ee900606984b60e74d094fbe89f9 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 15 Nov 2024 16:17:13 +0700 Subject: update fix code --- src/pages/api/shop/preferredBrand.js | 69 ++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 27 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/preferredBrand.js b/src/pages/api/shop/preferredBrand.js index 9480c63b..4cb35c84 100644 --- a/src/pages/api/shop/preferredBrand.js +++ b/src/pages/api/shop/preferredBrand.js @@ -1,6 +1,7 @@ -import odooApi from '@/core/api/odooApi'; +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)); @@ -12,35 +13,49 @@ const connectRedis = async () => { }; export default async function handler(req, res) { + await connectRedis(); + try { - await connectRedis(); - // await client.del('preferredBrand'); - - let cachedData; - if (req.method === 'GET') { - cachedData = await client.get('preferredBrand'); - - if (!cachedData) { - const items = await odooApi( - 'GET', - '/api/v1/manufacture?level=prioritas' - ); - await client.set( - 'preferredBrand', - JSON.stringify(items), - 'EX', - 259200 // Expiry 3 hari - ); - cachedData = await client.get('preferredBrand'); + 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(); } - const data = cachedData ? JSON.parse(cachedData) : null; - res.status(200).json({ data }); - } else { - res.setHeader('Allow', ['GET']); - res.status(405).end(`Method ${req.method} Not Allowed`); } + 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 interacting with Redis:', error); - res.status(500).json({ error: 'Error interacting with Redis' }); + 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 || '', + }; + }); +}; -- cgit v1.2.3 From c3ff9d2b35efd260bf7bfa7cc924ed05b8f9418f Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Mon, 18 Nov 2024 08:29:50 +0700 Subject: fix bug --- src/pages/api/shop/search.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 1f0ff504..4b59f741 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -89,8 +89,8 @@ export default async function handler(req, res) { ]; if (fq && source != 'similar') { - // filterQueries.push(fq); - fq.push(...filterQueries); + filterQueries.push(fq); + // fq.push(...filterQueries); } const fq_ = filterQueries.join(' AND '); -- cgit v1.2.3 From 986e2e47037c6d99b184ec0edc1fbf591f0582a8 Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Mon, 18 Nov 2024 10:51:34 +0700 Subject: bug fix search category dinanmis --- src/pages/api/shop/search.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 4fc2ab2b..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 = ''; @@ -89,9 +93,10 @@ export default async function handler(req, res) { 'price_tier1_v2_f:[1 TO *]', ]; - if (fq && source != 'similar') { - filterQueries.push(fq); - // fq.push(...filterQueries); + + if (fq && source != 'similar' && typeof fq != 'string') { + // filterQueries.push(fq); + fq.push(...filterQueries); } const fq_ = filterQueries.join(' AND '); @@ -159,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( -- cgit v1.2.3 From 4d1dd6ad5b22ffb02e4347cf3159f10988fd7c03 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Tue, 19 Nov 2024 10:49:07 +0700 Subject: redis v2 --- src/pages/api/flashsale-header.js | 40 ++++++++++++++++++++++++++++++++++ src/pages/api/search-flashsale.js | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/pages/api/flashsale-header.js create mode 100644 src/pages/api/search-flashsale.js (limited to 'src/pages/api') diff --git a/src/pages/api/flashsale-header.js b/src/pages/api/flashsale-header.js new file mode 100644 index 00000000..31f8efdd --- /dev/null +++ b/src/pages/api/flashsale-header.js @@ -0,0 +1,40 @@ +import odooApi from '@/core/api/odooApi'; +import { createClient } from 'redis'; + +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) { + try { + await connectRedis(); + const cacheKey = `flashsale_header`; + // await client.del(cacheKey); + let cachedData = await client.get(cacheKey); + + if (cachedData) { + const data = JSON.parse(cachedData); + return res.status(200).json({ data }); + } else { + const flashSale = await odooApi('GET', `/api/v1/flashsale/header`); + + await client.set( + cacheKey, + JSON.stringify(flashSale), + 'EX', + flashSale.duration + ); + cachedData = await client.get(cacheKey); + return res.status(200).json({ data: cachedData }); + } + } catch (error) { + console.error('Error interacting with Redis or fetching data:', error); + return res.status(500).json({ error: 'Internal Server Error' }); + } +} diff --git a/src/pages/api/search-flashsale.js b/src/pages/api/search-flashsale.js new file mode 100644 index 00000000..d9e56c83 --- /dev/null +++ b/src/pages/api/search-flashsale.js @@ -0,0 +1,45 @@ +import odooApi from '@/core/api/odooApi'; +import { createClient } from 'redis'; +import _ from 'lodash-contrib'; +import axios from 'axios'; + +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) { + const { query, operation, duration } = req.query; + try { + await connectRedis(); + const cacheKey = `flashsale_product`; + // await client.del(cacheKey); + let cachedData = await client.get(cacheKey); + + if (cachedData) { + const data = JSON.parse(cachedData); + return res.status(200).json({ data }); + } else { + const dataProductSearch = await axios( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?${query}&operation=${operation}]` + ); + + await client.set( + cacheKey, + JSON.stringify(dataProductSearch.data), + 'EX', + duration + ); + cachedData = await client.get(cacheKey); + return res.status(200).json({ data: cachedData }); + } + } catch (error) { + console.error('Error interacting with Redis or fetching data:', error); + return res.status(500).json({ error: 'Internal Server Error' }); + } +} -- cgit v1.2.3 From 9f10b502da7e1d102ddb0b16175b828834ff6f44 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Tue, 19 Nov 2024 10:50:07 +0700 Subject: delete console log --- src/pages/api/page-content.js | 1 - src/pages/api/shop/search.js | 9 +-------- 2 files changed, 1 insertion(+), 9 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/page-content.js b/src/pages/api/page-content.js index 3cb8a237..a6514505 100644 --- a/src/pages/api/page-content.js +++ b/src/pages/api/page-content.js @@ -22,7 +22,6 @@ export default async function handler(req, res) { if (!cachedData) { const items = await getPageContent({ path }); - console.log('items', items); await client.set( `page-content:${path}`, JSON.stringify(items), diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index 65927bbc..63ec7ca0 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -19,12 +19,7 @@ export default async function handler(req, res) { source = '', } = req.query; - - - console.log('fq new', fq); - let { stock = '' } = req.query; - let paramOrderBy = ''; switch (orderBy) { case 'flashsale-discount-desc': @@ -93,7 +88,6 @@ export default async function handler(req, res) { 'price_tier1_v2_f:[1 TO *]', ]; - if (fq && source != 'similar' && typeof fq != 'string') { // filterQueries.push(fq); fq.push(...filterQueries); @@ -103,7 +97,7 @@ export default async function handler(req, res) { let keywords = newQ; if (source === 'similar' || checkQ.length < 3) { if (checkQ.length < 2 || checkQ[1].length < 2) { - keywords = newQ ; + keywords = newQ; } else { keywords = newQ + '*'; } @@ -170,7 +164,6 @@ export default async function handler(req, res) { parameter = parameter.concat( fq.map((val) => `fq=${encodeURIComponent(val)}`) ); - let result = await axios( process.env.SOLR_HOST + '/solr/product/select?' + parameter.join('&') ); -- cgit v1.2.3 From cbd9bc991a572a80a840a8105640a1b0f1a8bdd4 Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Wed, 20 Nov 2024 10:50:39 +0700 Subject: bugs fix filter variant price != 0 --- src/pages/api/shop/product-detail.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/pages/api') diff --git a/src/pages/api/shop/product-detail.js b/src/pages/api/shop/product-detail.js index 247f2a04..faa96028 100644 --- a/src/pages/api/shop/product-detail.js +++ b/src/pages/api/shop/product-detail.js @@ -8,7 +8,7 @@ export default async function handler(req, res) { ) let productVariants = await axios( process.env.SOLR_HOST + - `/solr/variants/select?q=template_id_i:${req.query.id}&q.op=OR&indent=true&rows=100&fq=-publish_b:false` + `/solr/variants/select?q=template_id_i:${req.query.id}&q.op=OR&indent=true&rows=100&fq=-publish_b:false AND price_tier1_v2_f:[1 TO *]` ) let auth = req.query.auth === 'false' ? JSON.parse(req.query.auth) : req.query.auth let result = productMappingSolr(productTemplate.data.response.docs, auth || false) -- cgit v1.2.3