@managesome/eposmatic-toolkit 0.2.316 → 0.2.318

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.
@@ -25,16 +25,16 @@ function getOrderSubtotalFromCategories(categories) {
25
25
  }
26
26
  return subtotal;
27
27
  }
28
- function isWalletCashbackSpecialDiscount(orderDiscountDetails) {
29
- return orderDiscountDetails?.special_discount_id === constants_1.WALLET_CASHBACK_SPECIAL_DISCOUNT_ID;
28
+ function isWalletCashbackLoyaltyDiscount(orderDiscountDetails) {
29
+ return orderDiscountDetails?.loyalty_discount_id === constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID;
30
30
  }
31
31
  function getWalletCashbackAmountFromOrderData({ orderDiscountDetails, walletRedemptions, }) {
32
- if (!isWalletCashbackSpecialDiscount(orderDiscountDetails)) {
32
+ if (!isWalletCashbackLoyaltyDiscount(orderDiscountDetails)) {
33
33
  return 0;
34
34
  }
35
- const specialDiscountAmount = Number(orderDiscountDetails?.special_discount ?? 0);
36
- if (Number.isFinite(specialDiscountAmount) && specialDiscountAmount > 0) {
37
- return (0, common_1.parseNum)(specialDiscountAmount);
35
+ const loyaltyDiscountAmount = Number(orderDiscountDetails?.loyalty_discount ?? 0);
36
+ if (Number.isFinite(loyaltyDiscountAmount) && loyaltyDiscountAmount > 0) {
37
+ return (0, common_1.parseNum)(loyaltyDiscountAmount);
38
38
  }
39
39
  const walletCashbackAmount = Number(walletRedemptions?.cashback_amount ?? 0);
40
40
  if (Number.isFinite(walletCashbackAmount) && walletCashbackAmount > 0) {
@@ -49,19 +49,21 @@ function getSyncedWalletCashbackOrderDiscountDetails({ orderDiscountDetails, wal
49
49
  if (walletCashbackAmount <= 0) {
50
50
  return {
51
51
  ...orderDiscountDetails,
52
- special_discount_id: undefined,
53
- special_discount_name: undefined,
54
- special_discount: undefined,
55
- is_special_discount_percentage: false,
56
- special_reference_number: undefined,
52
+ loyalty_discount_id: undefined,
53
+ loyalty_discount_name: undefined,
54
+ loyalty_discount: undefined,
55
+ is_loyalty_discount_percentage: false,
56
+ loyalty_reference_number: undefined,
57
57
  total_discount: 0,
58
58
  };
59
59
  }
60
60
  return {
61
61
  ...orderDiscountDetails,
62
- special_discount_id: constants_1.WALLET_CASHBACK_SPECIAL_DISCOUNT_ID,
63
- special_discount: (0, common_1.parseNum)(walletCashbackAmount),
64
- is_special_discount_percentage: false,
62
+ loyalty_discount_id: constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID,
63
+ loyalty_discount_name: constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_NAME,
64
+ loyalty_discount: (0, common_1.parseNum)(walletCashbackAmount),
65
+ is_loyalty_discount_percentage: false,
66
+ loyalty_reference_number: constants_1.WALLET_CASHBACK_LOYALTY_REFERENCE,
65
67
  };
66
68
  }
67
69
  // ================================><================================= //
@@ -137,6 +139,7 @@ function buildOfferDiscountUpdate({ offer, orderDiscountDetails, categories, })
137
139
  loyalty_discount_name: offer.name,
138
140
  loyalty_discount: discount_value,
139
141
  is_loyalty_discount_percentage: is_discount_percentage,
142
+ loyalty_reference_number: undefined,
140
143
  }
141
144
  : {
142
145
  voucher_discount_id: offer._id,
@@ -225,12 +228,12 @@ function buildOfferRewardUpdate({ offer, orderDiscountDetails, }) {
225
228
  function buildGiftCardDiscountUpdate({ giftCard, orderDiscountDetails, categories, }) {
226
229
  const rewardType = giftCard.reward_type;
227
230
  if (rewardType === constants_1.GIFT_CARD_REWARD_TYPE_ENUM.DISCOUNT) {
228
- // Already-applied guard for discount-slot-based rewards.
229
- if (orderDiscountDetails != null && orderDiscountDetails.special_discount_id) {
231
+ // Already-applied guard for loyalty-slot-based rewards.
232
+ if (orderDiscountDetails != null && orderDiscountDetails.loyalty_discount_id) {
230
233
  return {
231
234
  success: false,
232
235
  discountDetails: null,
233
- errorMessage: 'You already have a gift card or special discount applied.',
236
+ errorMessage: 'You already have a loyalty reward discount applied.',
234
237
  };
235
238
  }
236
239
  const reward = giftCard.reward_discount;
@@ -269,11 +272,11 @@ function buildGiftCardDiscountUpdate({ giftCard, orderDiscountDetails, categorie
269
272
  : discount_value;
270
273
  const discountDetails = {
271
274
  ...orderDiscountDetails,
272
- special_discount_id: giftCard._id,
273
- special_discount_name: giftCard.name,
274
- special_discount: resolvedDiscount,
275
- is_special_discount_percentage: shouldNormalizeToStatic ? false : is_discount_percentage,
276
- special_reference_number: giftCard.code,
275
+ loyalty_discount_id: giftCard._id,
276
+ loyalty_discount_name: giftCard.name,
277
+ loyalty_discount: resolvedDiscount,
278
+ is_loyalty_discount_percentage: shouldNormalizeToStatic ? false : is_discount_percentage,
279
+ loyalty_reference_number: giftCard.code.trim().toUpperCase(),
277
280
  total_discount: 0,
278
281
  };
279
282
  return { success: true, discountDetails, errorMessage: null };
@@ -509,345 +512,8 @@ function getWalletRedemptionsMap(existingWalletRedemptions) {
509
512
  }
510
513
  function getSyncOffersWithCartData(data, courses, listnerSettings, unlockedOffersApplicableTimes, // Record<offer_id, times can be applied>
511
514
  offers) {
512
- const unlockedOffersMap = {
513
- ...unlockedOffersApplicableTimes,
514
- };
515
- const removedOfferIds = new Set();
516
- let hasChanges = false;
517
- const categories = data.categories ?? [];
518
- const orderLevelDiscountBaseAmount = getOrderSubtotalFromCategories(categories);
519
- let order_discount_details = data.order_discount_details;
520
- let redeemed_offers = getNormalizedRedeemedOffersMap(data.redeemed_offers);
521
- let wallet_redemptions = getNormalizedWalletRedemptionsMap(data.wallet_redemptions);
522
- if (!isRedeemedOffersMapNormalized(data.redeemed_offers)) {
523
- hasChanges = true;
524
- }
525
- const previousWalletCashbackAmount = (0, common_1.parseNum)(Number(wallet_redemptions?.cashback_amount ?? 0));
526
- const hasWalletCashbackSpecialDiscount = isWalletCashbackSpecialDiscount(order_discount_details);
527
- if (hasWalletCashbackSpecialDiscount) {
528
- const requestedWalletCashbackAmount = getWalletCashbackAmountFromOrderData({
529
- orderDiscountDetails: order_discount_details,
530
- walletRedemptions: wallet_redemptions,
531
- });
532
- const maxApplicableWalletCashback = (0, common_1.parseNum)(Math.max(0, orderLevelDiscountBaseAmount));
533
- const syncedWalletCashbackAmount = (0, common_1.parseNum)(Math.min(requestedWalletCashbackAmount, maxApplicableWalletCashback));
534
- const prevSpecialDiscountId = order_discount_details?.special_discount_id;
535
- const prevSpecialDiscountAmount = (0, common_1.parseNum)(Number(order_discount_details?.special_discount ?? 0));
536
- const prevIsSpecialDiscountPercentage = order_discount_details?.is_special_discount_percentage === true;
537
- const nextOrderDiscountDetails = getSyncedWalletCashbackOrderDiscountDetails({
538
- orderDiscountDetails: order_discount_details,
539
- walletCashbackAmount: syncedWalletCashbackAmount,
540
- });
541
- const nextSpecialDiscountId = nextOrderDiscountDetails?.special_discount_id;
542
- const nextSpecialDiscountAmount = (0, common_1.parseNum)(Number(nextOrderDiscountDetails?.special_discount ?? 0));
543
- const nextIsSpecialDiscountPercentage = nextOrderDiscountDetails?.is_special_discount_percentage === true;
544
- if (prevSpecialDiscountId !== nextSpecialDiscountId ||
545
- prevSpecialDiscountAmount !== nextSpecialDiscountAmount ||
546
- prevIsSpecialDiscountPercentage !== nextIsSpecialDiscountPercentage) {
547
- hasChanges = true;
548
- }
549
- order_discount_details = nextOrderDiscountDetails;
550
- wallet_redemptions = getNormalizedWalletRedemptionsMap({
551
- ...(wallet_redemptions ?? {}),
552
- cashback_amount: syncedWalletCashbackAmount > 0 ? syncedWalletCashbackAmount : undefined,
553
- });
554
- const nextWalletCashbackAmount = (0, common_1.parseNum)(Number(wallet_redemptions?.cashback_amount ?? 0));
555
- if (previousWalletCashbackAmount !== nextWalletCashbackAmount) {
556
- hasChanges = true;
557
- }
558
- }
559
- else if (previousWalletCashbackAmount > 0) {
560
- // Offers can reward cashback into wallet, but cannot activate cashback discount on current order.
561
- wallet_redemptions = getNormalizedWalletRedemptionsMap({
562
- ...(wallet_redemptions ?? {}),
563
- cashback_amount: undefined,
564
- });
565
- hasChanges = true;
566
- }
567
- const hasNonWalletSpecialDiscount = order_discount_details?.special_discount_id != null &&
568
- order_discount_details.special_discount_id !== constants_1.WALLET_CASHBACK_SPECIAL_DISCOUNT_ID;
569
- const walletGiftCardRedeemedTimes = Number(wallet_redemptions?.gift_cards?.value_redeemed ?? 0);
570
- const hasWalletGiftCardMetadata = wallet_redemptions?.gift_cards?.gift_card_id != null &&
571
- wallet_redemptions.gift_cards.gift_card_id.trim().length > 0 &&
572
- Number.isFinite(walletGiftCardRedeemedTimes) &&
573
- walletGiftCardRedeemedTimes > 0;
574
- // Empty carts must not retain non-wallet special discounts or gift-card wallet claims.
575
- if (orderLevelDiscountBaseAmount <= 0) {
576
- if (hasNonWalletSpecialDiscount && order_discount_details != null) {
577
- order_discount_details = {
578
- ...order_discount_details,
579
- special_discount_id: undefined,
580
- special_discount_name: undefined,
581
- special_discount: undefined,
582
- is_special_discount_percentage: false,
583
- special_reference_number: undefined,
584
- total_discount: 0,
585
- };
586
- hasChanges = true;
587
- }
588
- if (redeemed_offers != null) {
589
- redeemed_offers = null;
590
- hasChanges = true;
591
- }
592
- if (hasWalletGiftCardMetadata) {
593
- wallet_redemptions = getNormalizedWalletRedemptionsMap({
594
- ...(wallet_redemptions ?? {}),
595
- gift_cards: undefined,
596
- });
597
- hasChanges = true;
598
- }
599
- }
600
- const redeemedOffersPresenceMap = getReconciledRedeemedOffersMap({
601
- existingRedeemedOffers: redeemed_offers,
602
- categories,
603
- offers,
604
- removedOfferIds: new Set(),
515
+ return (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(data, courses, listnerSettings, undefined, {
605
516
  unlockedOffersApplicableTimes,
606
- });
607
- const clearOrderLevelDiscount = (discountType) => {
608
- if (order_discount_details == null) {
609
- return;
610
- }
611
- const appliedOfferId = discountType === 'loyalty'
612
- ? order_discount_details.loyalty_discount_id
613
- : order_discount_details.voucher_discount_id;
614
- const normalizedAppliedOfferId = getNormalizedOfferId(appliedOfferId);
615
- if (normalizedAppliedOfferId != null) {
616
- removedOfferIds.add(normalizedAppliedOfferId);
617
- }
618
- if (discountType === 'loyalty') {
619
- order_discount_details = {
620
- ...order_discount_details,
621
- loyalty_discount_id: undefined,
622
- loyalty_discount_name: undefined,
623
- loyalty_discount: undefined,
624
- is_loyalty_discount_percentage: false,
625
- };
626
- }
627
- else {
628
- order_discount_details = {
629
- ...order_discount_details,
630
- voucher_discount_id: undefined,
631
- voucher_discount_name: undefined,
632
- voucher_discount: undefined,
633
- is_voucher_discount_percentage: false,
634
- };
635
- }
636
- hasChanges = true;
637
- };
638
- if (order_discount_details != null) {
639
- // we check if either the loyalty_discount_id or voucher_discount_id
640
- const loyaltyDiscountId = order_discount_details.loyalty_discount_id;
641
- if (loyaltyDiscountId != null) {
642
- const normalizedLoyaltyDiscountId = getNormalizedOfferId(loyaltyDiscountId);
643
- const hasRedeemedEntryForLoyalty = normalizedLoyaltyDiscountId != null && (redeemedOffersPresenceMap?.[normalizedLoyaltyDiscountId] ?? 0) > 0;
644
- if (!hasRedeemedEntryForLoyalty) {
645
- clearOrderLevelDiscount('loyalty');
646
- }
647
- else if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
648
- clearOrderLevelDiscount('loyalty');
649
- }
650
- else {
651
- // Find the offer to check for excluded_refs
652
- const loyaltyOffer = offers.find((o) => o._id === loyaltyDiscountId);
653
- if (loyaltyOffer?.reward_discount != null) {
654
- const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = loyaltyOffer.reward_discount;
655
- const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
656
- ? (0, cart_actions_1.calculateEligibleOfferDiscountDetails)(categories, excluded_refs)
657
- : {
658
- subtotal: orderLevelDiscountBaseAmount,
659
- hasExcludedRefs: false,
660
- };
661
- // Static discounts with exclusions are not supported - silently skip
662
- if (hasExcludedRefs && !is_discount_percentage) {
663
- clearOrderLevelDiscount('loyalty');
664
- }
665
- else {
666
- const hasRelevantCap = is_discount_percentage &&
667
- max_discount_amount != null &&
668
- Number.isFinite(max_discount_amount) &&
669
- max_discount_amount >= 0;
670
- const hasRelevantPercentageCap = !is_discount_percentage &&
671
- max_discount_percentage != null &&
672
- Number.isFinite(max_discount_percentage) &&
673
- max_discount_percentage >= 0;
674
- const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
675
- const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
676
- if (shouldNormalizeToStatic) {
677
- if (discountBaseAmount <= 0) {
678
- clearOrderLevelDiscount('loyalty');
679
- }
680
- else {
681
- const calculatedDiscount = (0, common_1.getCappedOfferDiscountAmount)({
682
- baseAmount: discountBaseAmount,
683
- discountValue: discount_value,
684
- isDiscountPercentage: is_discount_percentage,
685
- maxDiscountAmount: max_discount_amount,
686
- maxDiscountPercentage: max_discount_percentage,
687
- });
688
- order_discount_details = {
689
- ...order_discount_details,
690
- loyalty_discount: (0, common_1.parseNum)(calculatedDiscount),
691
- is_loyalty_discount_percentage: false,
692
- };
693
- hasChanges = true;
694
- }
695
- }
696
- }
697
- }
698
- unlockedOffersMap[loyaltyDiscountId] -= 1;
699
- }
700
- }
701
- const voucherDiscountId = order_discount_details.voucher_discount_id;
702
- if (voucherDiscountId != null) {
703
- const normalizedVoucherDiscountId = getNormalizedOfferId(voucherDiscountId);
704
- const hasRedeemedEntryForVoucher = normalizedVoucherDiscountId != null && (redeemedOffersPresenceMap?.[normalizedVoucherDiscountId] ?? 0) > 0;
705
- if (!hasRedeemedEntryForVoucher) {
706
- clearOrderLevelDiscount('voucher');
707
- }
708
- else if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
709
- clearOrderLevelDiscount('voucher');
710
- }
711
- else {
712
- // Find the offer to check for excluded_refs
713
- const voucherOffer = offers.find((o) => o._id === voucherDiscountId);
714
- if (voucherOffer?.reward_discount != null) {
715
- const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = voucherOffer.reward_discount;
716
- const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
717
- ? (0, cart_actions_1.calculateEligibleOfferDiscountDetails)(categories, excluded_refs)
718
- : {
719
- subtotal: orderLevelDiscountBaseAmount,
720
- hasExcludedRefs: false,
721
- };
722
- // Static discounts with exclusions are not supported - silently skip
723
- if (hasExcludedRefs && !is_discount_percentage) {
724
- clearOrderLevelDiscount('voucher');
725
- }
726
- else {
727
- const hasRelevantCap = is_discount_percentage &&
728
- max_discount_amount != null &&
729
- Number.isFinite(max_discount_amount) &&
730
- max_discount_amount >= 0;
731
- const hasRelevantPercentageCap = !is_discount_percentage &&
732
- max_discount_percentage != null &&
733
- Number.isFinite(max_discount_percentage) &&
734
- max_discount_percentage >= 0;
735
- const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
736
- const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
737
- if (shouldNormalizeToStatic) {
738
- if (discountBaseAmount <= 0) {
739
- clearOrderLevelDiscount('voucher');
740
- }
741
- else {
742
- const calculatedDiscount = (0, common_1.getCappedOfferDiscountAmount)({
743
- baseAmount: discountBaseAmount,
744
- discountValue: discount_value,
745
- isDiscountPercentage: is_discount_percentage,
746
- maxDiscountAmount: max_discount_amount,
747
- maxDiscountPercentage: max_discount_percentage,
748
- });
749
- order_discount_details = {
750
- ...order_discount_details,
751
- voucher_discount: (0, common_1.parseNum)(calculatedDiscount),
752
- is_voucher_discount_percentage: false,
753
- };
754
- hasChanges = true;
755
- }
756
- }
757
- }
758
- }
759
- unlockedOffersMap[voucherDiscountId] -= 1;
760
- }
761
- }
762
- }
763
- const newCategories = [];
764
- for (let i = 0; i < categories.length; i++) {
765
- const category = categories[i];
766
- const newItems = [];
767
- for (let j = 0; j < category.items.length; j++) {
768
- const item = category.items[j];
769
- // we check if either the loyalty_discount_id or voucher_discount_id
770
- // is present in the item_discount_details
771
- // if it is then we remove the items from cart
772
- if (item.item_discount_details != null) {
773
- const loyaltyDiscountId = item.item_discount_details.loyalty_discount_id;
774
- if (loyaltyDiscountId != null) {
775
- const normalizedLoyaltyDiscountId = getNormalizedOfferId(loyaltyDiscountId);
776
- const hasRedeemedEntryForLoyalty = normalizedLoyaltyDiscountId != null && (redeemedOffersPresenceMap?.[normalizedLoyaltyDiscountId] ?? 0) > 0;
777
- if (!hasRedeemedEntryForLoyalty) {
778
- hasChanges = true;
779
- if (normalizedLoyaltyDiscountId != null) {
780
- removedOfferIds.add(normalizedLoyaltyDiscountId);
781
- }
782
- continue;
783
- }
784
- if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
785
- hasChanges = true;
786
- if (normalizedLoyaltyDiscountId != null) {
787
- removedOfferIds.add(normalizedLoyaltyDiscountId);
788
- }
789
- continue;
790
- }
791
- else {
792
- unlockedOffersMap[loyaltyDiscountId] -= toPositiveInteger(item.quantity, 1);
793
- }
794
- }
795
- const voucherDiscountId = item.item_discount_details.voucher_discount_id;
796
- if (voucherDiscountId != null) {
797
- const normalizedVoucherDiscountId = getNormalizedOfferId(voucherDiscountId);
798
- const hasRedeemedEntryForVoucher = normalizedVoucherDiscountId != null && (redeemedOffersPresenceMap?.[normalizedVoucherDiscountId] ?? 0) > 0;
799
- if (!hasRedeemedEntryForVoucher) {
800
- hasChanges = true;
801
- if (normalizedVoucherDiscountId != null) {
802
- removedOfferIds.add(normalizedVoucherDiscountId);
803
- }
804
- continue;
805
- }
806
- if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
807
- hasChanges = true;
808
- if (normalizedVoucherDiscountId != null) {
809
- removedOfferIds.add(normalizedVoucherDiscountId);
810
- }
811
- continue;
812
- }
813
- else {
814
- unlockedOffersMap[voucherDiscountId] -= toPositiveInteger(item.quantity, 1);
815
- }
816
- }
817
- newItems.push(item);
818
- }
819
- else {
820
- newItems.push(item);
821
- }
822
- }
823
- newCategories.push({
824
- ...category,
825
- items: newItems,
826
- });
827
- }
828
- const reconciledRedeemedOffers = getReconciledRedeemedOffersMap({
829
- existingRedeemedOffers: redeemed_offers,
830
- categories: newCategories,
831
517
  offers,
832
- removedOfferIds,
833
- unlockedOffersApplicableTimes,
834
518
  });
835
- if (!areRedeemedOffersEqual(redeemed_offers, reconciledRedeemedOffers)) {
836
- redeemed_offers = reconciledRedeemedOffers;
837
- hasChanges = true;
838
- }
839
- if (hasChanges) {
840
- const order = {
841
- ...data,
842
- categories: newCategories,
843
- order_discount_details,
844
- redeemed_offers,
845
- wallet_redemptions: getWalletRedemptionsMap(wallet_redemptions),
846
- };
847
- const newData = (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(order, courses, listnerSettings);
848
- return newData;
849
- }
850
- else {
851
- return data;
852
- }
853
519
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.316",
3
+ "version": "0.2.318",
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
8
  "test": "jest --config jestconfig.json",
9
- "build": "tsc",
9
+ "build": "npm run test && tsc",
10
10
  "prepare": "npm run build",
11
11
  "prepublishOnly": "npm run lint",
12
12
  "preversion": "npm run lint",