@managesome/eposmatic-toolkit 0.2.339 → 0.2.341

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.
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DEFAULT_WALLET_CASHBACK_PERCENTAGE_STEPS = exports.getTotalSelectedOfferItemsAgainstOffer = void 0;
4
4
  exports.getOfferWithPrice = getOfferWithPrice;
5
5
  exports.convertOffersToWorkableOffers = convertOffersToWorkableOffers;
6
+ exports.getVoucherPathwayCartContext = getVoucherPathwayCartContext;
7
+ exports.isVoucherPathwayOfferBlockedByCartContext = isVoucherPathwayOfferBlockedByCartContext;
6
8
  exports.isExhaustedVoucherOffer = isExhaustedVoucherOffer;
7
9
  exports.findLinkedVoucherOffers = findLinkedVoucherOffers;
8
10
  exports.buildClearVoucherDiscountDetails = buildClearVoucherDiscountDetails;
@@ -323,6 +325,69 @@ function getOfferRedeemedClaims(redeemedOffers, offerId) {
323
325
  }
324
326
  return 0;
325
327
  }
328
+ function getVoucherPathwayCartContext({ offers, vouchers, redeemedOffers, orderDiscountDetails, }) {
329
+ const consumedVoucherCodes = new Set();
330
+ const consumedVoucherIds = new Set();
331
+ const redeemedOfferIds = new Set();
332
+ const voucherCodeById = new Map();
333
+ const referencedVoucherCode = normalizeVoucherCode(orderDiscountDetails?.voucher_reference_number);
334
+ if (vouchers != null) {
335
+ for (let i = 0; i < vouchers.length; i++) {
336
+ const voucher = vouchers[i];
337
+ const normalizedVoucherCode = normalizeVoucherCode(voucher.code);
338
+ if (normalizedVoucherCode == null)
339
+ continue;
340
+ voucherCodeById.set(voucher._id, normalizedVoucherCode);
341
+ }
342
+ }
343
+ for (let i = 0; i < offers.length; i++) {
344
+ const offer = offers[i];
345
+ if (offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
346
+ continue;
347
+ }
348
+ const redeemedTimes = getOfferRedeemedClaims(redeemedOffers, offer._id);
349
+ if (redeemedTimes <= 0) {
350
+ continue;
351
+ }
352
+ const pathwayVouchers = offer.pathway_to_unlock_type_vouchers;
353
+ if (pathwayVouchers == null || pathwayVouchers.voucher_ids.length === 0) {
354
+ continue;
355
+ }
356
+ redeemedOfferIds.add(offer._id);
357
+ const linkedVoucherCodes = new Set();
358
+ for (let j = 0; j < pathwayVouchers.voucher_ids.length; j++) {
359
+ const voucherId = pathwayVouchers.voucher_ids[j];
360
+ consumedVoucherIds.add(voucherId);
361
+ const linkedVoucherCode = voucherCodeById.get(voucherId);
362
+ if (linkedVoucherCode == null)
363
+ continue;
364
+ linkedVoucherCodes.add(linkedVoucherCode);
365
+ }
366
+ if (referencedVoucherCode != null && linkedVoucherCodes.has(referencedVoucherCode)) {
367
+ consumedVoucherCodes.add(referencedVoucherCode);
368
+ continue;
369
+ }
370
+ for (const linkedVoucherCode of linkedVoucherCodes) {
371
+ consumedVoucherCodes.add(linkedVoucherCode);
372
+ }
373
+ }
374
+ const inferredVoucherCode = referencedVoucherCode ?? (consumedVoucherCodes.size === 1 ? consumedVoucherCodes.values().next().value : null);
375
+ return {
376
+ activeVoucherCode: inferredVoucherCode ?? null,
377
+ consumedVoucherCodes,
378
+ consumedVoucherIds,
379
+ redeemedOfferIds,
380
+ };
381
+ }
382
+ function isVoucherPathwayOfferBlockedByCartContext({ offer, cartContext, }) {
383
+ if (offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
384
+ return false;
385
+ }
386
+ if (cartContext.redeemedOfferIds.size === 0) {
387
+ return false;
388
+ }
389
+ return !cartContext.redeemedOfferIds.has(offer._id);
390
+ }
326
391
  // ================================><================================= //
327
392
  // === isExhaustedVoucherOffer === //
328
393
  // ================================><================================= //
