@managesome/eposmatic-toolkit 0.2.248 → 0.2.251

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/lib/constants/ENUMS.d.ts +36 -21
  2. package/lib/constants/ENUMS.js +48 -30
  3. package/lib/constants/SOCKET.d.ts +6 -0
  4. package/lib/constants/SOCKET.js +11 -2
  5. package/lib/helpers/store/cart-actions.d.ts +1 -0
  6. package/lib/helpers/utils/offers-helpers.d.ts +23 -3
  7. package/lib/helpers/utils/offers-helpers.js +1071 -389
  8. package/lib/schemas/models/customers.d.ts +9 -0
  9. package/lib/schemas/models/customers.js +14 -0
  10. package/lib/schemas/models/gift_cards.d.ts +113 -0
  11. package/lib/schemas/models/gift_cards.js +150 -0
  12. package/lib/schemas/models/index.d.ts +3 -0
  13. package/lib/schemas/models/index.js +3 -0
  14. package/lib/schemas/models/offers.d.ts +133 -66
  15. package/lib/schemas/models/offers.js +112 -12
  16. package/lib/schemas/models/order.d.ts +3 -0
  17. package/lib/schemas/models/order.js +5 -0
  18. package/lib/schemas/models/rewards_usage.d.ts +53 -0
  19. package/lib/schemas/models/rewards_usage.js +64 -0
  20. package/lib/schemas/models/vouchers.d.ts +68 -0
  21. package/lib/schemas/models/vouchers.js +85 -0
  22. package/lib/schemas/types/customers.d.ts +3 -0
  23. package/lib/schemas/types/gift_cards.d.ts +91 -0
  24. package/lib/schemas/types/gift_cards.js +2 -0
  25. package/lib/schemas/types/index.d.ts +3 -0
  26. package/lib/schemas/types/index.js +3 -0
  27. package/lib/schemas/types/offers.d.ts +139 -15
  28. package/lib/schemas/types/order.d.ts +1 -0
  29. package/lib/schemas/types/rewards_usage.d.ts +13 -0
  30. package/lib/schemas/types/rewards_usage.js +2 -0
  31. package/lib/schemas/types/vouchers.d.ts +17 -0
  32. package/lib/schemas/types/vouchers.js +2 -0
  33. package/package.json +2 -2
@@ -22,41 +22,83 @@ function getOfferWithPrice(offer, writeable) {
22
22
  if (newOffer.offer_special_hours != null && newOffer.offer_special_hours.length === 0) {
23
23
  newOffer.offer_special_hours = null;
24
24
  }
25
- if (newOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ITEMS &&
26
- newOffer.pathway_to_unlock_type_items != null) {
27
- newOffer.pathway_to_unlock_type_orders = null;
28
- newOffer.pathway_to_unlock_type_points = null;
29
- }
30
- else if (newOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS &&
31
- newOffer.pathway_to_unlock_type_orders != null) {
32
- newOffer.pathway_to_unlock_type_items = null;
33
- newOffer.pathway_to_unlock_type_points = null;
34
- }
35
- else if (newOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS &&
36
- newOffer.pathway_to_unlock_type_points != null) {
37
- newOffer.pathway_to_unlock_type_items = null;
38
- newOffer.pathway_to_unlock_type_orders = null;
39
- }
40
- if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS && newOffer.reward_items != null) {
41
- newOffer.reward_discount = null;
42
- newOffer.reward_discount_items = null;
43
- newOffer.reward_points = null;
44
- }
45
- else if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT && newOffer.reward_discount != null) {
46
- newOffer.reward_items = null;
47
- newOffer.reward_discount_items = null;
48
- newOffer.reward_points = null;
49
- }
50
- else if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS &&
51
- newOffer.reward_discount_items != null) {
52
- newOffer.reward_items = null;
53
- newOffer.reward_points = null;
54
- newOffer.reward_discount = null;
55
- }
56
- else if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS && newOffer.reward_points != null) {
57
- newOffer.reward_items = null;
58
- newOffer.reward_discount = null;
59
- newOffer.reward_discount_items = null;
25
+ // Always remove non-selected pathway payloads based on selector.
26
+ if (newOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ITEMS) {
27
+ delete newOffer.pathway_to_unlock_type_orders;
28
+ delete newOffer.pathway_to_unlock_type_points;
29
+ delete newOffer.pathway_to_unlock_type_vouchers;
30
+ }
31
+ else if (newOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS) {
32
+ delete newOffer.pathway_to_unlock_type_items;
33
+ delete newOffer.pathway_to_unlock_type_points;
34
+ delete newOffer.pathway_to_unlock_type_vouchers;
35
+ }
36
+ else if (newOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
37
+ delete newOffer.pathway_to_unlock_type_items;
38
+ delete newOffer.pathway_to_unlock_type_orders;
39
+ delete newOffer.pathway_to_unlock_type_vouchers;
40
+ }
41
+ else if (newOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
42
+ delete newOffer.pathway_to_unlock_type_items;
43
+ delete newOffer.pathway_to_unlock_type_orders;
44
+ delete newOffer.pathway_to_unlock_type_points;
45
+ }
46
+ // Always remove non-selected reward payloads based on selector.
47
+ if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
48
+ delete newOffer.reward_discount;
49
+ delete newOffer.reward_discount_items;
50
+ delete newOffer.reward_points;
51
+ delete newOffer.reward_cashback;
52
+ delete newOffer.reward_gift_cards;
53
+ delete newOffer.reward_voucher;
54
+ }
55
+ else if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT) {
56
+ delete newOffer.reward_items;
57
+ delete newOffer.reward_discount_items;
58
+ delete newOffer.reward_points;
59
+ delete newOffer.reward_cashback;
60
+ delete newOffer.reward_gift_cards;
61
+ delete newOffer.reward_voucher;
62
+ }
63
+ else if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
64
+ delete newOffer.reward_items;
65
+ delete newOffer.reward_points;
66
+ delete newOffer.reward_discount;
67
+ delete newOffer.reward_cashback;
68
+ delete newOffer.reward_gift_cards;
69
+ delete newOffer.reward_voucher;
70
+ }
71
+ else if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
72
+ delete newOffer.reward_items;
73
+ delete newOffer.reward_discount;
74
+ delete newOffer.reward_discount_items;
75
+ delete newOffer.reward_cashback;
76
+ delete newOffer.reward_gift_cards;
77
+ delete newOffer.reward_voucher;
78
+ }
79
+ else if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK) {
80
+ delete newOffer.reward_items;
81
+ delete newOffer.reward_discount;
82
+ delete newOffer.reward_discount_items;
83
+ delete newOffer.reward_points;
84
+ delete newOffer.reward_gift_cards;
85
+ delete newOffer.reward_voucher;
86
+ }
87
+ else if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS) {
88
+ delete newOffer.reward_items;
89
+ delete newOffer.reward_discount;
90
+ delete newOffer.reward_discount_items;
91
+ delete newOffer.reward_points;
92
+ delete newOffer.reward_cashback;
93
+ delete newOffer.reward_voucher;
94
+ }
95
+ else if (newOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
96
+ delete newOffer.reward_items;
97
+ delete newOffer.reward_discount;
98
+ delete newOffer.reward_discount_items;
99
+ delete newOffer.reward_points;
100
+ delete newOffer.reward_cashback;
101
+ delete newOffer.reward_gift_cards;
60
102
  }
61
103
  }
62
104
  return {
@@ -93,6 +135,17 @@ function getOfferWithPrice(offer, writeable) {
93
135
  },
94
136
  }
95
137
  : {}),
138
+ ...(newOffer.pathway_to_unlock_type_vouchers != null
139
+ ? {
140
+ pathway_to_unlock_type_vouchers: {
141
+ ...newOffer.pathway_to_unlock_type_vouchers,
142
+ total_amount: convertToNum(newOffer.pathway_to_unlock_type_vouchers.total_amount),
143
+ max_amount: newOffer.pathway_to_unlock_type_vouchers.max_amount != null
144
+ ? convertToNum(newOffer.pathway_to_unlock_type_vouchers.max_amount)
145
+ : null,
146
+ },
147
+ }
148
+ : {}),
96
149
  ...(newOffer.reward_discount != null
97
150
  ? {
98
151
  reward_discount: {
@@ -109,6 +162,24 @@ function getOfferWithPrice(offer, writeable) {
109
162
  },
110
163
  }
111
164
  : {}),
165
+ ...(newOffer.reward_cashback != null
166
+ ? {
167
+ reward_cashback: {
168
+ ...newOffer.reward_cashback,
169
+ cashback_value: convertToNum(newOffer.reward_cashback.cashback_value),
170
+ max_cashback_amount: newOffer.reward_cashback.max_cashback_amount != null
171
+ ? convertToNum(newOffer.reward_cashback.max_cashback_amount)
172
+ : null,
173
+ },
174
+ }
175
+ : {}),
176
+ ...(newOffer.reward_gift_cards != null
177
+ ? {
178
+ reward_gift_cards: {
179
+ ...newOffer.reward_gift_cards,
180
+ },
181
+ }
182
+ : {}),
112
183
  };
113
184
  }
114
185
  // ====================================================================================================================
@@ -141,13 +212,15 @@ function calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categor
141
212
  // then we need to calculate the points redeemed for 2 pizzas
