summaryrefslogtreecommitdiff
path: root/src/api/promoApi.js
blob: 8adc46475ca0d4758d38612aed67605d417b9cf6 (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
67
68
69
70
71
72
73
74
75
76
77
// src/api/promoApi.js
import odooApi from '@/core/api/odooApi';
import { type } from 'os';
// import { SolrResponse } from "../../../../src-migrate/types/solr.ts";

export const fetchPromoItems = async (type) => {
  try {
    const response = await odooApi('GET', `/api/v1/program-line?type=${type}&limit=3`);
    return response.map((item) => ({ value: item.id, label: item.name,  product: item.products ,price:item.price}));
  } catch (error) {
    console.error('Error fetching promo items:', error);
    return [];
  }
};

export const fetchPromoItemsSolr = async (type, start, rows) => {
  // let query = type ? `type_value_s:${type}` : '*:*';
  let sort ='sort=if(exists(sequence_i),0,1) asc, sequence_i asc, if(exists(total_qty_sold_f), total_qty_sold_f, -1) desc';
  // let start = 0
  // let rows = 100
  // let start = 0
  // let rows = 10
  try {
    const queryParams = new URLSearchParams({ q: type });
    const response = await fetch(
      `/solr/promotion_program_lines/select?${queryParams.toString()}&rows=${rows}&start=${start}&${sort}&fq=active_b%3Atrue`
    );
    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 [];
  }
};

export const fetchVariantSolr = async(data)=>{
  try {
    const queryParams = new URLSearchParams({ q: data });
    const response = await fetch(`/solr/variants/select?${queryParams.toString()}`);
    const responseData = await response.json();
    return responseData;
  } catch (error) {
    console.error("Error fetching promotion data:", error);
    return [];
  }
};

const map = async (promotions) => {
  const result = [];
  for (const promotion of promotions) {
    const data = {
      id: promotion.id,
      program_id: promotion.program_id_i,
      name: promotion.name_s,
      type: {
        value: promotion.type_value_s,
        label: promotion.type_label_s,
      },
      limit: promotion.package_limit_i,
      limit_user: promotion.package_limit_user_i,
      limit_trx: promotion.package_limit_trx_i,
      price: promotion.price_f,
      sequence: promotion.sequence_i,
      total_qty: promotion.total_qty_i,
      products: JSON.parse(promotion.products_s),
      product_id: promotion.product_ids[0],
      qty_sold_f:promotion.total_qty_sold_f,
      free_products: JSON.parse(promotion.free_products_s),
    };
    result.push(data);
  }
  return result;
};