@@ -344,7 +409,8 @@ function isExhaustedVoucherOffer({ offer, customerOfferRedemptionCounts, redeeme
344
409
  if (!isThisOfferRedeemed)
345
410
  return false;
346
411
  const pathwayMaxUsesPerCustomer = offer.pathway_to_unlock_type_vouchers.max_uses_per_customer;
347
- return (pathwayMaxUsesPerCustomer != null && (customerOfferRedemptionCounts[offer._id] ?? 0) >= pathwayMaxUsesPerCustomer);
412
+ return (pathwayMaxUsesPerCustomer != null &&
413
+ getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, offer._id) >= pathwayMaxUsesPerCustomer);
348
414
  }
349
415
  // ================================><================================= //
350
416
  // === findLinkedVoucherOffers === //
@@ -399,7 +465,7 @@ function calculateCustomerSpendableVoucherCodes({ customerVoucherCodes, orderDis
399
465
  }
400
466
  }
401
467
  const activeVoucherCode = normalizeVoucherCode(orderDiscountDetails?.voucher_reference_number);
402
- if (activeVoucherCode == null || vouchers == null || offers.length === 0) {
468
+ if (vouchers == null || offers.length === 0 || redeemedOffers == null) {
403
469
  return {
404
470
  voucherCodes,
405
471
  totalVouchers: Object.values(voucherCodes).reduce((acc, count) => acc + count, 0),
@@ -407,45 +473,59 @@ function calculateCustomerSpendableVoucherCodes({ customerVoucherCodes, orderDis
407
473
  consumedVoucherClaims: 0,
408
474
  };
409
475
  }
410
- const activeVoucherIds = new Set();
476
+ const voucherCodeById = new Map();
411
477
  for (let i = 0; i < vouchers.length; i++) {
412
478
  const voucher = vouchers[i];
413
- if (normalizeVoucherCode(voucher.code) === activeVoucherCode) {
414
- activeVoucherIds.add(voucher._id);
415
- }
479
+ const normalizedVoucherCode = normalizeVoucherCode(voucher.code);
480
+ if (normalizedVoucherCode == null)
481
+ continue;
482
+ voucherCodeById.set(voucher._id, normalizedVoucherCode);
416
483
  }
417
- let consumedVoucherClaims = 0;
418
- if (activeVoucherIds.size > 0) {
419
- for (let i = 0; i < offers.length; i++) {
420
- const offer = offers[i];
421
- if (offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
422
- continue;
423
- }
424
- const pathwayVouchers = offer.pathway_to_unlock_type_vouchers;
425
- if (pathwayVouchers == null || pathwayVouchers.voucher_ids.length === 0) {
426
- continue;
427
- }
428
- let supportsActiveVoucherCode = false;
429
- for (let j = 0; j < pathwayVouchers.voucher_ids.length; j++) {
430
- if (activeVoucherIds.has(pathwayVouchers.voucher_ids[j])) {
431
- supportsActiveVoucherCode = true;
432
- break;
433
- }
434
- }
435
- if (!supportsActiveVoucherCode)
484
+ const consumedVoucherClaimsByCode = {};
485
+ for (let i = 0; i < offers.length; i++) {
486
+ const offer = offers[i];
487
+ if (offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
488
+ continue;
489
+ }
490
+ const pathwayVouchers = offer.pathway_to_unlock_type_vouchers;
491
+ if (pathwayVouchers == null || pathwayVouchers.voucher_ids.length === 0) {
492
+ continue;
493
+ }
494
+ const redeemedTimes = getOfferRedeemedClaims(redeemedOffers, offer._id);
495
+ if (redeemedTimes <= 0) {
496
+ continue;
497
+ }
498
+ const linkedVoucherCodes = new Set();
499
+ for (let j = 0; j < pathwayVouchers.voucher_ids.length; j++) {
500
+ const linkedVoucherCode = voucherCodeById.get(pathwayVouchers.voucher_ids[j]);
501
+ if (linkedVoucherCode == null)
436
502
  continue;
437
- const redeemedTimes = getOfferRedeemedClaims(redeemedOffers, offer._id);
438
- if (redeemedTimes > 0) {
439
- // Count 1 voucher use per redeemed offer (not redeemedTimes which
440
- // can be > 1 for repeat claims). Each distinct offer consumes one
441
- // voucher code use, regardless of how many times it was redeemed.
442
- consumedVoucherClaims += 1;
443
- }
503
+ linkedVoucherCodes.add(linkedVoucherCode);
504
+ }
505
+ if (linkedVoucherCodes.size === 0) {
506
+ continue;
444
507
  }
508
+ const [singleLinkedVoucherCode] = linkedVoucherCodes;
509
+ const targetVoucherCode = linkedVoucherCodes.size === 1
510
+ ? singleLinkedVoucherCode
511
+ : activeVoucherCode != null && linkedVoucherCodes.has(activeVoucherCode)
512
+ ? activeVoucherCode
513
+ : null;
514
+ if (targetVoucherCode == null) {
515
+ continue;
516
+ }
517
+ consumedVoucherClaimsByCode[targetVoucherCode] =
518
+ (consumedVoucherClaimsByCode[targetVoucherCode] ?? 0) + redeemedTimes;
445
519
  }
446
- if (voucherCodes[activeVoucherCode] != null && consumedVoucherClaims > 0) {
447
- voucherCodes[activeVoucherCode] = Math.max(0, voucherCodes[activeVoucherCode] - consumedVoucherClaims);
520
+ const consumedVoucherCodes = Object.keys(consumedVoucherClaimsByCode);
521
+ for (let i = 0; i < consumedVoucherCodes.length; i++) {
522
+ const voucherCode = consumedVoucherCodes[i];
523
+ if (voucherCodes[voucherCode] == null) {
524
+ continue;
525
+ }
526
+ voucherCodes[voucherCode] = Math.max(0, voucherCodes[voucherCode] - consumedVoucherClaimsByCode[voucherCode]);
448
527
  }
528
+ const consumedVoucherClaims = activeVoucherCode != null ? (consumedVoucherClaimsByCode[activeVoucherCode] ?? 0) : 0;
449
529
  return {
450
530
  voucherCodes,
451
531
  totalVouchers: Object.values(voucherCodes).reduce((acc, count) => acc + count, 0),
@@ -794,6 +874,27 @@ function toPositiveInteger(value, fallback = 0) {
794
874
  }
795
875
  return Math.floor(parsedValue);
796
876
  }
877
+ function getCustomerRedemptionCount(customerOfferRedemptionCounts, type, id) {
878
+ const normalizedId = normalizeOfferId(id);
879
+ if (customerOfferRedemptionCounts == null || normalizedId == null) {
880
+ return 0;
881
+ }
882
+ const typedCount = customerOfferRedemptionCounts[type]?.[normalizedId];
883
+ if (typedCount != null) {
884
+ return toPositiveInteger(typedCount, 0);
885
+ }
886
+ const legacyFlatCounts = customerOfferRedemptionCounts;
887
+ return toPositiveInteger(legacyFlatCounts[normalizedId], 0);
888
+ }
889
+ function getRewardMaxAssignmentsPerCustomer(rewardResource) {
890
+ return rewardResource.max_assignments_per_customer ?? rewardResource.max_uses_per_customer ?? null;
891
+ }
892
+ function getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, id) {
893
+ return getCustomerRedemptionCount(customerOfferRedemptionCounts, constants_1.CUSTOMER_REDEMPTION_TYPE_ENUM.USED, id);
894
+ }
895
+ function getCustomerAssignedRedemptionCount(customerOfferRedemptionCounts, id) {
896
+ return getCustomerRedemptionCount(customerOfferRedemptionCounts, constants_1.CUSTOMER_REDEMPTION_TYPE_ENUM.ASSIGNED, id);
897
+ }
797
898
  function getOfferPathwayMaxUsesPerCustomer(offer) {
798
899
  if (offer == null) {
799
900
  return null;
@@ -834,7 +935,7 @@ function getOfferConsumedUses({ offer, usageLimit, customerOfferRedemptionCounts
834
935
  if (usageKey == null) {
835
936
  return null;
836
937
  }
837
- return toPositiveInteger(customerOfferRedemptionCounts[usageKey], 0);
938
+ return getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, usageKey);
838
939
  }
839
940
  function isOfferFullyConsumedForCustomer({ offer, usageLimit, customerOfferRedemptionCounts, }) {
840
941
  const consumedUses = getOfferConsumedUses({
@@ -991,8 +1092,13 @@ function normalizeWalletRedemptions(walletRedemptions) {
991
1092
  if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
992
1093
  normalizedWalletRedemptions.cashback_amount = (0, common_1.parseNum)(cashbackAmount);
993
1094
  }
994
- const giftCardRedemptions = walletRedemptions.gift_cards;
995
- if (Array.isArray(giftCardRedemptions)) {
1095
+ const rawGiftCardRedemptions = walletRedemptions.gift_cards;
1096
+ const giftCardRedemptions = rawGiftCardRedemptions == null
1097
+ ? []
1098
+ : Array.isArray(rawGiftCardRedemptions)
1099
+ ? rawGiftCardRedemptions
1100
+ : [rawGiftCardRedemptions];
1101
+ if (giftCardRedemptions.length > 0) {
996
1102
  const normalizedGiftCardRedemptionsMap = {};
997
1103
  for (let i = 0; i < giftCardRedemptions.length; i++) {
998
1104
  const giftCardRedemption = giftCardRedemptions[i];
@@ -1292,7 +1398,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1292
1398
  let totalClaimableOffers = [];
1293
1399
  let totalLoyaltyPointsOfCustomer = null;
1294
1400
  const nowTs = Date.now();
1295
- const isVoucherRewardResourceUnavailable = (voucher) => {
1401
+ const isVoucherUseResourceUnavailable = (voucher) => {
1296
1402
  if (voucher == null) {
1297
1403
  return true;
1298
1404
  }
@@ -1300,13 +1406,21 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1300
1406
  const voucherUsesExhausted = voucher.max_uses != null && voucher.used_count >= voucher.max_uses;
1301
1407
  return !voucher.active || voucherIsExpired || voucherUsesExhausted;
1302
1408
  };
1409
+ const isVoucherRewardResourceUnavailable = (voucher) => {
1410
+ if (voucher == null) {
1411
+ return true;
1412
+ }
1413
+ const voucherIsExpired = voucher.expire_at != null && new Date(voucher.expire_at).getTime() <= nowTs;
1414
+ const voucherAssignmentsExhausted = voucher.max_assignments != null && voucher.assigned_count >= voucher.max_assignments;
1415
+ return !voucher.active || voucherIsExpired || voucherAssignmentsExhausted;
1416
+ };
1303
1417
  const isGiftCardRewardResourceUnavailable = (giftCard) => {
1304
1418
  if (giftCard == null) {
1305
1419
  return true;
1306
1420
  }
1307
1421
  const giftCardIsExpired = giftCard.expire_at != null && new Date(giftCard.expire_at).getTime() <= nowTs;
1308
- const giftCardUsesExhausted = giftCard.max_uses != null && giftCard.used_count >= giftCard.max_uses;
1309
- return !giftCard.active || giftCardIsExpired || giftCardUsesExhausted;
1422
+ const giftCardAssignmentsExhausted = giftCard.max_assignments != null && giftCard.assigned_count >= giftCard.max_assignments;
1423
+ return !giftCard.active || giftCardIsExpired || giftCardAssignmentsExhausted;
1310
1424
  };
1311
1425
  const { voucherCodes: spendableVoucherCodesByCode } = calculateCustomerSpendableVoucherCodes({
1312
1426
  customerVoucherCodes: customer?.voucher_codes,
@@ -1315,6 +1429,12 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1315
1429
  offers,
1316
1430
  vouchers,
1317
1431
  });
1432
+ const voucherPathwayCartContext = getVoucherPathwayCartContext({
1433
+ offers,
1434
+ vouchers,
1435
+ redeemedOffers: redeemed_offers,
1436
+ orderDiscountDetails: order_discount_details,
1437
+ });
1318
1438
  const redeemedOffersById = redeemed_offers ?? null;
1319
1439
  const vouchersById = new Map();
1320
1440
  const allVouchersById = new Map();
@@ -1322,7 +1442,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1322
1442
  for (let i = 0; i < vouchers.length; i++) {
1323
1443
  const voucher = vouchers[i];
1324
1444
  allVouchersById.set(voucher._id, voucher);
1325
- if (isVoucherRewardResourceUnavailable(voucher)) {
1445
+ if (isVoucherUseResourceUnavailable(voucher)) {
1326
1446
  continue;
1327
1447
  }
1328
1448
  vouchersById.set(voucher._id, voucher);
@@ -1349,9 +1469,11 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1349
1469
  rewardVoucherUnavailable = true;
1350
1470
  break;
1351
1471
  }
1352
- if (customerOfferRedemptionCounts != null &&
1353
- rewardVoucher.max_uses_per_customer != null &&
1354
- (customerOfferRedemptionCounts[rewardVoucher._id] ?? 0) >= rewardVoucher.max_uses_per_customer) {
1472
+ const maxAssignmentsPerCustomer = getRewardMaxAssignmentsPerCustomer(rewardVoucher);
1473
+ if (maxAssignmentsPerCustomer != null &&
1474
+ (customerOfferRedemptionCounts == null ||
1475
+ getCustomerAssignedRedemptionCount(customerOfferRedemptionCounts, rewardVoucher._id) >=
1476
+ maxAssignmentsPerCustomer)) {
1355
1477
  rewardVoucherLimitExhausted = true;
1356
1478
  break;
1357
1479
  }
@@ -1370,9 +1492,11 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1370
1492
  rewardGiftCardUnavailable = true;
1371
1493
  break;
1372
1494
  }
1373
- if (customerOfferRedemptionCounts != null &&
1374
- rewardGiftCard.max_uses_per_customer != null &&
1375
- (customerOfferRedemptionCounts[rewardGiftCard._id] ?? 0) >= rewardGiftCard.max_uses_per_customer) {
1495
+ const maxAssignmentsPerCustomer = getRewardMaxAssignmentsPerCustomer(rewardGiftCard);
1496
+ if (maxAssignmentsPerCustomer != null &&
1497
+ (customerOfferRedemptionCounts == null ||
1498
+ getCustomerAssignedRedemptionCount(customerOfferRedemptionCounts, rewardGiftCard._id) >=
1499
+ maxAssignmentsPerCustomer)) {
1376
1500
  rewardGiftCardLimitExhausted = true;
1377
1501
  break;
1378
1502
  }
@@ -1382,6 +1506,19 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1382
1506
  rewardGiftCardLimitExhausted,
1383
1507
  };
1384
1508
  };
1509
+ const clearBlockedRewardApplicableQuantity = (offerToCheck, statsToUpdate) => {
1510
+ if (statsToUpdate.claimable ||
1511
+ (offerToCheck.reward_type !== constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER &&
1512
+ offerToCheck.reward_type !== constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS)) {
1513
+ return;
1514
+ }
1515
+ if (statsToUpdate.message !== '⛔️ This reward is no longer available ⛔️' &&
1516
+ statsToUpdate.message !== '⛔️ You have reached your personal limit for this reward ⛔️') {
1517
+ return;
1518
+ }
1519
+ statsToUpdate.applicable_quantity = 0;
1520
+ unlockedOffersApplicableTimes[offerToCheck._id] = 0;
1521
+ };
1385
1522
  const getOfferRedeemedTimes = (offerId) => {
1386
1523
  const redeemedTimes = Number(redeemedOffersById?.[offerId]);
1387
1524
  if (Number.isFinite(redeemedTimes) && redeemedTimes > 0) {
@@ -1459,7 +1596,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1459
1596
  return false;
1460
1597
  if (customerOfferRedemptionCounts == null)
1461
1598
  return true;
1462
- return (customerOfferRedemptionCounts[offerToCheck._id] ?? 0) >= maxUsesPerCustomer;
1599
+ return getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, offerToCheck._id) >= maxUsesPerCustomer;
1463
1600
  };
1464
1601
  const getRemainingOfferUsesForCustomer = (offerToCheck) => {
1465
1602
  const maxUsesPerCustomer = getOfferMaxUsesPerCustomer(offerToCheck);
@@ -1467,7 +1604,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1467
1604
  return Infinity;
1468
1605
  if (customerOfferRedemptionCounts == null)
1469
1606
  return 0;
1470
- const redeemedByCustomer = customerOfferRedemptionCounts[offerToCheck._id] ?? 0;
1607
+ const redeemedByCustomer = getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, offerToCheck._id);
1471
1608
  return Math.max(0, maxUsesPerCustomer - redeemedByCustomer);
1472
1609
  };
1473
1610
  const getOrderValueClaimTimes = (orderValue, minAmount) => {
@@ -1547,6 +1684,25 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1547
1684
  totalClaimableOffers.push(offerToCheck._id);
1548
1685
  }
1549
1686
  };
1687
+ const applyVoucherPathwayCartContextBlock = ({ offerToCheck, statsToUpdate, }) => {
1688
+ if (!isVoucherPathwayOfferBlockedByCartContext({
1689
+ offer: offerToCheck,
1690
+ cartContext: voucherPathwayCartContext,
1691
+ })) {
1692
+ return;
1693
+ }
1694
+ if (!statsToUpdate.claimable && statsToUpdate.applicable_quantity <= 0 && statsToUpdate.progress < 100) {
1695
+ return;
1696
+ }
1697
+ totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offerToCheck._id);
1698
+ statsToUpdate.claimable = false;
1699
+ statsToUpdate.applicable_quantity = 0;
1700
+ unlockedOffersApplicableTimes[offerToCheck._id] = 0;
1701
+ statsToUpdate.message =
1702
+ voucherPathwayCartContext.activeVoucherCode != null
1703
+ ? `You already selected a reward for voucher ${voucherPathwayCartContext.activeVoucherCode}.`
1704
+ : 'You already selected a voucher reward for this order.';
1705
+ };
1550
1706
  for (let i = 0; i < offers.length; i++) {
1551
1707
  const offer = offers[i];
1552
1708
  const stats = {
@@ -1729,22 +1885,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1729
1885
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
1730
1886
  if (offer.reward_voucher == null)
1731
1887
  continue;
1732
- const vIds = Object.keys(offer.reward_voucher.voucher_ids);
1733
- let rewardVoucherUnavailable = false;
1734
- let rewardVoucherLimitExhausted = false;
1735
- for (let vIdx = 0; vIdx < vIds.length; vIdx++) {
1736
- const rewardVoucher = allVouchersById.get(vIds[vIdx]);
1737
- if (rewardVoucher == null || isVoucherRewardResourceUnavailable(rewardVoucher)) {
1738
- rewardVoucherUnavailable = true;
1739
- break;
1740
- }
1741
- if (customerOfferRedemptionCounts != null &&
1742
- rewardVoucher.max_uses_per_customer != null &&
1743
- (customerOfferRedemptionCounts[rewardVoucher._id] ?? 0) >= rewardVoucher.max_uses_per_customer) {
1744
- rewardVoucherLimitExhausted = true;
1745
- break;
1746
- }
1747
- }
1888
+ const { rewardVoucherUnavailable, rewardVoucherLimitExhausted } = getRewardVoucherAvailability(Object.keys(offer.reward_voucher.voucher_ids));
1748
1889
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1749
1890
  const isRedeemed = redeemedTimes > 0;
1750
1891
  stats.redeemed_times = redeemedTimes;
@@ -1805,22 +1946,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1805
1946
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS) {
1806
1947
  if (offer.reward_gift_cards == null)
1807
1948
  continue;
1808
- const gcIds = Object.keys(offer.reward_gift_cards.gift_card_ids);
1809
- let rewardGiftCardUnavailable = false;
1810
- let rewardGiftCardLimitExhausted = false;
1811
- for (let gcIdx = 0; gcIdx < gcIds.length; gcIdx++) {
1812
- const giftCard = allGiftCardsById.get(gcIds[gcIdx]);
1813
- if (giftCard == null || isGiftCardRewardResourceUnavailable(giftCard)) {
1814
- rewardGiftCardUnavailable = true;
1815
- break;
1816
- }
1817
- if (customerOfferRedemptionCounts != null &&
1818
- giftCard.max_uses_per_customer != null &&
1819
- (customerOfferRedemptionCounts[giftCard._id] ?? 0) >= giftCard.max_uses_per_customer) {
1820
- rewardGiftCardLimitExhausted = true;
1821
- break;
1822
- }
1823
- }
1949
+ const { rewardGiftCardUnavailable, rewardGiftCardLimitExhausted } = getRewardGiftCardAvailability(Object.keys(offer.reward_gift_cards.gift_card_ids));
1824
1950
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1825
1951
  const isRedeemed = redeemedTimes > 0;
1826
1952
  stats.redeemed_times = redeemedTimes;
@@ -1881,6 +2007,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1881
2007
  stats.claimable = false;
1882
2008
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
1883
2009
  }
2010
+ clearBlockedRewardApplicableQuantity(offer, stats);
1884
2011
  stats.action_hint = getActionHint(offer, stats.claimable);
1885
2012
  offersWithStats.push({
1886
2013
  offer,
@@ -2132,6 +2259,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2132
2259
  stats.claimable = false;
2133
2260
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
2134
2261
  }
2262
+ clearBlockedRewardApplicableQuantity(offer, stats);
2135
2263
  stats.action_hint = getActionHint(offer, stats.claimable);
2136
2264
  offersWithStats.push({
2137
2265
  offer,
@@ -2384,6 +2512,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2384
2512
  stats.claimable = false;
2385
2513
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
2386
2514
  }
2515
+ clearBlockedRewardApplicableQuantity(offer, stats);
2387
2516
  stats.action_hint = getActionHint(offer, stats.claimable);
2388
2517
  offersWithStats.push({
2389
2518
  offer,
@@ -2408,18 +2537,44 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2408
2537
  const referencedVouchers = voucher_ids
2409
2538
  .map((voucherId) => vouchersById.get(voucherId))
2410
2539
  .filter((voucher) => voucher != null);
2411
- const matchedVoucher = normalizedVoucherRef == null
2540
+ const getVoucherCode = (voucher) => voucher.code.trim().toUpperCase();
2541
+ const getVoucherSpendableCount = (voucher) => spendableVoucherCodesByCode[getVoucherCode(voucher)] ?? 0;
2542
+ const isVoucherPathwayUsageAvailable = (voucher) => {
2543
+ const maxUsesPerCustomer = voucher.max_uses_per_customer;
2544
+ if (maxUsesPerCustomer == null) {
2545
+ return true;
2546
+ }
2547
+ if (customerOfferRedemptionCounts == null) {
2548
+ // Defensive block when capped voucher counts are unavailable.
2549
+ return false;
2550
+ }
2551
+ return getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, voucher._id) < maxUsesPerCustomer;
2552
+ };
2553
+ // Wallet-first unlock policy for voucher pathways:
2554
+ // - A referenced voucher must exist in wallet inventory (spendable > 0)
2555
+ // to unlock claimability.
2556
+ // - Applied voucher code is optional and only used to disambiguate when
2557
+ // multiple referenced vouchers are available in wallet.
2558
+ const referencedVouchersWithWalletAccess = referencedVouchers.filter((voucher) => getVoucherSpendableCount(voucher) > 0 && isVoucherPathwayUsageAvailable(voucher));
2559
+ const matchedReferencedVoucherByReference = normalizedVoucherRef == null
2560
+ ? null
2561
+ : (referencedVouchers.find((voucher) => getVoucherCode(voucher) === normalizedVoucherRef) ?? null);
2562
+ const matchedVoucherForAccess = normalizedVoucherRef == null
2412
2563
  ? null
2413
- : (referencedVouchers.find((voucher) => voucher.code.trim().toUpperCase() === normalizedVoucherRef) ?? null);
2414
- const hasMatchingVoucher = matchedVoucher != null;
2415
- const normalizedMatchedVoucherCode = matchedVoucher?.code.trim().toUpperCase() ?? null;
2564
+ : (referencedVouchersWithWalletAccess.find((voucher) => getVoucherCode(voucher) === normalizedVoucherRef) ??
2565
+ null);
2566
+ const firstReferencedVoucherWithWalletInventory = referencedVouchers.find((voucher) => getVoucherSpendableCount(voucher) > 0) ?? null;
2567
+ const matchedVoucher = matchedVoucherForAccess ?? referencedVouchersWithWalletAccess[0] ?? null;
2568
+ const matchingVoucherContext = matchedVoucher ?? matchedReferencedVoucherByReference ?? firstReferencedVoucherWithWalletInventory ?? null;
2569
+ const hasMatchingVoucher = matchingVoucherContext != null;
2570
+ const normalizedMatchedVoucherCode = matchingVoucherContext != null ? getVoucherCode(matchingVoucherContext) : null;
2416
2571
  const redeemedTimesForOffer = getOfferRedeemedTimes(offer._id);
2417
2572
  const spendableVoucherCount = normalizedMatchedVoucherCode == null ? 0 : (spendableVoucherCodesByCode[normalizedMatchedVoucherCode] ?? 0);
2418
- const hasVoucherAccess = matchedVoucher != null && (matchedVoucher.is_public || spendableVoucherCount > 0 || redeemedTimesForOffer > 0);
2573
+ const hasVoucherAccess = matchedVoucher != null || (redeemedTimesForOffer > 0 && matchingVoucherContext != null);
2419
2574
  const amountProgress = getPreUnlockProgressPercentage(orderValue, total_amount || 1);
2420
2575
  if (!hasMatchingVoucher) {
2421
2576
  stats.progress = 0;
2422
- stats.message = `Apply a matching voucher to unlock this offer`;
2577
+ stats.message = `Add a matching voucher to your wallet to unlock this offer`;
2423
2578
  }
2424
2579
  else if (!hasVoucherAccess) {
2425
2580
  stats.progress = 0;
@@ -2504,9 +2659,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2504
2659
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2505
2660
  const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
2506
2661
  const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
2507
- const voucherInventoryClaimCap = matchedVoucher != null && !matchedVoucher.is_public
2508
- ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer)
2509
- : Infinity;
2662
+ const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
2510
2663
  const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
2511
2664
  const maxRedeemableClaims = getPointsMaxRedeemableClaims({
2512
2665
  offerToCheck: offer,
@@ -2563,9 +2716,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2563
2716
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2564
2717
  const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
2565
2718
  const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
2566
- const voucherInventoryClaimCap = matchedVoucher != null && !matchedVoucher.is_public
2567
- ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer)
2568
- : Infinity;
2719
+ const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
2569
2720
  const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
2570
2721
  const maxRedeemableClaims = getPathwayMaxRedeemableClaims({
2571
2722
  offerToCheck: offer,
@@ -2635,6 +2786,10 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2635
2786
  offerToCheck: offer,
2636
2787
  stats,
2637
2788
  });
2789
+ applyVoucherPathwayCartContextBlock({
2790
+ offerToCheck: offer,
2791
+ statsToUpdate: stats,
2792
+ });
2638
2793
  if (stats.progress >= 100 && isOfferUsesPerCustomerExhausted(offer)) {
2639
2794
  if (stats.claimable) {
2640
2795
  totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offer._id);
@@ -2644,6 +2799,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2644
2799
  stats.claimable = false;
2645
2800
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
2646
2801
  }
2802
+ clearBlockedRewardApplicableQuantity(offer, stats);
2647
2803
  stats.action_hint = getActionHint(offer, stats.claimable);
2648
2804
  offersWithStats.push({
2649
2805
  offer,
@@ -2661,7 +2817,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2661
2817
  }
2662
2818
  }
2663
2819
  // When customerOfferRedemptionCounts is null, invalidate claimability for offers whose
2664
- // reward vouchers/gift-cards have max_uses_per_customer set, since we cannot verify the limit.
2820
+ // reward vouchers/gift-cards have assignment limits, since we cannot verify the limit.
2665
2821
  if (customerOfferRedemptionCounts == null) {
2666
2822
  for (let i = 0; i < offersWithStats.length; i++) {
2667
2823
  const { offer, stats } = offersWithStats[i];
@@ -2670,8 +2826,8 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2670
2826
  if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER && offer.reward_voucher != null) {
2671
2827
  const vIds = Object.keys(offer.reward_voucher.voucher_ids);
2672
2828
  for (let vIdx = 0; vIdx < vIds.length; vIdx++) {
2673
- const rewardVoucher = vouchersById.get(vIds[vIdx]);
2674
- if (rewardVoucher != null && rewardVoucher.max_uses_per_customer != null) {
2829
+ const rewardVoucher = allVouchersById.get(vIds[vIdx]);
2830
+ if (rewardVoucher != null && getRewardMaxAssignmentsPerCustomer(rewardVoucher) != null) {
2675
2831
  stats.claimable = false;
2676
2832
  stats.applicable_quantity = 0;
2677
2833
  unlockedOffersApplicableTimes[offer._id] = 0;
@@ -2687,8 +2843,8 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2687
2843
  stats.claimable) {
2688
2844
  const gcIds = Object.keys(offer.reward_gift_cards.gift_card_ids);
2689
2845
  for (let gcIdx = 0; gcIdx < gcIds.length; gcIdx++) {
2690
- const giftCard = giftCardsById.get(gcIds[gcIdx]);
2691
- if (giftCard != null && giftCard.max_uses_per_customer != null) {
2846
+ const giftCard = allGiftCardsById.get(gcIds[gcIdx]);
2847
+ if (giftCard != null && getRewardMaxAssignmentsPerCustomer(giftCard) != null) {
2692
2848
  stats.claimable = false;
2693
2849
  stats.applicable_quantity = 0;
2694
2850
  unlockedOffersApplicableTimes[offer._id] = 0;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.339",
3
+ "version": "0.2.341",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "scripts": {
8
- "test": "jest --config jestconfig.json",
8
+ "test": "jest --config jestconfig.json --passWithNoTests",
9
9
  "build": "npm run test && tsc",
10
10
  "prepare": "npm run build",
11
11
  "prepublishOnly": "npm run lint",