142
213
  if (redeemedOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ITEMS) {
143
214
  if (redeemedOffer.pathway_to_unlock_type_items != null) {
144
- const { num_of_items, total_amount, excluded_refs, exclusive_refs, item_ids } = redeemedOffer.pathway_to_unlock_type_items;
215
+ const { num_of_items, max_claims, max_same_item_quantity, total_amount, excluded_refs, exclusive_refs, item_ids, category_ids, } = redeemedOffer.pathway_to_unlock_type_items;
145
216
  // A Bachelor Item is an item that meets the criteria of the redeemedOffer and doesn't have any redeemedOffer applied to it
146
217
  const bachelorItems = getCartItemsApplicableToOfferItemsQuantity({
147
218
  categories,
148
219
  excluded_refs,
149
220
  exclusive_refs,
150
221
  item_ids,
222
+ category_ids,
223
+ max_same_item_quantity,
151
224
  });
152
225
  if (bachelorItems >= num_of_items) {
153
226
  if (totalOrderPrice >= total_amount) {
@@ -155,8 +228,10 @@ function calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categor
155
228
  const timesForAmount = Math.floor(totalOrderPrice / (total_amount || 1));
156
229
  // we need to find the minimum of the two
157
230
  const minTimes = Math.min(timesForItems, timesForAmount);
158
- if (minTimes > 1) {
159
- times = minTimes;
231
+ const maxClaims = max_claims == null ? Infinity : Math.max(1, max_claims);
232
+ const cappedTimes = Math.min(minTimes, maxClaims);
233
+ if (cappedTimes > 1) {
234
+ times = cappedTimes;
160
235
  }
161
236
  }
162
237
  }
@@ -300,10 +375,13 @@ function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDe
300
375
  deductedLoyaltyPoints,
301
376
  };
302
377
  }
303
- function getCartItemsApplicableToOfferItemsQuantity({ categories, excluded_refs, exclusive_refs, item_ids, }) {
378
+ function getCartItemsApplicableToOfferItemsQuantity({ categories, excluded_refs, exclusive_refs, item_ids, category_ids, max_same_item_quantity, }) {
304
379
  if (categories == null)
305
380
  return 0;
306
381
  const checkIfItemHasAnyRef = (item, refs_arr) => {
382
+ if (refs_arr.includes(item.item_id)) {
383
+ return true;
384
+ }
307
385
  let hasRef = false;
308
386
  if (item.modifiers != null) {
309
387
  const recursiveCheck = (modifiers) => {
@@ -333,11 +411,14 @@ function getCartItemsApplicableToOfferItemsQuantity({ categories, excluded_refs,
333
411
  };
334
412
  const applicableItems = categories.reduce((acc, category) => {
335
413
  category.items.forEach((item) => {
414
+ const isItemMatched = item_ids.length > 0 && item_ids.includes(item.item_id);
415
+ const isCategoryMatched = category_ids.length > 0 && category_ids.includes(category.category_id);
336
416
  if (
337
417
  // We only want to check for items that don't have any loyalty or voucher discounts
338
418
  item.item_discount_details?.loyalty_discount_id == null &&
339
419
  item.item_discount_details?.voucher_discount_id == null &&
340
- item_ids.includes(item.item_id) &&
420
+ // Item matches either explicit item list or selected category list.
421
+ (isItemMatched || isCategoryMatched) &&
341
422
  // We only want to check for items that don't have any modifiers/options that are excluded
342
423
  (excluded_refs.length === 0 || (excluded_refs.length > 0 && !checkIfItemHasAnyRef(item, excluded_refs))) &&
343
424
  // We only want to check for items that have at least one modifier/option that is inclusive
@@ -351,8 +432,13 @@ function getCartItemsApplicableToOfferItemsQuantity({ categories, excluded_refs,
351
432
  });
352
433
  return acc;
353
434
  }, []) ?? [];
354
- const totalQuantity = applicableItems.reduce((acc, item) => {
355
- acc += item.quantity;
435
+ const maxSameItemQuantity = max_same_item_quantity == null ? Infinity : Math.max(1, max_same_item_quantity);
436
+ const quantityByItemId = applicableItems.reduce((acc, item) => {
437
+ acc[item.item_id] = (acc[item.item_id] ?? 0) + item.quantity;
438
+ return acc;
439
+ }, {});
440
+ const totalQuantity = Object.values(quantityByItemId).reduce((acc, quantity) => {
441
+ acc += Math.min(quantity, maxSameItemQuantity);
356
442
  return acc;
357
443
  }, 0);
358
444
  return totalQuantity;
@@ -397,259 +483,222 @@ const getTotalSelectedOfferItemsAgainstOffer = (categories, offer_id) => {
397
483
  return totalOfferItems;
398
484
  };
399
485
  exports.getTotalSelectedOfferItemsAgainstOffer = getTotalSelectedOfferItemsAgainstOffer;
400
- // export function getOffersWithStats({
401
- // offers,
402
- // customer,
403
- // currency_symbol,
404
- // currentItemsPrice,
405
- // totalDiscountApplied,
406
- // categories,
407
- // order_discount_details,
408
- // }: IGetOffersWithStats) {
409
- // let totalLoyaltyPointsOfCustomer: number | null = null;
410
- // let totalAddedLoyaltyPoints = 0;
411
- // let totalClaimableOffers = 0;
412
- // const unlockedOffersApplicableTimes: Record<string, number> = {};
413
- // const offersWithStats = offers.map((offer) => {
414
- // const orderValue = (currentItemsPrice ?? 0) - (totalDiscountApplied ?? 0);
415
- // const totalOrders = customer?.total_orders ?? 0;
416
- // const stats: IOfferStats = {
417
- // progress: 0,
418
- // is_unlocked: false,
419
- // message: '',
420
- // invalid_offer: false,
421
- // redeemed_times: 0,
422
- // claimable: false,
423
- // applicable_quantity: 0,
424
- // };
425
- // let totalOfferItemsSelected = 0;
426
- // let applicableQuantity = 1;
427
- // let maxRedeemableTimes = 1;
428
- // // First We Check if the offer is already redeemed
429
- // if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.ITEMS && offer.reward_items != null) {
430
- // // we check if the newCustomer has redeemed enough items to mark the offer as redeemed
431
- // totalOfferItemsSelected = getTotalSelectedOfferItemsAgainstOffer(categories, offer._id);
432
- // applicableQuantity = offer.reward_items.num_of_items || 1;
433
- // stats.redeemed_times = Math.floor(totalOfferItemsSelected / applicableQuantity);
434
- // maxRedeemableTimes = 100;
435
- // } else if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS && offer.reward_discount_items != null) {
436
- // // we check if the newCustomer has redeemed enough items to mark the offer as redeemed
437
- // totalOfferItemsSelected = getTotalSelectedOfferItemsAgainstOffer(categories, offer._id);
438
- // applicableQuantity = offer.reward_discount_items.num_of_items || 1;
439
- // stats.redeemed_times = Math.floor(totalOfferItemsSelected / applicableQuantity);
440
- // maxRedeemableTimes = 100;
441
- // } else if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.POINTS && offer.reward_points) {
442
- // // we check if the order_discount_details.loyalty_discount_id has the same id as the offer._id
443
- // const isRedeemed =
444
- // order_discount_details != null &&
445
- // (order_discount_details.loyalty_discount_id === offer._id ||
446
- // order_discount_details.voucher_discount_id === offer._id);
447
- // stats.redeemed_times = isRedeemed ? 1 : 0;
448
- // maxRedeemableTimes = 1;
449
- // } else if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.DISCOUNT && offer.reward_discount != null) {
450
- // // we check if the order_discount_details.loyalty_discount_id has the same id as the offer._id
451
- // const isRedeemed =
452
- // order_discount_details != null &&
453
- // (order_discount_details.loyalty_discount_id === offer._id ||
454
- // order_discount_details.voucher_discount_id === offer._id);
455
- // stats.redeemed_times = isRedeemed ? 1 : 0;
456
- // maxRedeemableTimes = 1;
457
- // }
458
- // if (offer.pathway_to_unlock_type === PATHWAY_TO_UNLOCK_TYPE_ENUM.ITEMS) {
459
- // if (offer.pathway_to_unlock_type_items != null) {
460
- // const { num_of_items, total_amount, excluded_refs, exclusive_refs, item_ids } =
461
- // offer.pathway_to_unlock_type_items;
462
- // // A Bachelor Item is an item that meets the criteria of the offer and doesn't have any offer applied to it
463
- // const bachelorItems = getCartItemsApplicableToOfferItemsQuantity({
464
- // categories,
465
- // excluded_refs,
466
- // exclusive_refs,
467
- // item_ids,
468
- // });
469
- // const totalItemsSelected = totalOfferItemsSelected + bachelorItems;
470
- // // This means that the offer is only applicable for the current day
471
- // if (totalItemsSelected >= num_of_items) {
472
- // if (orderValue >= total_amount) {
473
- // if (bachelorItems >= num_of_items) {
474
- // // This means that the customer has enough items to unlock the offer
475
- // stats.progress = 100;
476
- // stats.is_unlocked = true;
477
- // stats.claimable = stats.redeemed_times < maxRedeemableTimes;
478
- // if (stats.claimable) {
479
- // stats.message = `🎉 You have unlocked this offer 🎉`;
480
- // stats.applicable_quantity = applicableQuantity * (stats.redeemed_times + 1);
481
- // } else {
482
- // stats.message = `You have already redeemed this offer ✅`;
483
- // stats.applicable_quantity = applicableQuantity * (stats.redeemed_times || 1);
484
- // }
485
- // unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
486
- // } else {
487
- // stats.progress = Math.round((bachelorItems / (applicableQuantity || 1)) * 100);
488
- // stats.message = `You are ${applicableQuantity - bachelorItems} items away from unlocking this offer`;
489
- // stats.is_unlocked = true;
490
- // stats.claimable = false;
491
- // if (stats.redeemed_times > 0) {
492
- // stats.applicable_quantity = applicableQuantity * stats.redeemed_times;
493
- // unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
494
- // }
495
- // }
496
- // } else {
497
- // // This means that the customer has not spent enough to unlock the offer
498
- // // But they have enough items to unlock the offer
499
- // stats.progress = 90;
500
- // stats.message = `You are almost there. Spend ${currency_symbol}${parseNum(
501
- // total_amount - orderValue,
502
- // )} more in this order to unlock this offer 🔓`;
503
- // }
504
- // } else {
505
- // // This means that the customer has not yet added enough items to unlock the offer
506
- // stats.progress = Math.round((bachelorItems / (num_of_items || 1)) * 100);
507
- // stats.message = `You are ${num_of_items - bachelorItems} items away from unlocking this offer`;
508
- // }
509
- // } else {
510
- // // If pathway to unlock type items is null then we can't proceed
511
- // stats.invalid_offer = true;
512
- // stats.message = `Offer pathway to unlock type items is empty`;
513
- // }
514
- // }
515
- // // If pathway to unlock type is orders
516
- // else if (offer.pathway_to_unlock_type === PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS) {
517
- // // If pathway to unlock type is orders then pathway to unlock type orders should not be null in order to proceed
518
- // if (offer.pathway_to_unlock_type_orders != null) {
519
- // const { min_num_of_orders, max_num_of_orders, total_amount } = offer.pathway_to_unlock_type_orders;
520
- // if (totalOrders >= min_num_of_orders) {
521
- // // Either the max_num_of_orders is infinity, or the customer has not reached the max_num_of_orders
522
- // if (max_num_of_orders == null || totalOrders <= max_num_of_orders) {
523
- // if (orderValue >= total_amount) {
524
- // stats.progress = 100;
525
- // stats.is_unlocked = true;
526
- // stats.claimable = stats.redeemed_times < maxRedeemableTimes;
527
- // if (stats.claimable) {
528
- // stats.message = `🎉 You have unlocked this offer 🎉`;
529
- // stats.applicable_quantity = applicableQuantity * (stats.redeemed_times + 1);
530
- // } else {
531
- // stats.message = `You have already redeemed this offer ✅`;
532
- // stats.applicable_quantity = applicableQuantity * (stats.redeemed_times || 1);
533
- // }
534
- // unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
535
- // } else {
536
- // // This means that the customer has not spent enough to unlock the offer
537
- // // But they have enough orders to unlock the offer
538
- // stats.progress = 90;
539
- // stats.message = `You are almost there. Spend ${currency_symbol}${parseNum(
540
- // total_amount - orderValue,
541
- // )} more in this order to unlock this offer 🔓`;
542
- // }
543
- // } else {
544
- // // This means that the customer has reached the max_num_of_orders
545
- // stats.progress = 100;
546
- // stats.is_unlocked = false;
547
- // stats.message = `⛔️ You are not eligible for this offer ⛔️`;
548
- // }
549
- // } else {
550
- // stats.progress = Math.round((totalOrders / (min_num_of_orders || 1)) * 100);
551
- // stats.message = `You are ${min_num_of_orders - totalOrders} orders away from unlocking this offer`;
552
- // }
553
- // } else {
554
- // // If pathway to unlock type orders is null then we can't proceed
555
- // stats.invalid_offer = true;
556
- // stats.message = `Offer pathway to unlock type orders is empty`;
557
- // }
558
- // }
559
- // // If pathway to unlock type is points
560
- // else if (offer.pathway_to_unlock_type === PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
561
- // // If pathway to unlock type is points then pathway to unlock type points should not be null in order to proceed
562
- // if (offer.pathway_to_unlock_type_points != null) {
563
- // // Pathway to unlock type points should have num_of_points, total_amount and applicable_unlock_days
564
- // // Applicant unlock days can be null if there is no restriction on days
565
- // const { num_of_points, total_amount } = offer.pathway_to_unlock_type_points;
566
- // if (totalLoyaltyPointsOfCustomer == null) {
567
- // const { addedLoyaltyPoints, loyaltyPoints } = calculateCustomerLoyaltyPoints({
568
- // customerLoyaltyPoints: customer?.loyalty_points,
569
- // orderDiscountDetails: order_discount_details,
570
- // currentItemsPrice,
571
- // categories,
572
- // totalDiscountApplied,
573
- // offers,
574
- // });
575
- // totalLoyaltyPointsOfCustomer = loyaltyPoints;
576
- // totalAddedLoyaltyPoints = addedLoyaltyPoints;
577
- // }
578
- // if (totalAddedLoyaltyPoints >= num_of_points) {
579
- // // This means that the offer is only applicable for the current day
580
- // if (orderValue >= total_amount) {
581
- // if (totalLoyaltyPointsOfCustomer >= num_of_points) {
582
- // stats.progress = 100;
583
- // stats.is_unlocked = true;
584
- // stats.claimable = stats.redeemed_times < maxRedeemableTimes;
585
- // if (stats.claimable) {
586
- // stats.message = `🎉 You have unlocked this offer 🎉`;
587
- // stats.applicable_quantity = applicableQuantity * (stats.redeemed_times + 1);
588
- // } else {
589
- // stats.message = `You have already redeemed this offer ✅`;
590
- // stats.applicable_quantity = applicableQuantity * (stats.redeemed_times || 1);
591
- // }
592
- // unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
593
- // } else {
594
- // stats.progress = Math.round((totalLoyaltyPointsOfCustomer / (num_of_points || 1)) * 100);
595
- // stats.message = `You are ${
596
- // num_of_points - totalLoyaltyPointsOfCustomer
597
- // } points away from unlocking this offer`;
598
- // stats.is_unlocked = true;
599
- // stats.claimable = false;
600
- // if (stats.redeemed_times > 0) {
601
- // // By Default
602
- // let redeemed_times = stats.redeemed_times;
603
- // let leftPoints = totalLoyaltyPointsOfCustomer;
604
- // while (leftPoints < 0) {
605
- // redeemed_times -= 1;
606
- // leftPoints += num_of_points;
607
- // }
608
- // stats.applicable_quantity = applicableQuantity * redeemed_times;
609
- // unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
610
- // }
611
- // }
612
- // } else {
613
- // // This means that the customer has not spent enough to unlock the offer
614
- // // But they have enough points to unlock the offer
615
- // stats.progress = 90;
616
- // stats.message = `You are almost there. Spend ${currency_symbol}${parseNum(
617
- // parseNum(total_amount - orderValue),
618
- // )} more in this order to unlock this offer 🔓`;
619
- // }
620
- // } else {
621
- // stats.progress = Math.round((totalLoyaltyPointsOfCustomer / (num_of_points || 1)) * 100);
622
- // stats.message = `You are ${
623
- // num_of_points - totalLoyaltyPointsOfCustomer
624
- // } points away from unlocking this offer`;
625
- // }
626
- // } else {
627
- // // If pathway to unlock type points is null then we can't proceed
628
- // stats.invalid_offer = true;
629
- // stats.message = `Offer pathway to unlock type points is empty`;
630
- // }
631
- // }
632
- // stats.claimable = stats.claimable && stats.is_unlocked;
633
- // if (stats.claimable) {
634
- // totalClaimableOffers += 1;
635
- // }
636
- // return {
637
- // offer,
638
- // stats,
639
- // };
640
- // });
641
- // return { offersWithStats, totalClaimableOffers, unlockedOffersApplicableTimes };
642
- // }
643
- // =====================><=====================
644
- function getOffersWithStats({ offers, customer, currency_symbol, currentItemsPrice, totalDiscountApplied, categories, order_discount_details, }) {
486
+ function buildRewardPreview(offer) {
487
+ switch (offer.reward_type) {
488
+ case constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT: {
489
+ if (offer.reward_discount == null)
490
+ return null;
491
+ const { discount_value, is_discount_percentage } = offer.reward_discount;
492
+ return {
493
+ type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT,
494
+ label: is_discount_percentage ? `${discount_value}% off` : `${discount_value} off`,
495
+ discount_value,
496
+ is_percentage: is_discount_percentage,
497
+ };
498
+ }
499
+ case constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS: {
500
+ if (offer.reward_discount_items == null)
501
+ return null;
502
+ const { discount_value, is_discount_percentage, num_of_items } = offer.reward_discount_items;
503
+ return {
504
+ type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS,
505
+ label: is_discount_percentage
506
+ ? `${discount_value}% off on ${num_of_items} item(s)`
507
+ : `${discount_value} off on ${num_of_items} item(s)`,
508
+ discount_value,
509
+ is_percentage: is_discount_percentage,
510
+ free_items_count: num_of_items,
511
+ };
512
+ }
513
+ case constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS: {
514
+ if (offer.reward_items == null)
515
+ return null;
516
+ return {
517
+ type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
518
+ label: `Free ${offer.reward_items.num_of_items} item(s)`,
519
+ free_items_count: offer.reward_items.num_of_items,
520
+ };
521
+ }
522
+ case constants_1.OFFER_REWARD_TYPE_ENUM.POINTS: {
523
+ if (offer.reward_points == null)
524
+ return null;
525
+ return {
526
+ type: constants_1.OFFER_REWARD_TYPE_ENUM.POINTS,
527
+ label: `${offer.reward_points.num_of_points} points`,
528
+ points_value: offer.reward_points.num_of_points,
529
+ };
530
+ }
531
+ case constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK: {
532
+ if (offer.reward_cashback == null)
533
+ return null;
534
+ const { cashback_value, is_cashback_percentage } = offer.reward_cashback;
535
+ return {
536
+ type: constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK,
537
+ label: is_cashback_percentage ? `${cashback_value}% cashback` : `${cashback_value} cashback`,
538
+ cashback_value,
539
+ is_percentage: is_cashback_percentage,
540
+ };
541
+ }
542
+ case constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS: {
543
+ if (offer.reward_gift_cards == null)
544
+ return null;
545
+ return {
546
+ type: constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS,
547
+ label: `Gift card reward`,
548
+ };
549
+ }
550
+ case constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER: {
551
+ if (offer.reward_voucher == null)
552
+ return null;
553
+ return {
554
+ type: constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER,
555
+ label: `Voucher code reward`,
556
+ };
557
+ }
558
+ default:
559
+ return null;
560
+ }
561
+ }
562
+ function getEligibleItemIds(offer) {
563
+ if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS && offer.reward_items != null) {
564
+ return offer.reward_items.item_ids ?? [];
565
+ }
566
+ if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS && offer.reward_discount_items != null) {
567
+ return offer.reward_discount_items.item_ids ?? [];
568
+ }
569
+ return [];
570
+ }
571
+ function getEligibleCategoryIds(offer) {
572
+ if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ITEMS &&
573
+ offer.pathway_to_unlock_type_items != null) {
574
+ return offer.pathway_to_unlock_type_items.category_ids ?? [];
575
+ }
576
+ return [];
577
+ }
578
+ function getActionHint(offer, claimable) {
579
+ if (!claimable)
580
+ return null;
581
+ if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS)
582
+ return 'Select your free items';
583
+ if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS)
584
+ return 'Select items for discount';
585
+ return 'Ready to apply';
586
+ }
587
+ function getOffersWithStats({ offers, customer, vouchers, customerVoucherUsage, customerGiftCardsUsage, redeemed_offers, platform, currency_symbol, currentItemsPrice, totalDiscountApplied, categories, order_discount_details, }) {
645
588
  const offersWithStats = [];
646
- const totalOrders = customer?.total_orders ?? 0;
647
589
  const orderPrice = currentItemsPrice ?? 0;
648
590
  const totalDiscount = totalDiscountApplied ?? 0;
649
591
  const orderDiscount = order_discount_details?.total_discount ?? 0;
650
592
  const itemsDiscount = totalDiscount - orderDiscount;
651
593
  const unlockedOffersApplicableTimes = {};
652
594
  let totalClaimableOffers = 0;
595
+ let totalLoyaltyPointsOfCustomer = null;
596
+ let totalAddedLoyaltyPoints = 0;
597
+ const nowTs = Date.now();
598
+ const customerOwnedVoucherCountByCode = {};
599
+ const customerVoucherCodes = customer?.voucher_codes;
600
+ if (customerVoucherCodes != null) {
601
+ const voucherCodeEntries = Object.entries(customerVoucherCodes);
602
+ for (let i = 0; i < voucherCodeEntries.length; i++) {
603
+ const [voucherCode, count] = voucherCodeEntries[i];
604
+ const normalizedCode = voucherCode.trim().toUpperCase();
605
+ if (normalizedCode.length === 0)
606
+ continue;
607
+ const parsedCount = Number(count);
608
+ if (!Number.isFinite(parsedCount) || parsedCount <= 0)
609
+ continue;
610
+ customerOwnedVoucherCountByCode[normalizedCode] =
611
+ (customerOwnedVoucherCountByCode[normalizedCode] ?? 0) + parsedCount;
612
+ }
613
+ }
614
+ const customerVoucherUsageById = {};
615
+ if (customerVoucherUsage != null) {
616
+ const voucherUsageEntries = Object.entries(customerVoucherUsage);
617
+ for (let i = 0; i < voucherUsageEntries.length; i++) {
618
+ const [voucherId, count] = voucherUsageEntries[i];
619
+ const parsedCount = Number(count);
620
+ if (!Number.isFinite(parsedCount) || parsedCount <= 0)
621
+ continue;
622
+ customerVoucherUsageById[voucherId] = (customerVoucherUsageById[voucherId] ?? 0) + parsedCount;
623
+ }
624
+ }
625
+ const customerGiftCardsUsageById = {};
626
+ if (customerGiftCardsUsage != null) {
627
+ const giftCardUsageEntries = Object.entries(customerGiftCardsUsage);
628
+ for (let i = 0; i < giftCardUsageEntries.length; i++) {
629
+ const [giftCardId, count] = giftCardUsageEntries[i];
630
+ const parsedCount = Number(count);
631
+ if (!Number.isFinite(parsedCount) || parsedCount <= 0)
632
+ continue;
633
+ customerGiftCardsUsageById[giftCardId] = (customerGiftCardsUsageById[giftCardId] ?? 0) + parsedCount;
634
+ }
635
+ }
636
+ const redeemedOffersById = redeemed_offers ?? null;
637
+ const vouchersById = new Map();
638
+ if (vouchers != null) {
639
+ for (let i = 0; i < vouchers.length; i++) {
640
+ const voucher = vouchers[i];
641
+ const voucherIsExpired = voucher.expire_at != null && new Date(voucher.expire_at).getTime() <= nowTs;
642
+ const voucherUsesExhausted = voucher.max_uses != null && voucher.used_count >= voucher.max_uses;
643
+ if (!voucher.active || voucherIsExpired || voucherUsesExhausted) {
644
+ continue;
645
+ }
646
+ vouchersById.set(voucher._id, voucher);
647
+ }
648
+ }
649
+ const getOfferRedeemedTimes = (offerId) => {
650
+ const redeemedTimes = Number(redeemedOffersById?.[offerId]);
651
+ if (Number.isFinite(redeemedTimes) && redeemedTimes > 0) {
652
+ return Math.floor(redeemedTimes);
653
+ }
654
+ const redeemedFromDiscountDetails = order_discount_details != null &&
655
+ (order_discount_details.loyalty_discount_id === offerId ||
656
+ order_discount_details.voucher_discount_id === offerId);
657
+ return redeemedFromDiscountDetails ? 1 : 0;
658
+ };
659
+ const getOrdersScopedStats = ({ scope, currentOrderValue, }) => {
660
+ if (scope === constants_1.PATHWAY_TO_UNLOCK_TYPE_ORDERS_SCOPE_ENUM.OVERALL) {
661
+ return {
662
+ valid: true,
663
+ totalOrders: customer?.total_orders ?? 0,
664
+ totalAmount: currentOrderValue,
665
+ };
666
+ }
667
+ if (platform == null) {
668
+ return {
669
+ valid: false,
670
+ totalOrders: 0,
671
+ totalAmount: 0,
672
+ };
673
+ }
674
+ const totalOrdersPerPlatform = customer?.total_orders_per_platform_type;
675
+ if (scope === constants_1.PATHWAY_TO_UNLOCK_TYPE_ORDERS_SCOPE_ENUM.PLATFORM) {
676
+ return {
677
+ valid: true,
678
+ totalOrders: totalOrdersPerPlatform?.[platform] ?? 0,
679
+ totalAmount: currentOrderValue,
680
+ };
681
+ }
682
+ const orderFrom = platform.split('_')[0];
683
+ const platformsForOrderFrom = constants_1.PLATFORMS_BY_ORDER_FROM[orderFrom];
684
+ if (platformsForOrderFrom == null) {
685
+ return {
686
+ valid: false,
687
+ totalOrders: 0,
688
+ totalAmount: 0,
689
+ };
690
+ }
691
+ let totalOrders = 0;
692
+ for (let i = 0; i < platformsForOrderFrom.length; i++) {
693
+ const groupedPlatform = platformsForOrderFrom[i];
694
+ totalOrders += totalOrdersPerPlatform?.[groupedPlatform] ?? 0;
695
+ }
696
+ return {
697
+ valid: true,
698
+ totalOrders,
699
+ totalAmount: currentOrderValue,
700
+ };
701
+ };
653
702
  for (let i = 0; i < offers.length; i++) {
654
703
  const offer = offers[i];
655
704
  const stats = {
@@ -659,18 +708,43 @@ function getOffersWithStats({ offers, customer, currency_symbol, currentItemsPri
659
708
  redeemed_times: 0,
660
709
  claimable: false,
661
710
  applicable_quantity: 0,
711
+ eligible_item_ids: getEligibleItemIds(offer),
712
+ eligible_category_ids: getEligibleCategoryIds(offer),
713
+ reward_preview: buildRewardPreview(offer),
714
+ action_hint: null,
715
+ auto_apply: offer.auto_apply ?? false,
662
716
  };
663
717
  if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS) {
664
- if (offer.pathway_to_unlock_type_orders == null)
718
+ if (offer.pathway_to_unlock_type_orders == null) {
719
+ stats.invalid_offer = true;
720
+ stats.message = `Offer pathway to unlock type orders is empty`;
721
+ offersWithStats.push({ offer, stats });
665
722
  continue;
723
+ }
666
724
  const orderValue = orderPrice - itemsDiscount;
667
725
  // Need to check if offer is unlocked
668
- const { min_num_of_orders, max_num_of_orders, total_amount, max_amount } = offer.pathway_to_unlock_type_orders;
726
+ const { scope = constants_1.PATHWAY_TO_UNLOCK_TYPE_ORDERS_SCOPE_ENUM.OVERALL, min_num_of_orders, max_num_of_orders, total_amount, max_amount, } = offer.pathway_to_unlock_type_orders;
727
+ const scopedStats = getOrdersScopedStats({
728
+ scope,
729
+ currentOrderValue: orderValue,
730
+ });
731
+ if (!scopedStats.valid) {
732
+ stats.progress = 0;
733
+ stats.claimable = false;
734
+ stats.invalid_offer = true;
735
+ stats.message = `⛔️ This offer requires platform context ⛔️`;
736
+ offersWithStats.push({
737
+ offer,
738
+ stats,
739
+ });
740
+ continue;
741
+ }
742
+ const { totalOrders, totalAmount } = scopedStats;
669
743
  if (totalOrders >= min_num_of_orders) {
670
744
  // Either the max_num_of_orders is infinity, or the customer has not reached the max_num_of_orders
671
745
  if ((max_num_of_orders == null || totalOrders <= max_num_of_orders) &&
672
- (max_amount == null || orderValue <= max_amount)) {
673
- if (orderValue >= total_amount) {
746
+ (max_amount == null || totalAmount <= max_amount)) {
747
+ if (totalAmount >= total_amount) {
674
748
  stats.progress = 100;
675
749
  stats.claimable = false;
676
750
  stats.message = `🎉 You have unlocked this offer 🎉`;
@@ -680,7 +754,7 @@ function getOffersWithStats({ offers, customer, currency_symbol, currentItemsPri
680
754
  // This means that the customer has not spent enough to unlock the offer
681
755
  // But they have enough orders to unlock the offer
682
756
  stats.progress = 90;
683
- stats.message = `You are almost there. Spend ${currency_symbol}${(0, common_1.parseNum)(total_amount - orderValue)} more in this order to unlock this offer 🔓`;
757
+ stats.message = `You are almost there. Spend ${currency_symbol}${(0, common_1.parseNum)(total_amount - totalAmount)} more to unlock this offer 🔓`;
684
758
  }
685
759
  }
686
760
  else {
@@ -724,19 +798,115 @@ function getOffersWithStats({ offers, customer, currency_symbol, currentItemsPri
724
798
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
725
799
  }
726
800
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
727
- // TODO:
728
- // if (offer.reward_discount_items == null)
729
- continue;
801
+ if (offer.reward_discount_items == null)
802
+ continue;
803
+ // Check if at least one item is eligible for the discount (not in excluded_refs)
804
+ const excluded_refs = offer.reward_discount_items.excluded_refs ?? [];
805
+ if (excluded_refs.length > 0 && !hasEligibleItemsForDiscount(categories, excluded_refs)) {
806
+ // All items are excluded, offer cannot be unlocked
807
+ stats.progress = 0;
808
+ stats.claimable = false;
809
+ stats.message = `⛔️ No eligible items for this offer ⛔️`;
810
+ offersWithStats.push({ offer, stats });
811
+ continue;
812
+ }
813
+ const applicableQuantity = offer.reward_discount_items.num_of_items || 1;
814
+ const totalOfferItemsSelected = (0, exports.getTotalSelectedOfferItemsAgainstOffer)(categories, offer._id);
815
+ const redeemedTimes = Math.floor(totalOfferItemsSelected / applicableQuantity);
816
+ const maxRedeemableClaims = 1;
817
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
818
+ stats.redeemed_times = redeemedTimes;
819
+ stats.applicable_quantity = applicableQuantity * maxRedeemableClaims;
820
+ if (isClaimable) {
821
+ totalClaimableOffers += 1;
822
+ stats.claimable = true;
823
+ stats.message = `🎉 You have unlocked this offer 🎉`;
824
+ }
825
+ else {
826
+ stats.claimable = false;
827
+ stats.message = `This offer is redeemed ✅`;
828
+ }
829
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
730
830
  }
731
831
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
732
- // TODO:
733
- // if (offer.reward_points == null)
734
- continue;
832
+ if (offer.reward_points == null)
833
+ continue;
834
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
835
+ const isRedeemed = redeemedTimes > 0;
836
+ stats.redeemed_times = redeemedTimes;
837
+ stats.applicable_quantity = 1;
838
+ if (isRedeemed) {
839
+ stats.claimable = false;
840
+ stats.message = 'This offer is redeemed ✅';
841
+ }
842
+ else {
843
+ stats.claimable = true;
844
+ stats.message = `🎉 You have unlocked this offer 🎉`;
845
+ totalClaimableOffers += 1;
846
+ }
847
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
735
848
  }
736
849
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
737
- // TODO:
738
- // if (offer.reward_voucher == null)
739
- continue;
850
+ if (offer.reward_voucher == null)
851
+ continue;
852
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
853
+ const isRedeemed = redeemedTimes > 0;
854
+ stats.redeemed_times = redeemedTimes;
855
+ stats.applicable_quantity = 1;
856
+ if (isRedeemed) {
857
+ stats.claimable = false;
858
+ stats.message = 'This offer is redeemed ✅';
859
+ }
860
+ else {
861
+ stats.claimable = true;
862
+ stats.message = `🎉 You have unlocked this offer 🎉`;
863
+ totalClaimableOffers += 1;
864
+ }
865
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
866
+ }
867
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK) {
868
+ if (offer.reward_cashback == null)
869
+ continue;
870
+ const rewardExcludedRefs = offer.reward_cashback.excluded_refs ?? [];
871
+ if (rewardExcludedRefs.length > 0 && !hasEligibleItemsForDiscount(categories, rewardExcludedRefs)) {
872
+ stats.progress = 0;
873
+ stats.claimable = false;
874
+ stats.message = `⛔️ No eligible items for this offer ⛔️`;
875
+ offersWithStats.push({ offer, stats });
876
+ continue;
877
+ }
878
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
879
+ const isRedeemed = redeemedTimes > 0;
880
+ stats.redeemed_times = redeemedTimes;
881
+ stats.applicable_quantity = 1;
882
+ if (isRedeemed) {
883
+ stats.claimable = false;
884
+ stats.message = 'This offer is redeemed ✅';
885
+ }
886
+ else {
887
+ stats.claimable = true;
888
+ stats.message = `🎉 You have unlocked this offer 🎉`;
889
+ totalClaimableOffers += 1;
890
+ }
891
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
892
+ }
893
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS) {
894
+ if (offer.reward_gift_cards == null)
895
+ continue;
896
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
897
+ const isRedeemed = redeemedTimes > 0;
898
+ stats.redeemed_times = redeemedTimes;
899
+ stats.applicable_quantity = 1;
900
+ if (isRedeemed) {
901
+ stats.claimable = false;
902
+ stats.message = 'This offer is redeemed ✅';
903
+ }
904
+ else {
905
+ stats.claimable = true;
906
+ stats.message = `🎉 You have unlocked this offer 🎉`;
907
+ totalClaimableOffers += 1;
908
+ }
909
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
740
910
  }
741
911
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
742
912
  if (offer.reward_items == null)
@@ -759,104 +929,616 @@ function getOffersWithStats({ offers, customer, currency_symbol, currentItemsPri
759
929
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
760
930
  }
761
931
  }
932
+ stats.action_hint = getActionHint(offer, stats.claimable);
762
933
  offersWithStats.push({
763
934
  offer,
764
935
  stats,
765
936
  });
766
937
  }
767
938
  else if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ITEMS) {
768
- if (offer.pathway_to_unlock_type_items == null)
769
- continue;
770
- if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT) {
771
- // TODO:
772
- // if (offer.reward_discount == null)
773
- continue;
774
- }
775
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
776
- // TODO:
777
- // if (offer.reward_discount_items == null)
778
- continue;
779
- }
780
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
781
- // TODO:
782
- // if (offer.reward_points == null)
783
- continue;
784
- }
785
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
786
- // TODO:
787
- // if (offer.reward_voucher == null) continue;
788
- continue;
789
- }
790
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
791
- // TODO:
792
- // if (offer.reward_items == null)
793
- continue;
794
- }
795
- offersWithStats.push({
796
- offer,
797
- stats,
798
- });
799
- }
800
- else if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
801
- if (offer.pathway_to_unlock_type_points == null)
802
- continue;
803
- if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT) {
804
- // TODO:
805
- // if (offer.reward_discount == null)
806
- continue;
807
- }
808
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
809
- // TODO:
810
- // if (offer.reward_discount_items == null)
939
+ if (offer.pathway_to_unlock_type_items == null) {
940
+ stats.invalid_offer = true;
941
+ stats.message = `Offer pathway to unlock type items is empty`;
942
+ offersWithStats.push({ offer, stats });
811
943
  continue;
812
944
  }
813
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
814
- // TODO:
815
- // if (offer.reward_points == null)
816
- continue;
817
- }
818
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
819
- // TODO:
820
- // if (offer.reward_voucher == null) continue;
821
- continue;
822
- }
823
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
824
- // TODO:
825
- // if (offer.reward_items == null)
826
- continue;
827
- }
828
- offersWithStats.push({
829
- offer,
830
- stats,
945
+ const orderValue = orderPrice - totalDiscount;
946
+ const { num_of_items, max_claims, max_same_item_quantity, total_amount, excluded_refs, exclusive_refs, item_ids, category_ids, } = offer.pathway_to_unlock_type_items;
947
+ const bachelorItems = getCartItemsApplicableToOfferItemsQuantity({
948
+ categories,
949
+ excluded_refs,
950
+ exclusive_refs,
951
+ item_ids,
952
+ category_ids,
953
+ max_same_item_quantity,
831
954
  });
832
- }
833
- else if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
834
- if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT) {
835
- // TODO:
836
- // if (offer.reward_discount == null)
837
- continue;
838
- }
839
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
840
- // TODO:
841
- // if (offer.reward_discount_items == null)
842
- continue;
843
- }
844
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
845
- // TODO:
846
- // if (offer.reward_points == null)
847
- continue;
848
- }
849
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
850
- // TODO:
851
- // if (offer.reward_voucher == null)
852
- continue;
853
- continue;
955
+ const totalOfferItemsSelected = (0, exports.getTotalSelectedOfferItemsAgainstOffer)(categories, offer._id);
956
+ const totalItemsSelected = totalOfferItemsSelected + bachelorItems;
957
+ const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
958
+ const unlockTimesForItems = Math.floor(totalItemsSelected / (num_of_items || 1));
959
+ const unlockTimesForAmount = Math.floor(orderValue / (total_amount || 1));
960
+ const totalUnlockableClaims = Math.min(unlockTimesForItems, unlockTimesForAmount, maxClaimsPerOrder);
961
+ if (totalItemsSelected >= num_of_items) {
962
+ if (orderValue >= total_amount) {
963
+ if (bachelorItems >= num_of_items) {
964
+ stats.progress = 100;
965
+ stats.message = `🎉 You have unlocked this offer 🎉`;
966
+ }
967
+ else {
968
+ stats.progress = Math.round((bachelorItems / (num_of_items || 1)) * 100);
969
+ stats.message = `You are ${num_of_items - bachelorItems} items away from unlocking this offer`;
970
+ }
971
+ }
972
+ else {
973
+ stats.progress = 90;
974
+ stats.message = `You are almost there. Spend ${currency_symbol}${(0, common_1.parseNum)(total_amount - orderValue)} more in this order to unlock this offer 🔓`;
975
+ }
854
976
  }
