@managesome/eposmatic-toolkit 0.2.342-beta.0 → 0.2.342

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;
@@ -13,6 +15,7 @@ exports.getOfferUsageLimitMeta = getOfferUsageLimitMeta;
13
15
  exports.getOfferConsumedUses = getOfferConsumedUses;
14
16
  exports.isOfferFullyConsumedForCustomer = isOfferFullyConsumedForCustomer;
15
17
  exports.getOfferClaimIncrementFromStats = getOfferClaimIncrementFromStats;
18
+ exports.getOffersIdsHashKey = getOffersIdsHashKey;
16
19
  exports.getSuppressedOfferIdsHashKey = getSuppressedOfferIdsHashKey;
17
20
  exports.getAutoApplyOfferCandidates = getAutoApplyOfferCandidates;
18
21
  exports.buildRedeemedOffersClaimPatch = buildRedeemedOffersClaimPatch;
@@ -323,6 +326,69 @@ function getOfferRedeemedClaims(redeemedOffers, offerId) {
323
326
  }
324
327
  return 0;
325
328
  }
329
+ function getVoucherPathwayCartContext({ offers, vouchers, redeemedOffers, orderDiscountDetails, }) {
330
+ const consumedVoucherCodes = new Set();
331
+ const consumedVoucherIds = new Set();
332
+ const redeemedOfferIds = new Set();
333
+ const voucherCodeById = new Map();
334
+ const referencedVoucherCode = normalizeVoucherCode(orderDiscountDetails?.voucher_reference_number);
335
+ if (vouchers != null) {
336
+ for (let i = 0; i < vouchers.length; i++) {
337
+ const voucher = vouchers[i];
338
+ const normalizedVoucherCode = normalizeVoucherCode(voucher.code);
339
+ if (normalizedVoucherCode == null)
340
+ continue;
341
+ voucherCodeById.set(voucher._id, normalizedVoucherCode);
342
+ }
343
+ }
344
+ for (let i = 0; i < offers.length; i++) {
345
+ const offer = offers[i];
346
+ if (offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
347
+ continue;
348
+ }
349
+ const redeemedTimes = getOfferRedeemedClaims(redeemedOffers, offer._id);
350
+ if (redeemedTimes <= 0) {
351
+ continue;
352
+ }
353
+ const pathwayVouchers = offer.pathway_to_unlock_type_vouchers;
354
+ if (pathwayVouchers == null || pathwayVouchers.voucher_ids.length === 0) {
355
+ continue;
356
+ }
357
+ redeemedOfferIds.add(offer._id);
358
+ const linkedVoucherCodes = new Set();
359
+ for (let j = 0; j < pathwayVouchers.voucher_ids.length; j++) {
360
+ const voucherId = pathwayVouchers.voucher_ids[j];
361
+ consumedVoucherIds.add(voucherId);
362
+ const linkedVoucherCode = voucherCodeById.get(voucherId);
363
+ if (linkedVoucherCode == null)
364
+ continue;
365
+ linkedVoucherCodes.add(linkedVoucherCode);
366
+ }
367
+ if (referencedVoucherCode != null && linkedVoucherCodes.has(referencedVoucherCode)) {
368
+ consumedVoucherCodes.add(referencedVoucherCode);
369
+ continue;
370
+ }
371
+ for (const linkedVoucherCode of linkedVoucherCodes) {
372
+ consumedVoucherCodes.add(linkedVoucherCode);
373
+ }
374
+ }
375
+ const inferredVoucherCode = referencedVoucherCode ?? (consumedVoucherCodes.size === 1 ? consumedVoucherCodes.values().next().value : null);
376
+ return {
377
+ activeVoucherCode: inferredVoucherCode ?? null,
378
+ consumedVoucherCodes,
379
+ consumedVoucherIds,
380
+ redeemedOfferIds,
381
+ };
382
+ }
383
+ function isVoucherPathwayOfferBlockedByCartContext({ offer, cartContext, }) {
384
+ if (offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
385
+ return false;
386
+ }
387
+ if (cartContext.redeemedOfferIds.size === 0) {
388
+ return false;
389
+ }
390
+ return !cartContext.redeemedOfferIds.has(offer._id);
391
+ }
326
392
  // ================================><================================= //
327
393
  // === isExhaustedVoucherOffer === //
