@managesome/eposmatic-toolkit 0.2.705 → 0.2.706

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.
@@ -989,10 +989,26 @@ function getAutoApplyOfferCandidates({ offersWithStats, suppressedOfferIds, }) {
989
989
  if (!stats.claimable || stats.invalid_offer || !shouldAutoApplyOffer) {
990
990
  continue;
991
991
  }
992
+ // ITEMS / DISCOUNT_ITEMS rewards are claimed manually through the reward
993
+ // drawers and their claimability tracks selected cart items rather than
994
+ // redeemed_offers. No app implements auto-apply for them, and applying via
995
+ // a redeemed_offers patch would leave them permanently claimable, causing
996
+ // the auto-apply effect to loop.
997
+ if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS ||
998
+ offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
999
+ continue;
1000
+ }
992
1001
  if (suppressedOfferIds?.[normalizedOfferId]) {
993
1002
  continue;
994
1003
  }
995
- const claimsIncrement = offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS
1004
+ // For repeatable POINTS and flat CASHBACK, claimsIncrement covers ALL
1005
+ // remaining claims in a single patch so the next stats snapshot flips
1006
+ // claimable to false. Callers MUST forward claimsIncrement into
1007
+ // buildRedeemedOffersClaimPatch; otherwise redeemed_times advances by 1
1008
+ // per cycle and the auto-apply effect loops until React throws
1009
+ // "Maximum update depth exceeded".
1010
+ const claimsIncrement = offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS ||
1011
+ (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK && offer.reward_cashback?.is_cashback_percentage !== true)
996
1012
  ? getOfferClaimIncrementFromStats({
997
1013
  stats,
998
1014
  })
@@ -1744,15 +1760,6 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1744
1760
  return true;
1745
1761
  return getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, offerToCheck._id) >= maxUsesPerCustomer;
1746
1762
  };
1747
- const getRemainingOfferUsesForCustomer = (offerToCheck) => {
1748
- const maxUsesPerCustomer = getOfferMaxUsesPerCustomer(offerToCheck);
1749
- if (maxUsesPerCustomer == null)
1750
- return Infinity;
1751
- if (customerOfferRedemptionCounts == null)
1752
- return 0;
1753
- const redeemedByCustomer = getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, offerToCheck._id);
1754
- return Math.max(0, maxUsesPerCustomer - redeemedByCustomer);
1755
- };
1756
1763
  const getOrderValueClaimTimes = (orderValue, minAmount) => {
1757
1764
  const safeRequiredAmount = Math.max(1, minAmount || 1);
1758
1765
  return Math.max(1, Math.floor(orderValue / safeRequiredAmount));
@@ -1788,14 +1795,20 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1788
1795
  if (normalizedUnlockableClaims === 0) {
1789
1796
  return 0;
1790
1797
  }
1791
- return Math.min(normalizedUnlockableClaims, getRemainingOfferUsesForCustomer(offerToCheck));
1798
+ // max_uses_per_customer is a lifetime gate checked separately against
1799
+ // the customer's settled-claim history (wallet USED counter,
1800
+ // isOfferUsesPerCustomerExhausted); it must not shrink the per-order
1801
+ // claim count — max_claims governs the ongoing session only.
1802
+ return normalizedUnlockableClaims;
1792
1803
  };
1793
- const getPathwayMaxRedeemableClaims = ({ offerToCheck, pathwayUnlockableClaims, }) => {
1804
+ const getPathwayMaxRedeemableClaims = (pathwayUnlockableClaims) => {
1794
1805
  const normalizedUnlockableClaims = Math.max(0, pathwayUnlockableClaims);
1795
1806
  if (normalizedUnlockableClaims === 0) {
1796
1807
  return 0;
1797
1808
  }
1798
- return Math.min(normalizedUnlockableClaims, getRemainingOfferUsesForCustomer(offerToCheck));
1809
+ // Same lifetime-vs-session separation as getPointsMaxRedeemableClaims:
1810
+ // max_uses_per_customer only gates on settled-claim history.
1811
+ return normalizedUnlockableClaims;
1799
1812
  };
1800
1813
  const isDiscountSlotOccupiedForOffer = (offerToCheck) => {
1801
1814
  if (order_discount_details == null) {
@@ -1986,11 +1999,15 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1986
1999
  totalOfferItemsSelected,
1987
2000
  applicableQuantity,
1988
2001
  });
1989
- const maxRedeemableClaims = 1;
2002
+ const maxClaimsPerOrder = offer.pathway_to_unlock_type_orders?.max_claims == null
2003
+ ? Infinity
2004
+ : Math.max(1, offer.pathway_to_unlock_type_orders.max_claims);
2005
+ const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, offer.pathway_to_unlock_type_orders?.total_amount);
2006
+ const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
1990
2007
  const discountSlotOccupied = isDiscountSlotOccupiedForOffer(offer);
