@managesome/eposmatic-toolkit 0.2.290 → 0.2.291

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.
@@ -2,16 +2,17 @@ import { OFFER_REWARD_TYPE_ENUM, PLATFORMS_ENUM } from '../../constants';
2
2
  import { ICustomer, IDiscountDetails, IGiftCard, IOffer, IOrder, IOrderCategory, ITenant, IVoucher } from '../../schemas';
3
3
  export declare function getOfferWithPrice<T extends Partial<IOffer> | IOffer>(offer: T, writeable?: boolean): T;
4
4
  export declare function convertOffersToWorkableOffers(offers: IOffer[], platform: PLATFORMS_ENUM): IOffer[];
5
- export declare function calculatePointsRedeemedForOffer(redeemedOffer: IOffer, totalOrderPrice: number, categories: IOrderCategory[] | undefined): number;
5
+ export declare function calculatePointsRedeemedForOffer(redeemedOffer: IOffer, totalOrderPrice: number, categories: IOrderCategory[] | undefined, claimedTimes?: number): number;
6
6
  interface ICalculateCustomerLoyaltyPoints {
7
7
  customerLoyaltyPoints: number | undefined;
8
8
  orderDiscountDetails: IDiscountDetails | null | undefined;
9
+ redeemedOffers?: IOrder['redeemed_offers'] | null | undefined;
9
10
  currentItemsPrice: number | undefined;
10
11
  totalDiscountApplied: number | undefined;
11
12
  categories: IOrderCategory[] | undefined;
12
13
  offers: IOffer[];
13
14
  }
14
- export declare function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, currentItemsPrice, totalDiscountApplied, categories, offers, }: ICalculateCustomerLoyaltyPoints): {
15
+ export declare function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, redeemedOffers, currentItemsPrice, totalDiscountApplied, categories, offers, }: ICalculateCustomerLoyaltyPoints): {
15
16
  loyaltyPoints: number;
16
17
  addedLoyaltyPoints: number;
17
18
  deductedLoyaltyPoints: number;
@@ -215,12 +215,16 @@ function convertOffersToWorkableOffers(offers, platform) {
215
215
  return filteredOffers;
216
216
  }
217
217
  // ====================================================================================================================
218
- function calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories) {
218
+ function calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories, claimedTimes) {
219
219
  if (redeemedOffer.reward_type !== constants_1.OFFER_REWARD_TYPE_ENUM.POINTS || redeemedOffer.reward_points == null) {
220
220
  return 0;
221
221
  }
222
222
  let times = 1;
223
223
  if (!redeemedOffer.reward_points.once_per_order) {
224
+ const getUnlockTimesByOrderAmount = (orderValue, totalAmount) => {
225
+ const safeRequiredAmount = Math.max(1, totalAmount || 1);
226
+ return Math.max(1, Math.floor(orderValue / safeRequiredAmount));
227
+ };
224
228
  // This means that we need to see if the condition to unlock the redeemedOffer is met multiple times
225
229
  // i.e. if the customer has ordered 2 pizzas, and the redeemedOffer is applicable for 1 pizza
226
230
  // then we need to calculate the points redeemed for 2 pizzas
@@ -254,23 +258,36 @@ function calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categor
254
258
  }
255
259
  else if (redeemedOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS) {
256
260
  if (redeemedOffer.pathway_to_unlock_type_orders != null) {
257
- const { total_amount } = redeemedOffer.pathway_to_unlock_type_orders;
258
- if (totalOrderPrice >= total_amount) {
259
- // ORDERS pathway does not define max_claims, so it can be redeemed only once per order.
260
- times = 1;
261
+ const { total_amount, max_amount, max_claims } = redeemedOffer.pathway_to_unlock_type_orders;
262
+ if (totalOrderPrice >= total_amount && (max_amount == null || totalOrderPrice <= max_amount)) {
263
+ const timesForAmount = getUnlockTimesByOrderAmount(totalOrderPrice, total_amount);
264
+ const maxClaims = max_claims == null ? Infinity : Math.max(1, max_claims);
265
+ times = Math.min(timesForAmount, maxClaims);
266
+ }
267
+ }
268
+ }
269
+ else if (redeemedOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
270
+ if (redeemedOffer.pathway_to_unlock_type_vouchers != null) {
271
+ const { total_amount, max_amount, max_claims } = redeemedOffer.pathway_to_unlock_type_vouchers;
272
+ if (totalOrderPrice >= total_amount && (max_amount == null || totalOrderPrice <= max_amount)) {
273
+ const timesForAmount = getUnlockTimesByOrderAmount(totalOrderPrice, total_amount);
274
+ const maxClaims = max_claims == null ? Infinity : Math.max(1, max_claims);
275
+ times = Math.min(timesForAmount, maxClaims);
261
276
  }
262
277
  }
263
278
  }
264
279
  // 💡 NOTE: We don't need to check for pathway_to_unlock_type_points because it is not possible to have
265
280
  // pathway_to_unlock_type_points and reward_points at the same time
266
- // TODO: Add pathway_to_unlock_type_vouchers
281
+ }
282
+ if (claimedTimes != null && Number.isFinite(claimedTimes) && claimedTimes > 0) {
283
+ times = Math.min(times, Math.floor(claimedTimes));
267
284
  }
268
285
  let pointsRedeemed = redeemedOffer.reward_points.num_of_points ?? 0;
269
286
  pointsRedeemed *= times;
270
287
  return pointsRedeemed;
271
288
  }
