@managesome/eposmatic-toolkit 0.2.322 → 0.2.324

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.
@@ -548,39 +548,138 @@ function clearSpecialDiscountFields(orderDiscountDetails) {
548
548
  special_reference_number: undefined,
549
549
  };
550
550
  }
551
+ function clearLoyaltyDiscountFields(orderDiscountDetails) {
552
+ return {
553
+ ...orderDiscountDetails,
554
+ loyalty_discount_id: undefined,
555
+ loyalty_discount_name: undefined,
556
+ loyalty_discount: undefined,
557
+ is_loyalty_discount_percentage: false,
558
+ loyalty_discount_reason: undefined,
559
+ loyalty_reference_number: undefined,
560
+ };
561
+ }
562
+ function clearVoucherDiscountFields(orderDiscountDetails) {
563
+ return {
564
+ ...orderDiscountDetails,
565
+ voucher_discount_id: undefined,
566
+ voucher_discount_name: undefined,
567
+ voucher_discount: undefined,
568
+ is_voucher_discount_percentage: false,
569
+ voucher_discount_reason: undefined,
570
+ voucher_reference_number: undefined,
571
+ };
572
+ }
573
+ function hasRedeemedOfferEvidenceForDiscountId({ discountId, redeemedOffers, }) {
574
+ const normalizedDiscountId = getNormalizedOfferId(discountId);
575
+ if (normalizedDiscountId == null) {
576
+ return false;
577
+ }
578
+ return toPositiveInteger(redeemedOffers?.[normalizedDiscountId], 0) > 0;
579
+ }
580
+ function hasWalletGiftCardEvidenceForDiscountId({ discountId, walletRedemptions, }) {
581
+ const normalizedDiscountId = getNormalizedOfferId(discountId);
582
+ if (normalizedDiscountId == null) {
583
+ return false;
584
+ }
585
+ const normalizedWalletRedemptions = getWalletRedemptionsMap(walletRedemptions);
586
+ if (normalizedWalletRedemptions?.gift_cards == null) {
587
+ return false;
588
+ }
589
+ for (let i = 0; i < normalizedWalletRedemptions.gift_cards.length; i++) {
590
+ const giftCardRedemption = normalizedWalletRedemptions.gift_cards[i];
591
+ const normalizedGiftCardId = getNormalizedOfferId(giftCardRedemption.gift_card_id);
592
+ if (normalizedGiftCardId === normalizedDiscountId) {
593
+ return true;
594
+ }
595
+ }
596
+ return false;
597
+ }
598
+ function hasWalletCashbackEvidence(walletRedemptions) {
599
+ return Number(walletRedemptions?.cashback_amount ?? 0) > 0;
600
+ }
601
+ function hasLoyaltyDiscountEvidence({ discountId, redeemedOffers, walletRedemptions, }) {
602
+ const normalizedDiscountId = getNormalizedOfferId(discountId);
603
+ if (normalizedDiscountId == null) {
604
+ return false;
605
+ }
606
+ if (normalizedDiscountId === constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID) {
607
+ return hasWalletCashbackEvidence(walletRedemptions);
608
+ }
609
+ if (hasRedeemedOfferEvidenceForDiscountId({ discountId: normalizedDiscountId, redeemedOffers })) {
610
+ return true;
611
+ }
612
+ return hasWalletGiftCardEvidenceForDiscountId({
613
+ discountId: normalizedDiscountId,
614
+ walletRedemptions,
615
+ });
616
+ }
617
+ function hasVoucherDiscountEvidence({ discountId, redeemedOffers, walletRedemptions, }) {
618
+ const normalizedDiscountId = getNormalizedOfferId(discountId);
619
+ if (normalizedDiscountId == null) {
620
+ return false;
621
+ }
622
+ if (hasRedeemedOfferEvidenceForDiscountId({ discountId: normalizedDiscountId, redeemedOffers })) {
623
+ return true;
624
+ }
625
+ return hasWalletGiftCardEvidenceForDiscountId({
626
+ discountId: normalizedDiscountId,
627
+ walletRedemptions,
628
+ });
629
+ }
551
630
  function getSyncedOrderDiscountDetailsWithoutRewardSpecialDiscount({ orderDiscountDetails, redeemedOffers, walletRedemptions, }) {
552
631
  if (orderDiscountDetails == null) {
553
632
  return orderDiscountDetails;
554
633
  }
555
- const normalizedSpecialDiscountId = getNormalizedOfferId(orderDiscountDetails.special_discount_id);
556
- if (normalizedSpecialDiscountId == null) {
557
- return orderDiscountDetails;
634
+ let syncedOrderDiscountDetails = orderDiscountDetails;
635
+ const normalizedSpecialDiscountId = getNormalizedOfferId(syncedOrderDiscountDetails.special_discount_id);
636
+ if (normalizedSpecialDiscountId != null) {
637
+ const hasRedeemedOfferForSpecialDiscount = redeemedOffers?.[normalizedSpecialDiscountId] != null;
638
+ const normalizedGiftCardIds = (walletRedemptions?.gift_cards ?? [])
639
+ .map((giftCardRedemption) => getNormalizedOfferId(giftCardRedemption.gift_card_id))
640
+ .filter((giftCardId) => giftCardId != null);
641
+ const isGiftCardRewardDiscount = normalizedGiftCardIds.includes(normalizedSpecialDiscountId);
642
+ const isWalletCashbackRewardDiscount = normalizedSpecialDiscountId === constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID;
643
+ const isRewardLinkedSpecialDiscount = hasRedeemedOfferForSpecialDiscount || isGiftCardRewardDiscount || isWalletCashbackRewardDiscount;
644
+ if (isRewardLinkedSpecialDiscount) {
645
+ const hasExistingRewardSlot = syncedOrderDiscountDetails.loyalty_discount_id != null ||
646
+ syncedOrderDiscountDetails.voucher_discount_id != null;
647
+ if (hasExistingRewardSlot) {
648
+ syncedOrderDiscountDetails = clearSpecialDiscountFields(syncedOrderDiscountDetails);
649
+ }
650
+ else {
651
+ syncedOrderDiscountDetails = {
652
+ ...clearSpecialDiscountFields(syncedOrderDiscountDetails),
653
+ loyalty_discount_id: normalizedSpecialDiscountId,
654
+ loyalty_discount_name: syncedOrderDiscountDetails.loyalty_discount_name ?? syncedOrderDiscountDetails.special_discount_name,
655
+ loyalty_discount: syncedOrderDiscountDetails.loyalty_discount ?? syncedOrderDiscountDetails.special_discount,
656
+ is_loyalty_discount_percentage: syncedOrderDiscountDetails.loyalty_discount_id != null ||
657
+ syncedOrderDiscountDetails.loyalty_discount != null
658
+ ? syncedOrderDiscountDetails.is_loyalty_discount_percentage
659
+ : syncedOrderDiscountDetails.is_special_discount_percentage,
660
+ loyalty_discount_reason: syncedOrderDiscountDetails.loyalty_discount_reason ?? syncedOrderDiscountDetails.special_discount_reason,
661
+ loyalty_reference_number: syncedOrderDiscountDetails.loyalty_reference_number ?? syncedOrderDiscountDetails.special_reference_number,
662
+ };
663
+ }
664
+ }
558
665
  }
559
- const hasRedeemedOfferForSpecialDiscount = redeemedOffers?.[normalizedSpecialDiscountId] != null;
560
- const normalizedGiftCardIds = (walletRedemptions?.gift_cards ?? [])
561
- .map((giftCardRedemption) => getNormalizedOfferId(giftCardRedemption.gift_card_id))
562
- .filter((giftCardId) => giftCardId != null);
563
- const isGiftCardRewardDiscount = normalizedGiftCardIds.includes(normalizedSpecialDiscountId);
564
- const isWalletCashbackRewardDiscount = normalizedSpecialDiscountId === constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID;
565
- const isRewardLinkedSpecialDiscount = hasRedeemedOfferForSpecialDiscount || isGiftCardRewardDiscount || isWalletCashbackRewardDiscount;
566
- if (!isRewardLinkedSpecialDiscount) {
567
- return orderDiscountDetails;
666
+ if (syncedOrderDiscountDetails.loyalty_discount_id != null &&
667
+ !hasLoyaltyDiscountEvidence({
668
+ discountId: syncedOrderDiscountDetails.loyalty_discount_id,
669
+ redeemedOffers,
670
+ walletRedemptions,
671
+ })) {
672
+ syncedOrderDiscountDetails = clearLoyaltyDiscountFields(syncedOrderDiscountDetails);
568
673
  }
569
- const hasExistingRewardSlot = orderDiscountDetails.loyalty_discount_id != null || orderDiscountDetails.voucher_discount_id != null;
570
- if (hasExistingRewardSlot) {
571
- return clearSpecialDiscountFields(orderDiscountDetails);
674
+ if (syncedOrderDiscountDetails.voucher_discount_id != null &&
675
+ !hasVoucherDiscountEvidence({
676
+ discountId: syncedOrderDiscountDetails.voucher_discount_id,
677
+ redeemedOffers,
678
+ walletRedemptions,
679
+ })) {
680
+ syncedOrderDiscountDetails = clearVoucherDiscountFields(syncedOrderDiscountDetails);
572
681
  }
573
- return {
574
- ...clearSpecialDiscountFields(orderDiscountDetails),
575
- loyalty_discount_id: normalizedSpecialDiscountId,
576
- loyalty_discount_name: orderDiscountDetails.loyalty_discount_name ?? orderDiscountDetails.special_discount_name,
577
- loyalty_discount: orderDiscountDetails.loyalty_discount ?? orderDiscountDetails.special_discount,
578
- is_loyalty_discount_percentage: orderDiscountDetails.loyalty_discount_id != null || orderDiscountDetails.loyalty_discount != null
579
- ? orderDiscountDetails.is_loyalty_discount_percentage
580
- : orderDiscountDetails.is_special_discount_percentage,
581
- loyalty_discount_reason: orderDiscountDetails.loyalty_discount_reason ?? orderDiscountDetails.special_discount_reason,
582
- loyalty_reference_number: orderDiscountDetails.loyalty_reference_number ?? orderDiscountDetails.special_reference_number,
583
- };
682
+ return syncedOrderDiscountDetails;
584
683
  }
585
684
  function getSyncedItemDiscountDetailsWithoutRewardSpecialDiscount({ itemDiscountDetails, redeemedOffers, walletRedemptions, }) {
586
685
  return getSyncedOrderDiscountDetailsWithoutRewardSpecialDiscount({
@@ -839,16 +938,19 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
839
938
  if (order_discount_details != null) {
840
939
  const loyaltyDiscountId = order_discount_details.loyalty_discount_id;
841
940
  if (loyaltyDiscountId != null) {
842
- if (loyaltyDiscountId !== constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID) {
941
+ const hasLoyaltyEvidence = hasLoyaltyDiscountEvidence({
942
+ discountId: loyaltyDiscountId,
943
+ redeemedOffers: redeemedOffersPresenceMap,
944
+ walletRedemptions: data.wallet_redemptions,
945
+ });
946
+ if (!hasLoyaltyEvidence) {
947
+ clearOrderLevelDiscount('loyalty');
948
+ }
949
+ else if (loyaltyDiscountId !== constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID) {
843
950
  const loyaltyOffer = syncContext.offers.find((o) => o._id === loyaltyDiscountId);
844
- // Preserve non-offer loyalty discounts (e.g. gift-card discount IDs).
951
+ // Non-offer loyalty ids may still be valid when they are backed by wallet evidence.
845
952
  if (loyaltyOffer != null) {
846
- const normalizedLoyaltyDiscountId = getNormalizedOfferId(loyaltyDiscountId);
847
- const hasRedeemedEntryForLoyalty = normalizedLoyaltyDiscountId != null && (redeemedOffersPresenceMap?.[normalizedLoyaltyDiscountId] ?? 0) > 0;
848
- if (!hasRedeemedEntryForLoyalty) {
849
- clearOrderLevelDiscount('loyalty');
850
- }
851
- else if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
953
+ if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
852
954
  clearOrderLevelDiscount('loyalty');
853
955
  }
854
956
  else {
@@ -903,9 +1005,12 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
903
1005
  }
904
1006
  const voucherDiscountId = order_discount_details.voucher_discount_id;
905
1007
  if (voucherDiscountId != null) {
906
- const normalizedVoucherDiscountId = getNormalizedOfferId(voucherDiscountId);
907
- const hasRedeemedEntryForVoucher = normalizedVoucherDiscountId != null && (redeemedOffersPresenceMap?.[normalizedVoucherDiscountId] ?? 0) > 0;
908
- if (!hasRedeemedEntryForVoucher) {
1008
+ const hasVoucherEvidence = hasVoucherDiscountEvidence({
1009
+ discountId: voucherDiscountId,
1010
+ redeemedOffers: redeemedOffersPresenceMap,
1011
+ walletRedemptions: data.wallet_redemptions,
1012
+ });
1013
+ if (!hasVoucherEvidence) {
909
1014
  clearOrderLevelDiscount('voucher');
910
1015
  }
911
1016
  else if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
@@ -1105,6 +1105,22 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1105
1105
  let totalClaimableOffers = [];
1106
1106
  let totalLoyaltyPointsOfCustomer = null;
1107
1107
  const nowTs = Date.now();
1108
+ const isVoucherRewardResourceUnavailable = (voucher) => {
1109
+ if (voucher == null) {
1110
+ return true;
1111
+ }
1112
+ const voucherIsExpired = voucher.expire_at != null && new Date(voucher.expire_at).getTime() <= nowTs;
1113
+ const voucherUsesExhausted = voucher.max_uses != null && voucher.used_count >= voucher.max_uses;
1114
+ return !voucher.active || voucherIsExpired || voucherUsesExhausted;
1115
+ };
1116
+ const isGiftCardRewardResourceUnavailable = (giftCard) => {
1117
+ if (giftCard == null) {
1118
+ return true;
1119
+ }
1120
+ const giftCardIsExpired = giftCard.expire_at != null && new Date(giftCard.expire_at).getTime() <= nowTs;
1121
+ const giftCardUsesExhausted = giftCard.max_uses != null && giftCard.used_count >= giftCard.max_uses;
1122
+ return !giftCard.active || giftCardIsExpired || giftCardUsesExhausted;
1123
+ };
1108
1124
  const { voucherCodes: spendableVoucherCodesByCode } = calculateCustomerSpendableVoucherCodes({
1109
1125
  customerVoucherCodes: customer?.voucher_codes,
1110
1126
  orderDiscountDetails: order_discount_details,
@@ -1114,24 +1130,24 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1114
1130
  });
1115
1131
  const redeemedOffersById = redeemed_offers ?? null;
1116
1132
  const vouchersById = new Map();
1133
+ const allVouchersById = new Map();
1117
1134
  if (vouchers != null) {
1118
1135
  for (let i = 0; i < vouchers.length; i++) {
1119
1136
  const voucher = vouchers[i];
1120
- const voucherIsExpired = voucher.expire_at != null && new Date(voucher.expire_at).getTime() <= nowTs;
1121
- const voucherUsesExhausted = voucher.max_uses != null && voucher.used_count >= voucher.max_uses;
1122
- if (!voucher.active || voucherIsExpired || voucherUsesExhausted) {
1137
+ allVouchersById.set(voucher._id, voucher);
1138
+ if (isVoucherRewardResourceUnavailable(voucher)) {
1123
1139
  continue;
1124
1140
  }
1125
1141
  vouchersById.set(voucher._id, voucher);
1126
1142
  }
1127
1143
  }
1128
1144
  const giftCardsById = new Map();
1145
+ const allGiftCardsById = new Map();
1129
1146
  if (giftCards != null) {
1130
1147
  for (let i = 0; i < giftCards.length; i++) {
1131
1148
  const giftCard = giftCards[i];
1132
- const giftCardIsExpired = giftCard.expire_at != null && new Date(giftCard.expire_at).getTime() <= nowTs;
1133
- const giftCardUsesExhausted = giftCard.max_uses != null && giftCard.used_count >= giftCard.max_uses;
1134
- if (!giftCard.active || giftCardIsExpired || giftCardUsesExhausted) {
1149
+ allGiftCardsById.set(giftCard._id, giftCard);
1150
+ if (isGiftCardRewardResourceUnavailable(giftCard)) {
1135
1151
  continue;
1136
1152
  }
1137
1153
  giftCardsById.set(giftCard._id, giftCard);
@@ -1460,24 +1476,31 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1460
1476
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
1461
1477
  if (offer.reward_voucher == null)
1462
1478
  continue;
1479
+ const vIds = Object.keys(offer.reward_voucher.voucher_ids);
1480
+ let rewardVoucherUnavailable = false;
1463
1481
  let rewardVoucherLimitExhausted = false;
1464
- if (customerOfferRedemptionCounts != null) {
1465
- const vIds = Object.keys(offer.reward_voucher.voucher_ids);
1466
- for (let vIdx = 0; vIdx < vIds.length; vIdx++) {
1467
- const rewardVoucher = vouchersById.get(vIds[vIdx]);
1468
- if (rewardVoucher != null &&
1469
- rewardVoucher.max_uses_per_customer != null &&
1470
- (customerOfferRedemptionCounts[rewardVoucher._id] ?? 0) >= rewardVoucher.max_uses_per_customer) {
1471
- rewardVoucherLimitExhausted = true;
1472
- break;
1473
- }
1482
+ for (let vIdx = 0; vIdx < vIds.length; vIdx++) {
1483
+ const rewardVoucher = allVouchersById.get(vIds[vIdx]);
1484
+ if (rewardVoucher == null || isVoucherRewardResourceUnavailable(rewardVoucher)) {
1485
+ rewardVoucherUnavailable = true;
1486
+ break;
1487
+ }
1488
+ if (customerOfferRedemptionCounts != null &&
1489
+ rewardVoucher.max_uses_per_customer != null &&
1490
+ (customerOfferRedemptionCounts[rewardVoucher._id] ?? 0) >= rewardVoucher.max_uses_per_customer) {
1491
+ rewardVoucherLimitExhausted = true;
1492
+ break;
1474
1493
  }
1475
1494
  }
1476
1495
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1477
1496
  const isRedeemed = redeemedTimes > 0;
1478
1497
  stats.redeemed_times = redeemedTimes;
1479
1498
  stats.applicable_quantity = 1;
1480
- if (rewardVoucherLimitExhausted) {
1499
+ if (rewardVoucherUnavailable) {
1500
+ stats.claimable = false;
1501
+ stats.message = '⛔️ This reward is no longer available ⛔️';
1502
+ }
1503
+ else if (rewardVoucherLimitExhausted) {
1481
1504
  stats.claimable = false;
1482
1505
  stats.message = '⛔️ You have reached your personal limit for this reward ⛔️';
1483
1506
  }
@@ -1529,24 +1552,31 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1529
1552
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS) {
1530
1553
  if (offer.reward_gift_cards == null)
1531
1554
  continue;
1555
+ const gcIds = Object.keys(offer.reward_gift_cards.gift_card_ids);
1556
+ let rewardGiftCardUnavailable = false;
1532
1557
  let rewardGiftCardLimitExhausted = false;
1533
- if (customerOfferRedemptionCounts != null) {
1534
- const gcIds = Object.keys(offer.reward_gift_cards.gift_card_ids);
1535
- for (let gcIdx = 0; gcIdx < gcIds.length; gcIdx++) {
1536
- const giftCard = giftCardsById.get(gcIds[gcIdx]);
1537
- if (giftCard != null &&
1538
- giftCard.max_uses_per_customer != null &&
1539
- (customerOfferRedemptionCounts[giftCard._id] ?? 0) >= giftCard.max_uses_per_customer) {
1540
- rewardGiftCardLimitExhausted = true;
1541
- break;
1542
- }
1558
+ for (let gcIdx = 0; gcIdx < gcIds.length; gcIdx++) {
1559
+ const giftCard = allGiftCardsById.get(gcIds[gcIdx]);
1560
+ if (giftCard == null || isGiftCardRewardResourceUnavailable(giftCard)) {
1561
+ rewardGiftCardUnavailable = true;
1562
+ break;
1563
+ }
1564
+ if (customerOfferRedemptionCounts != null &&
1565
+ giftCard.max_uses_per_customer != null &&
1566
+ (customerOfferRedemptionCounts[giftCard._id] ?? 0) >= giftCard.max_uses_per_customer) {
1567
+ rewardGiftCardLimitExhausted = true;
1568
+ break;
1543
1569
  }
1544
1570
  }
1545
1571
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1546
1572
  const isRedeemed = redeemedTimes > 0;
1547
1573
  stats.redeemed_times = redeemedTimes;
1548
1574
  stats.applicable_quantity = 1;
1549
- if (rewardGiftCardLimitExhausted) {
1575
+ if (rewardGiftCardUnavailable) {
1576
+ stats.claimable = false;
1577
+ stats.message = '⛔️ This reward is no longer available ⛔️';
1578
+ }
1579
+ else if (rewardGiftCardLimitExhausted) {
1550
1580
  stats.claimable = false;
1551
1581
  stats.message = '⛔️ You have reached your personal limit for this reward ⛔️';
1552
1582
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.322",
3
+ "version": "0.2.324",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",