@managesome/eposmatic-toolkit 0.2.322 → 0.2.323

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) {
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.323",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",