272
289
  // The purpose of this function is to calculate the total number of points that are applied/consumed by the cart items
273
- function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, currentItemsPrice, totalDiscountApplied, categories, offers, }) {
290
+ function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, redeemedOffers, currentItemsPrice, totalDiscountApplied, categories, offers, }) {
274
291
  const totalOrderPrice = (currentItemsPrice ?? 0) - (totalDiscountApplied ?? 0);
275
292
  let loyaltyPoints = customerLoyaltyPoints ?? 0;
276
293
  let addedLoyaltyPoints = loyaltyPoints;
@@ -287,13 +304,15 @@ function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDe
287
304
  offerConsumedQuantity[redeemedOffer._id] = 0;
288
305
  }
289
306
  if (!offerConsumedQuantity[redeemedOffer._id]) {
307
+ const rawClaimedTimes = Number(redeemedOffers?.[redeemedOffer._id]);
308
+ const claimedTimes = Number.isFinite(rawClaimedTimes) && rawClaimedTimes > 0 ? Math.floor(rawClaimedTimes) : 1;
290
309
  if (redeemedOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
291
- const points = calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories);
310
+ const points = calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories, claimedTimes);
292
311
  loyaltyPoints += points;
293
312
  addedLoyaltyPoints += points;
294
313
  }
295
314
  else if (redeemedOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
296
- const points = redeemedOffer.pathway_to_unlock_type_points?.num_of_points ?? 0;
315
+ const points = (redeemedOffer.pathway_to_unlock_type_points?.num_of_points ?? 0) * claimedTimes;
297
316
  loyaltyPoints -= points;
298
317
  deductedLoyaltyPoints += points;
299
318
  }
@@ -809,6 +828,30 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
809
828
  const maxUsesPerCustomer = getOfferMaxUsesPerCustomer(offerToCheck);
810
829
  return maxUsesPerCustomer != null && (customerOfferRedemptionCounts?.[offerToCheck._id] ?? 0) >= maxUsesPerCustomer;
811
830
  };
