summaryrefslogtreecommitdiff
path: root/src/lib/home/api/categoryManagementApi.js
blob: 54a6c1ac6034c8be461b569d0487895a9e42d7a9 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// import odooApi from '@/core/api/odooApi'

// const categoryManagementApi = async () => {
//     const dataCategoryManagement = await odooApi('GET', '/api/v1/categories_management')
//     return dataCategoryManagement
// }

// export default categoryManagementApi



export const fetchPopulerProductSolr = async () => {
    let sort ='sort=sequence_i asc';
    try {
        const response = await fetch(`/solr/category_management/query?q=*:*&q.op=OR&indent=true&${sort}`);
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        const data = await response.json();
      const promotions = await map(data.response.docs);
      return promotions;
    } catch (error) {
      console.error("Error fetching promotion data:", error);
      return [];
    }
  };

  const map = async (promotions) => {
    console.log("promotions",promotions)
    const result = [];
    for (const promotion of promotions) {
      console.log("promotion",promotion)
      const mappedCategories = promotion.categories.map((level2) => {
        console.log("level2",JSON.parse(level2))
        const childFrontend = level2.child_frontend_id_i.map((level3) => {
          return {
            id_level_3: level3.id_level_3,
            name: level3.name,
            numFound: level3.numFound,
            image: level3.image
          };
        });
  
        return {
          id_level_2: level2.id_level_2,
          name: level2.name,
          numFound: level2.numFound,
          image: level2.image,
          child_frontend_id_i: childFrontend
        };
      });
  
      const data = {
        id: promotion.id,
        category_id_i: promotion.category_id_i,
        name: promotion.name_s,
        sequence: promotion.sequence_i,
        numFound: promotion.numFound_i,
        image: promotion.image_s,
        categories: mappedCategories
      };
      result.push(data);
    }
    return result;
  };