diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2023-03-01 09:18:52 +0000 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2023-03-01 09:18:52 +0000 |
| commit | a7abbf4ddc70068620e9f44b74dc162ce2e16ee2 (patch) | |
| tree | 74f66253717515d364ce74bd8275015c1f829cbc /src/pages/api | |
| parent | 90e1edab9b6a8ccc09a49fed3addbec2cbc4e4c3 (diff) | |
| parent | a1b9b647a6c4bda1f5db63879639d44543f9557e (diff) | |
Merged in refactor (pull request #1)
Refactor
Diffstat (limited to 'src/pages/api')
| -rw-r--r-- | src/pages/api/activation-request.js | 28 | ||||
| -rw-r--r-- | src/pages/api/activation.js | 16 | ||||
| -rw-r--r-- | src/pages/api/login.js | 15 | ||||
| -rw-r--r-- | src/pages/api/register.js | 15 | ||||
| -rw-r--r-- | src/pages/api/shop/search.js | 92 | ||||
| -rw-r--r-- | src/pages/api/shop/suggest.js | 18 | ||||
| -rw-r--r-- | src/pages/api/token.js | 10 |
7 files changed, 71 insertions, 123 deletions
diff --git a/src/pages/api/activation-request.js b/src/pages/api/activation-request.js index 3f33875c..7fae2fd1 100644 --- a/src/pages/api/activation-request.js +++ b/src/pages/api/activation-request.js @@ -1,15 +1,11 @@ -import apiOdoo from "@/core/utils/apiOdoo"; -import mailer from "@/core/utils/mailer"; +import odooApi from '@/core/api/odooApi' +import mailer from '@/core/utils/mailer' export default async function handler(req, res) { try { - const { email } = req.body; - let result = await apiOdoo( - 'POST', - '/api/v1/user/activation-request', - {email} - ); - if (result.activation_request) { + const { email } = req.body + let result = await odooApi('POST', '/api/v1/user/activation-request', { email }) + if (result.activationRequest) { mailer.sendMail({ from: 'sales@indoteknik.com', to: result.user.email, @@ -19,13 +15,13 @@ export default async function handler(req, res) { <br> <p>Aktivasi akun anda melalui link berikut: <a href="${process.env.SELF_HOST}/activate?token=${result.token}">Aktivasi Akun</a></p> ` - }); + }) } - delete result.user; - delete result.token; - res.status(200).json(result); + delete result.user + delete result.token + res.status(200).json(result) } catch (error) { - console.log(error); - res.status(400).json({ error: error.message }); + console.log(error) + res.status(400).json({ error: error.message }) } -}
\ No newline at end of file +} diff --git a/src/pages/api/activation.js b/src/pages/api/activation.js deleted file mode 100644 index 8b22af8d..00000000 --- a/src/pages/api/activation.js +++ /dev/null @@ -1,16 +0,0 @@ -import apiOdoo from "@/core/utils/apiOdoo"; - -export default async function handler(req, res) { - try { - const { token } = req.body; - let result = await apiOdoo( - 'POST', - '/api/v1/user/activation', - {token} - ); - res.status(200).json(result); - } catch (error) { - console.log(error); - res.status(400).json({ error: error.message }); - } -}
\ No newline at end of file diff --git a/src/pages/api/login.js b/src/pages/api/login.js deleted file mode 100644 index e02a73cb..00000000 --- a/src/pages/api/login.js +++ /dev/null @@ -1,15 +0,0 @@ -import apiOdoo from "@/core/utils/apiOdoo"; - -export default async function handler(req, res) { - try { - const { email, password } = req.body; - let result = await apiOdoo( - 'POST', - '/api/v1/user/login', - {email, password} - ); - res.status(200).json(result); - } catch (error) { - res.status(400).json({ error: error.message }); - } -}
\ No newline at end of file diff --git a/src/pages/api/register.js b/src/pages/api/register.js deleted file mode 100644 index 7c8d8b39..00000000 --- a/src/pages/api/register.js +++ /dev/null @@ -1,15 +0,0 @@ -import apiOdoo from "@/core/utils/apiOdoo"; - -export default async function handler(req, res) { - try { - const { email, name, password } = req.body; - let result = await apiOdoo( - 'POST', - '/api/v1/user/register', - {email, name, password} - ); - res.status(200).json(result); - } catch (error) { - res.status(400).json({ error: error.message }); - } -}
\ No newline at end of file diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index ad986c86..c1e00d16 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -1,4 +1,5 @@ -import axios from "axios"; +import axios from 'axios' +import camelcaseObjectDeep from 'camelcase-object-deep' const productResponseMap = (products) => { return products.map((product) => { @@ -7,65 +8,65 @@ const productResponseMap = (products) => { image: product.image ? product.image[0] : '', code: product.default_code ? product.default_code[0] : '', name: product.product_name ? product.product_name[0] : '', - lowest_price: { + lowestPrice: { 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, + priceDiscount: product.price_discount ? product.price_discount[0] : 0, + discountPercentage: 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, + variantTotal: product.variant_total ? product.variant_total[0] : 0, + stockTotal: product.stock_total ? product.stock_total[0] : 0, weight: product.weight ? product.weight[0] : 0, manufacture: {}, - categories: [], - }; + categories: [] + } if (product.manufacture_id && product.brand) { productMapped.manufacture = { id: product.manufacture_id ? product.manufacture_id[0] : '', - name: product.brand ? product.brand[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] : '', + name: product.category_name ? product.category_name[0] : '' } - ]; + ] - return productMapped; - }); + return productMapped + }) } export default async function handler(req, res) { - const { - q, + const { + q = '*', page = 1, brand = '', category = '', - price_from = 0, - price_to = 0, - order_by = '' - } = req.query; + priceFrom = 0, + priceTo = 0, + orderBy = '' + } = req.query - let paramOrderBy = ''; - switch (order_by) { + let paramOrderBy = '' + switch (orderBy) { case 'price-asc': - paramOrderBy = ', price_discount ASC'; - break; + paramOrderBy = ', price_discount ASC' + break case 'price-desc': - paramOrderBy = ', price_discount DESC'; - break; + paramOrderBy = ', price_discount DESC' + break case 'popular': - paramOrderBy = ', search_rank DESC'; - break; + paramOrderBy = ', search_rank DESC' + break case 'stock': - paramOrderBy = ', stock_total DESC'; - break; + paramOrderBy = ', stock_total DESC' + break } - let limit = 30; - let offset = (page - 1) * limit; + let limit = 30 + let offset = (page - 1) * limit let parameter = [ `facet.query=${q}`, 'facet=true', @@ -77,20 +78,21 @@ export default async function handler(req, res) { `start=${offset}`, `rows=${limit}`, `sort=product_rating DESC ${paramOrderBy}`, - `fq=price_discount:[${price_from == '' ? '*' : price_from} TO ${price_to == '' ? '*' : price_to}]` - ]; + `fq=price_discount:[${priceFrom == '' ? '*' : priceFrom} TO ${priceTo == '' ? '*' : priceTo}]` + ] - 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('&')); + 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); + 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 + result.data = camelcaseObjectDeep(result.data) + res.status(200).json(result.data) } catch (error) { - res.status(400).json({ error: error.message }); + res.status(400).json({ error: error.message }) } -}
\ No newline at end of file +} diff --git a/src/pages/api/shop/suggest.js b/src/pages/api/shop/suggest.js index 6db1a851..cc0ff2b3 100644 --- a/src/pages/api/shop/suggest.js +++ b/src/pages/api/shop/suggest.js @@ -1,12 +1,18 @@ -import axios from "axios"; +import axios from 'axios' export default async function handler(req, res) { - const { q } = req.query; + const { q = '' } = req.query - let result = await axios(process.env.SOLR_HOST + `/solr/products/suggest?suggest=true&suggest.dictionary=mySuggester&suggest.q=${q}`); + let result = await axios( + process.env.SOLR_HOST + + `/solr/products/suggest?suggest=true&suggest.dictionary=mySuggester&suggest.q=${q}` + ) try { - res.status(200).json(result.data); + res.status(200).json(result.data.suggest.mySuggester[q]) } catch (error) { - res.status(400).json({ error: error.message }); + res.status(400).json({ + numFound: 0, + suggestions: [] + }) } -}
\ No newline at end of file +} diff --git a/src/pages/api/token.js b/src/pages/api/token.js deleted file mode 100644 index ec048158..00000000 --- a/src/pages/api/token.js +++ /dev/null @@ -1,10 +0,0 @@ -import axios from "axios"; - -export default async function handler(req, res) { - try { - let result = await axios.get(process.env.ODOO_HOST + '/api/token'); - res.status(200).json(result.data.result); - } catch (error) { - res.status(400).json({ error: error.message }); - } -}
\ No newline at end of file |
