@managesome/eposmatic-toolkit 0.2.292 → 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.
|
@@ -434,7 +434,7 @@ function getTotalSelectedOfferItemsAgainstOffer(categories, offerId) {
|
|
|
434
434
|
}
|
|
435
435
|
return totalSelectedOfferItems;
|
|
436
436
|
}
|
|
437
|
-
function getReconciledRedeemedOffersMap({ existingRedeemedOffers, categories, offers, removedOfferIds, }) {
|
|
437
|
+
function getReconciledRedeemedOffersMap({ existingRedeemedOffers, categories, offers, removedOfferIds, unlockedOffersApplicableTimes, }) {
|
|
438
438
|
const nextRedeemedOffers = {
|
|
439
439
|
...(getNormalizedRedeemedOffersMap(existingRedeemedOffers) ?? {}),
|
|
440
440
|
};
|
|
@@ -451,17 +451,28 @@ function getReconciledRedeemedOffersMap({ existingRedeemedOffers, categories, of
|
|
|
451
451
|
if (normalizedOfferId == null)
|
|
452
452
|
continue;
|
|
453
453
|
const applicableQuantity = getItemRewardApplicableQuantity(offer);
|
|
454
|
-
if (applicableQuantity
|
|
454
|
+
if (applicableQuantity != null) {
|
|
455
|
+
const selectedItemsCount = getTotalSelectedOfferItemsAgainstOffer(categories, normalizedOfferId);
|
|
456
|
+
// Percentage strategy: any partial usage of an item reward counts as a redeemed slot.
|
|
457
|
+
const redeemedTimes = selectedItemsCount > 0 ? Math.ceil(selectedItemsCount / applicableQuantity) : 0;
|
|
458
|
+
if (redeemedTimes > 0) {
|
|
459
|
+
nextRedeemedOffers[normalizedOfferId] = redeemedTimes;
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
delete nextRedeemedOffers[normalizedOfferId];
|
|
463
|
+
}
|
|
455
464
|
continue;
|
|
456
|
-
const selectedItemsCount = getTotalSelectedOfferItemsAgainstOffer(categories, normalizedOfferId);
|
|
457
|
-
// Percentage strategy: any partial usage of an item reward counts as a redeemed slot.
|
|
458
|
-
const redeemedTimes = selectedItemsCount > 0 ? Math.ceil(selectedItemsCount / applicableQuantity) : 0;
|
|
459
|
-
if (redeemedTimes > 0) {
|
|
460
|
-
nextRedeemedOffers[normalizedOfferId] = redeemedTimes;
|
|
461
465
|
}
|
|
462
|
-
|
|
466
|
+
const existingRedeemedTimes = toPositiveInteger(nextRedeemedOffers[normalizedOfferId], 0);
|
|
467
|
+
if (existingRedeemedTimes <= 0) {
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
const maxApplicableTimes = toPositiveInteger(unlockedOffersApplicableTimes?.[offer._id], 0);
|
|
471
|
+
if (maxApplicableTimes <= 0) {
|
|
463
472
|
delete nextRedeemedOffers[normalizedOfferId];
|
|
473
|
+
continue;
|
|
464
474
|
}
|
|
475
|
+
nextRedeemedOffers[normalizedOfferId] = Math.min(existingRedeemedTimes, maxApplicableTimes);
|
|
465
476
|
}
|
|
466
477
|
return getNormalizedRedeemedOffersMap(nextRedeemedOffers);
|
|
467
478
|
}
|
|
@@ -2103,6 +2114,7 @@ offers) {
|
|
|
2103
2114
|
categories,
|
|
2104
2115
|
offers,
|
|
2105
2116
|
removedOfferIds: new Set(),
|
|
2117
|
+
unlockedOffersApplicableTimes,
|
|
2106
2118
|
});
|
|
2107
2119
|
const clearOrderLevelDiscount = (discountType) => {
|
|
2108
2120
|
if (order_discount_details == null) {
|
|
@@ -2330,6 +2342,7 @@ offers) {
|
|
|
2330
2342
|
categories: newCategories,
|
|
2331
2343
|
offers,
|
|
2332
2344
|
removedOfferIds,
|
|
2345
|
+
unlockedOffersApplicableTimes,
|
|
2333
2346
|
});
|
|
2334
2347
|
if (!areRedeemedOffersEqual(redeemed_offers, reconciledRedeemedOffers)) {
|
|
2335
2348
|
redeemed_offers = reconciledRedeemedOffers;
|
|
@@ -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");
|
|
@@ -371,112 +374,114 @@ function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDe
|
|
|
371
374
|
let loyaltyPoints = customerLoyaltyPoints ?? 0;
|
|
372
375
|
let addedLoyaltyPoints = loyaltyPoints;
|
|
373
376
|
let deductedLoyaltyPoints = loyaltyPoints;
|
|
377
|
+
const offersThatHavePointsMap = {};
|
|
374
378
|
const offerApplicableQuantity = {};
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
const consumedQuantity = offerConsumedQuantity[redeemedOffer._id] ?? 0;
|
|
382
|
-
if (consumedQuantity >= applicableQuantity) {
|
|
383
|
-
offerConsumedQuantity[redeemedOffer._id] = 0;
|
|
379
|
+
for (let i = 0; i < offers.length; i++) {
|
|
380
|
+
const offer = offers[i];
|
|
381
|
+
const isRewardPointsOffer = offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS;
|
|
382
|
+
const isPointsPathwayOffer = offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS;
|
|
383
|
+
if (!isRewardPointsOffer && !isPointsPathwayOffer) {
|
|
384
|
+
continue;
|
|
384
385
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
if (redeemedOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS && includePendingRewardPoints) {
|
|
389
|
-
const points = calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories, claimedTimes);
|
|
390
|
-
loyaltyPoints += points;
|
|
391
|
-
addedLoyaltyPoints += points;
|
|
392
|
-
}
|
|
393
|
-
else if (redeemedOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
|
|
394
|
-
const points = (redeemedOffer.pathway_to_unlock_type_points?.num_of_points ?? 0) * claimedTimes;
|
|
395
|
-
loyaltyPoints -= points;
|
|
396
|
-
deductedLoyaltyPoints += points;
|
|
397
|
-
}
|
|
398
|
-
offerConsumedQuantity[redeemedOffer._id] = 1;
|
|
386
|
+
offersThatHavePointsMap[offer._id] = offer;
|
|
387
|
+
if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
|
|
388
|
+
offerApplicableQuantity[offer._id] = Math.max(1, offer.reward_items?.num_of_items ?? 1);
|
|
399
389
|
}
|
|
400
|
-
else {
|
|
401
|
-
|
|
390
|
+
else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
|
|
391
|
+
offerApplicableQuantity[offer._id] = Math.max(1, offer.reward_discount_items?.num_of_items ?? 1);
|
|
402
392
|
}
|
|
403
|
-
|
|
404
|
-
const offersThatHavePointsMap = {};
|
|
405
|
-
let hasOffersThatHavePoints = false;
|
|
406
|
-
for (let i = 0; i < offers.length; i++) {
|
|
407
|
-
const offer = offers[i];
|
|
408
|
-
if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
|
|
409
|
-
offersThatHavePointsMap[offer._id] = offer;
|
|
410
|
-
hasOffersThatHavePoints = true;
|
|
393
|
+
else {
|
|
411
394
|
offerApplicableQuantity[offer._id] = 1;
|
|
412
395
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
396
|
+
}
|
|
397
|
+
const pointsRelevantOfferIds = Object.keys(offersThatHavePointsMap);
|
|
398
|
+
if (pointsRelevantOfferIds.length > 0) {
|
|
399
|
+
const claimedTimesByOfferId = {};
|
|
400
|
+
const markClaimedTimes = (offerId, claimsCount) => {
|
|
401
|
+
if (offerId == null) {
|
|
402
|
+
return;
|
|
418
403
|
}
|
|
419
|
-
|
|
420
|
-
|
|
404
|
+
const offer = offersThatHavePointsMap[offerId];
|
|
405
|
+
if (offer == null) {
|
|
406
|
+
return;
|
|
421
407
|
}
|
|
422
|
-
|
|
423
|
-
|
|
408
|
+
const parsedClaimsCount = Number(claimsCount);
|
|
409
|
+
if (!Number.isFinite(parsedClaimsCount) || parsedClaimsCount <= 0) {
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
const safeClaimsCount = Math.max(1, Math.floor(parsedClaimsCount));
|
|
413
|
+
claimedTimesByOfferId[offerId] = Math.max(claimedTimesByOfferId[offerId] ?? 0, safeClaimsCount);
|
|
414
|
+
};
|
|
415
|
+
// Canonical claim source: redeemed_offers map.
|
|
416
|
+
if (redeemedOffers != null) {
|
|
417
|
+
const redeemedOfferIds = Object.keys(redeemedOffers);
|
|
418
|
+
for (let i = 0; i < redeemedOfferIds.length; i++) {
|
|
419
|
+
const offerId = redeemedOfferIds[i];
|
|
420
|
+
markClaimedTimes(offerId, redeemedOffers[offerId]);
|
|
424
421
|
}
|
|
425
422
|
}
|
|
426
|
-
|
|
427
|
-
if (hasOffersThatHavePoints) {
|
|
428
|
-
// Check if the order has any loyalty or voucher applied
|
|
423
|
+
// Fallback claim signals from order-level discount ids.
|
|
429
424
|
if (orderDiscountDetails != null) {
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
425
|
+
markClaimedTimes(orderDiscountDetails.loyalty_discount_id, 1);
|
|
426
|
+
markClaimedTimes(orderDiscountDetails.voucher_discount_id, 1);
|
|
427
|
+
}
|
|
428
|
+
// Secondary claim signal from selected reward items in cart.
|
|
429
|
+
if (categories != null && categories.length > 0) {
|
|
430
|
+
const selectedItemsCountByOfferId = {};
|
|
431
|
+
for (let i = 0; i < categories.length; i++) {
|
|
432
|
+
const category = categories[i];
|
|
433
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
434
|
+
const item = category.items[j];
|
|
435
|
+
const itemDiscountDetails = item.item_discount_details;
|
|
436
|
+
if (itemDiscountDetails == null) {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
const safeQuantity = Number(item.quantity);
|
|
440
|
+
if (!Number.isFinite(safeQuantity) || safeQuantity <= 0) {
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
const quantity = Math.floor(safeQuantity);
|
|
444
|
+
const loyaltyOffer = offersThatHavePointsMap[itemDiscountDetails.loyalty_discount_id ?? ''];
|
|
445
|
+
if (loyaltyOffer != null) {
|
|
446
|
+
selectedItemsCountByOfferId[loyaltyOffer._id] =
|
|
447
|
+
(selectedItemsCountByOfferId[loyaltyOffer._id] ?? 0) + quantity;
|
|
448
|
+
}
|
|
449
|
+
const voucherOffer = offersThatHavePointsMap[itemDiscountDetails.voucher_discount_id ?? ''];
|
|
450
|
+
if (voucherOffer != null) {
|
|
451
|
+
selectedItemsCountByOfferId[voucherOffer._id] =
|
|
452
|
+
(selectedItemsCountByOfferId[voucherOffer._id] ?? 0) + quantity;
|
|
453
|
+
}
|
|
435
454
|
}
|
|
436
455
|
}
|
|
437
|
-
const
|
|
438
|
-
|
|
439
|
-
const
|
|
440
|
-
|
|
441
|
-
|
|
456
|
+
const selectedItemOfferIds = Object.keys(selectedItemsCountByOfferId);
|
|
457
|
+
for (let i = 0; i < selectedItemOfferIds.length; i++) {
|
|
458
|
+
const offerId = selectedItemOfferIds[i];
|
|
459
|
+
const selectedItemsCount = selectedItemsCountByOfferId[offerId] ?? 0;
|
|
460
|
+
if (selectedItemsCount <= 0) {
|
|
461
|
+
continue;
|
|
442
462
|
}
|
|
463
|
+
const applicableQuantity = Math.max(1, offerApplicableQuantity[offerId] ?? 1);
|
|
464
|
+
const derivedClaimsCount = Math.ceil(selectedItemsCount / applicableQuantity);
|
|
465
|
+
markClaimedTimes(offerId, derivedClaimsCount);
|
|
443
466
|
}
|
|
444
467
|
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
const
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
// we skip the item if it doesn't have any loyalty or voucher applied
|
|
463
|
-
if (item.item_discount_details == null)
|
|
464
|
-
continue;
|
|
465
|
-
// Step 1: Use Case for Offers
|
|
466
|
-
if (item.item_discount_details.loyalty_discount_id != null) {
|
|
467
|
-
const loyaltyDiscountId = item.item_discount_details.loyalty_discount_id;
|
|
468
|
-
const redeemedOffer = offersThatHavePointsMap[loyaltyDiscountId];
|
|
469
|
-
if (redeemedOffer != null) {
|
|
470
|
-
renderClaimPoints(redeemedOffer);
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
if (item.item_discount_details.voucher_discount_id != null) {
|
|
474
|
-
const voucherDiscountId = item.item_discount_details.voucher_discount_id;
|
|
475
|
-
const redeemedOffer = offersThatHavePointsMap[voucherDiscountId];
|
|
476
|
-
if (redeemedOffer != null) {
|
|
477
|
-
renderClaimPoints(redeemedOffer);
|
|
478
|
-
}
|
|
479
|
-
}
|
|
468
|
+
const claimedOfferIds = Object.keys(claimedTimesByOfferId);
|
|
469
|
+
for (let i = 0; i < claimedOfferIds.length; i++) {
|
|
470
|
+
const offerId = claimedOfferIds[i];
|
|
471
|
+
const redeemedOffer = offersThatHavePointsMap[offerId];
|
|
472
|
+
const claimedTimes = claimedTimesByOfferId[offerId] ?? 0;
|
|
473
|
+
if (redeemedOffer == null || claimedTimes <= 0) {
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
if (redeemedOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS && includePendingRewardPoints) {
|
|
477
|
+
const points = calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories, claimedTimes);
|
|
478
|
+
loyaltyPoints += points;
|
|
479
|
+
addedLoyaltyPoints += points;
|
|
480
|
+
}
|
|
481
|
+
if (redeemedOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
|
|
482
|
+
const points = (redeemedOffer.pathway_to_unlock_type_points?.num_of_points ?? 0) * claimedTimes;
|
|
483
|
+
loyaltyPoints -= points;
|
|
484
|
+
deductedLoyaltyPoints += points;
|
|
480
485
|
}
|
|
481
486
|
}
|
|
482
487
|
}
|
|
@@ -611,6 +616,55 @@ const getTotalSelectedOfferItemsAgainstOffer = (categories, offer_id) => {
|
|
|
611
616
|
return totalOfferItems;
|
|
612
617
|
};
|
|
613
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
|
+
}
|
|
614
668
|
function buildRewardPreview(offer) {
|
|
615
669
|
switch (offer.reward_type) {
|
|
616
670
|
case constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT: {
|