export const promoMappingSolr = (promotions) => { return promotions.map((promotion) => { let productMapped = { 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), }; return productMapped; }); }; 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 || '', imageCarousel: product.image_carousel_ss || '', imageMobile: product.image_mobile_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, isTkdn: product?.tkdn_b || false, isSni: product?.sni_b || false, newVoucherPastiHemat: [], is_in_bu: product?.is_in_bu_b || false, voucherPastiHemat: product?.voucher_pastihemat || [], }; 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 || '', logo: product.x_logo_manufacture_s || '', }; } productMapped.categories = [ { id: product.category_id_i || '', name: product.category_name_s || '', }, ]; productMapped.newVoucherPastiHemat = [ { min_purchase: product.voucher_min_purchase_f || 0, discount_type: product.voucher_discount_type_s || '', discount_amount: product.voucher_discount_amount_f || 0, max_discount: product.voucher_max_discount_f || 0, }, ]; 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, attribute_set_id: product.attribute_set_id_i || 0, attribute_set_name: product.attribute_set_name_s || '', search_keywords: product.search_keywords_t || '', manufacture: {}, parent: {}, qtySold: product?.qty_sold_f || 0, is_in_bu: product?.is_in_bu_b || false, }; if (product.manufacture_id_i && product.manufacture_name_s) { productMapped.manufacture = { id: product.manufacture_id_i || '', name: product.manufacture_name_s || '', logo: parent[0]?.x_logo_manufacture_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, }; };