831
+ const getRemainingOfferUsesForCustomer = (offerToCheck) => {
832
+ const maxUsesPerCustomer = getOfferMaxUsesPerCustomer(offerToCheck);
833
+ if (maxUsesPerCustomer == null)
834
+ return Infinity;
835
+ const redeemedByCustomer = customerOfferRedemptionCounts?.[offerToCheck._id] ?? 0;
836
+ return Math.max(0, maxUsesPerCustomer - redeemedByCustomer);
837
+ };
838
+ const getOrderValueClaimTimes = (orderValue, minAmount) => {
839
+ const safeRequiredAmount = Math.max(1, minAmount || 1);
840
+ return Math.max(1, Math.floor(orderValue / safeRequiredAmount));
841
+ };
842
+ const getPointsMaxRedeemableClaims = ({ offerToCheck, pathwayUnlockableClaims, }) => {
843
+ if (offerToCheck.reward_type !== constants_1.OFFER_REWARD_TYPE_ENUM.POINTS || offerToCheck.reward_points == null) {
844
+ return 1;
845
+ }
846
+ if (offerToCheck.reward_points.once_per_order) {
847
+ return 1;
848
+ }
849
+ const normalizedUnlockableClaims = Math.max(0, pathwayUnlockableClaims);
850
+ if (normalizedUnlockableClaims === 0) {
851
+ return 0;
852
+ }
853
+ return Math.min(normalizedUnlockableClaims, getRemainingOfferUsesForCustomer(offerToCheck));
854
+ };
812
855
  for (let i = 0; i < offers.length; i++) {
813
856
  const offer = offers[i];
814
857
  const stats = {
@@ -946,15 +989,23 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
946
989
  if (offer.reward_points == null)
947
990
  continue;
948
991
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
949
- const isRedeemed = redeemedTimes > 0;
992
+ const maxClaimsPerOrder = offer.pathway_to_unlock_type_orders?.max_claims == null
993
+ ? Infinity
994
+ : Math.max(1, offer.pathway_to_unlock_type_orders.max_claims);
995
+ const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, offer.pathway_to_unlock_type_orders?.total_amount);
996
+ const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
997
+ const maxRedeemableClaims = getPointsMaxRedeemableClaims({
998
+ offerToCheck: offer,
999
+ pathwayUnlockableClaims: totalUnlockableClaims,
1000
+ });
1001
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
950
1002
  stats.redeemed_times = redeemedTimes;
951
- stats.applicable_quantity = 1;
952
- if (isRedeemed) {
953
- stats.claimable = false;
1003
+ stats.applicable_quantity = maxRedeemableClaims;
1004
+ stats.claimable = isClaimable;
1005
+ if (!isClaimable) {
954
1006
  stats.message = 'This offer is redeemed ✅';
955
1007
  }
956
1008
  else {
957
- stats.claimable = true;
958
1009
  stats.message = `🎉 You have unlocked this offer 🎉`;
959
1010
  totalClaimableOffers += 1;
960
1011
  }
@@ -1199,11 +1250,15 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1199
1250
  if (offer.reward_points == null)
1200
1251
  continue;
1201
1252
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1202
- const isRedeemed = redeemedTimes > 0;
1253
+ const maxRedeemableClaims = getPointsMaxRedeemableClaims({
1254
+ offerToCheck: offer,
1255
+ pathwayUnlockableClaims: totalUnlockableClaims,
1256
+ });
1257
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
1203
1258
  stats.redeemed_times = redeemedTimes;
1204
- stats.applicable_quantity = 1;
1205
- stats.claimable = !isRedeemed;
1206
- if (isRedeemed) {
1259
+ stats.applicable_quantity = maxRedeemableClaims;
1260
+ stats.claimable = isClaimable;
1261
+ if (!isClaimable) {
1207
1262
  stats.message = 'This offer is redeemed ✅';
1208
1263
  }
1209
1264
  else {
@@ -1352,6 +1407,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1352
1407
  const { addedLoyaltyPoints, loyaltyPoints } = calculateCustomerLoyaltyPoints({
1353
1408
  customerLoyaltyPoints: customer?.loyalty_points,
1354
1409
  orderDiscountDetails: order_discount_details,
1410
+ redeemedOffers: redeemed_offers,
1355
1411
  currentItemsPrice,
1356
1412
  categories,
1357
1413
  totalDiscountApplied,
@@ -1446,11 +1502,15 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1446
1502
  if (offer.reward_points == null)
1447
1503
  continue;
1448
1504
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1449
- const isRedeemed = redeemedTimes > 0;
1505
+ const maxRedeemableClaims = getPointsMaxRedeemableClaims({
1506
+ offerToCheck: offer,
1507
+ pathwayUnlockableClaims: totalUnlockableClaims,
1508
+ });
1509
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
1450
1510
  stats.redeemed_times = redeemedTimes;
1451
- stats.applicable_quantity = 1;
1452
- stats.claimable = !isRedeemed;
1453
- if (isRedeemed) {
1511
+ stats.applicable_quantity = maxRedeemableClaims;
1512
+ stats.claimable = isClaimable;
1513
+ if (!isClaimable) {
1454
1514
  stats.message = 'This offer is redeemed ✅';
1455
1515
  }
1456
1516
  else {
@@ -1598,7 +1658,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1598
1658
  continue;
1599
1659
  }
1600
1660
  const orderValue = orderPrice - totalDiscount;
1601
- const { total_amount, max_amount, voucher_ids } = offer.pathway_to_unlock_type_vouchers;
1661
+ const { total_amount, max_amount, max_claims, voucher_ids } = offer.pathway_to_unlock_type_vouchers;
1602
1662
  const voucherReferenceNumber = order_discount_details?.voucher_reference_number;
1603
1663
  const normalizedVoucherRef = voucherReferenceNumber?.trim().toUpperCase();
1604
1664
  const referencedVouchers = voucher_ids
@@ -1696,11 +1756,18 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1696
1756
  if (offer.reward_points == null)
1697
1757
  continue;
1698
1758
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1699
- const isRedeemed = redeemedTimes > 0;
1759
+ const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
1760
+ const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
1761
+ const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
1762
+ const maxRedeemableClaims = getPointsMaxRedeemableClaims({
1763
+ offerToCheck: offer,
1764
+ pathwayUnlockableClaims: totalUnlockableClaims,
1765
+ });
1766
+ const isClaimable = redeemedTimes < maxRedeemableClaims;
1700
1767
  stats.redeemed_times = redeemedTimes;
1701
- stats.applicable_quantity = 1;
1702
- stats.claimable = !isRedeemed;
1703
- if (isRedeemed) {
1768
+ stats.applicable_quantity = maxRedeemableClaims;
1769
+ stats.claimable = isClaimable;
1770
+ if (!isClaimable) {
1704
1771
  stats.message = 'This offer is redeemed ✅';
1705
1772
  }
1706
1773
  else {
@@ -380,6 +380,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
380
380
  min_num_of_orders: number;
381
381
  max_amount?: number | null | undefined;
382
382
  max_uses_per_customer?: number | null | undefined;
383
+ max_claims?: number | null | undefined;
383
384
  max_num_of_orders?: number | null | undefined;
384
385
  } | null | undefined;
385
386
  pathway_to_unlock_type_points?: {
@@ -393,6 +394,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
393
394
  total_amount: number;
394
395
  max_amount?: number | null | undefined;
395
396
  max_uses_per_customer?: number | null | undefined;
397
+ max_claims?: number | null | undefined;
396
398
  } | null | undefined;
397
399
  reward_gift_cards?: {
398
400
  gift_card_ids: Map<string, number>;
@@ -773,6 +775,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
773
775
  min_num_of_orders: number;
774
776
  max_amount?: number | null | undefined;
775
777
  max_uses_per_customer?: number | null | undefined;
778
+ max_claims?: number | null | undefined;
776
779
  max_num_of_orders?: number | null | undefined;
777
780
  } | null | undefined;
778
781
  pathway_to_unlock_type_points?: {
@@ -786,6 +789,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
786
789
  total_amount: number;
787
790
  max_amount?: number | null | undefined;
788
791
  max_uses_per_customer?: number | null | undefined;
792
+ max_claims?: number | null | undefined;
789
793
  } | null | undefined;
790
794
  reward_gift_cards?: {
791
795
  gift_card_ids: Map<string, number>;
@@ -1166,6 +1170,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
1166
1170
  min_num_of_orders: number;
1167
1171
  max_amount?: number | null | undefined;
1168
1172
  max_uses_per_customer?: number | null | undefined;
1173
+ max_claims?: number | null | undefined;
1169
1174
  max_num_of_orders?: number | null | undefined;
1170
1175
  } | null | undefined;
1171
1176
  pathway_to_unlock_type_points?: {
@@ -1179,6 +1184,7 @@ export declare const OfferSchema: Schema<any, import("mongoose").Model<any, any,
1179
1184
  total_amount: number;
1180
1185
  max_amount?: number | null | undefined;
1181
1186
  max_uses_per_customer?: number | null | undefined;
1187
+ max_claims?: number | null | undefined;
1182
1188
  } | null | undefined;
1183
1189
  reward_gift_cards?: {
1184
1190
  gift_card_ids: Map<string, number>;
@@ -68,6 +68,11 @@ const PathwayToUnlockTypeOrdersSchema = new mongoose_1.Schema({
68
68
  required: false,
69
69
  default: null,
70
70
  },
71
+ max_claims: {
72
+ type: Number,
73
+ required: false,
74
+ default: null,
75
+ },
71
76
  min_num_of_orders: {
72
77
  type: Number,
73
78
  required: true,
@@ -128,6 +133,11 @@ const PathwayToUnlockTypeVouchersSchema = new mongoose_1.Schema({
128
133
  required: false,
129
134
  default: null,
130
135
  },
136
+ max_claims: {
137
+ type: Number,
138
+ required: false,
139
+ default: null,
140
+ },
131
141
  voucher_ids: {
132
142
  type: [mongoose_1.Schema.Types.ObjectId],
133
143
  required: true,
@@ -3,6 +3,7 @@ import { IItemSpecialHour } from './common';
3
3
  export interface IPathwayToUnlockTypeOrders {
4
4
  scope: PATHWAY_TO_UNLOCK_TYPE_ORDERS_SCOPE_ENUM;
5
5
  max_uses_per_customer: number | null;
6
+ max_claims: number | null;
6
7
  min_num_of_orders: number;
7
8
  max_num_of_orders: number | null;
8
9
  total_amount: number;
@@ -30,6 +31,7 @@ export interface IPathwayToUnlockTypeVouchers {
30
31
  total_amount: number;
31
32
  max_amount: number | null;
32
33
  max_uses_per_customer: number | null;
34
+ max_claims: number | null;
33
35
  voucher_ids: string[];
34
36
  }
35
37
  type IOfferPathwayItems = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.290",
3
+ "version": "0.2.291",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",