1991
- const isClaimable = redeemedTimes < maxRedeemableClaims && !discountSlotOccupied;
2008
+ const isClaimable = redeemedTimes < totalUnlockableClaims && !discountSlotOccupied;
1992
2009
  stats.redeemed_times = redeemedTimes;
1993
- stats.applicable_quantity = applicableQuantity * maxRedeemableClaims;
2010
+ stats.applicable_quantity = applicableQuantity * Math.max(1, totalUnlockableClaims);
1994
2011
  if (isClaimable) {
1995
2012
  totalClaimableOffers.push(offer._id);
1996
2013
  stats.claimable = true;
@@ -1999,7 +2016,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1999
2016
  else {
2000
2017
  stats.claimable = false;
2001
2018
  stats.message =
2002
- redeemedTimes > 0
2019
+ redeemedTimes >= totalUnlockableClaims
2003
2020
  ? `This offer is redeemed ✅`
2004
2021
  : discountSlotOccupied
2005
2022
  ? 'You already have an offer applied to your order.'
@@ -2072,25 +2089,41 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2072
2089
  continue;
2073
2090
  }
2074
2091
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2075
- const maxClaimsPerOrder = offer.pathway_to_unlock_type_orders?.max_claims == null
2076
- ? Infinity
2077
- : Math.max(1, offer.pathway_to_unlock_type_orders.max_claims);
2078
- const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, offer.pathway_to_unlock_type_orders?.total_amount);
2079
- const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
2080
- const maxRedeemableClaims = getPathwayMaxRedeemableClaims({
2081
- offerToCheck: offer,
2082
- pathwayUnlockableClaims: totalUnlockableClaims,
2083
- });
2084
- const isClaimable = redeemedTimes < maxRedeemableClaims;
2085
- stats.redeemed_times = redeemedTimes;
2086
- stats.applicable_quantity = maxRedeemableClaims;
2087
- stats.claimable = isClaimable;
2088
- if (!isClaimable) {
2089
- stats.message = 'This offer is redeemed ✅';
2092
+ if (offer.reward_cashback.is_cashback_percentage === true) {
2093
+ // Percentage cashback scales with the order total and is claimed
2094
+ // once per order; total_amount / max_claims do not apply.
2095
+ const isRedeemed = redeemedTimes > 0;
2096
+ stats.redeemed_times = redeemedTimes;
2097
+ stats.applicable_quantity = 1;
2098
+ stats.claimable = !isRedeemed;
2099
+ if (isRedeemed) {
2100
+ stats.message = 'This offer is redeemed ✅';
2101
+ }
2102
+ else {
2103
+ stats.message = `🎉 You have unlocked this offer 🎉`;
2104
+ totalClaimableOffers.push(offer._id);
2105
+ }
2090
2106
  }
2091
2107
  else {
2092
- stats.message = `🎉 You have unlocked this offer 🎉`;
2093
- totalClaimableOffers.push(offer._id);
2108
+ // Flat cashback repeats per qualifying spend chunk
2109
+ // (every total_amount unlocks one claim), capped by max_claims.
2110
+ const maxClaimsPerOrder = offer.pathway_to_unlock_type_orders?.max_claims == null
2111
+ ? Infinity
2112
+ : Math.max(1, offer.pathway_to_unlock_type_orders.max_claims);
2113
+ const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, offer.pathway_to_unlock_type_orders?.total_amount);
2114
+ const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
2115
+ const maxRedeemableClaims = getPathwayMaxRedeemableClaims(totalUnlockableClaims);
2116
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
2117
+ stats.redeemed_times = redeemedTimes;
2118
+ stats.applicable_quantity = maxRedeemableClaims;
2119
+ stats.claimable = isClaimable;
2120
+ if (!isClaimable) {
2121
+ stats.message = 'This offer is redeemed ✅';
2122
+ }
2123
+ else {
2124
+ stats.message = `🎉 You have unlocked this offer 🎉`;
2125
+ totalClaimableOffers.push(offer._id);
2126
+ }
2094
2127
  }
2095
2128
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
2096
2129
  }
@@ -2130,9 +2163,14 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2130
2163
  totalOfferItemsSelected,
2131
2164
  applicableQuantity,
2132
2165
  });
2133
- const isClaimable = redeemedTimes < 1;
2166
+ const maxClaimsPerOrder = offer.pathway_to_unlock_type_orders?.max_claims == null
2167
+ ? Infinity
2168
+ : Math.max(1, offer.pathway_to_unlock_type_orders.max_claims);
2169
+ const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, offer.pathway_to_unlock_type_orders?.total_amount);
2170
+ const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
2171
+ const isClaimable = redeemedTimes < totalUnlockableClaims;
2134
2172
  stats.redeemed_times = redeemedTimes;
