@managesome/eposmatic-toolkit 0.2.317 → 0.2.319

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.
@@ -9,6 +9,7 @@ export declare function getOrderItemModifiersWithPrice(modifiers: IOrderModifier
9
9
  export declare function getOrderItemsWithPrice(items: IOrderItem[] | undefined, convertToPrice: (price: number) => number): IOrderItem[];
10
10
  export declare function getOrderCategoriesWithPrice(categories: IOrderCategory[] | undefined, convertToPrice: (price: number) => number): IOrderCategory[];
11
11
  export declare function getOrderDiscountDetailsWithPrice(discount: IDiscountDetails | null | undefined, convertToPrice: (price: number) => number): IDiscountDetails | null;
12
+ export declare function getOrderWalletRedemptionsWithPrice(walletRedemptions: IOrder['wallet_redemptions'] | null | undefined, convertToPrice: (price: number) => number): IOrder['wallet_redemptions'];
12
13
  export declare function sortByCreatedAt<T extends {
13
14
  createdAt: Date;
14
15
  }>(a: T, b: T): number;
@@ -11,6 +11,7 @@ exports.getOrderItemModifiersWithPrice = getOrderItemModifiersWithPrice;
11
11
  exports.getOrderItemsWithPrice = getOrderItemsWithPrice;
12
12
  exports.getOrderCategoriesWithPrice = getOrderCategoriesWithPrice;
13
13
  exports.getOrderDiscountDetailsWithPrice = getOrderDiscountDetailsWithPrice;
14
+ exports.getOrderWalletRedemptionsWithPrice = getOrderWalletRedemptionsWithPrice;
14
15
  exports.sortByCreatedAt = sortByCreatedAt;
15
16
  exports.sortByIdTimestamp = sortByIdTimestamp;
16
17
  exports.getOrderWithPrice = getOrderWithPrice;
@@ -108,6 +109,43 @@ function getOrderDiscountDetailsWithPrice(discount, convertToPrice) {
108
109
  total_discount: convertToPrice(total_discount ?? 0),
109
110
  };
110
111
  }
112
+ function getOrderWalletRedemptionsWithPrice(walletRedemptions, convertToPrice) {
113
+ if (walletRedemptions == null)
114
+ return null;
115
+ const normalizedGiftCardRedemptions = (() => {
116
+ const giftCardRedemptions = walletRedemptions.gift_cards;
117
+ if (!Array.isArray(giftCardRedemptions))
118
+ return null;
119
+ const normalizedGiftCardRedemptionsMap = {};
120
+ for (let i = 0; i < giftCardRedemptions.length; i++) {
121
+ const giftCardRedemption = giftCardRedemptions[i];
122
+ const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
123
+ const redeemedTimes = Number(giftCardRedemption.redeemed_times);
124
+ if (giftCardId == null || giftCardId.length === 0 || !Number.isFinite(redeemedTimes) || redeemedTimes <= 0) {
125
+ continue;
126
+ }
127
+ const existingRedemption = normalizedGiftCardRedemptionsMap[giftCardId];
128
+ normalizedGiftCardRedemptionsMap[giftCardId] = {
129
+ gift_card_id: giftCardId,
130
+ redeemed_times: (existingRedemption?.redeemed_times ?? 0) + Math.floor(redeemedTimes),
131
+ ...(giftCardRedemption.mystery_gift_type != null
132
+ ? { mystery_gift_type: giftCardRedemption.mystery_gift_type }
133
+ : existingRedemption?.mystery_gift_type != null
134
+ ? { mystery_gift_type: existingRedemption.mystery_gift_type }
135
+ : {}),
136
+ };
137
+ }
138
+ const dedupedGiftCardRedemptions = Object.values(normalizedGiftCardRedemptionsMap);
139
+ return dedupedGiftCardRedemptions.length > 0 ? dedupedGiftCardRedemptions : null;
140
+ })();
141
+ return {
142
+ ...walletRedemptions,
143
+ ...(walletRedemptions.cashback_amount != null
144
+ ? { cashback_amount: convertToPrice(walletRedemptions.cashback_amount) }
145
+ : {}),
146
+ ...(normalizedGiftCardRedemptions != null ? { gift_cards: normalizedGiftCardRedemptions } : {}),
147
+ };
148
+ }
111
149
  function sortByCreatedAt(a, b) {
112
150
  return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
113
151
  }
