summaryrefslogtreecommitdiff
path: root/src2/pages/api/shop/search.js
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2023-03-01 09:18:52 +0000
committerIT Fixcomart <it@fixcomart.co.id>2023-03-01 09:18:52 +0000
commita7abbf4ddc70068620e9f44b74dc162ce2e16ee2 (patch)
tree74f66253717515d364ce74bd8275015c1f829cbc /src2/pages/api/shop/search.js
parent90e1edab9b6a8ccc09a49fed3addbec2cbc4e4c3 (diff)
parenta1b9b647a6c4bda1f5db63879639d44543f9557e (diff)
Merged in refactor (pull request #1)
Refactor
Diffstat (limited to 'src2/pages/api/shop/search.js')
-rw-r--r--src2/pages/api/shop/search.js96
1 files changed, 96 insertions, 0 deletions
diff --git a/src2/pages/api/shop/search.js b/src2/pages/api/shop/search.js
new file mode 100644
index 00000000..ad986c86
--- /dev/null
+++ b/src2/pages/api/shop/search.js
@@ -0,0 +1,96 @@
+import axios from "axios";
+
+const productResponseMap = (products) => {
+ return products.map((product) => {
+ let productMapped = {
+ id: product.product_id ? product.product_id[0] : '',
+ image: product.image ? product.image[0] : '',
+ code: product.default_code ? product.default_code[0] : '',
+ name: product.product_name ? product.product_name[0] : '',
+ lowest_price: {
+ price: product.price ? product.price[0] : 0,
+ price_discount: product.price_discount ? product.price_discount[0] : 0,
+ discount_percentage: product.discount ? product.discount[0] : 0,
+ },
+ variant_total: product.variant_total ? product.variant_total[0] : 0,
+ stock_total: product.stock_total ? product.stock_total[0] : 0,
+ weight: product.weight ? product.weight[0] : 0,
+ manufacture: {},
+ categories: [],
+ };
+
+ if (product.manufacture_id && product.brand) {
+ productMapped.manufacture = {
+ id: product.manufacture_id ? product.manufacture_id[0] : '',
+ name: product.brand ? product.brand[0] : '',
+ };
+ }
+
+ productMapped.categories = [
+ {
+ id: product.category_id ? product.category_id[0] : '',
+ name: product.category_name ? product.category_name[0] : '',
+ }
+ ];
+
+ return productMapped;
+ });
+}
+
+export default async function handler(req, res) {
+ const {
+ q,
+ page = 1,
+ brand = '',
+ category = '',
+ price_from = 0,
+ price_to = 0,
+ order_by = ''
+ } = req.query;
+
+ let paramOrderBy = '';
+ switch (order_by) {
+ case 'price-asc':
+ paramOrderBy = ', price_discount ASC';
+ break;
+ case 'price-desc':
+ paramOrderBy = ', price_discount DESC';
+ break;
+ case 'popular':
+ paramOrderBy = ', search_rank DESC';
+ break;
+ case 'stock':
+ paramOrderBy = ', stock_total DESC';
+ break;
+ }
+
+ let limit = 30;
+ let offset = (page - 1) * limit;
+ let parameter = [
+ `facet.query=${q}`,
+ 'facet=true',
+ 'indent=true',
+ 'q.op=AND',
+ `q=${q}`,
+ 'facet.field=brand_str',
+ 'facet.field=category_name_str',
+ `start=${offset}`,
+ `rows=${limit}`,
+ `sort=product_rating DESC ${paramOrderBy}`,
+ `fq=price_discount:[${price_from == '' ? '*' : price_from} TO ${price_to == '' ? '*' : price_to}]`
+ ];
+
+ if (brand) parameter.push(`fq=brand:${brand}`);
+ if (category) parameter.push(`fq=category_name:${category}`);
+
+ let result = await axios(process.env.SOLR_HOST + '/solr/products/select?' + parameter.join('&'));
+ try {
+ result.data.response.products = productResponseMap(result.data.response.docs);
+ 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;
+ res.status(200).json(result.data);
+ } catch (error) {
+ res.status(400).json({ error: error.message });
+ }
+} \ No newline at end of file