@managesome/eposmatic-toolkit 0.2.293 → 0.2.294

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.
@@ -61,6 +61,16 @@ export type IOfferWithStats = {
61
61
  offer: IOffer;
62
62
  stats: IOfferStats;
63
63
  };
64
+ export declare function getOfferRemainingClaimsFromStats(stats: Pick<IOfferStats, 'applicable_quantity' | 'redeemed_times'> | null | undefined): number;
65
+ export declare function getOfferClaimIncrementFromStats({ stats, fallbackClaims, }: {
66
+ stats: Pick<IOfferStats, 'applicable_quantity' | 'redeemed_times'> | null | undefined;
67
+ fallbackClaims?: number;
68
+ }): number;
69
+ export declare function buildRedeemedOffersClaimPatch({ currentRedeemedOffers, offerId, claimsIncrement, }: {
70
+ currentRedeemedOffers: IOrder['redeemed_offers'] | null | undefined;
71
+ offerId: string | null | undefined;
72
+ claimsIncrement?: number;
73
+ }): Partial<Pick<IOrder, 'redeemed_offers'>>;
64
74
  interface IGetOffersWithStats {
65
75
  offers: IOffer[];
66
76
  customer: Partial<ICustomer> | null;
@@ -6,6 +6,9 @@ exports.convertOffersToWorkableOffers = convertOffersToWorkableOffers;
6
6
  exports.calculatePointsRedeemedForOffer = calculatePointsRedeemedForOffer;
7
7
  exports.calculateCustomerSpendableVoucherCodes = calculateCustomerSpendableVoucherCodes;
8
8
  exports.calculateCustomerLoyaltyPoints = calculateCustomerLoyaltyPoints;
9
+ exports.getOfferRemainingClaimsFromStats = getOfferRemainingClaimsFromStats;
10
+ exports.getOfferClaimIncrementFromStats = getOfferClaimIncrementFromStats;
11
+ exports.buildRedeemedOffersClaimPatch = buildRedeemedOffersClaimPatch;
9
12
  exports.getOffersWithStats = getOffersWithStats;
10
13
  const constants_1 = require("../../constants");
11
14
  const common_1 = require("./common");
@@ -613,6 +616,55 @@ const getTotalSelectedOfferItemsAgainstOffer = (categories, offer_id) => {
613
616
  return totalOfferItems;
614
617
  };
615
618
  exports.getTotalSelectedOfferItemsAgainstOffer = getTotalSelectedOfferItemsAgainstOffer;
619
+ function normalizeOfferId(offerId) {
620
+ if (offerId == null)
621
+ return null;
622
+ const normalizedOfferId = offerId.toString().trim();
623
+ return normalizedOfferId.length > 0 ? normalizedOfferId : null;
624
+ }
625
+ function toPositiveInteger(value, fallback = 0) {
626
+ const parsedValue = Number(value);
627
+ if (!Number.isFinite(parsedValue) || parsedValue <= 0) {
628
+ return fallback;
629
+ }
630
+ return Math.floor(parsedValue);
631
+ }
632
+ function getOfferRemainingClaimsFromStats(stats) {
633
+ const applicableQuantity = toPositiveInteger(stats?.applicable_quantity, 0);
634
+ const redeemedTimes = toPositiveInteger(stats?.redeemed_times, 0);
635
+ return Math.max(0, applicableQuantity - redeemedTimes);
636
+ }
637
+ function getOfferClaimIncrementFromStats({ stats, fallbackClaims = 0, }) {
638
+ const remainingClaims = getOfferRemainingClaimsFromStats(stats);
639
+ if (remainingClaims > 0) {
640
+ return remainingClaims;
641
+ }
642
+ return toPositiveInteger(fallbackClaims, 0);
643
+ }
644
+ function buildRedeemedOffersClaimPatch({ currentRedeemedOffers, offerId, claimsIncrement = 1, }) {
645
+ const normalizedOfferId = normalizeOfferId(offerId);
646
+ if (normalizedOfferId == null) {
647
+ return {};
648
+ }
649
+ const nextRedeemedOffers = {};
650
+ const redeemedOfferEntries = Object.entries(currentRedeemedOffers ?? {});
651
+ for (let i = 0; i < redeemedOfferEntries.length; i++) {
652
+ const [offerKey, redeemedTimes] = redeemedOfferEntries[i];
653
+ const normalizedOfferKey = normalizeOfferId(offerKey);
654
+ if (normalizedOfferKey == null)
655
+ continue;
656
+ const safeRedeemedTimes = toPositiveInteger(redeemedTimes, 0);
657
+ if (safeRedeemedTimes <= 0)
658
+ continue;
659
+ nextRedeemedOffers[normalizedOfferKey] = safeRedeemedTimes;
660
+ }
661
+ const safeClaimsIncrement = toPositiveInteger(claimsIncrement, 1);
662
+ const currentRedeemedTimes = toPositiveInteger(nextRedeemedOffers[normalizedOfferId], 0);
663
+ nextRedeemedOffers[normalizedOfferId] = currentRedeemedTimes + safeClaimsIncrement;
664
+ return {
665
+ redeemed_offers: Object.keys(nextRedeemedOffers).length > 0 ? nextRedeemedOffers : null,
666
+ };
667
+ }
616
668
  function buildRewardPreview(offer) {
617
669
  switch (offer.reward_type) {
618
670
  case constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.293",
3
+ "version": "0.2.294",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",