@managesome/eposmatic-toolkit 0.2.291 → 0.2.292

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.
@@ -11,8 +11,22 @@ interface ICalculateCustomerLoyaltyPoints {
11
11
  totalDiscountApplied: number | undefined;
12
12
  categories: IOrderCategory[] | undefined;
13
13
  offers: IOffer[];
14
+ includePendingRewardPoints?: boolean;
14
15
  }
15
- export declare function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, redeemedOffers, currentItemsPrice, totalDiscountApplied, categories, offers, }: ICalculateCustomerLoyaltyPoints): {
16
+ interface ICalculateCustomerSpendableVoucherCodes {
17
+ customerVoucherCodes: ICustomer['voucher_codes'] | null | undefined;
18
+ orderDiscountDetails: IDiscountDetails | null | undefined;
19
+ redeemedOffers?: IOrder['redeemed_offers'] | null | undefined;
20
+ offers: IOffer[];
21
+ vouchers: IVoucher[] | undefined;
22
+ }
23
+ export declare function calculateCustomerSpendableVoucherCodes({ customerVoucherCodes, orderDiscountDetails, redeemedOffers, offers, vouchers, }: ICalculateCustomerSpendableVoucherCodes): {
24
+ voucherCodes: Record<string, number>;
25
+ totalVouchers: number;
26
+ activeVoucherCode: string | null;
27
+ consumedVoucherClaims: number;
28
+ };
29
+ export declare function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, redeemedOffers, currentItemsPrice, totalDiscountApplied, categories, offers, includePendingRewardPoints, }: ICalculateCustomerLoyaltyPoints): {
16
30
  loyaltyPoints: number;
17
31
  addedLoyaltyPoints: number;
18
32
  deductedLoyaltyPoints: number;
@@ -4,6 +4,7 @@ exports.getAppliedOfferIds = exports.getTotalSelectedOfferItemsAgainstOffer = vo
4
4
  exports.getOfferWithPrice = getOfferWithPrice;
5
5
  exports.convertOffersToWorkableOffers = convertOffersToWorkableOffers;
6
6
  exports.calculatePointsRedeemedForOffer = calculatePointsRedeemedForOffer;
7
+ exports.calculateCustomerSpendableVoucherCodes = calculateCustomerSpendableVoucherCodes;
7
8
  exports.calculateCustomerLoyaltyPoints = calculateCustomerLoyaltyPoints;
8
9
  exports.getOffersWithStats = getOffersWithStats;
9
10
  const constants_1 = require("../../constants");
@@ -286,8 +287,86 @@ function calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categor
286
287
  pointsRedeemed *= times;
287
288
  return pointsRedeemed;
288
289
  }
