diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2023-09-15 02:04:32 +0000 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2023-09-15 02:04:32 +0000 |
| commit | 6b4a109615799bd6994499c62221b635c9539898 (patch) | |
| tree | b78dbb6789e1449fad74cdc7321ea6bfa091a11a /src | |
| parent | 8bb683d2c695f0df292f6a7965efcbf3abd72a3d (diff) | |
| parent | ce8a2e4ed9534d6fd5c1d8734aa06f51426101cd (diff) | |
Merged in CR/migrasi_product_detail_to_solr (pull request #67)
CR/migrasi product detail to solr
Diffstat (limited to 'src')
| -rw-r--r-- | src/pages/api/shop/product-detail.js | 147 | ||||
| -rw-r--r-- | src/pages/shop/product/[slug].jsx | 10 |
2 files changed, 155 insertions, 2 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 } +} diff --git a/src/pages/shop/product/[slug].jsx b/src/pages/shop/product/[slug].jsx index 5d706dec..af20413f 100644 --- a/src/pages/shop/product/[slug].jsx +++ b/src/pages/shop/product/[slug].jsx @@ -6,6 +6,7 @@ import PageNotFound from '@/pages/404' import dynamic from 'next/dynamic' import { useRouter } from 'next/router' import cookie from 'cookie' +import axios from 'axios' const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout')) const Product = dynamic(() => import('@/lib/product/components/Product/Product')) @@ -17,7 +18,12 @@ export async function getServerSideProps(context) { const auth = cookieObj.auth ? JSON.parse(cookieObj.auth) : {} const authToken = auth?.token || '' - let product = await productApi({ id: getIdFromSlug(slug), headers: { Token: authToken } }) + let response = await axios( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=`+getIdFromSlug(slug) + ) + let product = response.data + // let productSolr = await productApi({ id: getIdFromSlug(slug), headers: { Token: authToken } }) + // let productSolr = null if (product?.length == 1) { product = product[0] const regexHtmlTags = /(<([^>]+)>)/gi @@ -35,7 +41,7 @@ export async function getServerSideProps(context) { } } -export default function ProductDetail({ product }) { +export default function ProductDetail({ product}) { const router = useRouter() if (!product) return <PageNotFound /> |
