summaryrefslogtreecommitdiff
path: root/src/lib/product/api/productSimilarApi.js
blob: ecd6724a64515ae660c2917cf45b8903997136ec (plain)
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
import odooApi from '@/core/api/odooApi'
import axios from 'axios'
import productSearchApi from './productSearchApi'

const productSimilarApi = async ({ query, source }) => {
  let dataflashSale = null
  const flashSale = await odooApi('GET', '/api/v1/flashsale/header')
  if (flashSale && flashSale.length > 0) {
    const dataFlash = await productSearchApi({
      query: `fq=flashsale_id_i:${flashSale[0].pricelistId}&fq=flashsale_price_f:[1 TO *]&limit=${
        source === 'bottom' ? '4' : '1'
      }`,
      operation: 'AND'
    })
    if (source === 'bottom') {
      dataflashSale = dataFlash.response.products.slice('2', '4')
    } else {
      dataflashSale = dataFlash.response.products
    }
  }
  const dataProductSimilar = await axios(
    `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?q=${query}&page=1&orderBy=popular-weekly&operation=OR&priceFrom=1`
  )
  if (dataflashSale) {
    dataProductSimilar.data.response.products = [
      ...dataflashSale,
      ...dataProductSimilar.data.response.products
    ]
  } else {
    dataProductSimilar.data.response.products = [...dataProductSimilar.data.response.products]
  }
  return dataProductSimilar.data.response
}

export default productSimilarApi