290
+ function normalizeVoucherCode(voucherCode) {
291
+ const normalizedCode = voucherCode?.trim().toUpperCase();
292
+ return normalizedCode != null && normalizedCode.length > 0 ? normalizedCode : null;
293
+ }
294
+ function getOfferRedeemedClaims(redeemedOffers, offerId) {
295
+ const redeemedTimes = Number(redeemedOffers?.[offerId]);
296
+ if (Number.isFinite(redeemedTimes) && redeemedTimes > 0) {
297
+ return Math.floor(redeemedTimes);
298
+ }
299
+ return 0;
300
+ }
301
+ function calculateCustomerSpendableVoucherCodes({ customerVoucherCodes, orderDiscountDetails, redeemedOffers, offers, vouchers, }) {
302
+ const voucherCodes = {};
303
+ if (customerVoucherCodes != null) {
304
+ const entries = Object.entries(customerVoucherCodes);
305
+ for (let i = 0; i < entries.length; i++) {
306
+ const [code, count] = entries[i];
307
+ const normalizedCode = normalizeVoucherCode(code);
308
+ if (normalizedCode == null)
309
+ continue;
310
+ const parsedCount = Number(count);
311
+ if (!Number.isFinite(parsedCount) || parsedCount <= 0)
312
+ continue;
313
+ voucherCodes[normalizedCode] = Math.floor(parsedCount);
314
+ }
315
+ }
316
+ const activeVoucherCode = normalizeVoucherCode(orderDiscountDetails?.voucher_reference_number);
317
+ if (activeVoucherCode == null || vouchers == null || offers.length === 0) {
318
+ return {
319
+ voucherCodes,
320
+ totalVouchers: Object.values(voucherCodes).reduce((acc, count) => acc + count, 0),
321
+ activeVoucherCode,
322
+ consumedVoucherClaims: 0,
323
+ };
324
+ }
325
+ const activeVoucherIds = new Set();
326
+ for (let i = 0; i < vouchers.length; i++) {
327
+ const voucher = vouchers[i];
328
+ if (normalizeVoucherCode(voucher.code) === activeVoucherCode) {
329
+ activeVoucherIds.add(voucher._id);
330
+ }
331
+ }
332
+ let consumedVoucherClaims = 0;
333
+ if (activeVoucherIds.size > 0) {
334
+ for (let i = 0; i < offers.length; i++) {
335
+ const offer = offers[i];
336
+ if (offer.pathway_to_unlock_type !== constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER) {
337
+ continue;
338
+ }
339
+ const pathwayVouchers = offer.pathway_to_unlock_type_vouchers;
340
+ if (pathwayVouchers == null || pathwayVouchers.voucher_ids.length === 0) {
341
+ continue;
342
+ }
343
+ let supportsActiveVoucherCode = false;
344
+ for (let j = 0; j < pathwayVouchers.voucher_ids.length; j++) {
345
+ if (activeVoucherIds.has(pathwayVouchers.voucher_ids[j])) {
346
+ supportsActiveVoucherCode = true;
347
+ break;
348
+ }
349
+ }
350
+ if (!supportsActiveVoucherCode)
351
+ continue;
352
+ const redeemedTimes = getOfferRedeemedClaims(redeemedOffers, offer._id);
353
+ if (redeemedTimes > 0) {
354
+ consumedVoucherClaims += redeemedTimes;
355
+ }
356
+ }
357
+ }
358
+ if (voucherCodes[activeVoucherCode] != null && consumedVoucherClaims > 0) {
359
+ voucherCodes[activeVoucherCode] = Math.max(0, voucherCodes[activeVoucherCode] - consumedVoucherClaims);
360
+ }
361
+ return {
362
+ voucherCodes,
363
+ totalVouchers: Object.values(voucherCodes).reduce((acc, count) => acc + count, 0),
364
+ activeVoucherCode,
365
+ consumedVoucherClaims,
366
+ };
367
+ }
289
368
  // The purpose of this function is to calculate the total number of points that are applied/consumed by the cart items
