@managesome/eposmatic-toolkit 0.2.290 → 0.2.292

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.
@@ -2,16 +2,31 @@ import { OFFER_REWARD_TYPE_ENUM, PLATFORMS_ENUM } from '../../constants';
2
2
  import { ICustomer, IDiscountDetails, IGiftCard, IOffer, IOrder, IOrderCategory, ITenant, IVoucher } from '../../schemas';
3
3
  export declare function getOfferWithPrice<T extends Partial<IOffer> | IOffer>(offer: T, writeable?: boolean): T;
4
4
  export declare function convertOffersToWorkableOffers(offers: IOffer[], platform: PLATFORMS_ENUM): IOffer[];
5
- export declare function calculatePointsRedeemedForOffer(redeemedOffer: IOffer, totalOrderPrice: number, categories: IOrderCategory[] | undefined): number;
5
+ export declare function calculatePointsRedeemedForOffer(redeemedOffer: IOffer, totalOrderPrice: number, categories: IOrderCategory[] | undefined, claimedTimes?: number): number;
6
6
  interface ICalculateCustomerLoyaltyPoints {
7
7
  customerLoyaltyPoints: number | undefined;
8
8
  orderDiscountDetails: IDiscountDetails | null | undefined;
9
+ redeemedOffers?: IOrder['redeemed_offers'] | null | undefined;
9
10
  currentItemsPrice: number | undefined;
10
11
  totalDiscountApplied: number | undefined;
11
12
  categories: IOrderCategory[] | undefined;
12
13
  offers: IOffer[];
14
+ includePendingRewardPoints?: boolean;
13
15
  }
