@managesome/eposmatic-toolkit 0.2.325 → 0.2.329

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.
@@ -1,4 +1,4 @@
1
- import { CARD_PROCESSING_PROVIDERS_ENUM, CURRENCY_SYMBOL_ENUM, DELIVERY_STATUS_ENUM, ORDER_STATUS_ENUM, PLATFORM_TYPE_ENUM, PLATFORMS_ENUM, VOID_ORDER_ITEM_TYPE_ENUM, WAREHOUSE_INVOICE_STATUS_ENUM } from '../../constants';
1
+ import { CARD_PROCESSING_PROVIDERS_ENUM, CURRENCY_SYMBOL_ENUM, DELIVERY_STATUS_ENUM, ORDER_STATUS_ENUM, PLATFORM_TYPE_ENUM, PLATFORMS_ENUM, VOID_ORDER_ITEM_TYPE_ENUM, WALLET_ASSET_TYPE_ENUM, WALLET_ASSIGNMENT_BEHAVIOR_ENUM, WAREHOUSE_INVOICE_STATUS_ENUM } from '../../constants';
2
2
  import { IDineinTableFingerprint, IInternalLead, IModifierExtra, IOrder, IOrderItem, IOrderPayment, IPurchaseOrder, IWarehouseClientInvoice } from '../../schemas';
3
3
  import { ISwitchPayEWalletRequestBody } from './switchpay';