2135
- stats.applicable_quantity = applicableQuantity;
2173
+ stats.applicable_quantity = applicableQuantity * Math.max(1, totalUnlockableClaims);
2136
2174
  if (isClaimable) {
2137
2175
  totalClaimableOffers.push(offer._id);
2138
2176
  stats.claimable = true;
@@ -2339,20 +2377,36 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2339
2377
  continue;
2340
2378
  }
2341
2379
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2342
- const maxRedeemableClaims = getPathwayMaxRedeemableClaims({
2343
- offerToCheck: offer,
2344
- pathwayUnlockableClaims: totalUnlockableClaims,
2345
- });
2346
- const isClaimable = redeemedTimes < maxRedeemableClaims;
2347
- stats.redeemed_times = redeemedTimes;
2348
- stats.applicable_quantity = maxRedeemableClaims;
2349
- stats.claimable = isClaimable;
2350
- if (!isClaimable) {
2351
- stats.message = 'This offer is redeemed ✅';
2380
+ if (offer.reward_cashback.is_cashback_percentage === true) {
2381
+ // Percentage cashback scales with the order total and is claimed
2382
+ // once per order; total_amount / max_claims do not apply.
2383
+ const isRedeemed = redeemedTimes > 0;
2384
+ stats.redeemed_times = redeemedTimes;
2385
+ stats.applicable_quantity = 1;
2386
+ stats.claimable = !isRedeemed;
2387
+ if (isRedeemed) {
2388
+ stats.message = 'This offer is redeemed ✅';
2389
+ }
2390
+ else {
2391
+ totalClaimableOffers.push(offer._id);
2392
+ stats.message = `🎉 You have unlocked this offer 🎉`;
2393
+ }
2352
2394
  }
2353
2395
  else {
2354
- totalClaimableOffers.push(offer._id);
2355
- stats.message = `🎉 You have unlocked this offer 🎉`;
2396
+ // Flat cashback repeats per qualifying spend chunk
2397
+ // (every total_amount unlocks one claim), capped by max_claims.
2398
+ const maxRedeemableClaims = getPathwayMaxRedeemableClaims(totalUnlockableClaims);
2399
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
2400
+ stats.redeemed_times = redeemedTimes;
2401
+ stats.applicable_quantity = maxRedeemableClaims;
2402
+ stats.claimable = isClaimable;
2403
+ if (!isClaimable) {
2404
+ stats.message = 'This offer is redeemed ✅';
2405
+ }
2406
+ else {
2407
+ totalClaimableOffers.push(offer._id);
2408
+ stats.message = `🎉 You have unlocked this offer 🎉`;
2409
+ }
2356
2410
  }
2357
2411
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
2358
2412
  }
@@ -2596,20 +2650,36 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2596
2650
  continue;
2597
2651
  }
2598
2652
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2599
- const maxRedeemableClaims = getPathwayMaxRedeemableClaims({
2600
- offerToCheck: offer,
2601
- pathwayUnlockableClaims: totalUnlockableClaims,
2602
- });
2603
- const isClaimable = redeemedTimes < maxRedeemableClaims;
2604
- stats.redeemed_times = redeemedTimes;
2605
- stats.applicable_quantity = maxRedeemableClaims;
2606
- stats.claimable = isClaimable;
2607
- if (!isClaimable) {
2608
- stats.message = 'This offer is redeemed ✅';
2653
+ if (offer.reward_cashback.is_cashback_percentage === true) {
2654
+ // Percentage cashback scales with the order total and is claimed
2655
+ // once per order; total_amount / max_claims do not apply.
2656
+ const isRedeemed = redeemedTimes > 0;
2657
+ stats.redeemed_times = redeemedTimes;
2658
+ stats.applicable_quantity = 1;
2659
+ stats.claimable = !isRedeemed;
2660
+ if (isRedeemed) {
2661
+ stats.message = 'This offer is redeemed ✅';
2662
+ }
2663
+ else {
2664
+ totalClaimableOffers.push(offer._id);
2665
+ stats.message = `🎉 You have unlocked this offer 🎉`;
2666
+ }
2609
2667
  }
2610
2668
  else {
2611
- totalClaimableOffers.push(offer._id);
2612
- stats.message = `🎉 You have unlocked this offer 🎉`;
2669
+ // Flat cashback repeats per qualifying spend chunk
2670
+ // (every total_amount unlocks one claim), capped by max_claims.
2671
+ const maxRedeemableClaims = getPathwayMaxRedeemableClaims(totalUnlockableClaims);
2672
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
2673
+ stats.redeemed_times = redeemedTimes;
2674
+ stats.applicable_quantity = maxRedeemableClaims;
2675
+ stats.claimable = isClaimable;
2676
+ if (!isClaimable) {
2677
+ stats.message = 'This offer is redeemed ✅';
2678
+ }
2679
+ else {
2680
+ totalClaimableOffers.push(offer._id);
2681
+ stats.message = `🎉 You have unlocked this offer 🎉`;
2682
+ }
2613
2683
  }
2614
2684
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
2615
2685
  }