14
- export declare function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, currentItemsPrice, totalDiscountApplied, categories, offers, }: ICalculateCustomerLoyaltyPoints): {
16
+ interface ICalculateCustomerSpendableVoucherCodes {
17
+ customerVoucherCodes: ICustomer['voucher_codes'] | null | undefined;
18
+ orderDiscountDetails: IDiscountDetails | null | undefined;
19
+ redeemedOffers?: IOrder['redeemed_offers'] | null | undefined;
20
+ offers: IOffer[];
21
+ vouchers: IVoucher[] | undefined;
22
+ }
23
+ export declare function calculateCustomerSpendableVoucherCodes({ customerVoucherCodes, orderDiscountDetails, redeemedOffers, offers, vouchers, }: ICalculateCustomerSpendableVoucherCodes): {
24
+ voucherCodes: Record<string, number>;
25
+ totalVouchers: number;
26
+ activeVoucherCode: string | null;
27
+ consumedVoucherClaims: number;
28
+ };
29
+ export declare function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, redeemedOffers, currentItemsPrice, totalDiscountApplied, categories, offers, includePendingRewardPoints, }: ICalculateCustomerLoyaltyPoints): {
15
30
  loyaltyPoints: number;
16
31
  addedLoyaltyPoints: number;
17
32
  deductedLoyaltyPoints: number;
@@ -4,6 +4,7 @@ exports.getAppliedOfferIds = exports.getTotalSelectedOfferItemsAgainstOffer = vo
4
4
  exports.getOfferWithPrice = getOfferWithPrice;
5
5
  exports.convertOffersToWorkableOffers = convertOffersToWorkableOffers;
6
6
  exports.calculatePointsRedeemedForOffer = calculatePointsRedeemedForOffer;
7
+ exports.calculateCustomerSpendableVoucherCodes = calculateCustomerSpendableVoucherCodes;
7
8
  exports.calculateCustomerLoyaltyPoints = calculateCustomerLoyaltyPoints;
8
9
  exports.getOffersWithStats = getOffersWithStats;
9
10
  const constants_1 = require("../../constants");
@@ -215,12 +216,16 @@ function convertOffersToWorkableOffers(offers, platform) {
215
216
  return filteredOffers;
216
217
  }
217
218
  // ====================================================================================================================
218
- function calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories) {
219
+ function calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories, claimedTimes) {
219
220
  if (redeemedOffer.reward_type !== constants_1.OFFER_REWARD_TYPE_ENUM.POINTS || redeemedOffer.reward_points == null) {
220
221
  return 0;
221
222
  }
222
223
  let times = 1;
223
224
  if (!redeemedOffer.reward_points.once_per_order) {
225
+ const getUnlockTimesByOrderAmount = (orderValue, totalAmount) => {
226
+ const safeRequiredAmount = Math.max(1, totalAmount || 1);
227
+ return Math.max(1, Math.floor(orderValue / safeRequiredAmount));
228
+ };
224
229
  // This means that we need to see if the condition to unlock the redeemedOffer is met multiple times
225
230
  // i.e. if the customer has ordered 2 pizzas, and the redeemedOffer is applicable for 1 pizza
226
231
  // then we need to calculate the points redeemed for 2 pizzas
@@ -254,23 +259,114 @@ function calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categor
254
259
  }
255
260
  else if (redeemedOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS) {
256
261
  if (redeemedOffer.pathway_to_unlock_type_orders != null) {
257
- const { total_amount } = redeemedOffer.pathway_to_unlock_type_orders;
258
- if (totalOrderPrice >= total_amount) {
259
- // ORDERS pathway does not define max_claims, so it can be redeemed only once per order.
260
- times = 1;
262
+ const { total_amount, max_amount, max_claims } = redeemedOffer.pathway_to_unlock_type_orders;
263
+ if (totalOrderPrice >= total_amount && (max_amount == null || totalOrderPrice <= max_amount)) {
264
+ const timesForAmount = getUnlockTimesByOrderAmount(totalOrderPrice, total_amount);
265
+ const maxClaims = max_claims == null ? Infinity : Math.max(1, max_claims);
266
+ times = Math.min(timesForAmount, maxClaims);
267
+ }
268
+ }
269
+ }
270
+ else if (redeemedOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
271
+ if (redeemedOffer.pathway_to_unlock_type_vouchers != null) {
272
+ const { total_amount, max_amount, max_claims } = redeemedOffer.pathway_to_unlock_type_vouchers;
273
+ if (totalOrderPrice >= total_amount && (max_amount == null || totalOrderPrice <= max_amount)) {
274
+ const timesForAmount = getUnlockTimesByOrderAmount(totalOrderPrice, total_amount);
275
+ const maxClaims = max_claims == null ? Infinity : Math.max(1, max_claims);
276
+ times = Math.min(timesForAmount, maxClaims);
261
277
  }
262
278
  }
263
279
  }
264
280
  // 💡 NOTE: We don't need to check for pathway_to_unlock_type_points because it is not possible to have
265
281
  // pathway_to_unlock_type_points and reward_points at the same time
266
- // TODO: Add pathway_to_unlock_type_vouchers
282
+ }
283
+ if (claimedTimes != null && Number.isFinite(claimedTimes) && claimedTimes > 0) {
284
+ times = Math.min(times, Math.floor(claimedTimes));
267
285
  }
268
286
  let pointsRedeemed = redeemedOffer.reward_points.num_of_points ?? 0;
269
287
  pointsRedeemed *= times;
270
288
  return pointsRedeemed;
271
289
  }