855
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
856
- // TODO:
857
- // if (offer.reward_items == null)
858
- continue;
977
+ else {
978
+ stats.progress = Math.round((bachelorItems / (num_of_items || 1)) * 100);
979
+ stats.message = `You are ${num_of_items - bachelorItems} items away from unlocking this offer`;
859
980
  }
981
+ if (stats.progress >= 100) {
982
+ if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT) {
983
+ if (offer.reward_discount == null)
984
+ continue;
985
+ const rewardExcludedRefs = offer.reward_discount.excluded_refs ?? [];
986
+ if (rewardExcludedRefs.length > 0 && !hasEligibleItemsForDiscount(categories, rewardExcludedRefs)) {
987
+ stats.progress = 0;
988
+ stats.claimable = false;
989
+ stats.message = `⛔️ No eligible items for this offer ⛔️`;
990
+ offersWithStats.push({ offer, stats });
991
+ continue;
992
+ }
993
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
994
+ const isRedeemed = redeemedTimes > 0;
995
+ stats.redeemed_times = redeemedTimes;
996
+ stats.applicable_quantity = 1;
997
+ stats.claimable = !isRedeemed;
998
+ if (isRedeemed) {
999
+ stats.message = 'This offer is redeemed ✅';
1000
+ }
1001
+ else {
1002
+ totalClaimableOffers += 1;
1003
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1004
+ }
1005
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1006
+ }
1007
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
1008
+ if (offer.reward_discount_items == null)
1009
+ continue;
1010
+ const rewardExcludedRefs = offer.reward_discount_items.excluded_refs ?? [];
1011
+ if (rewardExcludedRefs.length > 0 && !hasEligibleItemsForDiscount(categories, rewardExcludedRefs)) {
1012
+ stats.progress = 0;
1013
+ stats.claimable = false;
1014
+ stats.message = `⛔️ No eligible items for this offer ⛔️`;
1015
+ offersWithStats.push({ offer, stats });
1016
+ continue;
1017
+ }
1018
+ const applicableQuantity = offer.reward_discount_items.num_of_items || 1;
1019
+ const redeemedTimes = Math.floor(totalOfferItemsSelected / applicableQuantity);
1020
+ const isClaimable = redeemedTimes < totalUnlockableClaims;
1021
+ stats.redeemed_times = redeemedTimes;
1022
+ stats.applicable_quantity = applicableQuantity * Math.max(1, totalUnlockableClaims);
1023
+ stats.claimable = isClaimable;
1024
+ if (isClaimable) {
1025
+ totalClaimableOffers += 1;
1026
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1027
+ }
1028
+ else {
1029
+ stats.message = `This offer is redeemed ✅`;
1030
+ }
1031
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1032
+ }
1033
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
1034
+ if (offer.reward_points == null)
1035
+ continue;
1036
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1037
+ const isRedeemed = redeemedTimes > 0;
1038
+ stats.redeemed_times = redeemedTimes;
1039
+ stats.applicable_quantity = 1;
1040
+ stats.claimable = !isRedeemed;
1041
+ if (isRedeemed) {
1042
+ stats.message = 'This offer is redeemed ✅';
1043
+ }
1044
+ else {
1045
+ totalClaimableOffers += 1;
1046
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1047
+ }
1048
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1049
+ }
1050
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
1051
+ if (offer.reward_voucher == null)
1052
+ continue;
1053
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1054
+ const isRedeemed = redeemedTimes > 0;
1055
+ stats.redeemed_times = redeemedTimes;
1056
+ stats.applicable_quantity = 1;
1057
+ stats.claimable = !isRedeemed;
1058
+ if (isRedeemed) {
1059
+ stats.message = 'This offer is redeemed ✅';
1060
+ }
1061
+ else {
1062
+ totalClaimableOffers += 1;
1063
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1064
+ }
1065
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1066
+ }
1067
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK) {
1068
+ if (offer.reward_cashback == null)
1069
+ continue;
1070
+ const rewardExcludedRefs = offer.reward_cashback.excluded_refs ?? [];
1071
+ if (rewardExcludedRefs.length > 0 && !hasEligibleItemsForDiscount(categories, rewardExcludedRefs)) {
1072
+ stats.progress = 0;
1073
+ stats.claimable = false;
1074
+ stats.message = `⛔️ No eligible items for this offer ⛔️`;
1075
+ offersWithStats.push({ offer, stats });
1076
+ continue;
1077
+ }
1078
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1079
+ const isRedeemed = redeemedTimes > 0;
1080
+ stats.redeemed_times = redeemedTimes;
1081
+ stats.applicable_quantity = 1;
1082
+ stats.claimable = !isRedeemed;
1083
+ if (isRedeemed) {
1084
+ stats.message = 'This offer is redeemed ✅';
1085
+ }
1086
+ else {
1087
+ totalClaimableOffers += 1;
1088
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1089
+ }
1090
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1091
+ }
1092
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS) {
1093
+ if (offer.reward_gift_cards == null)
1094
+ continue;
1095
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1096
+ const isRedeemed = redeemedTimes > 0;
1097
+ stats.redeemed_times = redeemedTimes;
1098
+ stats.applicable_quantity = 1;
1099
+ stats.claimable = !isRedeemed;
1100
+ if (isRedeemed) {
1101
+ stats.message = 'This offer is redeemed ✅';
1102
+ }
1103
+ else {
1104
+ totalClaimableOffers += 1;
1105
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1106
+ }
1107
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1108
+ }
1109
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
1110
+ if (offer.reward_items == null)
1111
+ continue;
1112
+ const applicableQuantity = offer.reward_items.num_of_items || 1;
1113
+ const redeemedTimes = Math.floor(totalOfferItemsSelected / applicableQuantity);
1114
+ const isClaimable = redeemedTimes < totalUnlockableClaims;
1115
+ stats.redeemed_times = redeemedTimes;
1116
+ stats.applicable_quantity = applicableQuantity * Math.max(1, totalUnlockableClaims);
1117
+ stats.claimable = isClaimable;
1118
+ if (isClaimable) {
1119
+ totalClaimableOffers += 1;
1120
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1121
+ }
1122
+ else {
1123
+ stats.message = `This offer is redeemed ✅`;
1124
+ }
1125
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1126
+ }
1127
+ }
1128
+ stats.action_hint = getActionHint(offer, stats.claimable);
1129
+ offersWithStats.push({
1130
+ offer,
1131
+ stats,
1132
+ });
1133
+ }
1134
+ else if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
1135
+ if (offer.pathway_to_unlock_type_points == null) {
1136
+ stats.invalid_offer = true;
1137
+ stats.message = `Offer pathway to unlock type points is empty`;
1138
+ offersWithStats.push({ offer, stats });
1139
+ continue;
1140
+ }
1141
+ const orderValue = orderPrice - totalDiscount;
1142
+ const { num_of_points, total_amount, max_claims } = offer.pathway_to_unlock_type_points;
1143
+ const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
1144
+ if (totalLoyaltyPointsOfCustomer == null) {
1145
+ const { addedLoyaltyPoints, loyaltyPoints } = calculateCustomerLoyaltyPoints({
1146
+ customerLoyaltyPoints: customer?.loyalty_points,
1147
+ orderDiscountDetails: order_discount_details,
1148
+ currentItemsPrice,
1149
+ categories,
1150
+ totalDiscountApplied,
1151
+ offers,
1152
+ });
1153
+ totalLoyaltyPointsOfCustomer = loyaltyPoints;
1154
+ totalAddedLoyaltyPoints = addedLoyaltyPoints;
1155
+ }
1156
+ if (totalAddedLoyaltyPoints >= num_of_points) {
1157
+ if (orderValue >= total_amount) {
1158
+ if ((totalLoyaltyPointsOfCustomer ?? 0) >= num_of_points) {
1159
+ stats.progress = 100;
1160
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1161
+ }
1162
+ else {
1163
+ stats.progress = Math.max(0, Math.round(((totalLoyaltyPointsOfCustomer ?? 0) / (num_of_points || 1)) * 100));
1164
+ stats.message = `You are ${(num_of_points - (totalLoyaltyPointsOfCustomer ?? 0)).toFixed(0)} points away from unlocking this offer`;
1165
+ }
1166
+ }
1167
+ else {
1168
+ stats.progress = 90;
1169
+ stats.message = `You are almost there. Spend ${currency_symbol}${(0, common_1.parseNum)((0, common_1.parseNum)(total_amount - orderValue))} more in this order to unlock this offer 🔓`;
1170
+ }
1171
+ }
1172
+ else {
1173
+ stats.progress = Math.max(0, Math.round(((totalLoyaltyPointsOfCustomer ?? 0) / (num_of_points || 1)) * 100));
1174
+ stats.message = `You are ${(num_of_points - (totalLoyaltyPointsOfCustomer ?? 0)).toFixed(0)} points away from unlocking this offer`;
1175
+ }
1176
+ if (stats.progress >= 100) {
1177
+ const pointBurnTimes = Math.floor((totalLoyaltyPointsOfCustomer ?? 0) / (num_of_points || 1));
1178
+ const amountTimes = Math.floor(orderValue / (total_amount || 1));
1179
+ const totalUnlockableClaims = Math.min(pointBurnTimes, amountTimes, maxClaimsPerOrder);
1180
+ if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT) {
1181
+ if (offer.reward_discount == null)
1182
+ continue;
1183
+ const rewardExcludedRefs = offer.reward_discount.excluded_refs ?? [];
1184
+ if (rewardExcludedRefs.length > 0 && !hasEligibleItemsForDiscount(categories, rewardExcludedRefs)) {
1185
+ stats.progress = 0;
1186
+ stats.claimable = false;
1187
+ stats.message = `⛔️ No eligible items for this offer ⛔️`;
1188
+ offersWithStats.push({ offer, stats });
1189
+ continue;
1190
+ }
1191
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1192
+ const isRedeemed = redeemedTimes > 0;
1193
+ stats.redeemed_times = redeemedTimes;
1194
+ stats.applicable_quantity = 1;
1195
+ stats.claimable = !isRedeemed;
1196
+ if (isRedeemed) {
1197
+ stats.message = 'This offer is redeemed ✅';
1198
+ }
1199
+ else {
1200
+ totalClaimableOffers += 1;
1201
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1202
+ }
1203
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1204
+ }
1205
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
1206
+ if (offer.reward_discount_items == null)
1207
+ continue;
1208
+ const rewardExcludedRefs = offer.reward_discount_items.excluded_refs ?? [];
1209
+ if (rewardExcludedRefs.length > 0 && !hasEligibleItemsForDiscount(categories, rewardExcludedRefs)) {
1210
+ stats.progress = 0;
1211
+ stats.claimable = false;
1212
+ stats.message = `⛔️ No eligible items for this offer ⛔️`;
1213
+ offersWithStats.push({ offer, stats });
1214
+ continue;
1215
+ }
1216
+ const totalOfferItemsSelected = (0, exports.getTotalSelectedOfferItemsAgainstOffer)(categories, offer._id);
1217
+ const applicableQuantity = offer.reward_discount_items.num_of_items || 1;
1218
+ const redeemedTimes = Math.floor(totalOfferItemsSelected / applicableQuantity);
1219
+ const isClaimable = redeemedTimes < totalUnlockableClaims;
1220
+ stats.redeemed_times = redeemedTimes;
1221
+ stats.applicable_quantity = applicableQuantity * Math.max(1, totalUnlockableClaims);
1222
+ stats.claimable = isClaimable;
1223
+ if (isClaimable) {
1224
+ totalClaimableOffers += 1;
1225
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1226
+ }
1227
+ else {
1228
+ stats.message = `This offer is redeemed ✅`;
1229
+ }
1230
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1231
+ }
1232
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
1233
+ if (offer.reward_points == null)
1234
+ continue;
1235
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1236
+ const isRedeemed = redeemedTimes > 0;
1237
+ stats.redeemed_times = redeemedTimes;
1238
+ stats.applicable_quantity = 1;
1239
+ stats.claimable = !isRedeemed;
1240
+ if (isRedeemed) {
1241
+ stats.message = 'This offer is redeemed ✅';
1242
+ }
1243
+ else {
1244
+ totalClaimableOffers += 1;
1245
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1246
+ }
1247
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1248
+ }
1249
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
1250
+ if (offer.reward_voucher == null)
1251
+ continue;
1252
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1253
+ const isRedeemed = redeemedTimes > 0;
1254
+ stats.redeemed_times = redeemedTimes;
1255
+ stats.applicable_quantity = 1;
1256
+ stats.claimable = !isRedeemed;
1257
+ if (isRedeemed) {
1258
+ stats.message = 'This offer is redeemed ✅';
1259
+ }
1260
+ else {
1261
+ totalClaimableOffers += 1;
1262
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1263
+ }
1264
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1265
+ }
1266
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK) {
1267
+ if (offer.reward_cashback == null)
1268
+ continue;
1269
+ const rewardExcludedRefs = offer.reward_cashback.excluded_refs ?? [];
1270
+ if (rewardExcludedRefs.length > 0 && !hasEligibleItemsForDiscount(categories, rewardExcludedRefs)) {
1271
+ stats.progress = 0;
1272
+ stats.claimable = false;
1273
+ stats.message = `⛔️ No eligible items for this offer ⛔️`;
1274
+ offersWithStats.push({ offer, stats });
1275
+ continue;
1276
+ }
1277
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1278
+ const isRedeemed = redeemedTimes > 0;
1279
+ stats.redeemed_times = redeemedTimes;
1280
+ stats.applicable_quantity = 1;
1281
+ stats.claimable = !isRedeemed;
1282
+ if (isRedeemed) {
1283
+ stats.message = 'This offer is redeemed ✅';
1284
+ }
1285
+ else {
1286
+ totalClaimableOffers += 1;
1287
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1288
+ }
1289
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1290
+ }
1291
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS) {
1292
+ if (offer.reward_gift_cards == null)
1293
+ continue;
1294
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1295
+ const isRedeemed = redeemedTimes > 0;
1296
+ stats.redeemed_times = redeemedTimes;
1297
+ stats.applicable_quantity = 1;
1298
+ stats.claimable = !isRedeemed;
1299
+ if (isRedeemed) {
1300
+ stats.message = 'This offer is redeemed ✅';
1301
+ }
1302
+ else {
1303
+ totalClaimableOffers += 1;
1304
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1305
+ }
1306
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1307
+ }
1308
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
1309
+ if (offer.reward_items == null)
1310
+ continue;
1311
+ const totalOfferItemsSelected = (0, exports.getTotalSelectedOfferItemsAgainstOffer)(categories, offer._id);
1312
+ const applicableQuantity = offer.reward_items.num_of_items || 1;
1313
+ const redeemedTimes = Math.floor(totalOfferItemsSelected / applicableQuantity);
1314
+ const isClaimable = redeemedTimes < totalUnlockableClaims;
1315
+ stats.redeemed_times = redeemedTimes;
1316
+ stats.applicable_quantity = applicableQuantity * Math.max(1, totalUnlockableClaims);
1317
+ stats.claimable = isClaimable;
1318
+ if (isClaimable) {
1319
+ totalClaimableOffers += 1;
1320
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1321
+ }
1322
+ else {
1323
+ stats.message = `This offer is redeemed ✅`;
1324
+ }
1325
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1326
+ }
1327
+ }
1328
+ stats.action_hint = getActionHint(offer, stats.claimable);
1329
+ offersWithStats.push({
1330
+ offer,
1331
+ stats,
1332
+ });
1333
+ }
1334
+ else if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
1335
+ if (offer.pathway_to_unlock_type_vouchers == null) {
1336
+ stats.invalid_offer = true;
1337
+ stats.claimable = false;
1338
+ stats.message = `Offer pathway to unlock type vouchers is empty`;
1339
+ offersWithStats.push({
1340
+ offer,
1341
+ stats,
1342
+ });
1343
+ continue;
1344
+ }
1345
+ const orderValue = orderPrice - totalDiscount;
1346
+ const { total_amount, max_amount, voucher_ids } = offer.pathway_to_unlock_type_vouchers;
1347
+ const voucherReferenceNumber = order_discount_details?.voucher_reference_number;
1348
+ const normalizedVoucherRef = voucherReferenceNumber?.trim().toUpperCase();
1349
+ const referencedVouchers = voucher_ids
1350
+ .map((voucherId) => vouchersById.get(voucherId))
1351
+ .filter((voucher) => voucher != null);
1352
+ const matchedVoucher = normalizedVoucherRef == null
1353
+ ? null
1354
+ : (referencedVouchers.find((voucher) => voucher.code.trim().toUpperCase() === normalizedVoucherRef) ?? null);
1355
+ const hasMatchingVoucher = matchedVoucher != null;
1356
+ const normalizedMatchedVoucherCode = matchedVoucher?.code.trim().toUpperCase() ?? null;
1357
+ const customerOwnedVoucherCount = normalizedMatchedVoucherCode == null ? 0 : (customerOwnedVoucherCountByCode[normalizedMatchedVoucherCode] ?? 0);
1358
+ const customerVoucherUsageCount = matchedVoucher == null ? 0 : (customerVoucherUsageById[matchedVoucher._id] ?? 0);
1359
+ const voucherUsesPerCustomerExhausted = matchedVoucher?.max_uses_per_customer != null &&
1360
+ customerVoucherUsageCount >= matchedVoucher.max_uses_per_customer;
1361
+ const hasVoucherAccess = matchedVoucher != null && (matchedVoucher.is_public || customerOwnedVoucherCount > 0);
1362
+ if (!hasMatchingVoucher) {
1363
+ stats.progress = 0;
1364
+ stats.message = `Apply a matching voucher to unlock this offer`;
1365
+ }
1366
+ else if (!hasVoucherAccess || voucherUsesPerCustomerExhausted) {
1367
+ stats.progress = 0;
1368
+ stats.message = `⛔️ You are not eligible for this offer ⛔️`;
1369
+ }
1370
+ else if (max_amount != null && orderValue > max_amount) {
1371
+ stats.progress = 0;
1372
+ stats.message = `⛔️ You are not eligible for this offer ⛔️`;
1373
+ }
1374
+ else if (orderValue >= total_amount) {
1375
+ stats.progress = 100;
1376
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1377
+ }
1378
+ else {
1379
+ stats.progress = 90;
1380
+ stats.message = `You are almost there. Spend ${currency_symbol}${(0, common_1.parseNum)(total_amount - orderValue)} more in this order to unlock this offer 🔓`;
1381
+ }
1382
+ if (stats.progress >= 100) {
1383
+ if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT) {
1384
+ if (offer.reward_discount == null)
1385
+ continue;
1386
+ const rewardExcludedRefs = offer.reward_discount.excluded_refs ?? [];
1387
+ if (rewardExcludedRefs.length > 0 && !hasEligibleItemsForDiscount(categories, rewardExcludedRefs)) {
1388
+ stats.progress = 0;
1389
+ stats.claimable = false;
1390
+ stats.message = `⛔️ No eligible items for this offer ⛔️`;
1391
+ offersWithStats.push({ offer, stats });
1392
+ continue;
1393
+ }
1394
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1395
+ const isRedeemed = redeemedTimes > 0;
1396
+ stats.redeemed_times = redeemedTimes;
1397
+ stats.applicable_quantity = 1;
1398
+ stats.claimable = !isRedeemed;
1399
+ if (isRedeemed) {
1400
+ stats.message = 'This offer is redeemed ✅';
1401
+ }
1402
+ else {
1403
+ totalClaimableOffers += 1;
1404
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1405
+ }
1406
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1407
+ }
1408
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
1409
+ if (offer.reward_discount_items == null)
1410
+ continue;
1411
+ const rewardExcludedRefs = offer.reward_discount_items.excluded_refs ?? [];
1412
+ if (rewardExcludedRefs.length > 0 && !hasEligibleItemsForDiscount(categories, rewardExcludedRefs)) {
1413
+ stats.progress = 0;
1414
+ stats.claimable = false;
1415
+ stats.message = `⛔️ No eligible items for this offer ⛔️`;
1416
+ offersWithStats.push({ offer, stats });
1417
+ continue;
1418
+ }
1419
+ const totalOfferItemsSelected = (0, exports.getTotalSelectedOfferItemsAgainstOffer)(categories, offer._id);
1420
+ const applicableQuantity = offer.reward_discount_items.num_of_items || 1;
1421
+ const redeemedTimes = Math.floor(totalOfferItemsSelected / applicableQuantity);
1422
+ const maxRedeemableClaims = 1;
1423
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
1424
+ stats.redeemed_times = redeemedTimes;
1425
+ stats.applicable_quantity = applicableQuantity * maxRedeemableClaims;
1426
+ stats.claimable = isClaimable;
1427
+ if (isClaimable) {
1428
+ totalClaimableOffers += 1;
1429
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1430
+ }
1431
+ else {
1432
+ stats.message = `This offer is redeemed ✅`;
1433
+ }
1434
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1435
+ }
1436
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
1437
+ if (offer.reward_points == null)
1438
+ continue;
1439
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1440
+ const isRedeemed = redeemedTimes > 0;
1441
+ stats.redeemed_times = redeemedTimes;
1442
+ stats.applicable_quantity = 1;
1443
+ stats.claimable = !isRedeemed;
1444
+ if (isRedeemed) {
1445
+ stats.message = 'This offer is redeemed ✅';
1446
+ }
1447
+ else {
1448
+ totalClaimableOffers += 1;
1449
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1450
+ }
1451
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1452
+ }
1453
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
1454
+ if (offer.reward_voucher == null)
1455
+ continue;
1456
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1457
+ const isRedeemed = redeemedTimes > 0;
1458
+ stats.redeemed_times = redeemedTimes;
1459
+ stats.applicable_quantity = 1;
1460
+ stats.claimable = !isRedeemed;
1461
+ if (isRedeemed) {
1462
+ stats.message = 'This offer is redeemed ✅';
1463
+ }
1464
+ else {
1465
+ totalClaimableOffers += 1;
1466
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1467
+ }
1468
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1469
+ }
1470
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK) {
1471
+ if (offer.reward_cashback == null)
1472
+ continue;
1473
+ const rewardExcludedRefs = offer.reward_cashback.excluded_refs ?? [];
1474
+ if (rewardExcludedRefs.length > 0 && !hasEligibleItemsForDiscount(categories, rewardExcludedRefs)) {
1475
+ stats.progress = 0;
1476
+ stats.claimable = false;
1477
+ stats.message = `⛔️ No eligible items for this offer ⛔️`;
1478
+ offersWithStats.push({ offer, stats });
1479
+ continue;
1480
+ }
1481
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1482
+ const isRedeemed = redeemedTimes > 0;
1483
+ stats.redeemed_times = redeemedTimes;
1484
+ stats.applicable_quantity = 1;
1485
+ stats.claimable = !isRedeemed;
1486
+ if (isRedeemed) {
1487
+ stats.message = 'This offer is redeemed ✅';
1488
+ }
1489
+ else {
1490
+ totalClaimableOffers += 1;
1491
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1492
+ }
1493
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1494
+ }
1495
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS) {
1496
+ if (offer.reward_gift_cards == null)
1497
+ continue;
1498
+ const redeemedTimes = getOfferRedeemedTimes(offer._id);
1499
+ const isRedeemed = redeemedTimes > 0;
1500
+ stats.redeemed_times = redeemedTimes;
1501
+ stats.applicable_quantity = 1;
1502
+ stats.claimable = !isRedeemed;
1503
+ if (isRedeemed) {
1504
+ stats.message = 'This offer is redeemed ✅';
1505
+ }
1506
+ else {
1507
+ totalClaimableOffers += 1;
1508
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1509
+ }
1510
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1511
+ }
1512
+ else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
1513
+ if (offer.reward_items == null)
1514
+ continue;
1515
+ const totalOfferItemsSelected = (0, exports.getTotalSelectedOfferItemsAgainstOffer)(categories, offer._id);
1516
+ const applicableQuantity = offer.reward_items.num_of_items || 1;
1517
+ const redeemedTimes = Math.floor(totalOfferItemsSelected / applicableQuantity);
1518
+ const isClaimable = totalOfferItemsSelected < applicableQuantity;
1519
+ stats.redeemed_times = redeemedTimes;
1520
+ stats.applicable_quantity = applicableQuantity;
1521
+ stats.claimable = isClaimable;
1522
+ if (isClaimable) {
1523
+ totalClaimableOffers += 1;
1524
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1525
+ }
1526
+ else {
1527
+ stats.message = `This offer is redeemed ✅`;
1528
+ }
1529
+ unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1530
+ }
1531
+ }
1532
+ stats.action_hint = getActionHint(offer, stats.claimable);
1533
+ offersWithStats.push({
1534
+ offer,
1535
+ stats,
1536
+ });
1537
+ }
1538
+ else {
1539
+ stats.invalid_offer = true;
1540
+ stats.claimable = false;
1541
+ stats.message = `Unsupported pathway to unlock type`;
860
1542
  offersWithStats.push({
861
1543
  offer,
862
1544
  stats,