summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/pages/api/shop/product-detail.js147
1 files changed, 147 insertions, 0 deletions
diff --git a/src/pages/api/shop/product-detail.js b/src/pages/api/shop/product-detail.js
new file mode 100644
index 00000000..9020103b
--- /dev/null
+++ b/src/pages/api/shop/product-detail.js
@@ -0,0 +1,147 @@
+import axios from 'axios'
+
+export default async function handler(req, res) {
+ try {
+ let productTemplate = await axios(
+ process.env.SOLR_HOST + `/solr/product/select?q=id:${req.query.id}&q.op=OR&indent=true`
+ )
+ let productVariants = await axios(
+ process.env.SOLR_HOST +
+ `/solr/variants/select?q=template_id_i:${req.query.id}&q.op=OR&indent=true`
+ )
+ let { auth } = req.cookies
+ if (auth) auth = JSON.parse(auth)
+ let result = productResponseMap(productTemplate.data.response.docs, auth?.pricelist || false)
+ result[0].variants = productVariantsResponseMap(
+ productTemplate.data.response.docs[0],
+ productVariants.data.response.docs,
+ auth?.pricelist || false
+ )
+ res.status(200).json(result)
+ } catch (error) {
+ console.error('Error fetching data from Solr:', error)
+ res.status(500).json({ error: 'Internal Server Error' })
+ }
+}
+
+const productResponseMap = (products, pricelist) => {
+ return products.map((product) => {
+ let price = product.price_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
+ }
+
+ 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 || '',
+ description: product.description_t || '',
+ displayName: product.display_name_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,
+ remainingTime: flashsaleTime(product?.flashsale_end_date_s)?.remainingTime,
+ 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 || '',
+ imagePromotion1: product.image_promotion_1_s || '',
+ imagePromotion2: product.image_promotion_2_s || ''
+ }
+ }
+
+ productMapped.categories = [
+ {
+ id: product.category_id_i || '',
+ name: product.category_name_s || ''
+ }
+ ]
+ return productMapped
+ })
+}
+const productVariantsResponseMap = (parent, products, pricelist) => {
+ return products.map((product) => {
+ let price = product.price_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
+ }
+
+ 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 = {
+ attributes: product.attributes || [],
+ id: product.product_id_i || '',
+ image: product.image_s || '',
+ code: product.default_code_s || '',
+ isFlashsale: flashsaleTime(product?.flashsale_end_date_s)?.isFlashSale,
+ isFlashsale: flashsaleTime(product?.flashsale_end_date_s)?.isFlashSale,
+ name: product.display_name_s || '',
+ price: { price, priceDiscount, discountPercentage },
+ variantTotal: product.variant_total_i || 0,
+ stockTotal: product.stock_total_f || 0,
+ weight: product.weight_f || 0,
+ manufacture: {},
+ parent: {}
+ }
+
+ if (product.manufacture_id_i && product.manufacture_name_s) {
+ productMapped.manufacture = {
+ id: product.manufacture_id_i || '',
+ name: product.manufacture_name_s || ''
+ }
+ }
+ productMapped.parent = {
+ id: parent.product_id_i || '',
+ image: parent.image_s || '',
+ name: parent.name_s || ''
+ }
+ return productMapped
+ })
+}
+
+const flashsaleTime = (endDate) => {
+ const flashsaleEndDate = new Date(endDate)
+ const currentTime = new Date()
+
+ const timeDifferenceInMillis = flashsaleEndDate - currentTime
+ const timeDifferenceInSeconds = timeDifferenceInMillis / 1000
+
+ return { remainingTime: timeDifferenceInSeconds, isFlashSale: flashsaleEndDate > currentTime }
+}