290
+ function normalizeVoucherCode(voucherCode) {
291
+ const normalizedCode = voucherCode?.trim().toUpperCase();
292
+ return normalizedCode != null && normalizedCode.length > 0 ? normalizedCode : null;
293
+ }
294
+ function getOfferRedeemedClaims(redeemedOffers, offerId) {
295
+ const redeemedTimes = Number(redeemedOffers?.[offerId]);
296
+ if (Number.isFinite(redeemedTimes) && redeemedTimes > 0) {
297
+ return Math.floor(redeemedTimes);
298
+ }
299
+ return 0;
300
+ }
301
+ function calculateCustomerSpendableVoucherCodes({ customerVoucherCodes, orderDiscountDetails, redeemedOffers, offers, vouchers, }) {
302
+ const voucherCodes = {};
303
+ if (customerVoucherCodes != null) {
304
+ const entries = Object.entries(customerVoucherCodes);
305
+ for (let i = 0; i < entries.length; i++) {
306
+ const [code, count] = entries[i];
307
+ const normalizedCode = normalizeVoucherCode(code);
308
+ if (normalizedCode == null)
309
+ continue;
310
+ const parsedCount = Number(count);
311
+ if (!Number.isFinite(parsedCount) || parsedCount <= 0)
312
+ continue;
313
+ voucherCodes[normalizedCode] = Math.floor(parsedCount);
314
+ }
315
+ }
316
+ const activeVoucherCode = normalizeVoucherCode(orderDiscountDetails?.voucher_reference_number);
317
+ if (activeVoucherCode == null || vouchers == null || offers.length === 0) {
318
+ return {
319
+ voucherCodes,
320
+ totalVouchers: Object.values(voucherCodes).reduce((acc, count) => acc + count, 0),
321
+ activeVoucherCode,
322
+ consumedVoucherClaims: 0,
323
+ };
324
+ }
325
+ const activeVoucherIds = new Set();
326
+ for (let i = 0; i < vouchers.length; i++) {
327
+ const voucher = vouchers[i];
328
+ if (normalizeVoucherCode(voucher.code) === activeVoucherCode) {
329
+ activeVoucherIds.add(voucher._id);
330
+ }
331
+ }
332
+ let consumedVoucherClaims = 0;
333
+ if (activeVoucherIds.size > 0) {
334
+ for (let i = 0; i < offers.length; i++) {
335
+ const offer = offers[i];
336
+ if (offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
337
+ continue;
338
+ }
339
+ const pathwayVouchers = offer.pathway_to_unlock_type_vouchers;
340
+ if (pathwayVouchers == null || pathwayVouchers.voucher_ids.length === 0) {
341
+ continue;
342
+ }
343
+ let supportsActiveVoucherCode = false;
344
+ for (let j = 0; j < pathwayVouchers.voucher_ids.length; j++) {
345
+ if (activeVoucherIds.has(pathwayVouchers.voucher_ids[j])) {
346
+ supportsActiveVoucherCode = true;
347
+ break;
348
+ }
349
+ }
350
+ if (!supportsActiveVoucherCode)
351
+ continue;
352
+ const redeemedTimes = getOfferRedeemedClaims(redeemedOffers, offer._id);
353
+ if (redeemedTimes > 0) {
354
+ consumedVoucherClaims += redeemedTimes;
355
+ }
356
+ }
357
+ }
358
+ if (voucherCodes[activeVoucherCode] != null && consumedVoucherClaims > 0) {
359
+ voucherCodes[activeVoucherCode] = Math.max(0, voucherCodes[activeVoucherCode] - consumedVoucherClaims);
360
+ }
361
+ return {
362
+ voucherCodes,
363
+ totalVouchers: Object.values(voucherCodes).reduce((acc, count) => acc + count, 0),
364
+ activeVoucherCode,
365
+ consumedVoucherClaims,
366
+ };
367
+ }
272
368
  // The purpose of this function is to calculate the total number of points that are applied/consumed by the cart items
273
- function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, currentItemsPrice, totalDiscountApplied, categories, offers, }) {
369
+ function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, redeemedOffers, currentItemsPrice, totalDiscountApplied, categories, offers, includePendingRewardPoints = false, }) {
274
370
  const totalOrderPrice = (currentItemsPrice ?? 0) - (totalDiscountApplied ?? 0);
275
371
  let loyaltyPoints = customerLoyaltyPoints ?? 0;
276
372
  let addedLoyaltyPoints = loyaltyPoints;
@@ -287,13 +383,15 @@ function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDe
287
383
  offerConsumedQuantity[redeemedOffer._id] = 0;
288
384
  }
289
385
  if (!offerConsumedQuantity[redeemedOffer._id]) {
290
- if (redeemedOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
291
- const points = calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories);
386
+ const rawClaimedTimes = Number(redeemedOffers?.[redeemedOffer._id]);
387
+ const claimedTimes = Number.isFinite(rawClaimedTimes) && rawClaimedTimes > 0 ? Math.floor(rawClaimedTimes) : 1;
388
+ if (redeemedOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS && includePendingRewardPoints) {
389
+ const points = calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories, claimedTimes);
292
390
  loyaltyPoints += points;
293
391
  addedLoyaltyPoints += points;
294
392
  }
