summaryrefslogtreecommitdiff
path: root/src/utils/solrMapping.js
blob: 199a7f35a31279aef0495a27b4c469c1b7e47652 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
export const productMappingSolr = (products, pricelist) => {
  return products.map((product) => {
    let price = product.price_tier1_v2_f || 0;
    let priceDiscount = price;
    let discountPercentage = 0;

    if (pricelist && product?.[`price_${pricelist}_f`] < price) {
      price = product?.[`price_${pricelist}_f`] || 0;
      priceDiscount = price;
    }

    if (product?.flashsale_id_i > 0) {
      price = product?.flashsale_base_price_f || 0;
      priceDiscount = product?.flashsale_price_f || 0;
      discountPercentage = product?.flashsale_discount_f || 0;
    }

    let productMapped = {
      id: product.product_id_i || '',
      image: product.image_s || '',
      code: product.default_code_s || '',
      description: product.description_t || '',
      displayName: product.display_name_s || '',
      name: product.name_s || '',
      lowestPrice: { price, priceDiscount, discountPercentage },
      variantTotal: product.variant_total_i || 0,
      stockTotal: product.stock_total_f || 0,
      weight: product.weight_f || 0,
      manufacture: {},
      categories: [],
      flashSale: {
        id: product?.flashsale_id_i,
        remainingTime: flashsaleTime(product?.flashsale_end_date_s)
          ?.remainingTime,
        name: product?.product?.flashsale_name_s,
        tag: product?.flashsale_tag_s || 'FLASH SALE',
      },
      qtySold: product?.qty_sold_f || 0,
    };

    if (product.manufacture_id_i && product.manufacture_name_s) {
      productMapped.manufacture = {
        id: product.manufacture_id_i || '',
        name: product.manufacture_name_s || '',
        imagePromotion1: product.image_promotion_1_s || '',
        imagePromotion2: product.image_promotion_2_s || '',
      };
    }

    productMapped.categories = [
      {
        id: product.category_id_i || '',
        name: product.category_name_s || '',
      },
    ];
    return productMapped;
  });
};

export const variantsMappingSolr = (parent, products, pricelist) => {
  return products.map((product) => {
    let price = product.price_tier1_v2_f || 0;
    let priceDiscount = price;
    let discountPercentage = 0;

    if (pricelist && product?.[`price_${pricelist}_f`] < price) {
      price = product?.[`price_${pricelist}_f`] || 0;
      priceDiscount = price;
    }

    if (product?.flashsale_id_i > 0 && product?.flashsale_price_f < price) {
      price = product?.flashsale_base_price_f || 0;
      priceDiscount = product?.flashsale_price_f || 0;
      discountPercentage = product?.flashsale_discount_f || 0;
    }

    let productMapped = {
      attributes: product.attributes || [],
      id: product.product_id_i || '',
      image: product.image_s || '',
      code: product.default_code_s || '',
      isFlashsale: flashsaleTime(product?.flashsale_end_date_s)?.isFlashSale,
      isFlashsale: flashsaleTime(product?.flashsale_end_date_s)?.isFlashSale,
      name: product.display_name_s || '',
      price: { price, priceDiscount, discountPercentage },
      variantTotal: product.variant_total_i || 0,
      stockTotal: product.stock_total_f || 0,
      weight: product.weight_f || 0,
      manufacture: {},
      parent: {},
      qtySold: product?.qty_sold_f || 0,
    };

    if (product.manufacture_id_i && product.manufacture_name_s) {
      productMapped.manufacture = {
        id: product.manufacture_id_i || '',
        name: product.manufacture_name_s || '',
      };
    }
    productMapped.parent = {
      id: parent[0]?.product_id_i || '',
      image: parent[0]?.image_s || '',
      name: parent[0]?.name_s || '',
      description: parent[0]?.description_t || '',
    };
    return productMapped;
  });
};

const flashsaleTime = (endDate) => {
  const flashsaleEndDate = new Date(endDate);
  const currentTime = new Date();

  const timeDifferenceInMillis = flashsaleEndDate - currentTime;
  const timeDifferenceInSeconds = timeDifferenceInMillis / 1000;

  return {
    remainingTime: timeDifferenceInSeconds,
    isFlashSale: flashsaleEndDate > currentTime,
  };
};