290
- function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, redeemedOffers, currentItemsPrice, totalDiscountApplied, categories, offers, }) {
369
+ function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDetails, redeemedOffers, currentItemsPrice, totalDiscountApplied, categories, offers, includePendingRewardPoints = false, }) {
291
370
  const totalOrderPrice = (currentItemsPrice ?? 0) - (totalDiscountApplied ?? 0);
292
371
  let loyaltyPoints = customerLoyaltyPoints ?? 0;
293
372
  let addedLoyaltyPoints = loyaltyPoints;
@@ -306,7 +385,7 @@ function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDe
306
385
  if (!offerConsumedQuantity[redeemedOffer._id]) {
307
386
  const rawClaimedTimes = Number(redeemedOffers?.[redeemedOffer._id]);
308
387
  const claimedTimes = Number.isFinite(rawClaimedTimes) && rawClaimedTimes > 0 ? Math.floor(rawClaimedTimes) : 1;
309
- if (redeemedOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
388
+ if (redeemedOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS && includePendingRewardPoints) {
310
389
  const points = calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories, claimedTimes);
311
390
  loyaltyPoints += points;
312
391
  addedLoyaltyPoints += points;
@@ -710,24 +789,14 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
710
789
  const unlockedOffersApplicableTimes = {};
711
790
  let totalClaimableOffers = 0;
712
791
  let totalLoyaltyPointsOfCustomer = null;
713
- let totalAddedLoyaltyPoints = 0;
714
792
  const nowTs = Date.now();
715
- const customerOwnedVoucherCountByCode = {};
716
- const customerVoucherCodes = customer?.voucher_codes;
717
- if (customerVoucherCodes != null) {
718
- const voucherCodeEntries = Object.entries(customerVoucherCodes);
719
- for (let i = 0; i < voucherCodeEntries.length; i++) {
720
- const [voucherCode, count] = voucherCodeEntries[i];
721
- const normalizedCode = voucherCode.trim().toUpperCase();
722
- if (normalizedCode.length === 0)
723
- continue;
724
- const parsedCount = Number(count);
725
- if (!Number.isFinite(parsedCount) || parsedCount <= 0)
726
- continue;
727
- customerOwnedVoucherCountByCode[normalizedCode] =
728
- (customerOwnedVoucherCountByCode[normalizedCode] ?? 0) + parsedCount;
729
- }
730
- }
793
+ const { voucherCodes: spendableVoucherCodesByCode } = calculateCustomerSpendableVoucherCodes({
794
+ customerVoucherCodes: customer?.voucher_codes,
795
+ orderDiscountDetails: order_discount_details,
796
+ redeemedOffers: redeemed_offers,
797
+ offers,
798
+ vouchers,
799
+ });
731
800
  const redeemedOffersById = redeemed_offers ?? null;
732
801
  const vouchersById = new Map();
733
802
  if (vouchers != null) {
@@ -1404,7 +1473,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1404
1473
  const { num_of_points, total_amount, max_claims } = offer.pathway_to_unlock_type_points;
1405
1474
  const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
1406
1475
  if (totalLoyaltyPointsOfCustomer == null) {
1407
- const { addedLoyaltyPoints, loyaltyPoints } = calculateCustomerLoyaltyPoints({
1476
+ const { loyaltyPoints } = calculateCustomerLoyaltyPoints({
1408
1477
  customerLoyaltyPoints: customer?.loyalty_points,
1409
1478
  orderDiscountDetails: order_discount_details,
1410
1479
  redeemedOffers: redeemed_offers,
@@ -1412,14 +1481,14 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1412
1481
  categories,
1413
1482
  totalDiscountApplied,
1414
1483
  offers,
1484
+ includePendingRewardPoints: false,
1415
1485
  });
1416
1486
  totalLoyaltyPointsOfCustomer = loyaltyPoints;
1417
- totalAddedLoyaltyPoints = addedLoyaltyPoints;
1418
1487
  }
1419
1488
  const pointsProgress = getPreUnlockProgressPercentage(totalLoyaltyPointsOfCustomer ?? 0, num_of_points || 1);
1420
1489
  const amountProgress = getPreUnlockProgressPercentage(orderValue, total_amount || 1);
1421
1490
  const combinedProgress = Math.min(pointsProgress, amountProgress);
1422
- if (totalAddedLoyaltyPoints >= num_of_points) {
1491
+ if ((totalLoyaltyPointsOfCustomer ?? 0) >= num_of_points) {
1423
1492
  if (orderValue >= total_amount) {
1424
1493
  if ((totalLoyaltyPointsOfCustomer ?? 0) >= num_of_points) {
1425
1494
  stats.progress = 100;
@@ -1669,11 +1738,12 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1669
1738
  : (referencedVouchers.find((voucher) => voucher.code.trim().toUpperCase() === normalizedVoucherRef) ?? null);
1670
1739
  const hasMatchingVoucher = matchedVoucher != null;
1671
1740
  const normalizedMatchedVoucherCode = matchedVoucher?.code.trim().toUpperCase() ?? null;
1672
- const customerOwnedVoucherCount = normalizedMatchedVoucherCode == null ? 0 : (customerOwnedVoucherCountByCode[normalizedMatchedVoucherCode] ?? 0);
1741
+ const redeemedTimesForOffer = getOfferRedeemedTimes(offer._id);
1742
+ const spendableVoucherCount = normalizedMatchedVoucherCode == null ? 0 : (spendableVoucherCodesByCode[normalizedMatchedVoucherCode] ?? 0);
1673
1743
  const customerVoucherUsageCount = matchedVoucher == null ? 0 : (customerOfferRedemptionCounts?.[matchedVoucher._id] ?? 0);
1674
1744
  const voucherUsesPerCustomerExhausted = matchedVoucher?.max_uses_per_customer != null &&
1675
1745
  customerVoucherUsageCount >= matchedVoucher.max_uses_per_customer;
1676
- const hasVoucherAccess = matchedVoucher != null && (matchedVoucher.is_public || customerOwnedVoucherCount > 0);
1746
+ const hasVoucherAccess = matchedVoucher != null && (matchedVoucher.is_public || spendableVoucherCount > 0 || redeemedTimesForOffer > 0);
1677
1747
  const amountProgress = getPreUnlockProgressPercentage(orderValue, total_amount || 1);
1678
1748
  if (!hasMatchingVoucher) {
1679
1749
  stats.progress = 0;
@@ -1758,7 +1828,10 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
1758
1828
  const redeemedTimes = getOfferRedeemedTimes(offer._id);
1759
1829
  const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
1760
1830
  const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
1761
- const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
1831
+ const voucherInventoryClaimCap = matchedVoucher != null && !matchedVoucher.is_public
1832
+ ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer)
1833
+ : Infinity;
1834
+ const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
1762
1835
  const maxRedeemableClaims = getPointsMaxRedeemableClaims({
1763
1836
  offerToCheck: offer,
1764
1837
  pathwayUnlockableClaims: totalUnlockableClaims,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.291",
3
+ "version": "0.2.292",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",