295
393
  else if (redeemedOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
296
- const points = redeemedOffer.pathway_to_unlock_type_points?.num_of_points ?? 0;
394
+ const points = (redeemedOffer.pathway_to_unlock_type_points?.num_of_points ?? 0) * claimedTimes;
297
395
  loyaltyPoints -= points;
298
396
  deductedLoyaltyPoints += points;
299
397
  }
@@ -691,24 +789,14 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
691
789
  const unlockedOffersApplicableTimes = {};
692
790
  let totalClaimableOffers = 0;
693
791
  let totalLoyaltyPointsOfCustomer = null;
694
- let totalAddedLoyaltyPoints = 0;
695
792
  const nowTs = Date.now();
696
- const customerOwnedVoucherCountByCode = {};
697
- const customerVoucherCodes = customer?.voucher_codes;
698
- if (customerVoucherCodes != null) {
699
- const voucherCodeEntries = Object.entries(customerVoucherCodes);
700
- for (let i = 0; i < voucherCodeEntries.length; i++) {
701
- const [voucherCode, count] = voucherCodeEntries[i];
702
- const normalizedCode = voucherCode.trim().toUpperCase();
703
- if (normalizedCode.length === 0)
704
- continue;
705
- const parsedCount = Number(count);
706
- if (!Number.isFinite(parsedCount) || parsedCount <= 0)
707
- continue;
708
- customerOwnedVoucherCountByCode[normalizedCode] =
709
- (customerOwnedVoucherCountByCode[normalizedCode] ?? 0) + parsedCount;
710
- }
711
- }
793
+ const { voucherCodes: spendableVoucherCodesByCode } = calculateCustomerSpendableVoucherCodes({
794
+ customerVoucherCodes: customer?.voucher_codes,
795
+ orderDiscountDetails: order_discount_details,
796
+ redeemedOffers: redeemed_offers,
797
+ offers,
798
+ vouchers,
799
+ });
712
800
  const redeemedOffersById = redeemed_offers ?? null;
713
801
  const vouchersById = new Map();
714
802
  if (vouchers != null) {
@@ -809,6 +897,30 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
809
897
  const maxUsesPerCustomer = getOfferMaxUsesPerCustomer(offerToCheck);
810
898
  return maxUsesPerCustomer != null && (customerOfferRedemptionCounts?.[offerToCheck._id] ?? 0) >= maxUsesPerCustomer;
811
899
  };
