summaryrefslogtreecommitdiff
path: root/src/pages/api/shop/brands.js
blob: cc64a7e7d4e4dc86aa94f3fef7e7c09e03f96b5d (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import axios from 'axios';

const SOLR_HOST = process.env.SOLR_HOST;

export default async function handler(req, res) {
  try {
    let params = '*:*';
    let sort =
      'sort=if(exists(sequence_i),0,1) asc,sequence_i asc, if(exists(image_s),0,1) asc ';
    let rows = 2000;

    if (req.query.params) {
      rows = 100;
      switch (req?.query?.params) {
        case 'level_s':
          params = 'level_s:prioritas';
          break;
        case 'search':
          params = `name_s:"${req?.query?.q.toLowerCase()}"`;
          sort = '';
          rows = 1;
          break;
        default:
          params = `name_s:${req.query.params}`.toLowerCase();
      }
    }
    const url = `${SOLR_HOST}/solr/brands/select?q=${params}&q.op=OR&indent=true&rows=${rows}&${sort}`;
    let brands = await axios(url);
    let dataBrands = responseMap(brands.data.response.docs);

    res.status(200).json(dataBrands);
  } catch (error) {
    console.error('Error fetching data from Solr:', error);
    res.status(500).json({ error: 'Internal Server Error' });
  }
}

const responseMap = (brands) => {
  return brands.map((brand) => {
    let brandMapping = {
      id: brand.id,
      name: brand.display_name_s,
      logo: brand.image_s || '',
      sequance: brand.sequence_i || '',
    };

    return brandMapping;
  });
};