4
4
  export interface IWorker_CreateDelivery {
@@ -266,3 +266,11 @@ export interface IWorker_IncrementCashInHand {
266
266
  staff_id: string;
267
267
  amount: number;
268
268
  }
269
+ export interface IWorker_UpdateCustomerRedemptionsOnWalletAssignment {
270
+ tenant_id: string;
271
+ brand_id: string;
272
+ asset_type: WALLET_ASSET_TYPE_ENUM;
273
+ behavior: WALLET_ASSIGNMENT_BEHAVIOR_ENUM;
274
+ code_quantities: Record<string, number>;
275
+ updated_phone_numbers: string[];
276
+ }
@@ -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 { TWalletGiftCardRedemptionsCompatInput } from './wallet-redemption-helpers';
3
4
  export declare function getOfferWithPrice<T extends Partial<IOffer> | IOffer>(offer: T, writeable?: boolean): T;
4
5
  export declare function convertOffersToWorkableOffers(offers: IOffer[], platform: PLATFORMS_ENUM): IOffer[];
5
6
  interface ICalculateCustomerLoyaltyPoints {
@@ -19,6 +20,13 @@ interface ICalculateCustomerSpendableVoucherCodes {
19
20
  offers: IOffer[];
20
21
  vouchers: IVoucher[] | undefined;
21
22
  }
23
+ interface ICalculateCustomerSpendableGiftCardCodes {
24
+ customerGiftCardCodes: ICustomer['gift_cards'] | null | undefined;
25
+ loyaltyReferenceNumber: string | null | undefined;
26
+ loyaltyDiscountId: string | null | undefined;
27
+ walletGiftCardRedemptions: TWalletGiftCardRedemptionsCompatInput;
28
+ giftCards: IGiftCard[] | undefined;
29
+ }
22
30
  /**
23
31
  * Determines whether a voucher-type offer should show "already claimed"
24
32
  * instead of the toolkit's generic "not eligible" message.
@@ -55,6 +63,10 @@ export declare function calculateCustomerSpendableVoucherCodes({ customerVoucher
55
63
  activeVoucherCode: string | null;
56
64
  consumedVoucherClaims: number;
57
65
  };
66
+ export declare function calculateCustomerSpendableGiftCardCodes({ customerGiftCardCodes, loyaltyReferenceNumber, loyaltyDiscountId, walletGiftCardRedemptions, giftCards, }: ICalculateCustomerSpendableGiftCardCodes): {
67
+ giftCardCodes: Record<string, number>;
68
+ totalGiftCards: number;
69
+ };
58
70
  export declare function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, redeemedOffers, currentItemsPrice, totalDiscountApplied, categories, offers, includePendingRewardPoints, }: ICalculateCustomerLoyaltyPoints): {
59
71
  loyaltyPoints: number;
60
72
  addedLoyaltyPoints: number;
@@ -90,10 +102,34 @@ export type IOfferWithStats = {
90
102
  offer: IOffer;
91
103
  stats: IOfferStats;
92
104
  };
105
+ export interface IOfferUsageLimitMeta {
106
+ maxUsesPerCustomer: number;
107
+ usageKey: string;
108
+ }
109
+ export interface IAutoApplyOfferCandidate {
110
+ offer: IOffer;
111
+ claimsIncrement?: number;
112
+ }
113
+ export declare function getOfferUsageLimitMeta(offer: IOffer | null | undefined): IOfferUsageLimitMeta | null;
114
+ export declare function getOfferConsumedUses({ offer, usageLimit, customerOfferRedemptionCounts, }: {
115
+ offer: IOffer | null | undefined;
116
+ usageLimit: IOfferUsageLimitMeta | null;
117
+ customerOfferRedemptionCounts: Record<string, number> | null | undefined;
118
+ }): number | null;
119
+ export declare function isOfferFullyConsumedForCustomer({ offer, usageLimit, customerOfferRedemptionCounts, }: {
120
+ offer: IOffer | null | undefined;
121
+ usageLimit: IOfferUsageLimitMeta | null;
122
+ customerOfferRedemptionCounts: Record<string, number> | null | undefined;
123
+ }): boolean;
93
124
  export declare function getOfferClaimIncrementFromStats({ stats, fallbackClaims, }: {
94
125
  stats: Pick<IOfferStats, 'applicable_quantity' | 'redeemed_times'> | null | undefined;
95
126
  fallbackClaims?: number;
96
127
  }): number;
128
+ export declare function getSuppressedOfferIdsHashKey(suppressedOfferIds: Record<string, true> | null | undefined): string;
129
+ export declare function getAutoApplyOfferCandidates({ offersWithStats, suppressedOfferIds, }: {
130
+ offersWithStats: IOfferWithStats[] | null | undefined;
131
+ suppressedOfferIds?: Record<string, true> | null;
132
+ }): IAutoApplyOfferCandidate[];
97
133
  export declare function buildRedeemedOffersClaimPatch({ currentRedeemedOffers, offerId, claimsIncrement, }: {
98
134
  currentRedeemedOffers: IOrder['redeemed_offers'] | null | undefined;
99
135
  offerId: string | null | undefined;
@@ -7,8 +7,14 @@ exports.isExhaustedVoucherOffer = isExhaustedVoucherOffer;
7
7
  exports.findLinkedVoucherOffers = findLinkedVoucherOffers;
8
8
  exports.buildClearVoucherDiscountDetails = buildClearVoucherDiscountDetails;
9
9
  exports.calculateCustomerSpendableVoucherCodes = calculateCustomerSpendableVoucherCodes;
10
+ exports.calculateCustomerSpendableGiftCardCodes = calculateCustomerSpendableGiftCardCodes;
10
11
  exports.calculateCustomerLoyaltyPoints = calculateCustomerLoyaltyPoints;
12
+ exports.getOfferUsageLimitMeta = getOfferUsageLimitMeta;
13
+ exports.getOfferConsumedUses = getOfferConsumedUses;
14
+ exports.isOfferFullyConsumedForCustomer = isOfferFullyConsumedForCustomer;
11
15
  exports.getOfferClaimIncrementFromStats = getOfferClaimIncrementFromStats;
16
+ exports.getSuppressedOfferIdsHashKey = getSuppressedOfferIdsHashKey;
17
+ exports.getAutoApplyOfferCandidates = getAutoApplyOfferCandidates;
12
18
  exports.buildRedeemedOffersClaimPatch = buildRedeemedOffersClaimPatch;
13
19
  exports.getNetItemsValueAfterItemDiscount = getNetItemsValueAfterItemDiscount;
14
20
  exports.getMaxApplicableWalletCashback = getMaxApplicableWalletCashback;
@@ -26,6 +32,7 @@ const common_1 = require("./common");
26
32
  const platform_catalog_1 = require("./platform-catalog");
27
33
  const platform_num_1 = require("./platform-num");
28
34
  const price_conversion_1 = require("./price-conversion");
35
+ const wallet_redemption_helpers_1 = require("./wallet-redemption-helpers");
29
36
  function getOfferWithPrice(offer, writeable) {
30
37
  // const convertToPrice = writeable ? convertPriceObjToCents : convertPriceObjFromCents;
31
38
  const convertItemSpecialHours = writeable ? platform_num_1.convertSpecialHoursToCents : platform_num_1.convertSpecialHoursFromCents;
@@ -305,6 +312,10 @@ function normalizeVoucherCode(voucherCode) {
305
312
  const normalizedCode = voucherCode?.trim().toUpperCase();
306
313
  return normalizedCode != null && normalizedCode.length > 0 ? normalizedCode : null;
307
314
  }
315
+ function normalizeGiftCardCode(giftCardCode) {
316
+ const normalizedCode = giftCardCode?.trim().toUpperCase();
317
+ return normalizedCode != null && normalizedCode.length > 0 ? normalizedCode : null;
318
+ }
308
319
  function getOfferRedeemedClaims(redeemedOffers, offerId) {
309
320
  const redeemedTimes = Number(redeemedOffers?.[offerId]);
310
321
  if (Number.isFinite(redeemedTimes) && redeemedTimes > 0) {
@@ -442,6 +453,86 @@ function calculateCustomerSpendableVoucherCodes({ customerVoucherCodes, orderDis
442
453
  consumedVoucherClaims,
443
454
  };
444
455
  }
456
+ function calculateCustomerSpendableGiftCardCodes({ customerGiftCardCodes, loyaltyReferenceNumber, loyaltyDiscountId, walletGiftCardRedemptions, giftCards, }) {
457
+ const giftCardCodes = {};
458
+ const shouldFilterByGiftCardEntities = giftCards != null && giftCards.length > 0;
459
+ const activeGiftCardCodes = new Set();
460
+ const giftCardCodeById = new Map();
461
+ if (shouldFilterByGiftCardEntities) {
462
+ const now = Date.now();
463
+ for (let i = 0; i < giftCards.length; i++) {
464
+ const giftCard = giftCards[i];
465
+ const normalizedCode = normalizeGiftCardCode(giftCard.code);
466
+ if (normalizedCode == null)
467
+ continue;
468
+ if (!giftCard.active)
469
+ continue;
470
+ if (giftCard.expire_at != null) {
471
+ const expiryTime = new Date(giftCard.expire_at).getTime();
472
+ if (Number.isFinite(expiryTime) && expiryTime < now) {
473
+ continue;
474
+ }
475
+ }
476
+ activeGiftCardCodes.add(normalizedCode);
477
+ giftCardCodeById.set(giftCard._id, normalizedCode);
478
+ }
479
+ }
480
+ if (customerGiftCardCodes != null) {
481
+ const entries = Object.entries(customerGiftCardCodes);
482
+ for (let i = 0; i < entries.length; i++) {
483
+ const [code, count] = entries[i];
484
+ const normalizedCode = normalizeGiftCardCode(code);
485
+ if (normalizedCode == null)
486
+ continue;
487
+ if (shouldFilterByGiftCardEntities && !activeGiftCardCodes.has(normalizedCode)) {
488
+ continue;
489
+ }
490
+ const parsedCount = Number(count);
491
+ if (!Number.isFinite(parsedCount) || parsedCount <= 0)
492
+ continue;
493
+ giftCardCodes[normalizedCode] = Math.floor(parsedCount);
494
+ }
495
+ }
496
+ const appliedGiftCardClaimsByCode = {};
497
+ const normalizedWalletGiftCardRedemptions = (0, wallet_redemption_helpers_1.getWalletGiftCardRedemptionClaims)(walletGiftCardRedemptions);
498
+ for (let i = 0; i < normalizedWalletGiftCardRedemptions.length; i++) {
499
+ const walletGiftCardRedemption = normalizedWalletGiftCardRedemptions[i];
500
+ const normalizedWalletGiftCardCode = giftCardCodeById.get(walletGiftCardRedemption.gift_card_id);
501
+ if (normalizedWalletGiftCardCode == null || normalizedWalletGiftCardCode.length === 0) {
502
+ continue;
503
+ }
504
+ appliedGiftCardClaimsByCode[normalizedWalletGiftCardCode] =
505
+ (appliedGiftCardClaimsByCode[normalizedWalletGiftCardCode] ?? 0) + walletGiftCardRedemption.redeemed_times;
506
+ }
507
+ const appliedGiftCardCodes = Object.keys(appliedGiftCardClaimsByCode);
508
+ if (appliedGiftCardCodes.length > 0) {
509
+ for (let i = 0; i < appliedGiftCardCodes.length; i++) {
510
+ const appliedGiftCardCode = appliedGiftCardCodes[i];
511
+ if (giftCardCodes[appliedGiftCardCode] == null)
512
+ continue;
513
+ giftCardCodes[appliedGiftCardCode] = Math.max(0, giftCardCodes[appliedGiftCardCode] - appliedGiftCardClaimsByCode[appliedGiftCardCode]);
514
+ }
515
+ }
516
+ else {
517
+ const normalizedLoyaltyReference = normalizeGiftCardCode(loyaltyReferenceNumber);
518
+ const normalizedAppliedCode = loyaltyDiscountId != null &&
519
+ loyaltyDiscountId !== constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID &&
520
+ normalizedLoyaltyReference != null &&
521
+ normalizedLoyaltyReference.length > 0
522
+ ? normalizedLoyaltyReference
523
+ : null;
524
+ if (normalizedAppliedCode != null && giftCardCodes[normalizedAppliedCode] != null) {
525
+ giftCardCodes[normalizedAppliedCode] = Math.max(0, giftCardCodes[normalizedAppliedCode] - 1);
526
+ }
527
+ }
528
+ return {
529
+ giftCardCodes,
530
+ totalGiftCards: Object.values(giftCardCodes).reduce((total, count) => {
531
+ const safeCount = Number(count);
532
+ return total + (Number.isFinite(safeCount) && safeCount > 0 ? safeCount : 0);
533
+ }, 0),
534
+ };
535
+ }
445
536
  // The purpose of this function is to calculate the total number of points that are applied/consumed by the cart items
446
537
  function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, redeemedOffers, currentItemsPrice, totalDiscountApplied, categories, offers, includePendingRewardPoints = false, }) {
447
538
  const totalOrderPrice = (currentItemsPrice ?? 0) - (totalDiscountApplied ?? 0);
@@ -703,6 +794,56 @@ function toPositiveInteger(value, fallback = 0) {
703
794
  }
704
795
  return Math.floor(parsedValue);
705
796
  }
797
+ function getOfferPathwayMaxUsesPerCustomer(offer) {
798
+ if (offer == null) {
799
+ return null;
800
+ }
801
+ if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS) {
802
+ return toPositiveInteger(offer.pathway_to_unlock_type_orders?.max_uses_per_customer, 0) || null;
803
+ }
804
+ if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ITEMS) {
805
+ return toPositiveInteger(offer.pathway_to_unlock_type_items?.max_uses_per_customer, 0) || null;
806
+ }
807
+ if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
808
+ return toPositiveInteger(offer.pathway_to_unlock_type_points?.max_uses_per_customer, 0) || null;
809
+ }
810
+ if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
811
+ return toPositiveInteger(offer.pathway_to_unlock_type_vouchers?.max_uses_per_customer, 0) || null;
812
+ }
813
+ return null;
814
+ }
815
+ function getOfferUsageLimitMeta(offer) {
816
+ const maxUsesPerCustomer = getOfferPathwayMaxUsesPerCustomer(offer);
817
+ if (maxUsesPerCustomer == null) {
818
+ return null;
819
+ }
820
+ const usageKey = normalizeOfferId(offer?._id);
821
+ if (usageKey == null) {
822
+ return null;
823
+ }
824
+ return {
825
+ maxUsesPerCustomer,
826
+ usageKey,
827
+ };
828
+ }
829
+ function getOfferConsumedUses({ offer, usageLimit, customerOfferRedemptionCounts, }) {
830
+ if (usageLimit == null || customerOfferRedemptionCounts == null) {
831
+ return null;
832
+ }
833
+ const usageKey = normalizeOfferId(offer?._id) ?? normalizeOfferId(usageLimit.usageKey);
834
+ if (usageKey == null) {
835
+ return null;
836
+ }
837
+ return toPositiveInteger(customerOfferRedemptionCounts[usageKey], 0);
838
+ }
839
+ function isOfferFullyConsumedForCustomer({ offer, usageLimit, customerOfferRedemptionCounts, }) {
840
+ const consumedUses = getOfferConsumedUses({
841
+ offer,
842
+ usageLimit,
843
+ customerOfferRedemptionCounts,
844
+ });
845
+ return usageLimit != null && consumedUses != null && consumedUses >= usageLimit.maxUsesPerCustomer;
846
+ }
706
847
  function getOfferRemainingClaimsFromStats(stats) {
707
848
  const applicableQuantity = toPositiveInteger(stats?.applicable_quantity, 0);
708
849
  const redeemedTimes = toPositiveInteger(stats?.redeemed_times, 0);
@@ -715,6 +856,52 @@ function getOfferClaimIncrementFromStats({ stats, fallbackClaims = 0, }) {
715
856
  }
716
857
  return toPositiveInteger(fallbackClaims, 0);
717
858
  }
859
+ function getSuppressedOfferIdsHashKey(suppressedOfferIds) {
860
+ return Object.keys(suppressedOfferIds ?? {})
861
+ .map((offerId) => normalizeOfferId(offerId))
862
+ .filter((offerId) => offerId != null)
863
+ .sort()
864
+ .join('|');
865
+ }
866
+ function getAutoApplyOfferCandidates({ offersWithStats, suppressedOfferIds, }) {
867
+ if (offersWithStats == null || offersWithStats.length === 0) {
868
+ return [];
869
+ }
870
+ const autoApplyOfferCandidates = [];
871
+ for (let i = 0; i < offersWithStats.length; i++) {
872
+ const offerWithStats = offersWithStats[i];
873
+ const offer = offerWithStats.offer;
874
+ const stats = offerWithStats.stats;
875
+ const normalizedOfferId = normalizeOfferId(offer._id);
876
+ if (normalizedOfferId == null) {
877
+ continue;
878
+ }
879
+ const isAutoApplyAfterFirstPointsClaim = offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS &&
880
+ offer.reward_points != null &&
881
+ !offer.reward_points.once_per_order &&
882
+ stats.redeemed_times > 0;
883
+ const shouldAutoApplyOffer = offer.auto_apply || isAutoApplyAfterFirstPointsClaim;
884
+ if (!stats.claimable || stats.invalid_offer || !shouldAutoApplyOffer) {
885
+ continue;
886
+ }
887
+ if (suppressedOfferIds?.[normalizedOfferId]) {
888
+ continue;
889
+ }
890
+ const claimsIncrement = offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS
891
+ ? getOfferClaimIncrementFromStats({
892
+ stats,
893
+ })
894
+ : undefined;
895
+ if (claimsIncrement != null && claimsIncrement <= 0) {
896
+ continue;
897
+ }
898
+ autoApplyOfferCandidates.push({
899
+ offer,
900
+ ...(claimsIncrement != null ? { claimsIncrement } : {}),
901
+ });
902
+ }
903
+ return autoApplyOfferCandidates;
904
+ }
718
905
  function buildRedeemedOffersClaimPatch({ currentRedeemedOffers, offerId, claimsIncrement = 1, }) {
719
906
  const normalizedOfferId = normalizeOfferId(offerId);
720
907
  if (normalizedOfferId == null) {
@@ -1153,6 +1340,48 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1153
1340
  giftCardsById.set(giftCard._id, giftCard);
1154
1341
  }
1155
1342
  }
1343
+ const getRewardVoucherAvailability = (voucherIds) => {
1344
+ let rewardVoucherUnavailable = false;
1345
+ let rewardVoucherLimitExhausted = false;
1346
+ for (let vIdx = 0; vIdx < voucherIds.length; vIdx++) {
1347
+ const rewardVoucher = allVouchersById.get(voucherIds[vIdx]);
1348
+ if (rewardVoucher == null || isVoucherRewardResourceUnavailable(rewardVoucher)) {
1349
+ rewardVoucherUnavailable = true;
1350
+ break;
1351
+ }
1352
+ if (customerOfferRedemptionCounts != null &&
1353
+ rewardVoucher.max_uses_per_customer != null &&
1354
+ (customerOfferRedemptionCounts[rewardVoucher._id] ?? 0) >= rewardVoucher.max_uses_per_customer) {
1355
+ rewardVoucherLimitExhausted = true;
1356
+ break;
1357
+ }
1358
+ }
1359
+ return {
1360
+ rewardVoucherUnavailable,
1361
+ rewardVoucherLimitExhausted,
1362
+ };
1363
+ };
1364
+ const getRewardGiftCardAvailability = (giftCardIds) => {
1365
+ let rewardGiftCardUnavailable = false;
1366
+ let rewardGiftCardLimitExhausted = false;
1367
+ for (let gcIdx = 0; gcIdx < giftCardIds.length; gcIdx++) {
1368
+ const rewardGiftCard = allGiftCardsById.get(giftCardIds[gcIdx]);
1369
+ if (rewardGiftCard == null || isGiftCardRewardResourceUnavailable(rewardGiftCard)) {
1370
+ rewardGiftCardUnavailable = true;
1371
+ break;
1372
+ }
1373
+ if (customerOfferRedemptionCounts != null &&
1374
+ rewardGiftCard.max_uses_per_customer != null &&
1375
+ (customerOfferRedemptionCounts[rewardGiftCard._id] ?? 0) >= rewardGiftCard.max_uses_per_customer) {
1376
+ rewardGiftCardLimitExhausted = true;
1377
+ break;
1378
+ }
1379
+ }
1380
+ return {
1381
+ rewardGiftCardUnavailable,
1382
+ rewardGiftCardLimitExhausted,
1383
+ };
1384
+ };
1156
1385
  const getOfferRedeemedTimes = (offerId) => {
1157
1386
  const redeemedTimes = Number(redeemedOffersById?.[offerId]);
1158
1387
  if (Number.isFinite(redeemedTimes) && redeemedTimes > 0) {
@@ -1294,6 +1523,30 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1294
1523
  }
1295
1524
  return Boolean(order_discount_details.loyalty_discount_id);
1296
1525
  };
1526
+ const applyItemRewardClaimableFallback = ({ offerToCheck, stats }) => {
1527
+ const isItemRewardType = offerToCheck.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS ||
1528
+ offerToCheck.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS;
1529
+ if (!isItemRewardType)
1530
+ return;
1531
+ if (stats.invalid_offer || stats.claimable)
1532
+ return;
1533
+ if (stats.progress < 100 || stats.redeemed_times > 0)
1534
+ return;
1535
+ const normalizedMessage = (stats.message ?? '').toLowerCase();
1536
+ if (!normalizedMessage.includes('redeemed')) {
1537
+ return;
1538
+ }
1539
+ const fallbackApplicableQuantity = offerToCheck.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS
1540
+ ? Math.max(1, offerToCheck.reward_items?.num_of_items ?? 1)
1541
+ : Math.max(1, offerToCheck.reward_discount_items?.num_of_items ?? 1);
1542
+ stats.claimable = true;
1543
+ stats.applicable_quantity = Math.max(stats.applicable_quantity, fallbackApplicableQuantity);
1544
+ stats.message = `🎉 You have unlocked this offer 🎉`;
1545
+ unlockedOffersApplicableTimes[offerToCheck._id] = stats.applicable_quantity;
1546
+ if (!totalClaimableOffers.includes(offerToCheck._id)) {
1547
+ totalClaimableOffers.push(offerToCheck._id);
1548
+ }
1549
+ };
1297
1550
  for (let i = 0; i < offers.length; i++) {
1298
1551
  const offer = offers[i];
1299
1552
  const stats = {
@@ -1615,6 +1868,10 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1615
1868
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1616
1869
  }
1617
1870
  }
1871
+ applyItemRewardClaimableFallback({
1872
+ offerToCheck: offer,
1873
+ stats,
1874
+ });
1618
1875
  if (stats.progress >= 100 && isOfferUsesPerCustomerExhausted(offer)) {
1619
1876
  if (stats.claimable) {
1620
1877
  totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offer._id);
@@ -1766,25 +2023,16 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1766
2023
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
1767
2024
  if (offer.reward_voucher == null)
1768
2025
  continue;
1769
- let rewardVoucherLimitExhausted = false;
1770
- if (customerOfferRedemptionCounts != null) {
1771
- const vIds = Object.keys(offer.reward_voucher.voucher_ids);
1772
- for (let vIdx = 0; vIdx < vIds.length; vIdx++) {
1773
- const rewardVoucher = vouchersById.get(vIds[vIdx]);
1774
- if (rewardVoucher != null &&
1775
- rewardVoucher.max_uses_per_customer != null &&
1776
- (customerOfferRedemptionCounts[rewardVoucher._id] ?? 0) >= rewardVoucher.max_uses_per_customer) {
1777
- rewardVoucherLimitExhausted = true;
1778
- break;
1779
- }
1780
- }
1781
- }
2026
+ const { rewardVoucherUnavailable, rewardVoucherLimitExhausted } = getRewardVoucherAvailability(Object.keys(offer.reward_voucher.voucher_ids));
1782
2027
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1783
2028
  const isRedeemed = redeemedTimes > 0;
1784
2029
  stats.redeemed_times = redeemedTimes;
1785
2030
  stats.applicable_quantity = 1;
1786
- stats.claimable = !isRedeemed && !rewardVoucherLimitExhausted;
1787
- if (rewardVoucherLimitExhausted) {
2031
+ stats.claimable = !isRedeemed && !rewardVoucherUnavailable && !rewardVoucherLimitExhausted;
2032
+ if (rewardVoucherUnavailable) {
2033
+ stats.message = '⛔️ This reward is no longer available ⛔️';
2034
+ }
2035
+ else if (rewardVoucherLimitExhausted) {
1788
2036
  stats.message = '⛔️ You have reached your personal limit for this reward ⛔️';
1789
2037
  }
1790
2038
  else if (isRedeemed) {
@@ -1828,25 +2076,16 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1828
2076
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS) {
1829
2077
  if (offer.reward_gift_cards == null)
1830
2078
  continue;
1831
- let rewardGiftCardLimitExhausted = false;
1832
- if (customerOfferRedemptionCounts != null) {
1833
- const gcIds = Object.keys(offer.reward_gift_cards.gift_card_ids);
1834
- for (let gcIdx = 0; gcIdx < gcIds.length; gcIdx++) {
1835
- const giftCard = giftCardsById.get(gcIds[gcIdx]);
1836
- if (giftCard != null &&
1837
- giftCard.max_uses_per_customer != null &&
1838
- (customerOfferRedemptionCounts[giftCard._id] ?? 0) >= giftCard.max_uses_per_customer) {
1839
- rewardGiftCardLimitExhausted = true;
1840
- break;
1841
- }
1842
- }
1843
- }
2079
+ const { rewardGiftCardUnavailable, rewardGiftCardLimitExhausted } = getRewardGiftCardAvailability(Object.keys(offer.reward_gift_cards.gift_card_ids));
1844
2080
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1845
2081
  const isRedeemed = redeemedTimes > 0;
1846
2082
  stats.redeemed_times = redeemedTimes;
1847
2083
  stats.applicable_quantity = 1;
1848
- stats.claimable = !isRedeemed && !rewardGiftCardLimitExhausted;
1849
- if (rewardGiftCardLimitExhausted) {
2084
+ stats.claimable = !isRedeemed && !rewardGiftCardUnavailable && !rewardGiftCardLimitExhausted;
2085
+ if (rewardGiftCardUnavailable) {
2086
+ stats.message = '⛔️ This reward is no longer available ⛔️';
2087
+ }
2088
+ else if (rewardGiftCardLimitExhausted) {
1850
2089
  stats.message = '⛔️ You have reached your personal limit for this reward ⛔️';
1851
2090
  }
1852
2091
  else if (isRedeemed) {
@@ -1880,6 +2119,10 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1880
2119
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
1881
2120
  }
1882
2121
  }
2122
+ applyItemRewardClaimableFallback({
2123
+ offerToCheck: offer,
2124
+ stats,
2125
+ });
1883
2126
  if (stats.progress >= 100 && isOfferUsesPerCustomerExhausted(offer)) {
1884
2127
  if (stats.claimable) {
1885
2128
  totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offer._id);
@@ -2031,25 +2274,16 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2031
2274
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
2032
2275
  if (offer.reward_voucher == null)
2033
2276
  continue;
2034
- let rewardVoucherLimitExhausted = false;
2035
- if (customerOfferRedemptionCounts != null) {
2036
- const vIds = Object.keys(offer.reward_voucher.voucher_ids);
2037
- for (let vIdx = 0; vIdx < vIds.length; vIdx++) {
2038
- const rewardVoucher = vouchersById.get(vIds[vIdx]);
2039
- if (rewardVoucher != null &&
2040
- rewardVoucher.max_uses_per_customer != null &&
2041
- (customerOfferRedemptionCounts[rewardVoucher._id] ?? 0) >= rewardVoucher.max_uses_per_customer) {
2042
- rewardVoucherLimitExhausted = true;
2043
- break;
2044
- }
2045
- }
2046
- }
2277
+ const { rewardVoucherUnavailable, rewardVoucherLimitExhausted } = getRewardVoucherAvailability(Object.keys(offer.reward_voucher.voucher_ids));
2047
2278
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2048
2279
  const isRedeemed = redeemedTimes > 0;
2049
2280
  stats.redeemed_times = redeemedTimes;
2050
2281
  stats.applicable_quantity = 1;
2051
- stats.claimable = !isRedeemed && !rewardVoucherLimitExhausted;
2052
- if (rewardVoucherLimitExhausted) {
2282
+ stats.claimable = !isRedeemed && !rewardVoucherUnavailable && !rewardVoucherLimitExhausted;
2283
+ if (rewardVoucherUnavailable) {
2284
+ stats.message = '⛔️ This reward is no longer available ⛔️';
2285
+ }
2286
+ else if (rewardVoucherLimitExhausted) {
2053
2287
  stats.message = '⛔️ You have reached your personal limit for this reward ⛔️';
2054
2288
  }
2055
2289
  else if (isRedeemed) {
@@ -2093,25 +2327,16 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2093
2327
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS) {
2094
2328
  if (offer.reward_gift_cards == null)
2095
2329
  continue;
2096
- let rewardGiftCardLimitExhausted = false;
2097
- if (customerOfferRedemptionCounts != null) {
2098
- const gcIds = Object.keys(offer.reward_gift_cards.gift_card_ids);
2099
- for (let gcIdx = 0; gcIdx < gcIds.length; gcIdx++) {
2100
- const giftCard = giftCardsById.get(gcIds[gcIdx]);
2101
- if (giftCard != null &&
2102
- giftCard.max_uses_per_customer != null &&
2103
- (customerOfferRedemptionCounts[giftCard._id] ?? 0) >= giftCard.max_uses_per_customer) {
2104
- rewardGiftCardLimitExhausted = true;
2105
- break;
2106
- }
2107
- }
2108
- }
2330
+ const { rewardGiftCardUnavailable, rewardGiftCardLimitExhausted } = getRewardGiftCardAvailability(Object.keys(offer.reward_gift_cards.gift_card_ids));
2109
2331
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2110
2332
  const isRedeemed = redeemedTimes > 0;
2111
2333
  stats.redeemed_times = redeemedTimes;
2112
2334
  stats.applicable_quantity = 1;
2113
- stats.claimable = !isRedeemed && !rewardGiftCardLimitExhausted;
2114
- if (rewardGiftCardLimitExhausted) {
2335
+ stats.claimable = !isRedeemed && !rewardGiftCardUnavailable && !rewardGiftCardLimitExhausted;
2336
+ if (rewardGiftCardUnavailable) {
2337
+ stats.message = '⛔️ This reward is no longer available ⛔️';
2338
+ }
2339
+ else if (rewardGiftCardLimitExhausted) {
2115
2340
  stats.message = '⛔️ You have reached your personal limit for this reward ⛔️';
2116
2341
  }
2117
2342
  else if (isRedeemed) {
@@ -2146,6 +2371,10 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2146
2371
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
2147
2372
  }
2148
2373
  }
2374
+ applyItemRewardClaimableFallback({
2375
+ offerToCheck: offer,
2376
+ stats,
2377
+ });
2149
2378
  if (stats.progress >= 100 && isOfferUsesPerCustomerExhausted(offer)) {
2150
2379
  if (stats.claimable) {
2151
2380
  totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offer._id);
@@ -2299,25 +2528,16 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2299
2528
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER) {
2300
2529
  if (offer.reward_voucher == null)
2301
2530
  continue;
2302
- let rewardVoucherLimitExhausted = false;
2303
- if (customerOfferRedemptionCounts != null) {
2304
- const vIds = Object.keys(offer.reward_voucher.voucher_ids);
2305
- for (let vIdx = 0; vIdx < vIds.length; vIdx++) {
2306
- const rewardVoucher = vouchersById.get(vIds[vIdx]);
2307
- if (rewardVoucher != null &&
2308
- rewardVoucher.max_uses_per_customer != null &&
2309
- (customerOfferRedemptionCounts[rewardVoucher._id] ?? 0) >= rewardVoucher.max_uses_per_customer) {
2310
- rewardVoucherLimitExhausted = true;
2311
- break;
2312
- }
2313
- }
2314
- }
2531
+ const { rewardVoucherUnavailable, rewardVoucherLimitExhausted } = getRewardVoucherAvailability(Object.keys(offer.reward_voucher.voucher_ids));
2315
2532
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2316
2533
  const isRedeemed = redeemedTimes > 0;
2317
2534
  stats.redeemed_times = redeemedTimes;
2318
2535
  stats.applicable_quantity = 1;
2319
- stats.claimable = !isRedeemed && !rewardVoucherLimitExhausted;
2320
- if (rewardVoucherLimitExhausted) {
2536
+ stats.claimable = !isRedeemed && !rewardVoucherUnavailable && !rewardVoucherLimitExhausted;
2537
+ if (rewardVoucherUnavailable) {
2538
+ stats.message = '⛔️ This reward is no longer available ⛔️';
2539
+ }
2540
+ else if (rewardVoucherLimitExhausted) {
2321
2541
  stats.message = '⛔️ You have reached your personal limit for this reward ⛔️';
2322
2542
  }
2323
2543
  else if (isRedeemed) {
@@ -2367,25 +2587,16 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2367
2587
  else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS) {
2368
2588
  if (offer.reward_gift_cards == null)
2369
2589
  continue;
2370
- let rewardGiftCardLimitExhausted = false;
2371
- if (customerOfferRedemptionCounts != null) {
2372
- const gcIds = Object.keys(offer.reward_gift_cards.gift_card_ids);
2373
- for (let gcIdx = 0; gcIdx < gcIds.length; gcIdx++) {
2374
- const giftCard = giftCardsById.get(gcIds[gcIdx]);
2375
- if (giftCard != null &&
2376
- giftCard.max_uses_per_customer != null &&
2377
- (customerOfferRedemptionCounts[giftCard._id] ?? 0) >= giftCard.max_uses_per_customer) {
2378
- rewardGiftCardLimitExhausted = true;
2379
- break;
2380
- }
2381
- }
2382
- }
2590
+ const { rewardGiftCardUnavailable, rewardGiftCardLimitExhausted } = getRewardGiftCardAvailability(Object.keys(offer.reward_gift_cards.gift_card_ids));
2383
2591
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
2384
2592
  const isRedeemed = redeemedTimes > 0;
2385
2593
  stats.redeemed_times = redeemedTimes;
2386
2594
  stats.applicable_quantity = 1;
2387
- stats.claimable = !isRedeemed && !rewardGiftCardLimitExhausted;
2388
- if (rewardGiftCardLimitExhausted) {
2595
+ stats.claimable = !isRedeemed && !rewardGiftCardUnavailable && !rewardGiftCardLimitExhausted;
2596
+ if (rewardGiftCardUnavailable) {
2597
+ stats.message = '⛔️ This reward is no longer available ⛔️';
2598
+ }
2599
+ else if (rewardGiftCardLimitExhausted) {
2389
2600
  stats.message = '⛔️ You have reached your personal limit for this reward ⛔️';
2390
2601
  }
2391
2602
  else if (isRedeemed) {
@@ -2420,6 +2631,10 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
2420
2631
  unlockedOffersApplicableTimes[offer._id] = stats.applicable_quantity;
2421
2632
  }
2422
2633
  }
2634
+ applyItemRewardClaimableFallback({
2635
+ offerToCheck: offer,
2636
+ stats,
2637
+ });
2423
2638
  if (stats.progress >= 100 && isOfferUsesPerCustomerExhausted(offer)) {
2424
2639
  if (stats.claimable) {
2425
2640
  totalClaimableOffers = totalClaimableOffers.filter((id) => id !== offer._id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.325",
3
+ "version": "0.2.329",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",