@managesome/eposmatic-toolkit 0.2.321 → 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) {
@@ -23,12 +23,11 @@ interface ICalculateCustomerSpendableVoucherCodes {
23
23
  * Determines whether a voucher-type offer should show "already claimed"
24
24
  * instead of the toolkit's generic "not eligible" message.
25
25
  *
26
- * Returns `true` when the offer itself has been redeemed AND the voucher's
26
+ * Returns `true` when the offer itself has been redeemed AND the offer's
27
27
  * per-customer usage count has been reached.
28
28
  */
29
- export declare function isExhaustedVoucherOffer({ offer, vouchers, customerOfferRedemptionCounts, redeemedOffers, isRedeemed, }: {
29
+ export declare function isExhaustedVoucherOffer({ offer, customerOfferRedemptionCounts, redeemedOffers, isRedeemed, }: {
30
30
  offer: IOffer | null | undefined;
31
- vouchers: IVoucher[] | undefined;
32
31
  customerOfferRedemptionCounts: Record<string, number> | null | undefined;
33
32
  redeemedOffers: IOrder['redeemed_offers'] | null | undefined;
34
33
  isRedeemed: boolean;
@@ -319,10 +319,10 @@ function getOfferRedeemedClaims(redeemedOffers, offerId) {
319
319
  * Determines whether a voucher-type offer should show "already claimed"
320
320
  * instead of the toolkit's generic "not eligible" message.
321
321
  *
322
- * Returns `true` when the offer itself has been redeemed AND the voucher's
322
+ * Returns `true` when the offer itself has been redeemed AND the offer's
323
323
  * per-customer usage count has been reached.
324
324
  */
325
- function isExhaustedVoucherOffer({ offer, vouchers, customerOfferRedemptionCounts, redeemedOffers, isRedeemed, }) {
325
+ function isExhaustedVoucherOffer({ offer, customerOfferRedemptionCounts, redeemedOffers, isRedeemed, }) {
326
326
  if (offer == null ||
327
327
  offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER ||
328
328
  offer.pathway_to_unlock_type_vouchers == null ||
@@ -333,29 +333,7 @@ function isExhaustedVoucherOffer({ offer, vouchers, customerOfferRedemptionCount
333
333
  if (!isThisOfferRedeemed)
334
334
  return false;
335
335
  const pathwayMaxUsesPerCustomer = offer.pathway_to_unlock_type_vouchers.max_uses_per_customer;
336
- if (pathwayMaxUsesPerCustomer != null &&
337
- (customerOfferRedemptionCounts[offer._id] ?? 0) >= pathwayMaxUsesPerCustomer) {
338
- return true;
339
- }
340
- if (vouchers == null || vouchers.length === 0) {
341
- return false;
342
- }
343
- const vouchersById = new Map();
344
- for (let i = 0; i < vouchers.length; i++) {
345
- vouchersById.set(vouchers[i]._id, vouchers[i]);
346
- }
347
- const voucherIds = offer.pathway_to_unlock_type_vouchers.voucher_ids;
348
- for (let i = 0; i < voucherIds.length; i++) {
349
- const voucherId = voucherIds[i];
350
- const voucher = vouchersById.get(voucherId);
351
- if (voucher?.max_uses_per_customer == null) {
352
- continue;
353
- }
354
- if ((customerOfferRedemptionCounts[voucherId] ?? 0) >= voucher.max_uses_per_customer) {
355
- return true;
356
- }
357
- }
358
- return false;
336
+ return (pathwayMaxUsesPerCustomer != null && (customerOfferRedemptionCounts[offer._id] ?? 0) >= pathwayMaxUsesPerCustomer);
359
337
  }
360
338
  // ================================><================================= //
361
339
  // === findLinkedVoucherOffers === //
@@ -2178,17 +2156,13 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2178
2156
  const normalizedMatchedVoucherCode = matchedVoucher?.code.trim().toUpperCase() ?? null;
2179
2157
  const redeemedTimesForOffer = getOfferRedeemedTimes(offer._id);
2180
2158
  const spendableVoucherCount = normalizedMatchedVoucherCode == null ? 0 : (spendableVoucherCodesByCode[normalizedMatchedVoucherCode] ?? 0);
2181
- // Voucher max_uses_per_customer is resource-scoped by voucher id.
2182
- const customerVoucherUsageCount = matchedVoucher == null ? 0 : (customerOfferRedemptionCounts?.[matchedVoucher._id] ?? 0);
2183
- const voucherUsesPerCustomerExhausted = matchedVoucher?.max_uses_per_customer != null &&
2184
- (customerOfferRedemptionCounts == null || customerVoucherUsageCount >= matchedVoucher.max_uses_per_customer);
2185
2159
  const hasVoucherAccess = matchedVoucher != null && (matchedVoucher.is_public || spendableVoucherCount > 0 || redeemedTimesForOffer > 0);
2186
2160
  const amountProgress = getPreUnlockProgressPercentage(orderValue, total_amount || 1);
2187
2161
  if (!hasMatchingVoucher) {
2188
2162
  stats.progress = 0;
2189
2163
  stats.message = `Apply a matching voucher to unlock this offer`;
2190
2164
  }
2191
- else if (!hasVoucherAccess || voucherUsesPerCustomerExhausted) {
2165
+ else if (!hasVoucherAccess) {
2192
2166
  stats.progress = 0;
2193
2167
  stats.message = `⛔️ You are not eligible for this offer ⛔️`;
2194
2168
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.321",
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",