@managesome/eposmatic-toolkit 0.2.339 → 0.2.341

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.
@@ -519,8 +519,13 @@ function getNormalizedWalletRedemptionsMap(walletRedemptions) {
519
519
  if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
520
520
  normalized.cashback_amount = cashbackAmount;
521
521
  }
522
- const giftCardRedemptions = walletRedemptions.gift_cards;
523
- if (Array.isArray(giftCardRedemptions)) {
522
+ const rawGiftCardRedemptions = walletRedemptions.gift_cards;
523
+ const giftCardRedemptions = rawGiftCardRedemptions == null
524
+ ? []
525
+ : Array.isArray(rawGiftCardRedemptions)
526
+ ? rawGiftCardRedemptions
527
+ : [rawGiftCardRedemptions];
528
+ if (giftCardRedemptions.length > 0) {
524
529
  const normalizedGiftCardRedemptionsMap = {};
525
530
  for (let i = 0; i < giftCardRedemptions.length; i++) {
526
531
  const giftCardRedemption = giftCardRedemptions[i];
@@ -553,8 +558,61 @@ function getWalletRedemptionsMap(existingWalletRedemptions) {
553
558
  }
554
559
  function getSyncOffersWithCartData(data, courses, listnerSettings, unlockedOffersApplicableTimes, // Record<offer_id, times can be applied>
555
560
  offers) {
556
- return (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(data, courses, listnerSettings, undefined, {
561
+ const syncedOrderData = (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(data, courses, listnerSettings, undefined, {
557
562
  unlockedOffersApplicableTimes,
558
563
  offers,
559
564
  });
565
+ const removedOfferIds = new Set();
566
+ const currentOrderDiscountDetails = data.order_discount_details;
567
+ const syncedOrderDiscountDetails = syncedOrderData.order_discount_details;
568
+ const currentLoyaltyDiscountId = getNormalizedOfferId(currentOrderDiscountDetails?.loyalty_discount_id);
569
+ const syncedLoyaltyDiscountId = getNormalizedOfferId(syncedOrderDiscountDetails?.loyalty_discount_id);
570
+ if (currentLoyaltyDiscountId != null && currentLoyaltyDiscountId !== syncedLoyaltyDiscountId) {
571
+ removedOfferIds.add(currentLoyaltyDiscountId);
572
+ }
573
+ const currentVoucherDiscountId = getNormalizedOfferId(currentOrderDiscountDetails?.voucher_discount_id);
574
+ const syncedVoucherDiscountId = getNormalizedOfferId(syncedOrderDiscountDetails?.voucher_discount_id);
575
+ if (currentVoucherDiscountId != null && currentVoucherDiscountId !== syncedVoucherDiscountId) {
576
+ removedOfferIds.add(currentVoucherDiscountId);
577
+ }
578
+ const syncedCategories = syncedOrderData.categories ?? [];
579
+ const hasCartItems = syncedCategories.some((category) => category.items.length > 0);
580
+ const nextRedeemedOffers = hasCartItems
581
+ ? getReconciledRedeemedOffersMap({
582
+ existingRedeemedOffers: data.redeemed_offers,
583
+ categories: syncedCategories,
584
+ offers,
585
+ removedOfferIds,
586
+ unlockedOffersApplicableTimes,
587
+ })
588
+ : null;
589
+ const normalizedSyncedWalletRedemptions = getWalletRedemptionsMap(syncedOrderData.wallet_redemptions);
590
+ const normalizedCurrentWalletRedemptions = getWalletRedemptionsMap(data.wallet_redemptions);
591
+ const shouldSyncWalletCashbackDiscount = isWalletCashbackLoyaltyDiscount(syncedOrderDiscountDetails) ||
592
+ isWalletCashbackLoyaltyDiscount(currentOrderDiscountDetails);
593
+ const nextWalletRedemptions = getWalletRedemptionsMap({
594
+ ...(normalizedCurrentWalletRedemptions ?? {}),
595
+ ...(normalizedSyncedWalletRedemptions ?? {}),
596
+ cashback_amount: shouldSyncWalletCashbackDiscount
597
+ ? (normalizedSyncedWalletRedemptions?.cashback_amount ?? normalizedCurrentWalletRedemptions?.cashback_amount)
598
+ : undefined,
599
+ gift_cards: hasCartItems
600
+ ? (normalizedSyncedWalletRedemptions?.gift_cards ?? normalizedCurrentWalletRedemptions?.gift_cards)
601
+ : undefined,
602
+ });
603
+ const nextOrderDiscountDetails = shouldSyncWalletCashbackDiscount
604
+ ? getSyncedWalletCashbackOrderDiscountDetails({
605
+ orderDiscountDetails: syncedOrderDiscountDetails ?? currentOrderDiscountDetails,
606
+ walletCashbackAmount: getWalletCashbackAmountFromOrderData({
607
+ orderDiscountDetails: syncedOrderDiscountDetails ?? currentOrderDiscountDetails,
608
+ walletRedemptions: nextWalletRedemptions,
609
+ }),
610
+ })
611
+ : syncedOrderDiscountDetails;
612
+ return {
613
+ ...syncedOrderData,
614
+ order_discount_details: nextOrderDiscountDetails,
615
+ redeemed_offers: nextRedeemedOffers,
616
+ wallet_redemptions: nextWalletRedemptions,
617
+ };
560
618
  }
@@ -1,5 +1,6 @@
1
1
  import { GIFT_CARD_REWARD_TYPE_ENUM, OFFER_REWARD_TYPE_ENUM, PLATFORMS_ENUM } from '../../constants';
2
2
  import { ICustomer, IDiscountDetails, IGiftCard, IOffer, IOrder, IOrderCategory, ITenant, IVoucher } from '../../schemas';
3
+ import { ICustomerRedemptionCount } from '../types';
3
4
  import { TWalletGiftCardRedemptionsCompatInput } from './wallet-redemption-helpers';
4
5
  export declare function getOfferWithPrice<T extends Partial<IOffer> | IOffer>(offer: T, writeable?: boolean): T;
5
6
  export declare function convertOffersToWorkableOffers(offers: IOffer[], platform: PLATFORMS_ENUM): IOffer[];
@@ -20,6 +21,18 @@ interface ICalculateCustomerSpendableVoucherCodes {
20
21
  offers: IOffer[];
21
22
  vouchers: IVoucher[] | undefined;
22
23
  }
24
+ interface IGetVoucherPathwayCartContext {
25
+ offers: IOffer[];
26
+ vouchers: IVoucher[] | undefined;
27
+ redeemedOffers?: IOrder['redeemed_offers'] | null | undefined;
28
+ orderDiscountDetails: IDiscountDetails | null | undefined;
29
+ }
30
+ export interface IVoucherPathwayCartContext {
31
+ activeVoucherCode: string | null;
32
+ consumedVoucherCodes: Set<string>;
33
+ consumedVoucherIds: Set<string>;
34
+ redeemedOfferIds: Set<string>;
35
+ }
23
36
  interface ICalculateCustomerSpendableGiftCardCodes {
24
37
  customerGiftCardCodes: ICustomer['gift_cards'] | null | undefined;
25
38
  loyaltyReferenceNumber: string | null | undefined;
@@ -27,6 +40,11 @@ interface ICalculateCustomerSpendableGiftCardCodes {
27
40
  walletGiftCardRedemptions: TWalletGiftCardRedemptionsCompatInput;
28
41
  giftCards: IGiftCard[] | undefined;
29
42
  }
43
+ export declare function getVoucherPathwayCartContext({ offers, vouchers, redeemedOffers, orderDiscountDetails, }: IGetVoucherPathwayCartContext): IVoucherPathwayCartContext;
44
+ export declare function isVoucherPathwayOfferBlockedByCartContext({ offer, cartContext, }: {
45
+ offer: IOffer;
46
+ cartContext: IVoucherPathwayCartContext;
47
+ }): boolean;
30
48
  /**
31
49
  * Determines whether a voucher-type offer should show "already claimed"
32
50
  * instead of the toolkit's generic "not eligible" message.
@@ -36,7 +54,7 @@ interface ICalculateCustomerSpendableGiftCardCodes {
36
54
  */
37
55
  export declare function isExhaustedVoucherOffer({ offer, customerOfferRedemptionCounts, redeemedOffers, isRedeemed, }: {
38
56
  offer: IOffer | null | undefined;
39
- customerOfferRedemptionCounts: Record<string, number> | null | undefined;
57
+ customerOfferRedemptionCounts: ICustomerRedemptionCount | null | undefined;
40
58
  redeemedOffers: IOrder['redeemed_offers'] | null | undefined;
41
59
  isRedeemed: boolean;
42
60
  }): boolean;
@@ -114,12 +132,12 @@ export declare function getOfferUsageLimitMeta(offer: IOffer | null | undefined)
114
132
  export declare function getOfferConsumedUses({ offer, usageLimit, customerOfferRedemptionCounts, }: {
115
133
  offer: IOffer | null | undefined;
116
134
  usageLimit: IOfferUsageLimitMeta | null;
117
- customerOfferRedemptionCounts: Record<string, number> | null | undefined;
135
+ customerOfferRedemptionCounts: ICustomerRedemptionCount | null | undefined;
118
136
  }): number | null;
119
137
  export declare function isOfferFullyConsumedForCustomer({ offer, usageLimit, customerOfferRedemptionCounts, }: {
120
138
  offer: IOffer | null | undefined;
121
139
  usageLimit: IOfferUsageLimitMeta | null;
122
- customerOfferRedemptionCounts: Record<string, number> | null | undefined;
140
+ customerOfferRedemptionCounts: ICustomerRedemptionCount | null | undefined;
123
141
  }): boolean;
124
142
  export declare function getOfferClaimIncrementFromStats({ stats, fallbackClaims, }: {
125
143
  stats: Pick<IOfferStats, 'applicable_quantity' | 'redeemed_times'> | null | undefined;
@@ -191,7 +209,7 @@ interface IGetOffersWithStats {
191
209
  customer: Partial<ICustomer> | null;
192
210
  vouchers: IVoucher[] | undefined;
193
211
  giftCards: IGiftCard[] | undefined;
194
- customerOfferRedemptionCounts: Record<string, number> | null | undefined;
212
+ customerOfferRedemptionCounts: ICustomerRedemptionCount | null | undefined;
195
213
  redeemed_offers: IOrder['redeemed_offers'] | null | undefined;
196
214
  platform: PLATFORMS_ENUM | undefined;
197
215
  currency_symbol: ITenant['currency_symbol'] | undefined;