900
+ const getRemainingOfferUsesForCustomer = (offerToCheck) => {
901
+ const maxUsesPerCustomer = getOfferMaxUsesPerCustomer(offerToCheck);
902
+ if (maxUsesPerCustomer == null)
903
+ return Infinity;
904
+ const redeemedByCustomer = customerOfferRedemptionCounts?.[offerToCheck._id] ?? 0;
905
+ return Math.max(0, maxUsesPerCustomer - redeemedByCustomer);
906
+ };
907
+ const getOrderValueClaimTimes = (orderValue, minAmount) => {
908
+ const safeRequiredAmount = Math.max(1, minAmount || 1);
909
+ return Math.max(1, Math.floor(orderValue / safeRequiredAmount));
910
+ };
911
+ const getPointsMaxRedeemableClaims = ({ offerToCheck, pathwayUnlockableClaims, }) => {
912
+ if (offerToCheck.reward_type !== constants_1.OFFER_REWARD_TYPE_ENUM.POINTS || offerToCheck.reward_points == null) {
913
+ return 1;
914
+ }
915
+ if (offerToCheck.reward_points.once_per_order) {
916
+ return 1;
917
+ }
918
+ const normalizedUnlockableClaims = Math.max(0, pathwayUnlockableClaims);
919
+ if (normalizedUnlockableClaims === 0) {
920
+ return 0;
921
+ }
922
+ return Math.min(normalizedUnlockableClaims, getRemainingOfferUsesForCustomer(offerToCheck));
923
+ };
812
924
  for (let i = 0; i < offers.length; i++) {
813
925
  const offer = offers[i];
814
926
  const stats = {
@@ -946,15 +1058,23 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
946
1058
  if (offer.reward_points == null)
947
1059
  continue;
948
1060
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
949
- const isRedeemed = redeemedTimes > 0;
1061
+ const maxClaimsPerOrder = offer.pathway_to_unlock_type_orders?.max_claims == null
1062
+ ? Infinity
1063
+ : Math.max(1, offer.pathway_to_unlock_type_orders.max_claims);
1064
+ const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, offer.pathway_to_unlock_type_orders?.total_amount);
1065
+ const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
1066
+ const maxRedeemableClaims = getPointsMaxRedeemableClaims({
1067
+ offerToCheck: offer,
1068
+ pathwayUnlockableClaims: totalUnlockableClaims,
1069
+ });
1070
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
950
1071
  stats.redeemed_times = redeemedTimes;
951
- stats.applicable_quantity = 1;
952
- if (isRedeemed) {
953
- stats.claimable = false;
1072
+ stats.applicable_quantity = maxRedeemableClaims;
1073
+ stats.claimable = isClaimable;
1074
+ if (!isClaimable) {
954
1075
  stats.message = 'This offer is redeemed ✅';
955
1076
  }
956
1077
  else {
957
- stats.claimable = true;
958
1078
  stats.message = `🎉 You have unlocked this offer 🎉`;
959
1079
  totalClaimableOffers += 1;
960
1080
  }
@@ -1199,11 +1319,15 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1199
1319
  if (offer.reward_points == null)
1200
1320
  continue;
1201
1321
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1202
- const isRedeemed = redeemedTimes > 0;
1322
+ const maxRedeemableClaims = getPointsMaxRedeemableClaims({
1323
+ offerToCheck: offer,
1324
+ pathwayUnlockableClaims: totalUnlockableClaims,
1325
+ });
1326
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
1203
1327
  stats.redeemed_times = redeemedTimes;
1204
- stats.applicable_quantity = 1;
1205
- stats.claimable = !isRedeemed;
1206
- if (isRedeemed) {
1328
+ stats.applicable_quantity = maxRedeemableClaims;
1329
+ stats.claimable = isClaimable;
1330
+ if (!isClaimable) {
1207
1331
  stats.message = 'This offer is redeemed ✅';
1208
1332
  }
1209
1333
  else {
@@ -1349,21 +1473,22 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1349
1473
  const { num_of_points, total_amount, max_claims } = offer.pathway_to_unlock_type_points;
1350
1474
  const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
1351
1475
  if (totalLoyaltyPointsOfCustomer == null) {
1352
- const { addedLoyaltyPoints, loyaltyPoints } = calculateCustomerLoyaltyPoints({
1476
+ const { loyaltyPoints } = calculateCustomerLoyaltyPoints({
1353
1477
  customerLoyaltyPoints: customer?.loyalty_points,
1354
1478
  orderDiscountDetails: order_discount_details,
1479
+ redeemedOffers: redeemed_offers,
1355
1480
  currentItemsPrice,
1356
1481
  categories,
1357
1482
  totalDiscountApplied,
1358
1483
  offers,
1484
+ includePendingRewardPoints: false,
1359
1485
  });
1360
1486
  totalLoyaltyPointsOfCustomer = loyaltyPoints;
1361
- totalAddedLoyaltyPoints = addedLoyaltyPoints;
1362
1487
  }
1363
1488
  const pointsProgress = getPreUnlockProgressPercentage(totalLoyaltyPointsOfCustomer ?? 0, num_of_points || 1);
1364
1489
  const amountProgress = getPreUnlockProgressPercentage(orderValue, total_amount || 1);
1365
1490
  const combinedProgress = Math.min(pointsProgress, amountProgress);
1366
- if (totalAddedLoyaltyPoints >= num_of_points) {
1491
+ if ((totalLoyaltyPointsOfCustomer ?? 0) >= num_of_points) {
1367
1492
  if (orderValue >= total_amount) {
1368
1493
  if ((totalLoyaltyPointsOfCustomer ?? 0) >= num_of_points) {
1369
1494
  stats.progress = 100;
@@ -1446,11 +1571,15 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1446
1571
  if (offer.reward_points == null)
1447
1572
  continue;
1448
1573
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1449
- const isRedeemed = redeemedTimes > 0;
1574
+ const maxRedeemableClaims = getPointsMaxRedeemableClaims({
1575
+ offerToCheck: offer,
1576
+ pathwayUnlockableClaims: totalUnlockableClaims,
1577
+ });
1578
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
1450
1579
  stats.redeemed_times = redeemedTimes;
1451
- stats.applicable_quantity = 1;
1452
- stats.claimable = !isRedeemed;
1453
- if (isRedeemed) {
1580
+ stats.applicable_quantity = maxRedeemableClaims;
1581
+ stats.claimable = isClaimable;
1582
+ if (!isClaimable) {
1454
1583
  stats.message = 'This offer is redeemed ✅';
1455
1584
  }
1456
1585
  else {
@@ -1598,7 +1727,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1598
1727
  continue;
1599
1728
  }
1600
1729
  const orderValue = orderPrice - totalDiscount;
1601
- const { total_amount, max_amount, voucher_ids } = offer.pathway_to_unlock_type_vouchers;
1730
+ const { total_amount, max_amount, max_claims, voucher_ids } = offer.pathway_to_unlock_type_vouchers;
1602
1731
  const voucherReferenceNumber = order_discount_details?.voucher_reference_number;
1603
1732
  const normalizedVoucherRef = voucherReferenceNumber?.trim().toUpperCase();
1604
1733
  const referencedVouchers = voucher_ids
@@ -1609,11 +1738,12 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1609
1738
  : (referencedVouchers.find((voucher) => voucher.code.trim().toUpperCase() === normalizedVoucherRef) ?? null);
1610
1739
  const hasMatchingVoucher = matchedVoucher != null;
1611
1740
  const normalizedMatchedVoucherCode = matchedVoucher?.code.trim().toUpperCase() ?? null;
1612
- const customerOwnedVoucherCount = normalizedMatchedVoucherCode == null ? 0 : (customerOwnedVoucherCountByCode[normalizedMatchedVoucherCode] ?? 0);
1741
+ const redeemedTimesForOffer = getOfferRedeemedTimes(offer._id);
1742
+ const spendableVoucherCount = normalizedMatchedVoucherCode == null ? 0 : (spendableVoucherCodesByCode[normalizedMatchedVoucherCode] ?? 0);
1613
1743
  const customerVoucherUsageCount = matchedVoucher == null ? 0 : (customerOfferRedemptionCounts?.[matchedVoucher._id] ?? 0);
1614
1744
  const voucherUsesPerCustomerExhausted = matchedVoucher?.max_uses_per_customer != null &&
1615
1745
  customerVoucherUsageCount >= matchedVoucher.max_uses_per_customer;
1616
- const hasVoucherAccess = matchedVoucher != null && (matchedVoucher.is_public || customerOwnedVoucherCount > 0);
1746
+ const hasVoucherAccess = matchedVoucher != null && (matchedVoucher.is_public || spendableVoucherCount > 0 || redeemedTimesForOffer > 0);
1617
1747
  const amountProgress = getPreUnlockProgressPercentage(orderValue, total_amount || 1);
1618
1748
  if (!hasMatchingVoucher) {
1619
1749
  stats.progress = 0;
@@ -1696,11 +1826,21 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1696
1826
  if (offer.reward_points == null)
1697
1827
  continue;
1698
1828
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1699
- const isRedeemed = redeemedTimes > 0;
1829
+ const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
1830
+ const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
1831
+ const voucherInventoryClaimCap = matchedVoucher != null && !matchedVoucher.is_public
1832
+ ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer)
1833
+ : Infinity;
1834
+ const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
1835
+ const maxRedeemableClaims = getPointsMaxRedeemableClaims({
1836
+ offerToCheck: offer,
1837
+ pathwayUnlockableClaims: totalUnlockableClaims,
1838
+ });
1839
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
1700
1840
  stats.redeemed_times = redeemedTimes;
1701
- stats.applicable_quantity = 1;
1702
- stats.claimable = !isRedeemed;
1703
- if (isRedeemed) {
1841
+ stats.applicable_quantity = maxRedeemableClaims;
1842
+ stats.claimable = isClaimable;
1843
+ if (!isClaimable) {
1704
1844
  stats.message = 'This offer is redeemed ✅';
1705
1845
  }
1706
1846
  else {
@@ -380,6 +380,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
380
380
  min_num_of_orders: number;
381
381
  max_amount?: number | null | undefined;
382
382
  max_uses_per_customer?: number | null | undefined;
383
+ max_claims?: number | null | undefined;
383
384
  max_num_of_orders?: number | null | undefined;
384
385
  } | null | undefined;
385
386
  pathway_to_unlock_type_points?: {
@@ -393,6 +394,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
393
394
  total_amount: number;
394
395
  max_amount?: number | null | undefined;
395
396
  max_uses_per_customer?: number | null | undefined;
397
+ max_claims?: number | null | undefined;
396
398
  } | null | undefined;
397
399
  reward_gift_cards?: {
398
400
  gift_card_ids: Map<string, number>;
@@ -773,6 +775,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
773
775
  min_num_of_orders: number;
774
776
  max_amount?: number | null | undefined;
775
777
  max_uses_per_customer?: number | null | undefined;
778
+ max_claims?: number | null | undefined;
776
779
  max_num_of_orders?: number | null | undefined;
777
780
  } | null | undefined;
778
781
  pathway_to_unlock_type_points?: {
@@ -786,6 +789,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
786
789
  total_amount: number;
787
790
  max_amount?: number | null | undefined;
788
791
  max_uses_per_customer?: number | null | undefined;
792
+ max_claims?: number | null | undefined;
789
793
  } | null | undefined;
790
794
  reward_gift_cards?: {
791
795
  gift_card_ids: Map<string, number>;
@@ -1166,6 +1170,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
1166
1170
  min_num_of_orders: number;
1167
1171
  max_amount?: number | null | undefined;
1168
1172
  max_uses_per_customer?: number | null | undefined;
1173
+ max_claims?: number | null | undefined;
1169
1174
  max_num_of_orders?: number | null | undefined;
1170
1175
  } | null | undefined;
1171
1176
  pathway_to_unlock_type_points?: {
@@ -1179,6 +1184,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
1179
1184
  total_amount: number;
1180
1185
  max_amount?: number | null | undefined;
1181
1186
  max_uses_per_customer?: number | null | undefined;
1187
+ max_claims?: number | null | undefined;
1182
1188
  } | null | undefined;
1183
1189
  reward_gift_cards?: {
1184
1190
  gift_card_ids: Map<string, number>;
@@ -68,6 +68,11 @@ const PathwayToUnlockTypeOrdersSchema = new mongoose_1.Schema({
68
68
  required: false,
69
69
  default: null,
70
70
  },
71
+ max_claims: {
72
+ type: Number,
73
+ required: false,
74
+ default: null,
75
+ },
71
76
  min_num_of_orders: {
72
77
  type: Number,
73
78
  required: true,
@@ -128,6 +133,11 @@ const PathwayToUnlockTypeVouchersSchema = new mongoose_1.Schema({
128
133
  required: false,
129
134
  default: null,
130
135
  },
136
+ max_claims: {
137
+ type: Number,
138
+ required: false,
139
+ default: null,
140
+ },
131
141
  voucher_ids: {
132
142
  type: [mongoose_1.Schema.Types.ObjectId],
133
143
  required: true,
@@ -3,6 +3,7 @@ import { IItemSpecialHour } from './common';
3
3
  export interface IPathwayToUnlockTypeOrders {
4
4
  scope: PATHWAY_TO_UNLOCK_TYPE_ORDERS_SCOPE_ENUM;
5
5
  max_uses_per_customer: number | null;
6
+ max_claims: number | null;
6
7
  min_num_of_orders: number;
7
8
  max_num_of_orders: number | null;
8
9
  total_amount: number;
@@ -30,6 +31,7 @@ export interface IPathwayToUnlockTypeVouchers {
30
31
  total_amount: number;
31
32
  max_amount: number | null;
32
33
  max_uses_per_customer: number | null;
34
+ max_claims: number | null;
33
35
  voucher_ids: string[];
34
36
  }
35
37
  type IOfferPathwayItems = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.290",
3
+ "version": "0.2.292",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",