328
394
  // ================================><================================= //
@@ -344,7 +410,8 @@ function isExhaustedVoucherOffer({ offer, customerOfferRedemptionCounts, redeeme
344
410
  if (!isThisOfferRedeemed)
345
411
  return false;
346
412
  const pathwayMaxUsesPerCustomer = offer.pathway_to_unlock_type_vouchers.max_uses_per_customer;
347
- return (pathwayMaxUsesPerCustomer != null && (customerOfferRedemptionCounts[offer._id] ?? 0) >= pathwayMaxUsesPerCustomer);
413
+ return (pathwayMaxUsesPerCustomer != null &&
414
+ getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, offer._id) >= pathwayMaxUsesPerCustomer);
348
415
  }
349
416
  // ================================><================================= //
350
417
  // === findLinkedVoucherOffers === //
@@ -399,7 +466,7 @@ function calculateCustomerSpendableVoucherCodes({ customerVoucherCodes, orderDis
399
466
  }
400
467
  }
401
468
  const activeVoucherCode = normalizeVoucherCode(orderDiscountDetails?.voucher_reference_number);
402
- if (activeVoucherCode == null || vouchers == null || offers.length === 0) {
469
+ if (vouchers == null || offers.length === 0 || redeemedOffers == null) {
403
470
  return {
404
471
  voucherCodes,
405
472
  totalVouchers: Object.values(voucherCodes).reduce((acc, count) => acc + count, 0),
@@ -407,45 +474,59 @@ function calculateCustomerSpendableVoucherCodes({ customerVoucherCodes, orderDis
407
474
  consumedVoucherClaims: 0,
408
475
  };
409
476
  }
410
- const activeVoucherIds = new Set();
477
+ const voucherCodeById = new Map();
411
478
  for (let i = 0; i < vouchers.length; i++) {
412
479
  const voucher = vouchers[i];
413
- if (normalizeVoucherCode(voucher.code) === activeVoucherCode) {
414
- activeVoucherIds.add(voucher._id);
415
- }
480
+ const normalizedVoucherCode = normalizeVoucherCode(voucher.code);
481
+ if (normalizedVoucherCode == null)
482
+ continue;
483
+ voucherCodeById.set(voucher._id, normalizedVoucherCode);
416
484
  }
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)
485
+ const consumedVoucherClaimsByCode = {};
486
+ for (let i = 0; i < offers.length; i++) {
487
+ const offer = offers[i];
488
+ if (offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
489
+ continue;
490
+ }
491
+ const pathwayVouchers = offer.pathway_to_unlock_type_vouchers;
492
+ if (pathwayVouchers == null || pathwayVouchers.voucher_ids.length === 0) {
493
+ continue;
494
+ }
495
+ const redeemedTimes = getOfferRedeemedClaims(redeemedOffers, offer._id);
496
+ if (redeemedTimes <= 0) {
497
+ continue;
498
+ }
499
+ const linkedVoucherCodes = new Set();
500
+ for (let j = 0; j < pathwayVouchers.voucher_ids.length; j++) {
501
+ const linkedVoucherCode = voucherCodeById.get(pathwayVouchers.voucher_ids[j]);
502
+ if (linkedVoucherCode == null)
436
503
  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
- }
504
+ linkedVoucherCodes.add(linkedVoucherCode);
505
+ }
506
+ if (linkedVoucherCodes.size === 0) {
507
+ continue;
508
+ }
509
+ const [singleLinkedVoucherCode] = linkedVoucherCodes;
510
+ const targetVoucherCode = linkedVoucherCodes.size === 1
511
+ ? singleLinkedVoucherCode
512
+ : activeVoucherCode != null && linkedVoucherCodes.has(activeVoucherCode)
513
+ ? activeVoucherCode
514
+ : null;
515
+ if (targetVoucherCode == null) {
516
+ continue;
444
517
  }
518
+ consumedVoucherClaimsByCode[targetVoucherCode] =
519
+ (consumedVoucherClaimsByCode[targetVoucherCode] ?? 0) + redeemedTimes;
445
520
  }
446
- if (voucherCodes[activeVoucherCode] != null && consumedVoucherClaims > 0) {
447
- voucherCodes[activeVoucherCode] = Math.max(0, voucherCodes[activeVoucherCode] - consumedVoucherClaims);
521
+ const consumedVoucherCodes = Object.keys(consumedVoucherClaimsByCode);
522
+ for (let i = 0; i < consumedVoucherCodes.length; i++) {
523
+ const voucherCode = consumedVoucherCodes[i];
524
+ if (voucherCodes[voucherCode] == null) {
525
+ continue;
526
+ }
527
+ voucherCodes[voucherCode] = Math.max(0, voucherCodes[voucherCode] - consumedVoucherClaimsByCode[voucherCode]);
448
528
  }
529
+ const consumedVoucherClaims = activeVoucherCode != null ? (consumedVoucherClaimsByCode[activeVoucherCode] ?? 0) : 0;
449
530
  return {
450
531
  voucherCodes,
451
532
  totalVouchers: Object.values(voucherCodes).reduce((acc, count) => acc + count, 0),
@@ -794,6 +875,27 @@ function toPositiveInteger(value, fallback = 0) {
794
875
  }
795
876
  return Math.floor(parsedValue);
796
877
  }
878
+ function getCustomerRedemptionCount(customerOfferRedemptionCounts, type, id) {
879
+ const normalizedId = normalizeOfferId(id);
880
+ if (customerOfferRedemptionCounts == null || normalizedId == null) {
881
+ return 0;
882
+ }
883
+ const typedCount = customerOfferRedemptionCounts[type]?.[normalizedId];
884
+ if (typedCount != null) {
885
+ return toPositiveInteger(typedCount, 0);
886
+ }
887
+ const legacyFlatCounts = customerOfferRedemptionCounts;
888
+ return toPositiveInteger(legacyFlatCounts[normalizedId], 0);
889
+ }
890
+ function getRewardMaxAssignmentsPerCustomer(rewardResource) {
891
+ return rewardResource.max_assignments_per_customer ?? rewardResource.max_uses_per_customer ?? null;
892
+ }
893
+ function getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, id) {
894
+ return getCustomerRedemptionCount(customerOfferRedemptionCounts, constants_1.CUSTOMER_REDEMPTION_TYPE_ENUM.USED, id);
895
+ }
896
+ function getCustomerAssignedRedemptionCount(customerOfferRedemptionCounts, id) {
897
+ return getCustomerRedemptionCount(customerOfferRedemptionCounts, constants_1.CUSTOMER_REDEMPTION_TYPE_ENUM.ASSIGNED, id);
898
+ }
797
899
  function getOfferPathwayMaxUsesPerCustomer(offer) {
798
900
  if (offer == null) {
799
901
  return null;
@@ -834,7 +936,7 @@ function getOfferConsumedUses({ offer, usageLimit, customerOfferRedemptionCounts
834
936
  if (usageKey == null) {
835
937
  return null;
836
938
  }
837
- return toPositiveInteger(customerOfferRedemptionCounts[usageKey], 0);
939
+ return getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, usageKey);
838
940
  }
839
941
  function isOfferFullyConsumedForCustomer({ offer, usageLimit, customerOfferRedemptionCounts, }) {
840
942
  const consumedUses = getOfferConsumedUses({
@@ -856,13 +958,15 @@ function getOfferClaimIncrementFromStats({ stats, fallbackClaims = 0, }) {
856
958
  }
857
959
  return toPositiveInteger(fallbackClaims, 0);
858
960
  }
859
- function getSuppressedOfferIdsHashKey(suppressedOfferIds) {
860
- return Object.keys(suppressedOfferIds ?? {})
961
+ function getOffersIdsHashKey(offerIds) {
962
+ return (offerIds ?? [])
861
963
  .map((offerId) => normalizeOfferId(offerId))
862
- .filter((offerId) => offerId != null)
863
964
  .sort()
864
965
  .join('|');
865
966
  }
967
+ function getSuppressedOfferIdsHashKey(suppressedOfferIds) {
968
+ return getOffersIdsHashKey(Object.keys(suppressedOfferIds ?? {}));
969
+ }
866
970
  function getAutoApplyOfferCandidates({ offersWithStats, suppressedOfferIds, }) {
867
971
  if (offersWithStats == null || offersWithStats.length === 0) {
868
972
  return [];
@@ -991,8 +1095,13 @@ function normalizeWalletRedemptions(walletRedemptions) {
991
1095
  if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
992
1096
  normalizedWalletRedemptions.cashback_amount = (0, common_1.parseNum)(cashbackAmount);
993
1097
  }
994
- const giftCardRedemptions = walletRedemptions.gift_cards;
995
- if (Array.isArray(giftCardRedemptions)) {
1098
+ const rawGiftCardRedemptions = walletRedemptions.gift_cards;
1099
+ const giftCardRedemptions = rawGiftCardRedemptions == null
1100
+ ? []
1101
+ : Array.isArray(rawGiftCardRedemptions)
1102
+ ? rawGiftCardRedemptions
1103
+ : [rawGiftCardRedemptions];
1104
+ if (giftCardRedemptions.length > 0) {
996
1105
  const normalizedGiftCardRedemptionsMap = {};
997
1106
  for (let i = 0; i < giftCardRedemptions.length; i++) {
998
1107
  const giftCardRedemption = giftCardRedemptions[i];
@@ -1292,7 +1401,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1292
1401
  let totalClaimableOffers = [];
1293
1402
  let totalLoyaltyPointsOfCustomer = null;
1294
1403
  const nowTs = Date.now();
1295
- const isVoucherRewardResourceUnavailable = (voucher) => {
1404
+ const isVoucherUseResourceUnavailable = (voucher) => {
1296
1405
  if (voucher == null) {
1297
1406
  return true;
1298
1407
  }
@@ -1300,13 +1409,21 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1300
1409
  const voucherUsesExhausted = voucher.max_uses != null && voucher.used_count >= voucher.max_uses;
1301
1410
  return !voucher.active || voucherIsExpired || voucherUsesExhausted;
1302
1411
  };
1412
+ const isVoucherRewardResourceUnavailable = (voucher) => {
1413
+ if (voucher == null) {
1414
+ return true;
1415
+ }
1416
+ const voucherIsExpired = voucher.expire_at != null && new Date(voucher.expire_at).getTime() <= nowTs;
1417
+ const voucherAssignmentsExhausted = voucher.max_assignments != null && voucher.assigned_count >= voucher.max_assignments;
1418
+ return !voucher.active || voucherIsExpired || voucherAssignmentsExhausted;
1419
+ };
1303
1420
  const isGiftCardRewardResourceUnavailable = (giftCard) => {
1304
1421
  if (giftCard == null) {
1305
1422
  return true;
1306
1423
  }
1307
1424
  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;
1425
+ const giftCardAssignmentsExhausted = giftCard.max_assignments != null && giftCard.assigned_count >= giftCard.max_assignments;
1426
+ return !giftCard.active || giftCardIsExpired || giftCardAssignmentsExhausted;
1310
1427
  };
1311
1428
  const { voucherCodes: spendableVoucherCodesByCode } = calculateCustomerSpendableVoucherCodes({
1312
1429
  customerVoucherCodes: customer?.voucher_codes,
@@ -1315,6 +1432,12 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1315
1432
  offers,
1316
1433
  vouchers,
1317
1434
  });
1435
+ const voucherPathwayCartContext = getVoucherPathwayCartContext({
1436
+ offers,
1437
+ vouchers,
1438
+ redeemedOffers: redeemed_offers,
1439
+ orderDiscountDetails: order_discount_details,
1440
+ });
1318
1441
  const redeemedOffersById = redeemed_offers ?? null;
1319
1442
  const vouchersById = new Map();
1320
1443
  const allVouchersById = new Map();
@@ -1322,7 +1445,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1322
1445
  for (let i = 0; i < vouchers.length; i++) {
1323
1446
  const voucher = vouchers[i];
1324
1447
  allVouchersById.set(voucher._id, voucher);
1325
- if (isVoucherRewardResourceUnavailable(voucher)) {
1448
+ if (isVoucherUseResourceUnavailable(voucher)) {
1326
1449
  continue;
1327
1450
  }
1328
1451
  vouchersById.set(voucher._id, voucher);
@@ -1349,9 +1472,11 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1349
1472
  rewardVoucherUnavailable = true;
1350
1473
  break;
1351
1474
  }
1352
- if (customerOfferRedemptionCounts != null &&
1353
- rewardVoucher.max_uses_per_customer != null &&
1354
- (customerOfferRedemptionCounts[rewardVoucher._id] ?? 0) >= rewardVoucher.max_uses_per_customer) {
1475
+ const maxAssignmentsPerCustomer = getRewardMaxAssignmentsPerCustomer(rewardVoucher);
1476
+ if (maxAssignmentsPerCustomer != null &&
1477
+ (customerOfferRedemptionCounts == null ||
1478
+ getCustomerAssignedRedemptionCount(customerOfferRedemptionCounts, rewardVoucher._id) >=
1479
+ maxAssignmentsPerCustomer)) {
1355
1480
  rewardVoucherLimitExhausted = true;
1356
1481
  break;
1357
1482
  }
@@ -1370,9 +1495,11 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1370
1495
  rewardGiftCardUnavailable = true;
1371
1496
  break;
1372
1497
  }
1373
- if (customerOfferRedemptionCounts != null &&
1374
- rewardGiftCard.max_uses_per_customer != null &&
1375
- (customerOfferRedemptionCounts[rewardGiftCard._id] ?? 0) >= rewardGiftCard.max_uses_per_customer) {
1498
+ const maxAssignmentsPerCustomer = getRewardMaxAssignmentsPerCustomer(rewardGiftCard);
1499
+ if (maxAssignmentsPerCustomer != null &&
1500
+ (customerOfferRedemptionCounts == null ||
1501
+ getCustomerAssignedRedemptionCount(customerOfferRedemptionCounts, rewardGiftCard._id) >=
1502
+ maxAssignmentsPerCustomer)) {
1376
1503
  rewardGiftCardLimitExhausted = true;
1377
1504
  break;
1378
1505
  }
@@ -1382,6 +1509,19 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1382
1509
  rewardGiftCardLimitExhausted,
1383
1510
  };
1384
1511
  };
1512
+ const clearBlockedRewardApplicableQuantity = (offerToCheck, statsToUpdate) => {
1513
+ if (statsToUpdate.claimable ||
1514
+ (offerToCheck.reward_type !== constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER &&
1515
+ offerToCheck.reward_type !== constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS)) {
1516
+ return;
1517
+ }
1518
+ if (statsToUpdate.message !== '⛔️ This reward is no longer available ⛔️' &&
1519
+ statsToUpdate.message !== '⛔️ You have reached your personal limit for this reward ⛔️') {
1520
+ return;
1521
+ }
1522
+ statsToUpdate.applicable_quantity = 0;
1523
+ unlockedOffersApplicableTimes[offerToCheck._id] = 0;
1524
+ };
1385
1525
  const getOfferRedeemedTimes = (offerId) => {
1386
1526
  const redeemedTimes = Number(redeemedOffersById?.[offerId]);
1387
1527
  if (Number.isFinite(redeemedTimes) && redeemedTimes > 0) {
@@ -1459,7 +1599,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1459
1599
  return false;
1460
1600
  if (customerOfferRedemptionCounts == null)
1461
1601
  return true;
1462
- return (customerOfferRedemptionCounts[offerToCheck._id] ?? 0) >= maxUsesPerCustomer;
1602
+ return getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, offerToCheck._id) >= maxUsesPerCustomer;
1463
1603
  };
1464
1604
  const getRemainingOfferUsesForCustomer = (offerToCheck) => {
1465
1605
  const maxUsesPerCustomer = getOfferMaxUsesPerCustomer(offerToCheck);
@@ -1467,7 +1607,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1467
1607
  return Infinity;
1468
1608
  if (customerOfferRedemptionCounts == null)
1469
1609
  return 0;
1470
- const redeemedByCustomer = customerOfferRedemptionCounts[offerToCheck._id] ?? 0;
1610
+ const redeemedByCustomer = getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, offerToCheck._id);
1471
1611
  return Math.max(0, maxUsesPerCustomer - redeemedByCustomer);
1472
1612
  };
1473
1613
  const getOrderValueClaimTimes = (orderValue, minAmount) => {
@@ -1547,6 +1687,25 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1547
1687
  totalClaimableOffers.push(offerToCheck._id);
1548
1688
  }
1549
1689
  };
1690
+ const applyVoucherPathwayCartContextBlock = ({ offerToCheck, statsToUpdate, }) => {
1691
+ if (!isVoucherPathwayOfferBlockedByCartContext({
1692
+ offer: offerToCheck,
1693
+ cartContext: voucherPathwayCartContext,
1694
+ })) {
1695
+ return;
1696
+ }
1697
+ if (!statsToUpdate.claimable && statsToUpdate.applicable_quantity <= 0 && statsToUpdate.progress < 100) {
1698
+ return;
1699
+ }
1700
+ totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offerToCheck._id);
1701
+ statsToUpdate.claimable = false;
1702
+ statsToUpdate.applicable_quantity = 0;
1703
+ unlockedOffersApplicableTimes[offerToCheck._id] = 0;
1704
+ statsToUpdate.message =
1705
+ voucherPathwayCartContext.activeVoucherCode != null
1706
+ ? `You already selected a reward for voucher ${voucherPathwayCartContext.activeVoucherCode}.`
1707
+ : 'You already selected a voucher reward for this order.';
1708
+ };
1550
1709
  for (let i = 0; i < offers.length; i++) {
1551
1710
  const offer = offers[i];
1552
1711
  const stats = {
@@ -1729,22 +1888,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1729
1888
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
1730
1889
  if (offer.reward_voucher == null)
1731
1890
  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
- }
1891
+ const { rewardVoucherUnavailable, rewardVoucherLimitExhausted } = getRewardVoucherAvailability(Object.keys(offer.reward_voucher.voucher_ids));
1748
1892
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1749
1893
  const isRedeemed = redeemedTimes > 0;
1750
1894
  stats.redeemed_times = redeemedTimes;
@@ -1805,22 +1949,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1805
1949
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS) {
1806
1950
  if (offer.reward_gift_cards == null)
1807
1951
  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
- }
1952
+ const { rewardGiftCardUnavailable, rewardGiftCardLimitExhausted } = getRewardGiftCardAvailability(Object.keys(offer.reward_gift_cards.gift_card_ids));
1824
1953
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1825
1954
  const isRedeemed = redeemedTimes > 0;
1826
1955
  stats.redeemed_times = redeemedTimes;
@@ -1881,6 +2010,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1881
2010
  stats.claimable = false;
1882
2011
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
1883
2012
  }
2013
+ clearBlockedRewardApplicableQuantity(offer, stats);
1884
2014
  stats.action_hint = getActionHint(offer, stats.claimable);
1885
2015
  offersWithStats.push({
1886
2016
  offer,
@@ -2132,6 +2262,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2132
2262
  stats.claimable = false;
2133
2263
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
2134
2264
  }
2265
+ clearBlockedRewardApplicableQuantity(offer, stats);
2135
2266
  stats.action_hint = getActionHint(offer, stats.claimable);
2136
2267
  offersWithStats.push({
2137
2268
  offer,
@@ -2384,6 +2515,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2384
2515
  stats.claimable = false;
2385
2516
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
2386
2517
  }
2518
+ clearBlockedRewardApplicableQuantity(offer, stats);
2387
2519
  stats.action_hint = getActionHint(offer, stats.claimable);
2388
2520
  offersWithStats.push({
2389
2521
  offer,
@@ -2408,18 +2540,44 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2408
2540
  const referencedVouchers = voucher_ids
2409
2541
  .map((voucherId) => vouchersById.get(voucherId))
2410
2542
  .filter((voucher) => voucher != null);
2411
- const matchedVoucher = normalizedVoucherRef == null
2543
+ const getVoucherCode = (voucher) => voucher.code.trim().toUpperCase();
2544
+ const getVoucherSpendableCount = (voucher) => spendableVoucherCodesByCode[getVoucherCode(voucher)] ?? 0;
2545
+ const isVoucherPathwayUsageAvailable = (voucher) => {
2546
+ const maxUsesPerCustomer = voucher.max_uses_per_customer;
2547
+ if (maxUsesPerCustomer == null) {
2548
+ return true;
2549
+ }
2550
+ if (customerOfferRedemptionCounts == null) {
2551
+ // Defensive block when capped voucher counts are unavailable.
2552
+ return false;
2553
+ }
2554
+ return getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, voucher._id) < maxUsesPerCustomer;
2555
+ };
2556
+ // Wallet-first unlock policy for voucher pathways:
2557
+ // - A referenced voucher must exist in wallet inventory (spendable > 0)
2558
+ // to unlock claimability.
2559
+ // - Applied voucher code is optional and only used to disambiguate when
2560
+ // multiple referenced vouchers are available in wallet.
2561
+ const referencedVouchersWithWalletAccess = referencedVouchers.filter((voucher) => getVoucherSpendableCount(voucher) > 0 && isVoucherPathwayUsageAvailable(voucher));
2562
+ const matchedReferencedVoucherByReference = 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
+ : (referencedVouchers.find((voucher) => getVoucherCode(voucher) === normalizedVoucherRef) ?? null);
2565
+ const matchedVoucherForAccess = normalizedVoucherRef == null
2566
+ ? null
2567
+ : (referencedVouchersWithWalletAccess.find((voucher) => getVoucherCode(voucher) === normalizedVoucherRef) ??
2568
+ null);
2569
+ const firstReferencedVoucherWithWalletInventory = referencedVouchers.find((voucher) => getVoucherSpendableCount(voucher) > 0) ?? null;
2570
+ const matchedVoucher = matchedVoucherForAccess ?? referencedVouchersWithWalletAccess[0] ?? null;
2571
+ const matchingVoucherContext = matchedVoucher ?? matchedReferencedVoucherByReference ?? firstReferencedVoucherWithWalletInventory ?? null;
2572
+ const hasMatchingVoucher = matchingVoucherContext != null;
2573
+ const normalizedMatchedVoucherCode = matchingVoucherContext != null ? getVoucherCode(matchingVoucherContext) : null;
2416
2574
  const redeemedTimesForOffer = getOfferRedeemedTimes(offer._id);
2417
2575
  const spendableVoucherCount = normalizedMatchedVoucherCode == null ? 0 : (spendableVoucherCodesByCode[normalizedMatchedVoucherCode] ?? 0);
2418
- const hasVoucherAccess = matchedVoucher != null && (matchedVoucher.is_public || spendableVoucherCount > 0 || redeemedTimesForOffer > 0);
2576
+ const hasVoucherAccess = matchedVoucher != null || (redeemedTimesForOffer > 0 && matchingVoucherContext != null);
2419
2577
  const amountProgress = getPreUnlockProgressPercentage(orderValue, total_amount || 1);
2420
2578
  if (!hasMatchingVoucher) {
2421
2579
  stats.progress = 0;
2422
- stats.message = `Apply a matching voucher to unlock this offer`;
2580
+ stats.message = `Add a matching voucher to your wallet to unlock this offer`;
2423
2581
  }
2424
2582
  else if (!hasVoucherAccess) {
2425
2583
  stats.progress = 0;
@@ -2504,9 +2662,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2504
2662
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2505
2663
  const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
2506
2664
  const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
2507
- const voucherInventoryClaimCap = matchedVoucher != null && !matchedVoucher.is_public
2508
- ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer)
2509
- : Infinity;
2665
+ const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
2510
2666
  const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
2511
2667
  const maxRedeemableClaims = getPointsMaxRedeemableClaims({
2512
2668
  offerToCheck: offer,
@@ -2563,9 +2719,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2563
2719
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2564
2720
  const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
2565
2721
  const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
2566
- const voucherInventoryClaimCap = matchedVoucher != null && !matchedVoucher.is_public
2567
- ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer)
2568
- : Infinity;
2722
+ const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
2569
2723
  const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
2570
2724
  const maxRedeemableClaims = getPathwayMaxRedeemableClaims({
2571
2725
  offerToCheck: offer,
@@ -2635,6 +2789,10 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2635
2789
  offerToCheck: offer,
2636
2790
  stats,
2637
2791
  });
2792
+ applyVoucherPathwayCartContextBlock({
2793
+ offerToCheck: offer,
2794
+ statsToUpdate: stats,
2795
+ });
2638
2796
  if (stats.progress >= 100 && isOfferUsesPerCustomerExhausted(offer)) {
2639
2797
  if (stats.claimable) {
2640
2798
  totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offer._id);
@@ -2644,6 +2802,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2644
2802
  stats.claimable = false;
2645
2803
  stats.message = '⛔️ You have reached your personal limit for this offer ⛔️';
2646
2804
  }
2805
+ clearBlockedRewardApplicableQuantity(offer, stats);
2647
2806
  stats.action_hint = getActionHint(offer, stats.claimable);
2648
2807
  offersWithStats.push({
2649
2808
  offer,
@@ -2661,7 +2820,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2661
2820
  }
2662
2821
  }
2663
2822
  // 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.
2823
+ // reward vouchers/gift-cards have assignment limits, since we cannot verify the limit.
2665
2824
  if (customerOfferRedemptionCounts == null) {
2666
2825
  for (let i = 0; i < offersWithStats.length; i++) {
2667
2826
  const { offer, stats } = offersWithStats[i];
@@ -2670,8 +2829,8 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2670
2829
  if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER && offer.reward_voucher != null) {
2671
2830
  const vIds = Object.keys(offer.reward_voucher.voucher_ids);
2672
2831
  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) {
2832
+ const rewardVoucher = allVouchersById.get(vIds[vIdx]);
2833
+ if (rewardVoucher != null && getRewardMaxAssignmentsPerCustomer(rewardVoucher) != null) {
2675
2834
  stats.claimable = false;
2676
2835
  stats.applicable_quantity = 0;
2677
2836
  unlockedOffersApplicableTimes[offer._id] = 0;
@@ -2687,8 +2846,8 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2687
2846
  stats.claimable) {
2688
2847
  const gcIds = Object.keys(offer.reward_gift_cards.gift_card_ids);
2689
2848
  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) {
2849
+ const giftCard = allGiftCardsById.get(gcIds[gcIdx]);
2850
+ if (giftCard != null && getRewardMaxAssignmentsPerCustomer(giftCard) != null) {
2692
2851
  stats.claimable = false;
2693
2852
  stats.applicable_quantity = 0;
2694
2853
  unlockedOffersApplicableTimes[offer._id] = 0;
@@ -38,9 +38,6 @@ export * from './operational_roles';
38
38
  export * from './order';
39
39
  export * from './printer_settings';
40
40
  export * from './public_holidays';
41
- export * from './purchase_order_receipts';
42
- export * from './purchase_order_rfq';
43
- export * from './purchase_order_vendor_returns';
44
41
  export * from './purchase_orders';
45
42
  export * from './reservations';
46
43
  export * from './reviews';
@@ -54,9 +54,6 @@ __exportStar(require("./operational_roles"), exports);
54
54
  __exportStar(require("./order"), exports);
55
55
  __exportStar(require("./printer_settings"), exports);
56
56
  __exportStar(require("./public_holidays"), exports);
57
- __exportStar(require("./purchase_order_receipts"), exports);
58
- __exportStar(require("./purchase_order_rfq"), exports);
59
- __exportStar(require("./purchase_order_vendor_returns"), exports);
60
57
  __exportStar(require("./purchase_orders"), exports);
61
58
  __exportStar(require("./reservations"), exports);
62
59
  __exportStar(require("./reviews"), exports);
@@ -891,7 +891,6 @@ export declare const PurchaseOrderSchema: Schema<any, import("mongoose").Model<a
891
891
  gst?: number | null | undefined;
892
892
  gst_amount?: number | null | undefined;
893
893
  is_gst_inclusive?: boolean | null | undefined;
894
- rfq_id?: import("mongoose").Types.ObjectId | null | undefined;
895
894
  }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
896
895
  createdAt: NativeDate;
897
896
  updatedAt: NativeDate;
@@ -1780,7 +1779,6 @@ export declare const PurchaseOrderSchema: Schema<any, import("mongoose").Model<a
1780
1779
  gst?: number | null | undefined;
1781
1780
  gst_amount?: number | null | undefined;
1782
1781
  is_gst_inclusive?: boolean | null | undefined;
1783
- rfq_id?: import("mongoose").Types.ObjectId | null | undefined;
1784
1782
  }>> & import("mongoose").FlatRecord<{
1785
1783
  createdAt: NativeDate;
1786
1784
  updatedAt: NativeDate;
@@ -2669,7 +2667,6 @@ export declare const PurchaseOrderSchema: Schema<any, import("mongoose").Model<a
2669
2667
  gst?: number | null | undefined;
2670
2668
  gst_amount?: number | null | undefined;
2671
2669
  is_gst_inclusive?: boolean | null | undefined;
2672
- rfq_id?: import("mongoose").Types.ObjectId | null | undefined;
2673
2670
  }> & Required<{
2674
2671
  _id: import("mongoose").Types.ObjectId;
2675
2672
  }> & {