@managesome/eposmatic-toolkit 0.2.291 → 0.2.293
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;
|
|
@@ -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
|
-
|
|
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,118 +287,198 @@ 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;
|
|
294
373
|
let deductedLoyaltyPoints = loyaltyPoints;
|
|
374
|
+
const offersThatHavePointsMap = {};
|
|
295
375
|
const offerApplicableQuantity = {};
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
const consumedQuantity = offerConsumedQuantity[redeemedOffer._id] ?? 0;
|
|
303
|
-
if (consumedQuantity >= applicableQuantity) {
|
|
304
|
-
offerConsumedQuantity[redeemedOffer._id] = 0;
|
|
376
|
+
for (let i = 0; i < offers.length; i++) {
|
|
377
|
+
const offer = offers[i];
|
|
378
|
+
const isRewardPointsOffer = offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS;
|
|
379
|
+
const isPointsPathwayOffer = offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS;
|
|
380
|
+
if (!isRewardPointsOffer && !isPointsPathwayOffer) {
|
|
381
|
+
continue;
|
|
305
382
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
if (redeemedOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
|
|
310
|
-
const points = calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories, claimedTimes);
|
|
311
|
-
loyaltyPoints += points;
|
|
312
|
-
addedLoyaltyPoints += points;
|
|
313
|
-
}
|
|
314
|
-
else if (redeemedOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
|
|
315
|
-
const points = (redeemedOffer.pathway_to_unlock_type_points?.num_of_points ?? 0) * claimedTimes;
|
|
316
|
-
loyaltyPoints -= points;
|
|
317
|
-
deductedLoyaltyPoints += points;
|
|
318
|
-
}
|
|
319
|
-
offerConsumedQuantity[redeemedOffer._id] = 1;
|
|
383
|
+
offersThatHavePointsMap[offer._id] = offer;
|
|
384
|
+
if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
|
|
385
|
+
offerApplicableQuantity[offer._id] = Math.max(1, offer.reward_items?.num_of_items ?? 1);
|
|
320
386
|
}
|
|
321
|
-
else {
|
|
322
|
-
|
|
387
|
+
else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
|
|
388
|
+
offerApplicableQuantity[offer._id] = Math.max(1, offer.reward_discount_items?.num_of_items ?? 1);
|
|
323
389
|
}
|
|
324
|
-
|
|
325
|
-
const offersThatHavePointsMap = {};
|
|
326
|
-
let hasOffersThatHavePoints = false;
|
|
327
|
-
for (let i = 0; i < offers.length; i++) {
|
|
328
|
-
const offer = offers[i];
|
|
329
|
-
if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS) {
|
|
330
|
-
offersThatHavePointsMap[offer._id] = offer;
|
|
331
|
-
hasOffersThatHavePoints = true;
|
|
390
|
+
else {
|
|
332
391
|
offerApplicableQuantity[offer._id] = 1;
|
|
333
392
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
393
|
+
}
|
|
394
|
+
const pointsRelevantOfferIds = Object.keys(offersThatHavePointsMap);
|
|
395
|
+
if (pointsRelevantOfferIds.length > 0) {
|
|
396
|
+
const claimedTimesByOfferId = {};
|
|
397
|
+
const markClaimedTimes = (offerId, claimsCount) => {
|
|
398
|
+
if (offerId == null) {
|
|
399
|
+
return;
|
|
339
400
|
}
|
|
340
|
-
|
|
341
|
-
|
|
401
|
+
const offer = offersThatHavePointsMap[offerId];
|
|
402
|
+
if (offer == null) {
|
|
403
|
+
return;
|
|
342
404
|
}
|
|
343
|
-
|
|
344
|
-
|
|
405
|
+
const parsedClaimsCount = Number(claimsCount);
|
|
406
|
+
if (!Number.isFinite(parsedClaimsCount) || parsedClaimsCount <= 0) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
const safeClaimsCount = Math.max(1, Math.floor(parsedClaimsCount));
|
|
410
|
+
claimedTimesByOfferId[offerId] = Math.max(claimedTimesByOfferId[offerId] ?? 0, safeClaimsCount);
|
|
411
|
+
};
|
|
412
|
+
// Canonical claim source: redeemed_offers map.
|
|
413
|
+
if (redeemedOffers != null) {
|
|
414
|
+
const redeemedOfferIds = Object.keys(redeemedOffers);
|
|
415
|
+
for (let i = 0; i < redeemedOfferIds.length; i++) {
|
|
416
|
+
const offerId = redeemedOfferIds[i];
|
|
417
|
+
markClaimedTimes(offerId, redeemedOffers[offerId]);
|
|
345
418
|
}
|
|
346
419
|
}
|
|
347
|
-
|
|
348
|
-
if (hasOffersThatHavePoints) {
|
|
349
|
-
// Check if the order has any loyalty or voucher applied
|
|
420
|
+
// Fallback claim signals from order-level discount ids.
|
|
350
421
|
if (orderDiscountDetails != null) {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
422
|
+
markClaimedTimes(orderDiscountDetails.loyalty_discount_id, 1);
|
|
423
|
+
markClaimedTimes(orderDiscountDetails.voucher_discount_id, 1);
|
|
424
|
+
}
|
|
425
|
+
// Secondary claim signal from selected reward items in cart.
|
|
426
|
+
if (categories != null && categories.length > 0) {
|
|
427
|
+
const selectedItemsCountByOfferId = {};
|
|
428
|
+
for (let i = 0; i < categories.length; i++) {
|
|
429
|
+
const category = categories[i];
|
|
430
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
431
|
+
const item = category.items[j];
|
|
432
|
+
const itemDiscountDetails = item.item_discount_details;
|
|
433
|
+
if (itemDiscountDetails == null) {
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
const safeQuantity = Number(item.quantity);
|
|
437
|
+
if (!Number.isFinite(safeQuantity) || safeQuantity <= 0) {
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
const quantity = Math.floor(safeQuantity);
|
|
441
|
+
const loyaltyOffer = offersThatHavePointsMap[itemDiscountDetails.loyalty_discount_id ?? ''];
|
|
442
|
+
if (loyaltyOffer != null) {
|
|
443
|
+
selectedItemsCountByOfferId[loyaltyOffer._id] =
|
|
444
|
+
(selectedItemsCountByOfferId[loyaltyOffer._id] ?? 0) + quantity;
|
|
445
|
+
}
|
|
446
|
+
const voucherOffer = offersThatHavePointsMap[itemDiscountDetails.voucher_discount_id ?? ''];
|
|
447
|
+
if (voucherOffer != null) {
|
|
448
|
+
selectedItemsCountByOfferId[voucherOffer._id] =
|
|
449
|
+
(selectedItemsCountByOfferId[voucherOffer._id] ?? 0) + quantity;
|
|
450
|
+
}
|
|
356
451
|
}
|
|
357
452
|
}
|
|
358
|
-
const
|
|
359
|
-
|
|
360
|
-
const
|
|
361
|
-
|
|
362
|
-
|
|
453
|
+
const selectedItemOfferIds = Object.keys(selectedItemsCountByOfferId);
|
|
454
|
+
for (let i = 0; i < selectedItemOfferIds.length; i++) {
|
|
455
|
+
const offerId = selectedItemOfferIds[i];
|
|
456
|
+
const selectedItemsCount = selectedItemsCountByOfferId[offerId] ?? 0;
|
|
457
|
+
if (selectedItemsCount <= 0) {
|
|
458
|
+
continue;
|
|
363
459
|
}
|
|
460
|
+
const applicableQuantity = Math.max(1, offerApplicableQuantity[offerId] ?? 1);
|
|
461
|
+
const derivedClaimsCount = Math.ceil(selectedItemsCount / applicableQuantity);
|
|
462
|
+
markClaimedTimes(offerId, derivedClaimsCount);
|
|
364
463
|
}
|
|
365
464
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
const
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
// we skip the item if it doesn't have any loyalty or voucher applied
|
|
384
|
-
if (item.item_discount_details == null)
|
|
385
|
-
continue;
|
|
386
|
-
// Step 1: Use Case for Offers
|
|
387
|
-
if (item.item_discount_details.loyalty_discount_id != null) {
|
|
388
|
-
const loyaltyDiscountId = item.item_discount_details.loyalty_discount_id;
|
|
389
|
-
const redeemedOffer = offersThatHavePointsMap[loyaltyDiscountId];
|
|
390
|
-
if (redeemedOffer != null) {
|
|
391
|
-
renderClaimPoints(redeemedOffer);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
if (item.item_discount_details.voucher_discount_id != null) {
|
|
395
|
-
const voucherDiscountId = item.item_discount_details.voucher_discount_id;
|
|
396
|
-
const redeemedOffer = offersThatHavePointsMap[voucherDiscountId];
|
|
397
|
-
if (redeemedOffer != null) {
|
|
398
|
-
renderClaimPoints(redeemedOffer);
|
|
399
|
-
}
|
|
400
|
-
}
|
|
465
|
+
const claimedOfferIds = Object.keys(claimedTimesByOfferId);
|
|
466
|
+
for (let i = 0; i < claimedOfferIds.length; i++) {
|
|
467
|
+
const offerId = claimedOfferIds[i];
|
|
468
|
+
const redeemedOffer = offersThatHavePointsMap[offerId];
|
|
469
|
+
const claimedTimes = claimedTimesByOfferId[offerId] ?? 0;
|
|
470
|
+
if (redeemedOffer == null || claimedTimes <= 0) {
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
if (redeemedOffer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.POINTS && includePendingRewardPoints) {
|
|
474
|
+
const points = calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categories, claimedTimes);
|
|
475
|
+
loyaltyPoints += points;
|
|
476
|
+
addedLoyaltyPoints += points;
|
|
477
|
+
}
|
|
478
|
+
if (redeemedOffer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
|
|
479
|
+
const points = (redeemedOffer.pathway_to_unlock_type_points?.num_of_points ?? 0) * claimedTimes;
|
|
480
|
+
loyaltyPoints -= points;
|
|
481
|
+
deductedLoyaltyPoints += points;
|
|
401
482
|
}
|
|
402
483
|
}
|
|
403
484
|
}
|
|
@@ -710,24 +791,14 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
710
791
|
const unlockedOffersApplicableTimes = {};
|
|
711
792
|
let totalClaimableOffers = 0;
|
|
712
793
|
let totalLoyaltyPointsOfCustomer = null;
|
|
713
|
-
let totalAddedLoyaltyPoints = 0;
|
|
714
794
|
const nowTs = Date.now();
|
|
715
|
-
const
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
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
|
-
}
|
|
795
|
+
const { voucherCodes: spendableVoucherCodesByCode } = calculateCustomerSpendableVoucherCodes({
|
|
796
|
+
customerVoucherCodes: customer?.voucher_codes,
|
|
797
|
+
orderDiscountDetails: order_discount_details,
|
|
798
|
+
redeemedOffers: redeemed_offers,
|
|
799
|
+
offers,
|
|
800
|
+
vouchers,
|
|
801
|
+
});
|
|
731
802
|
const redeemedOffersById = redeemed_offers ?? null;
|
|
732
803
|
const vouchersById = new Map();
|
|
733
804
|
if (vouchers != null) {
|
|
@@ -1404,7 +1475,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
1404
1475
|
const { num_of_points, total_amount, max_claims } = offer.pathway_to_unlock_type_points;
|
|
1405
1476
|
const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
|
|
1406
1477
|
if (totalLoyaltyPointsOfCustomer == null) {
|
|
1407
|
-
const {
|
|
1478
|
+
const { loyaltyPoints } = calculateCustomerLoyaltyPoints({
|
|
1408
1479
|
customerLoyaltyPoints: customer?.loyalty_points,
|
|
1409
1480
|
orderDiscountDetails: order_discount_details,
|
|
1410
1481
|
redeemedOffers: redeemed_offers,
|
|
@@ -1412,14 +1483,14 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
1412
1483
|
categories,
|
|
1413
1484
|
totalDiscountApplied,
|
|
1414
1485
|
offers,
|
|
1486
|
+
includePendingRewardPoints: false,
|
|
1415
1487
|
});
|
|
1416
1488
|
totalLoyaltyPointsOfCustomer = loyaltyPoints;
|
|
1417
|
-
totalAddedLoyaltyPoints = addedLoyaltyPoints;
|
|
1418
1489
|
}
|
|
1419
1490
|
const pointsProgress = getPreUnlockProgressPercentage(totalLoyaltyPointsOfCustomer ?? 0, num_of_points || 1);
|
|
1420
1491
|
const amountProgress = getPreUnlockProgressPercentage(orderValue, total_amount || 1);
|
|
1421
1492
|
const combinedProgress = Math.min(pointsProgress, amountProgress);
|
|
1422
|
-
if (
|
|
1493
|
+
if ((totalLoyaltyPointsOfCustomer ?? 0) >= num_of_points) {
|
|
1423
1494
|
if (orderValue >= total_amount) {
|
|
1424
1495
|
if ((totalLoyaltyPointsOfCustomer ?? 0) >= num_of_points) {
|
|
1425
1496
|
stats.progress = 100;
|
|
@@ -1669,11 +1740,12 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
1669
1740
|
: (referencedVouchers.find((voucher) => voucher.code.trim().toUpperCase() === normalizedVoucherRef) ?? null);
|
|
1670
1741
|
const hasMatchingVoucher = matchedVoucher != null;
|
|
1671
1742
|
const normalizedMatchedVoucherCode = matchedVoucher?.code.trim().toUpperCase() ?? null;
|
|
1672
|
-
const
|
|
1743
|
+
const redeemedTimesForOffer = getOfferRedeemedTimes(offer._id);
|
|
1744
|
+
const spendableVoucherCount = normalizedMatchedVoucherCode == null ? 0 : (spendableVoucherCodesByCode[normalizedMatchedVoucherCode] ?? 0);
|
|
1673
1745
|
const customerVoucherUsageCount = matchedVoucher == null ? 0 : (customerOfferRedemptionCounts?.[matchedVoucher._id] ?? 0);
|
|
1674
1746
|
const voucherUsesPerCustomerExhausted = matchedVoucher?.max_uses_per_customer != null &&
|
|
1675
1747
|
customerVoucherUsageCount >= matchedVoucher.max_uses_per_customer;
|
|
1676
|
-
const hasVoucherAccess = matchedVoucher != null && (matchedVoucher.is_public ||
|
|
1748
|
+
const hasVoucherAccess = matchedVoucher != null && (matchedVoucher.is_public || spendableVoucherCount > 0 || redeemedTimesForOffer > 0);
|
|
1677
1749
|
const amountProgress = getPreUnlockProgressPercentage(orderValue, total_amount || 1);
|
|
1678
1750
|
if (!hasMatchingVoucher) {
|
|
1679
1751
|
stats.progress = 0;
|
|
@@ -1758,7 +1830,10 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
1758
1830
|
const redeemedTimes = getOfferRedeemedTimes(offer._id);
|
|
1759
1831
|
const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
|
|
1760
1832
|
const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
|
|
1761
|
-
const
|
|
1833
|
+
const voucherInventoryClaimCap = matchedVoucher != null && !matchedVoucher.is_public
|
|
1834
|
+
? Math.max(0, spendableVoucherCount + redeemedTimesForOffer)
|
|
1835
|
+
: Infinity;
|
|
1836
|
+
const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
|
|
1762
1837
|
const maxRedeemableClaims = getPointsMaxRedeemableClaims({
|
|
1763
1838
|
offerToCheck: offer,
|
|
1764
1839
|
pathwayUnlockableClaims: totalUnlockableClaims,
|