@@ -2814,10 +2884,13 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2814
2884
  totalOfferItemsSelected,
2815
2885
  applicableQuantity,
2816
2886
  });
2817
- const maxRedeemableClaims = 1;
2818
- const isClaimable = redeemedTimes < maxRedeemableClaims;
2887
+ const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
2888
+ const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
2889
+ const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
2890
+ const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
2891
+ const isClaimable = redeemedTimes < totalUnlockableClaims;
2819
2892
  stats.redeemed_times = redeemedTimes;
2820
- stats.applicable_quantity = applicableQuantity * maxRedeemableClaims;
2893
+ stats.applicable_quantity = applicableQuantity * Math.max(1, totalUnlockableClaims);
2821
2894
  stats.claimable = isClaimable;
2822
2895
  if (isClaimable) {
2823
2896
  totalClaimableOffers.push(offer._id);
@@ -2889,24 +2962,41 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2889
2962
  continue;
2890
2963
  }
2891
2964
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2892
- const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
2893
- const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
2894
- const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
2895
- const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
2896
- const maxRedeemableClaims = getPathwayMaxRedeemableClaims({
2897
- offerToCheck: offer,
2898
- pathwayUnlockableClaims: totalUnlockableClaims,
2899
- });
2900
- const isClaimable = redeemedTimes < maxRedeemableClaims;
2901
- stats.redeemed_times = redeemedTimes;
2902
- stats.applicable_quantity = maxRedeemableClaims;
2903
- stats.claimable = isClaimable;
2904
- if (!isClaimable) {
2905
- stats.message = 'This offer is redeemed ✅';
2965
+ if (offer.reward_cashback.is_cashback_percentage === true) {
2966
+ // Percentage cashback scales with the order total and is claimed
2967
+ // once per order; total_amount / max_claims do not apply.
2968
+ const isRedeemed = redeemedTimes > 0;
2969
+ stats.redeemed_times = redeemedTimes;
2970
+ stats.applicable_quantity = 1;
2971
+ stats.claimable = !isRedeemed;
2972
+ if (isRedeemed) {
2973
+ stats.message = 'This offer is redeemed ✅';
2974
+ }
2975
+ else {
2976
+ totalClaimableOffers.push(offer._id);
2977
+ stats.message = `🎉 You have unlocked this offer 🎉`;
2978
+ }
2906
2979
  }
2907
2980
  else {
2908
- totalClaimableOffers.push(offer._id);
2909
- stats.message = `🎉 You have unlocked this offer 🎉`;
2981
+ // Flat cashback repeats per qualifying spend chunk
2982
+ // (every total_amount unlocks one claim), capped by max_claims
2983
+ // and the voucher inventory available for this order.
2984
+ const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
2985
+ const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
2986
+ const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
2987
+ const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
2988
+ const maxRedeemableClaims = getPathwayMaxRedeemableClaims(totalUnlockableClaims);
2989
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
2990
+ stats.redeemed_times = redeemedTimes;
2991
+ stats.applicable_quantity = maxRedeemableClaims;
2992
+ stats.claimable = isClaimable;
2993
+ if (!isClaimable) {
2994
+ stats.message = 'This offer is redeemed ✅';
2995
+ }
2996
+ else {
2997
+ totalClaimableOffers.push(offer._id);
2998
+ stats.message = `🎉 You have unlocked this offer 🎉`;
2999
+ }
2910
3000
  }
2911
3001
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
2912
3002
  }
@@ -2943,9 +3033,13 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2943
3033
  totalOfferItemsSelected,
2944
3034
  applicableQuantity,
2945
3035
  });
2946
- const isClaimable = redeemedTimes < 1;
3036
+ const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
3037
+ const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
3038
+ const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
3039
+ const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
3040
+ const isClaimable = redeemedTimes < totalUnlockableClaims;
2947
3041
  stats.redeemed_times = redeemedTimes;
2948
- stats.applicable_quantity = applicableQuantity;
3042
+ stats.applicable_quantity = applicableQuantity * Math.max(1, totalUnlockableClaims);
2949
3043
  stats.claimable = isClaimable;
2950
3044
  if (isClaimable) {
2951
3045
  totalClaimableOffers.push(offer._id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.705",
3
+ "version": "0.2.706",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",