1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
import { productMappingSolr, promoMappingSolr } from '@/utils/solrMapping';
import axios from 'axios';
import camelcaseObjectDeep from 'camelcase-object-deep';
export default async function handler(req, res) {
const {
q = '*',
page = 1,
brand = '',
category = '',
priceFrom = 0,
priceTo = 0,
orderBy = 'if(exists(sequence_i),0,1) asc, sequence_i asc,',
operation = 'AND',
fq = '',
limit = 30,
} = req.query;
let { stock = '' } = req.query;
let paramOrderBy = 'if(exists(sequence_i),0,1) asc, sequence_i asc,';
switch (orderBy) {
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 = checkQ.length > 1 ? escapeSolrQuery(q) + '*' : escapeSolrQuery(q);
let offset = (page - 1) * limit;
let parameter = [
'facet.field=manufacture_name_s',
'facet.field=category_name',
'facet=true',
'indent=true',
`facet.limit=-1`,
// `facet.query=${escapeSolrQuery(q)}`,
`q.op=${operation}`,
`q=${q}`,
// 'qf=name_s',
`start=${parseInt(offset)}`,
`rows=${limit}`,
`sort=${paramOrderBy}`,
`fq=product_ids:[* TO *]`,
`fq=active_b:true`,
];
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)
parameter.push(
`fq=${brand
.split(',')
.map(
(manufacturer) =>
`manufacture_name_s:"${encodeURIComponent(manufacturer)}"`
)
.join(' OR ')}`
);
if (category)
parameter.push(
`fq=${category
.split(',')
.map((cat) => `category_name:"${encodeURIComponent(cat)}"`)
.join(' OR ')}`
);
// 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=${fq}`);
// Multi fq in url params
if (Array.isArray(fq))
parameter = parameter.concat(fq.map((val) => `fq=${val}`));
let result = await axios(
process.env.SOLR_HOST + '/solr/promotion_program_lines/select?' + parameter.join('&')
);
try {
result.data.response.products = promoMappingSolr(
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;
// result.data = camelcaseObjectDeep(result.data);
result.data = 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
})
}*/
|