@@ -167,6 +205,9 @@ function getOrderWithPrice(order, toCents) {
167
205
  },
168
206
  }
169
207
  : {}),
208
+ ...(order.wallet_redemptions != null
209
+ ? { wallet_redemptions: getOrderWalletRedemptionsWithPrice(order.wallet_redemptions, convertToPrice) }
210
+ : {}),
170
211
  };
171
212
  if (toCents) {
172
213
  const orderType = order.platform != null ? order.platform.split('_')[1] : null;
@@ -491,19 +491,32 @@ function getNormalizedWalletRedemptionsMap(walletRedemptions) {
491
491
  if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
492
492
  normalized.cashback_amount = cashbackAmount;
493
493
  }
494
- const giftCardRedemption = walletRedemptions.gift_cards;
495
- if (giftCardRedemption != null) {
496
- const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
497
- const redeemedTimes = toPositiveInteger(giftCardRedemption.value_redeemed, 0);
498
- if (giftCardId != null && giftCardId.length > 0 && redeemedTimes > 0) {
499
- normalized.gift_cards = {
494
+ const giftCardRedemptions = walletRedemptions.gift_cards;
495
+ if (Array.isArray(giftCardRedemptions)) {
496
+ const normalizedGiftCardRedemptionsMap = {};
497
+ for (let i = 0; i < giftCardRedemptions.length; i++) {
498
+ const giftCardRedemption = giftCardRedemptions[i];
499
+ const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
500
+ const legacyValueRedeemed = giftCardRedemption.value_redeemed;
501
+ const redeemedTimes = toPositiveInteger(giftCardRedemption.redeemed_times ?? legacyValueRedeemed, 0);
502
+ if (giftCardId == null || giftCardId.length === 0 || redeemedTimes <= 0) {
503
+ continue;
504
+ }
505
+ const existingRedemption = normalizedGiftCardRedemptionsMap[giftCardId];
506
+ normalizedGiftCardRedemptionsMap[giftCardId] = {
500
507
  gift_card_id: giftCardId,
501
- value_redeemed: redeemedTimes,
508
+ redeemed_times: (existingRedemption?.redeemed_times ?? 0) + redeemedTimes,
502
509
  ...(giftCardRedemption.mystery_gift_type != null
503
510
  ? { mystery_gift_type: giftCardRedemption.mystery_gift_type }
504
- : {}),
511
+ : existingRedemption?.mystery_gift_type != null
512
+ ? { mystery_gift_type: existingRedemption.mystery_gift_type }
513
+ : {}),
505
514
  };
506
515
  }
516
+ const normalizedGiftCardRedemptions = Object.values(normalizedGiftCardRedemptionsMap);
517
+ if (normalizedGiftCardRedemptions.length > 0) {
518
+ normalized.gift_cards = normalizedGiftCardRedemptions;
519
+ }
507
520
  }
508
521
  return Object.keys(normalized).length > 0 ? normalized : null;
509
522
  }
@@ -512,369 +525,8 @@ function getWalletRedemptionsMap(existingWalletRedemptions) {
512
525
  }
513
526
  function getSyncOffersWithCartData(data, courses, listnerSettings, unlockedOffersApplicableTimes, // Record<offer_id, times can be applied>
514
527
  offers) {
515
- const unlockedOffersMap = {
516
- ...unlockedOffersApplicableTimes,
517
- };
518
- const removedOfferIds = new Set();
519
- let hasChanges = false;
520
- const categories = data.categories ?? [];
521
- const orderLevelDiscountBaseAmount = getOrderSubtotalFromCategories(categories);
522
- let order_discount_details = data.order_discount_details;
523
- let redeemed_offers = getNormalizedRedeemedOffersMap(data.redeemed_offers);
524
- let wallet_redemptions = getNormalizedWalletRedemptionsMap(data.wallet_redemptions);
525
- if (!isRedeemedOffersMapNormalized(data.redeemed_offers)) {
526
- hasChanges = true;
527
- }
528
- const previousWalletCashbackAmount = (0, common_1.parseNum)(Number(wallet_redemptions?.cashback_amount ?? 0));
529
- const hasWalletCashbackLoyaltyDiscount = isWalletCashbackLoyaltyDiscount(order_discount_details);
530
- if (hasWalletCashbackLoyaltyDiscount) {
531
- const requestedWalletCashbackAmount = getWalletCashbackAmountFromOrderData({
532
- orderDiscountDetails: order_discount_details,
533
- walletRedemptions: wallet_redemptions,
534
- });
535
- const maxApplicableWalletCashback = (0, common_1.parseNum)(Math.max(0, orderLevelDiscountBaseAmount));
536
- const syncedWalletCashbackAmount = (0, common_1.parseNum)(Math.min(requestedWalletCashbackAmount, maxApplicableWalletCashback));
537
- const prevLoyaltyDiscountId = order_discount_details?.loyalty_discount_id;
538
- const prevLoyaltyDiscountAmount = (0, common_1.parseNum)(Number(order_discount_details?.loyalty_discount ?? 0));
539
- const prevIsLoyaltyDiscountPercentage = order_discount_details?.is_loyalty_discount_percentage === true;
540
- const prevLoyaltyReferenceNumber = order_discount_details?.loyalty_reference_number;
541
- const nextOrderDiscountDetails = getSyncedWalletCashbackOrderDiscountDetails({
542
- orderDiscountDetails: order_discount_details,
543
- walletCashbackAmount: syncedWalletCashbackAmount,
544
- });
545
- const nextLoyaltyDiscountId = nextOrderDiscountDetails?.loyalty_discount_id;
546
- const nextLoyaltyDiscountAmount = (0, common_1.parseNum)(Number(nextOrderDiscountDetails?.loyalty_discount ?? 0));
547
- const nextIsLoyaltyDiscountPercentage = nextOrderDiscountDetails?.is_loyalty_discount_percentage === true;
548
- const nextLoyaltyReferenceNumber = nextOrderDiscountDetails?.loyalty_reference_number;
549
- if (prevLoyaltyDiscountId !== nextLoyaltyDiscountId ||
550
- prevLoyaltyDiscountAmount !== nextLoyaltyDiscountAmount ||
551
- prevIsLoyaltyDiscountPercentage !== nextIsLoyaltyDiscountPercentage ||
552
- prevLoyaltyReferenceNumber !== nextLoyaltyReferenceNumber) {
553
- hasChanges = true;
554
- }
555
- order_discount_details = nextOrderDiscountDetails;
556
- wallet_redemptions = getNormalizedWalletRedemptionsMap({
557
- ...(wallet_redemptions ?? {}),
558
- cashback_amount: syncedWalletCashbackAmount > 0 ? syncedWalletCashbackAmount : undefined,
559
- });
560
- const nextWalletCashbackAmount = (0, common_1.parseNum)(Number(wallet_redemptions?.cashback_amount ?? 0));
561
- if (previousWalletCashbackAmount !== nextWalletCashbackAmount) {
562
- hasChanges = true;
563
- }
564
- }
565
- else if (previousWalletCashbackAmount > 0) {
566
- // Offers can reward cashback into wallet, but cannot activate cashback discount on current order.
567
- wallet_redemptions = getNormalizedWalletRedemptionsMap({
568
- ...(wallet_redemptions ?? {}),
569
- cashback_amount: undefined,
570
- });
571
- hasChanges = true;
572
- }
573
- const hasOrderLevelLoyaltyDiscount = order_discount_details?.loyalty_discount_id != null;
574
- const hasOrderLevelVoucherDiscount = order_discount_details?.voucher_discount_id != null;
575
- const walletGiftCardRedeemedTimes = Number(wallet_redemptions?.gift_cards?.value_redeemed ?? 0);
576
- const hasWalletGiftCardMetadata = wallet_redemptions?.gift_cards?.gift_card_id != null &&
577
- wallet_redemptions.gift_cards.gift_card_id.trim().length > 0 &&
578
- Number.isFinite(walletGiftCardRedeemedTimes) &&
579
- walletGiftCardRedeemedTimes > 0;
580
- // Empty carts must not retain reward-driven loyalty/voucher discounts or gift-card wallet claims.
581
- if (orderLevelDiscountBaseAmount <= 0) {
582
- if (hasOrderLevelLoyaltyDiscount && order_discount_details != null) {
583
- order_discount_details = {
584
- ...order_discount_details,
585
- loyalty_discount_id: undefined,
586
- loyalty_discount_name: undefined,
587
- loyalty_discount: undefined,
588
- is_loyalty_discount_percentage: false,
589
- loyalty_reference_number: undefined,
590
- total_discount: 0,
591
- };
592
- hasChanges = true;
593
- }
594
- if (hasOrderLevelVoucherDiscount && order_discount_details != null) {
595
- order_discount_details = {
596
- ...order_discount_details,
597
- voucher_discount_id: undefined,
598
- voucher_discount_name: undefined,
599
- voucher_discount: undefined,
600
- is_voucher_discount_percentage: false,
601
- total_discount: 0,
602
- };
603
- hasChanges = true;
604
- }
605
- if (redeemed_offers != null) {
606
- redeemed_offers = null;
607
- hasChanges = true;
608
- }
609
- if (hasWalletGiftCardMetadata) {
610
- wallet_redemptions = getNormalizedWalletRedemptionsMap({
611
- ...(wallet_redemptions ?? {}),
612
- gift_cards: undefined,
613
- });
614
- hasChanges = true;
615
- }
616
- }
617
- const redeemedOffersPresenceMap = getReconciledRedeemedOffersMap({
618
- existingRedeemedOffers: redeemed_offers,
619
- categories,
620
- offers,
621
- removedOfferIds: new Set(),
528
+ return (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(data, courses, listnerSettings, undefined, {
622
529
  unlockedOffersApplicableTimes,
623
- });
624
- const clearOrderLevelDiscount = (discountType) => {
625
- if (order_discount_details == null) {
626
- return;
627
- }
628
- const appliedOfferId = discountType === 'loyalty'
629
- ? order_discount_details.loyalty_discount_id
630
- : order_discount_details.voucher_discount_id;
631
- const normalizedAppliedOfferId = getNormalizedOfferId(appliedOfferId);
632
- if (normalizedAppliedOfferId != null) {
633
- removedOfferIds.add(normalizedAppliedOfferId);
634
- }
635
- if (discountType === 'loyalty') {
636
- order_discount_details = {
637
- ...order_discount_details,
638
- loyalty_discount_id: undefined,
639
- loyalty_discount_name: undefined,
640
- loyalty_discount: undefined,
641
- is_loyalty_discount_percentage: false,
642
- loyalty_reference_number: undefined,
643
- };
644
- }
645
- else {
646
- order_discount_details = {
647
- ...order_discount_details,
648
- voucher_discount_id: undefined,
649
- voucher_discount_name: undefined,
650
- voucher_discount: undefined,
651
- is_voucher_discount_percentage: false,
652
- };
653
- }
654
- hasChanges = true;
655
- };
656
- if (order_discount_details != null) {
657
- // we check if either the loyalty_discount_id or voucher_discount_id
658
- const loyaltyDiscountId = order_discount_details.loyalty_discount_id;
659
- if (loyaltyDiscountId != null) {
660
- if (loyaltyDiscountId === constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID) {
661
- // Wallet cashback is synced separately above and does not participate in offer unlock accounting.
662
- }
663
- else {
664
- const loyaltyOffer = offers.find((o) => o._id === loyaltyDiscountId);
665
- if (loyaltyOffer == null) {
666
- // Preserve non-offer loyalty discounts (for example gift-card discounts).
667
- }
668
- else {
669
- const normalizedLoyaltyDiscountId = getNormalizedOfferId(loyaltyDiscountId);
670
- const hasRedeemedEntryForLoyalty = normalizedLoyaltyDiscountId != null && (redeemedOffersPresenceMap?.[normalizedLoyaltyDiscountId] ?? 0) > 0;
671
- if (!hasRedeemedEntryForLoyalty) {
672
- clearOrderLevelDiscount('loyalty');
673
- }
674
- else if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
675
- clearOrderLevelDiscount('loyalty');
676
- }
677
- else {
678
- if (loyaltyOffer.reward_discount != null) {
679
- const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = loyaltyOffer.reward_discount;
680
- const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
681
- ? (0, cart_actions_1.calculateEligibleOfferDiscountDetails)(categories, excluded_refs)
682
- : {
683
- subtotal: orderLevelDiscountBaseAmount,
684
- hasExcludedRefs: false,
685
- };
686
- // Static discounts with exclusions are not supported - silently skip
687
- if (hasExcludedRefs && !is_discount_percentage) {
688
- clearOrderLevelDiscount('loyalty');
689
- }
690
- else {
691
- const hasRelevantCap = is_discount_percentage &&
692
- max_discount_amount != null &&
693
- Number.isFinite(max_discount_amount) &&
694
- max_discount_amount >= 0;
695
- const hasRelevantPercentageCap = !is_discount_percentage &&
696
- max_discount_percentage != null &&
697
- Number.isFinite(max_discount_percentage) &&
698
- max_discount_percentage >= 0;
699
- const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
700
- const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
701
- if (shouldNormalizeToStatic) {
702
- if (discountBaseAmount <= 0) {
703
- clearOrderLevelDiscount('loyalty');
704
- }
705
- else {
706
- const calculatedDiscount = (0, common_1.getCappedOfferDiscountAmount)({
707
- baseAmount: discountBaseAmount,
708
- discountValue: discount_value,
709
- isDiscountPercentage: is_discount_percentage,
710
- maxDiscountAmount: max_discount_amount,
711
- maxDiscountPercentage: max_discount_percentage,
712
- });
713
- order_discount_details = {
714
- ...order_discount_details,
715
- loyalty_discount: (0, common_1.parseNum)(calculatedDiscount),
716
- is_loyalty_discount_percentage: false,
717
- };
718
- hasChanges = true;
719
- }
720
- }
721
- }
722
- }
723
- unlockedOffersMap[loyaltyDiscountId] -= 1;
724
- }
725
- }
726
- }
727
- }
728
- const voucherDiscountId = order_discount_details.voucher_discount_id;
729
- if (voucherDiscountId != null) {
730
- const normalizedVoucherDiscountId = getNormalizedOfferId(voucherDiscountId);
731
- const hasRedeemedEntryForVoucher = normalizedVoucherDiscountId != null && (redeemedOffersPresenceMap?.[normalizedVoucherDiscountId] ?? 0) > 0;
732
- if (!hasRedeemedEntryForVoucher) {
733
- clearOrderLevelDiscount('voucher');
734
- }
735
- else if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
736
- clearOrderLevelDiscount('voucher');
737
- }
738
- else {
739
- // Find the offer to check for excluded_refs
740
- const voucherOffer = offers.find((o) => o._id === voucherDiscountId);
741
- if (voucherOffer?.reward_discount != null) {
742
- const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = voucherOffer.reward_discount;
743
- const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
744
- ? (0, cart_actions_1.calculateEligibleOfferDiscountDetails)(categories, excluded_refs)
745
- : {
746
- subtotal: orderLevelDiscountBaseAmount,
747
- hasExcludedRefs: false,
748
- };
749
- // Static discounts with exclusions are not supported - silently skip
750
- if (hasExcludedRefs && !is_discount_percentage) {
751
- clearOrderLevelDiscount('voucher');
752
- }
753
- else {
754
- const hasRelevantCap = is_discount_percentage &&
755
- max_discount_amount != null &&
756
- Number.isFinite(max_discount_amount) &&
757
- max_discount_amount >= 0;
758
- const hasRelevantPercentageCap = !is_discount_percentage &&
759
- max_discount_percentage != null &&
760
- Number.isFinite(max_discount_percentage) &&
761
- max_discount_percentage >= 0;
762
- const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
763
- const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
764
- if (shouldNormalizeToStatic) {
765
- if (discountBaseAmount <= 0) {
766
- clearOrderLevelDiscount('voucher');
767
- }
768
- else {
769
- const calculatedDiscount = (0, common_1.getCappedOfferDiscountAmount)({
770
- baseAmount: discountBaseAmount,
771
- discountValue: discount_value,
772
- isDiscountPercentage: is_discount_percentage,
773
- maxDiscountAmount: max_discount_amount,
774
- maxDiscountPercentage: max_discount_percentage,
775
- });
776
- order_discount_details = {
777
- ...order_discount_details,
778
- voucher_discount: (0, common_1.parseNum)(calculatedDiscount),
779
- is_voucher_discount_percentage: false,
780
- };
781
- hasChanges = true;
782
- }
783
- }
784
- }
785
- }
786
- unlockedOffersMap[voucherDiscountId] -= 1;
787
- }
788
- }
789
- }
790
- const newCategories = [];
791
- for (let i = 0; i < categories.length; i++) {
792
- const category = categories[i];
793
- const newItems = [];
794
- for (let j = 0; j < category.items.length; j++) {
795
- const item = category.items[j];
796
- // we check if either the loyalty_discount_id or voucher_discount_id
797
- // is present in the item_discount_details
798
- // if it is then we remove the items from cart
799
- if (item.item_discount_details != null) {
800
- const loyaltyDiscountId = item.item_discount_details.loyalty_discount_id;
801
- if (loyaltyDiscountId != null) {
802
- const normalizedLoyaltyDiscountId = getNormalizedOfferId(loyaltyDiscountId);
803
- const hasRedeemedEntryForLoyalty = normalizedLoyaltyDiscountId != null && (redeemedOffersPresenceMap?.[normalizedLoyaltyDiscountId] ?? 0) > 0;
804
- if (!hasRedeemedEntryForLoyalty) {
805
- hasChanges = true;
806
- if (normalizedLoyaltyDiscountId != null) {
807
- removedOfferIds.add(normalizedLoyaltyDiscountId);
808
- }
809
- continue;
810
- }
811
- if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
812
- hasChanges = true;
813
- if (normalizedLoyaltyDiscountId != null) {
814
- removedOfferIds.add(normalizedLoyaltyDiscountId);
815
- }
816
- continue;
817
- }
818
- else {
819
- unlockedOffersMap[loyaltyDiscountId] -= toPositiveInteger(item.quantity, 1);
820
- }
821
- }
822
- const voucherDiscountId = item.item_discount_details.voucher_discount_id;
823
- if (voucherDiscountId != null) {
824
- const normalizedVoucherDiscountId = getNormalizedOfferId(voucherDiscountId);
825
- const hasRedeemedEntryForVoucher = normalizedVoucherDiscountId != null && (redeemedOffersPresenceMap?.[normalizedVoucherDiscountId] ?? 0) > 0;
826
- if (!hasRedeemedEntryForVoucher) {
827
- hasChanges = true;
828
- if (normalizedVoucherDiscountId != null) {
829
- removedOfferIds.add(normalizedVoucherDiscountId);
830
- }
831
- continue;
832
- }
833
- if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
834
- hasChanges = true;
835
- if (normalizedVoucherDiscountId != null) {
836
- removedOfferIds.add(normalizedVoucherDiscountId);
837
- }
838
- continue;
839
- }
840
- else {
841
- unlockedOffersMap[voucherDiscountId] -= toPositiveInteger(item.quantity, 1);
842
- }
843
- }
844
- newItems.push(item);
845
- }
846
- else {
847
- newItems.push(item);
848
- }
849
- }
850
- newCategories.push({
851
- ...category,
852
- items: newItems,
853
- });
854
- }
855
- const reconciledRedeemedOffers = getReconciledRedeemedOffersMap({
856
- existingRedeemedOffers: redeemed_offers,
857
- categories: newCategories,
858
530
  offers,
859
- removedOfferIds,
860
- unlockedOffersApplicableTimes,
861
531
  });
862
- if (!areRedeemedOffersEqual(redeemed_offers, reconciledRedeemedOffers)) {
863
- redeemed_offers = reconciledRedeemedOffers;
864
- hasChanges = true;
865
- }
866
- if (hasChanges) {
867
- const order = {
868
- ...data,
869
- categories: newCategories,
870
- order_discount_details,
871
- redeemed_offers,
872
- wallet_redemptions: getWalletRedemptionsMap(wallet_redemptions),
873
- };
874
- const newData = (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(order, courses, listnerSettings);
875
- return newData;
876
- }
877
- else {
878
- return data;
879
- }
880
532
  }
@@ -123,7 +123,7 @@ export declare function getWalletCashbackRecommendedValues({ maxApplicableCashba
123
123
  maxApplicableCashback: number | null | undefined;
124
124
  percentageSteps?: readonly number[];
125
125
  }): number[];
126
- type TGiftCardRedemptionPayload = NonNullable<NonNullable<IOrder['wallet_redemptions']>['gift_cards']>;
126
+ type TGiftCardRedemptionPayload = NonNullable<NonNullable<IOrder['wallet_redemptions']>['gift_cards']>[number];
127
127
  export declare function buildWalletRedemptionsPatch({ walletRedemptions, }: {
128
128
  walletRedemptions: IOrder['wallet_redemptions'] | null | undefined;
129
129
  }): Partial<Pick<IOrder, 'wallet_redemptions'>>;
@@ -137,7 +137,7 @@ export declare function buildWalletCashbackRedemptionPatch({ currentWalletRedemp
137
137
  }): Partial<Pick<IOrder, 'wallet_redemptions'>>;
138
138
  export declare function buildWalletGiftCardRedemptionPatch({ currentWalletRedemptions, giftCardRedemption, }: {
139
139
  currentWalletRedemptions: IOrder['wallet_redemptions'] | null | undefined;
140
- giftCardRedemption: TGiftCardRedemptionPayload | null | undefined;
140
+ giftCardRedemption: TGiftCardRedemptionPayload | TGiftCardRedemptionPayload[] | null | undefined;
141
141
  }): Partial<Pick<IOrder, 'wallet_redemptions'>>;
142
142
  export declare function buildWalletGiftCardRedemptionClaimPatch({ currentWalletRedemptions, giftCardId, claimsIncrement, mysteryGiftType, }: {
143
143
  currentWalletRedemptions: IOrder['wallet_redemptions'] | null | undefined;
@@ -826,19 +826,32 @@ function normalizeWalletRedemptions(walletRedemptions) {
826
826
  if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
827
827
  normalizedWalletRedemptions.cashback_amount = (0, common_1.parseNum)(cashbackAmount);
828
828
  }
829
- const giftCardRedemption = walletRedemptions.gift_cards;
830
- if (giftCardRedemption != null) {
831
- const giftCardId = normalizeGiftCardId(giftCardRedemption.gift_card_id);
832
- const redeemedTimes = toPositiveInteger(giftCardRedemption.value_redeemed, 0);
833
- if (giftCardId != null && redeemedTimes > 0) {
834
- normalizedWalletRedemptions.gift_cards = {
829
+ const giftCardRedemptions = walletRedemptions.gift_cards;
830
+ if (Array.isArray(giftCardRedemptions)) {
831
+ const normalizedGiftCardRedemptionsMap = {};
832
+ for (let i = 0; i < giftCardRedemptions.length; i++) {
833
+ const giftCardRedemption = giftCardRedemptions[i];
834
+ const giftCardId = normalizeGiftCardId(giftCardRedemption.gift_card_id);
835
+ const legacyValueRedeemed = giftCardRedemption.value_redeemed;
836
+ const redeemedTimes = toPositiveInteger(giftCardRedemption.redeemed_times ?? legacyValueRedeemed, 0);
837
+ if (giftCardId == null || redeemedTimes <= 0) {
838
+ continue;
839
+ }
840
+ const existingRedemption = normalizedGiftCardRedemptionsMap[giftCardId];
841
+ normalizedGiftCardRedemptionsMap[giftCardId] = {
835
842
  gift_card_id: giftCardId,
836
- value_redeemed: redeemedTimes,
843
+ redeemed_times: (existingRedemption?.redeemed_times ?? 0) + redeemedTimes,
837
844
  ...(giftCardRedemption.mystery_gift_type != null
838
845
  ? { mystery_gift_type: giftCardRedemption.mystery_gift_type }
839
- : {}),
846
+ : existingRedemption?.mystery_gift_type != null
847
+ ? { mystery_gift_type: existingRedemption.mystery_gift_type }
848
+ : {}),
840
849
  };
841
850
  }
851
+ const normalizedGiftCardRedemptions = Object.values(normalizedGiftCardRedemptionsMap);
852
+ if (normalizedGiftCardRedemptions.length > 0) {
853
+ normalizedWalletRedemptions.gift_cards = normalizedGiftCardRedemptions;
854
+ }
842
855
  }
843
856
  return Object.keys(normalizedWalletRedemptions).length > 0 ? normalizedWalletRedemptions : null;
844
857
  }
@@ -866,10 +879,15 @@ function buildWalletCashbackRedemptionPatch({ currentWalletRedemptions, cashback
866
879
  });
867
880
  }
868
881
  function buildWalletGiftCardRedemptionPatch({ currentWalletRedemptions, giftCardRedemption, }) {
882
+ const giftCardRedemptionsPatch = giftCardRedemption == null
883
+ ? undefined
884
+ : Array.isArray(giftCardRedemption)
885
+ ? giftCardRedemption
886
+ : [giftCardRedemption];
869
887
  return buildWalletRedemptionsMergePatch({
870
888
  currentWalletRedemptions,
871
889
  walletRedemptionsPatch: {
872
- gift_cards: giftCardRedemption,
890
+ gift_cards: giftCardRedemptionsPatch,
873
891
  },
874
892
  });
875
893
  }
@@ -880,17 +898,29 @@ function buildWalletGiftCardRedemptionClaimPatch({ currentWalletRedemptions, gif
880
898
  }
881
899
  const safeClaimsIncrement = toPositiveInteger(claimsIncrement, 1);
882
900
  const normalizedCurrentWalletRedemptions = normalizeWalletRedemptions(currentWalletRedemptions);
883
- const existingGiftCardRedemption = normalizedCurrentWalletRedemptions?.gift_cards;
884
- const currentRedeemedTimes = existingGiftCardRedemption?.gift_card_id === normalizedGiftCardId
885
- ? toPositiveInteger(existingGiftCardRedemption.value_redeemed, 0)
886
- : 0;
887
- return buildWalletGiftCardRedemptionPatch({
888
- currentWalletRedemptions,
889
- giftCardRedemption: {
901
+ const existingGiftCardRedemptions = normalizedCurrentWalletRedemptions?.gift_cards ?? [];
902
+ let hasUpdatedGiftCardClaim = false;
903
+ const nextGiftCardRedemptions = existingGiftCardRedemptions.map((existingGiftCardRedemption) => {
904
+ if (existingGiftCardRedemption.gift_card_id !== normalizedGiftCardId) {
905
+ return existingGiftCardRedemption;
906
+ }
907
+ hasUpdatedGiftCardClaim = true;
908
+ return {
909
+ ...existingGiftCardRedemption,
910
+ redeemed_times: toPositiveInteger(existingGiftCardRedemption.redeemed_times, 0) + safeClaimsIncrement,
911
+ ...(mysteryGiftType != null ? { mystery_gift_type: mysteryGiftType } : {}),
912
+ };
913
+ });
914
+ if (!hasUpdatedGiftCardClaim) {
915
+ nextGiftCardRedemptions.push({
890
916
  gift_card_id: normalizedGiftCardId,
891
- value_redeemed: currentRedeemedTimes + safeClaimsIncrement,
917
+ redeemed_times: safeClaimsIncrement,
892
918
  ...(mysteryGiftType != null ? { mystery_gift_type: mysteryGiftType } : {}),
893
- },
919
+ });
920
+ }
921
+ return buildWalletGiftCardRedemptionPatch({
922
+ currentWalletRedemptions,
923
+ giftCardRedemption: nextGiftCardRedemptions,
894
924
  });
895
925
  }
896
926
  function buildClearWalletRedemptionsPatch({ currentWalletRedemptions, clearCashbackAmount = false, clearGiftCardRedemption = false, clearAll = false, }) {