@managesome/eposmatic-toolkit 0.2.292 → 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 == null)
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
- else {
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;
@@ -371,112 +371,114 @@ function calculateCustomerLoyaltyPoints({ customerLoyaltyPoints, orderDiscountDe
371
371
  let loyaltyPoints = customerLoyaltyPoints ?? 0;
372
372
  let addedLoyaltyPoints = loyaltyPoints;
373
373
  let deductedLoyaltyPoints = loyaltyPoints;
374
+ const offersThatHavePointsMap = {};
374
375
  const offerApplicableQuantity = {};
375
- const offerConsumedQuantity = {};
376
- const renderClaimPoints = (redeemedOffer) => {
377
- // if we find the offer that has been redeemed
378
- if (redeemedOffer == null)
379
- return;
380
- const applicableQuantity = offerApplicableQuantity[redeemedOffer._id] ?? 1;
381
- const consumedQuantity = offerConsumedQuantity[redeemedOffer._id] ?? 0;
382
- if (consumedQuantity >= applicableQuantity) {
383
- 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;
384
382
  }
385
- if (!offerConsumedQuantity[redeemedOffer._id]) {
386
- const rawClaimedTimes = Number(redeemedOffers?.[redeemedOffer._id]);
387
- const claimedTimes = Number.isFinite(rawClaimedTimes) && rawClaimedTimes > 0 ? Math.floor(rawClaimedTimes) : 1;
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;
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);
399
386
  }
400
- else {
401
- offerConsumedQuantity[redeemedOffer._id] += 1;
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);
402
389
  }
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;
390
+ else {
411
391
  offerApplicableQuantity[offer._id] = 1;
412
392
  }
413
- else if (offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
414
- offersThatHavePointsMap[offer._id] = offer;
415
- hasOffersThatHavePoints = true;
416
- if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
417
- offerApplicableQuantity[offer._id] = offer.reward_items?.num_of_items ?? 1;
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;
418
400
  }
419
- else if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
420
- offerApplicableQuantity[offer._id] = offer.reward_discount_items?.num_of_items ?? 1;
401
+ const offer = offersThatHavePointsMap[offerId];
402
+ if (offer == null) {
403
+ return;
421
404
  }
422
- else {
423
- offerApplicableQuantity[offer._id] = 1;
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]);
424
418
  }
425
419
  }
426
- }
427
- if (hasOffersThatHavePoints) {
428
- // Check if the order has any loyalty or voucher applied
420
+ // Fallback claim signals from order-level discount ids.
429
421
  if (orderDiscountDetails != null) {
430
- const loyaltyDiscountId = orderDiscountDetails.loyalty_discount_id;
431
- if (loyaltyDiscountId != null) {
432
- const redeemedOffer = offersThatHavePointsMap[loyaltyDiscountId];
433
- if (redeemedOffer != null) {
434
- renderClaimPoints(redeemedOffer);
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
+ }
435
451
  }
436
452
  }
437
- const voucherDiscountId = orderDiscountDetails.voucher_discount_id;
438
- if (voucherDiscountId != null) {
439
- const redeemedOffer = offersThatHavePointsMap[voucherDiscountId];
440
- if (redeemedOffer != null) {
441
- renderClaimPoints(redeemedOffer);
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;
442
459
  }
460
+ const applicableQuantity = Math.max(1, offerApplicableQuantity[offerId] ?? 1);
461
+ const derivedClaimsCount = Math.ceil(selectedItemsCount / applicableQuantity);
462
+ markClaimedTimes(offerId, derivedClaimsCount);
443
463
  }
444
464
  }
445
- // ===========================================================================================
446
- // Calculate the total number of points that are applied/consumed by the cart items
447
- const itemsThatHaveLoyaltyOrVoucherApplied = categories?.reduce((acc, category) => {
448
- const items = [];
449
- category.items.forEach((item) => {
450
- if (item.item_discount_details != null &&
451
- (item.item_discount_details.voucher_discount_id != null ||
452
- item.item_discount_details.loyalty_discount_id != null)) {
453
- items.push(item);
454
- }
455
- });
456
- return acc.concat(items);
457
- }, []) ?? [];
458
- if (itemsThatHaveLoyaltyOrVoucherApplied.length > 0) {
459
- // it means there are items that have loyalty or voucher applied
460
- for (let i = 0; i < itemsThatHaveLoyaltyOrVoucherApplied.length; i++) {
461
- const item = itemsThatHaveLoyaltyOrVoucherApplied[i];
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
- }
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;
480
482
  }
481
483
  }
482
484
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.292",
3
+ "version": "0.2.293",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",