@managesome/eposmatic-toolkit 0.2.488 → 0.2.489

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.
@@ -972,23 +972,33 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
972
972
  clearOrderLevelDiscount('loyalty');
973
973
  }
974
974
  else {
975
- const hasRelevantCap = is_discount_percentage &&
976
- max_discount_amount != null &&
977
- Number.isFinite(max_discount_amount) &&
978
- max_discount_amount >= 0;
979
975
  const hasRelevantPercentageCap = !is_discount_percentage &&
980
976
  max_discount_percentage != null &&
981
977
  Number.isFinite(max_discount_percentage) &&
982
978
  max_discount_percentage >= 0;
983
- const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
984
- const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
985
- if (shouldNormalizeToStatic) {
986
- if (discountBaseAmount <= 0) {
979
+ const hasMaxAmountCap = is_discount_percentage &&
980
+ max_discount_amount != null &&
981
+ Number.isFinite(max_discount_amount) &&
982
+ max_discount_amount >= 0;
983
+ const hasFlatMaxPercentage = !is_discount_percentage && hasRelevantPercentageCap;
984
+ if (hasExcludedRefs && !is_discount_percentage && !hasFlatMaxPercentage) {
985
+ clearOrderLevelDiscount('loyalty');
986
+ }
987
+ else if (hasExcludedRefs) {
988
+ // Excluded items present: must apply percentage against eligible subtotal only.
989
+ const eligibleBase = hasFlatMaxPercentage
990
+ ? (0, utils_1.parseNum)((0, utils_1.getNetItemsValueAfterItemDiscount)({
991
+ itemsPrice: eligibleSubtotal,
992
+ totalDiscountApplied: 0,
993
+ orderLevelDiscount: 0,
994
+ }))
995
+ : eligibleSubtotal;
996
+ if (eligibleBase <= 0) {
987
997
  clearOrderLevelDiscount('loyalty');
988
998
  }
989
999
  else {
990
1000
  const calculatedDiscount = (0, utils_1.getCappedOfferDiscountAmount)({
991
- baseAmount: discountBaseAmount,
1001
+ baseAmount: eligibleBase,
992
1002
  discountValue: discount_value,
993
1003
  isDiscountPercentage: is_discount_percentage,
994
1004
  maxDiscountAmount: max_discount_amount,
@@ -1002,6 +1012,67 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
1002
1012
  hasChanges = true;
1003
1013
  }
1004
1014
  }
1015
+ else if (hasMaxAmountCap) {
1016
+ // Percentage offer with max_amount cap (no excluded items).
1017
+ const projectedPercentage = (0, utils_1.getPercentageAmount)(orderLevelDiscountBaseAmount, discount_value);
1018
+ if (projectedPercentage > max_discount_amount) {
1019
+ // Cap would be breached -- normalize to flat at cap.
1020
+ order_discount_details = {
1021
+ ...order_discount_details,
1022
+ loyalty_discount: (0, utils_1.parseNum)(max_discount_amount),
1023
+ is_loyalty_discount_percentage: false,
1024
+ };
1025
+ }
1026
+ else {
1027
+ // Cap not breached -- un-cap back to percentage (or stay percentage).
1028
+ order_discount_details = {
1029
+ ...order_discount_details,
1030
+ loyalty_discount: (0, utils_1.parseNum)(discount_value),
1031
+ is_loyalty_discount_percentage: true,
1032
+ };
1033
+ }
1034
+ hasChanges = true;
1035
+ }
1036
+ else if (hasFlatMaxPercentage) {
1037
+ // Flat discount with max_percentage cap -- resolve against full cart.
1038
+ if (orderLevelDiscountBaseAmount <= 0) {
1039
+ clearOrderLevelDiscount('loyalty');
1040
+ }
1041
+ else {
1042
+ const calculatedDiscount = (0, utils_1.getCappedOfferDiscountAmount)({
1043
+ baseAmount: orderLevelDiscountBaseAmount,
1044
+ discountValue: discount_value,
1045
+ isDiscountPercentage: false,
1046
+ maxDiscountAmount: undefined,
1047
+ maxDiscountPercentage: max_discount_percentage,
1048
+ });
1049
+ order_discount_details = {
1050
+ ...order_discount_details,
1051
+ loyalty_discount: (0, utils_1.parseNum)(calculatedDiscount),
1052
+ is_loyalty_discount_percentage: false,
1053
+ };
1054
+ hasChanges = true;
1055
+ }
1056
+ }
1057
+ else if (is_discount_percentage) {
1058
+ // Plain percentage offer, no excluded items, no caps.
1059
+ const shouldRevert = order_discount_details.is_loyalty_discount_percentage === false &&
1060
+ order_discount_details.loyalty_discount != null;
1061
+ if (shouldRevert) {
1062
+ order_discount_details = {
1063
+ ...order_discount_details,
1064
+ loyalty_discount: (0, utils_1.parseNum)(discount_value),
1065
+ is_loyalty_discount_percentage: true,
1066
+ };
1067
+ hasChanges = true;
1068
+ }
1069
+ }
1070
+ else {
1071
+ // Plain flat offer, no caps -- keep as-is.
1072
+ if (orderLevelDiscountBaseAmount <= 0) {
1073
+ clearOrderLevelDiscount('loyalty');
1074
+ }
1075
+ }
1005
1076
  }
1006
1077
  }
1007
1078
  unlockedOffersMap[loyaltyDiscountId] -= 1;
@@ -1032,39 +1103,103 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
1032
1103
  subtotal: orderLevelDiscountBaseAmount,
1033
1104
  hasExcludedRefs: false,
1034
1105
  };
1035
- if (hasExcludedRefs && !is_discount_percentage) {
1106
+ const hasRelevantPercentageCap = !is_discount_percentage &&
1107
+ max_discount_percentage != null &&
1108
+ Number.isFinite(max_discount_percentage) &&
1109
+ max_discount_percentage >= 0;
1110
+ const hasMaxAmountCap = is_discount_percentage &&
1111
+ max_discount_amount != null &&
1112
+ Number.isFinite(max_discount_amount) &&
1113
+ max_discount_amount >= 0;
1114
+ const hasFlatMaxPercentage = !is_discount_percentage && hasRelevantPercentageCap;
1115
+ if (hasExcludedRefs && !is_discount_percentage && !hasFlatMaxPercentage) {
1036
1116
  clearOrderLevelDiscount('voucher');
1037
1117
  }
1118
+ else if (hasExcludedRefs) {
1119
+ // Excluded items present: must apply percentage against eligible subtotal only.
1120
+ const eligibleBase = hasFlatMaxPercentage
1121
+ ? (0, utils_1.parseNum)((0, utils_1.getNetItemsValueAfterItemDiscount)({
1122
+ itemsPrice: eligibleSubtotal,
1123
+ totalDiscountApplied: 0,
1124
+ orderLevelDiscount: 0,
1125
+ }))
1126
+ : eligibleSubtotal;
1127
+ if (eligibleBase <= 0) {
1128
+ clearOrderLevelDiscount('voucher');
1129
+ }
1130
+ else {
1131
+ const calculatedDiscount = (0, utils_1.getCappedOfferDiscountAmount)({
1132
+ baseAmount: eligibleBase,
1133
+ discountValue: discount_value,
1134
+ isDiscountPercentage: is_discount_percentage,
1135
+ maxDiscountAmount: max_discount_amount,
1136
+ maxDiscountPercentage: max_discount_percentage,
1137
+ });
1138
+ order_discount_details = {
1139
+ ...order_discount_details,
1140
+ voucher_discount: (0, utils_1.parseNum)(calculatedDiscount),
1141
+ is_voucher_discount_percentage: false,
1142
+ };
1143
+ hasChanges = true;
1144
+ }
1145
+ }
1146
+ else if (hasMaxAmountCap) {
1147
+ // Percentage offer with max_amount cap (no excluded items).
1148
+ const projectedPercentage = (0, utils_1.getPercentageAmount)(orderLevelDiscountBaseAmount, discount_value);
1149
+ if (projectedPercentage > max_discount_amount) {
1150
+ // Cap would be breached -- normalize to flat at cap.
1151
+ order_discount_details = {
1152
+ ...order_discount_details,
1153
+ voucher_discount: (0, utils_1.parseNum)(max_discount_amount),
1154
+ is_voucher_discount_percentage: false,
1155
+ };
1156
+ }
1157
+ else {
1158
+ // Cap not breached -- un-cap back to percentage (or stay percentage).
1159
+ order_discount_details = {
1160
+ ...order_discount_details,
1161
+ voucher_discount: (0, utils_1.parseNum)(discount_value),
1162
+ is_voucher_discount_percentage: true,
1163
+ };
1164
+ }
1165
+ hasChanges = true;
1166
+ }
1167
+ else if (hasFlatMaxPercentage) {
1168
+ if (orderLevelDiscountBaseAmount <= 0) {
1169
+ clearOrderLevelDiscount('voucher');
1170
+ }
1171
+ else {
1172
+ const calculatedDiscount = (0, utils_1.getCappedOfferDiscountAmount)({
1173
+ baseAmount: orderLevelDiscountBaseAmount,
1174
+ discountValue: discount_value,
1175
+ isDiscountPercentage: false,
1176
+ maxDiscountAmount: undefined,
1177
+ maxDiscountPercentage: max_discount_percentage,
1178
+ });
1179
+ order_discount_details = {
1180
+ ...order_discount_details,
1181
+ voucher_discount: (0, utils_1.parseNum)(calculatedDiscount),
1182
+ is_voucher_discount_percentage: false,
1183
+ };
1184
+ hasChanges = true;
1185
+ }
1186
+ }
1187
+ else if (is_discount_percentage) {
1188
+ // Plain percentage offer, no excluded items, no caps.
1189
+ const shouldRevert = order_discount_details.is_voucher_discount_percentage === false &&
1190
+ order_discount_details.voucher_discount != null;
1191
+ if (shouldRevert) {
1192
+ order_discount_details = {
1193
+ ...order_discount_details,
1194
+ voucher_discount: (0, utils_1.parseNum)(discount_value),
1195
+ is_voucher_discount_percentage: true,
1196
+ };
1197
+ hasChanges = true;
1198
+ }
1199
+ }
1038
1200
  else {
1039
- const hasRelevantCap = is_discount_percentage &&
1040
- max_discount_amount != null &&
1041
- Number.isFinite(max_discount_amount) &&
1042
- max_discount_amount >= 0;
1043
- const hasRelevantPercentageCap = !is_discount_percentage &&
1044
- max_discount_percentage != null &&
1045
- Number.isFinite(max_discount_percentage) &&
1046
- max_discount_percentage >= 0;
1047
- const shouldNormalizeToStatic = hasExcludedRefs || hasRelevantCap || hasRelevantPercentageCap;
1048
- const discountBaseAmount = hasExcludedRefs ? eligibleSubtotal : orderLevelDiscountBaseAmount;
1049
- if (shouldNormalizeToStatic) {
1050
- if (discountBaseAmount <= 0) {
1051
- clearOrderLevelDiscount('voucher');
1052
- }
1053
- else {
1054
- const calculatedDiscount = (0, utils_1.getCappedOfferDiscountAmount)({
1055
- baseAmount: discountBaseAmount,
1056
- discountValue: discount_value,
1057
- isDiscountPercentage: is_discount_percentage,
1058
- maxDiscountAmount: max_discount_amount,
1059
- maxDiscountPercentage: max_discount_percentage,
1060
- });
1061
- order_discount_details = {
1062
- ...order_discount_details,
1063
- voucher_discount: (0, utils_1.parseNum)(calculatedDiscount),
1064
- is_voucher_discount_percentage: false,
1065
- };
1066
- hasChanges = true;
1067
- }
1201
+ if (orderLevelDiscountBaseAmount <= 0) {
1202
+ clearOrderLevelDiscount('voucher');
1068
1203
  }
1069
1204
  }
1070
1205
  }
@@ -1050,24 +1050,29 @@ function getOrderSubtotalFromCategoriesForOfferStats(categories) {
1050
1050
  if (categories == null)
1051
1051
  return null;
1052
1052
  let subtotal = 0;
1053
+ let itemsCount = 0;
1053
1054
  for (let i = 0; i < categories.length; i++) {
1054
1055
  const items = categories[i].items;
1055
1056
  for (let j = 0; j < items.length; j++) {
1056
1057
  subtotal += getPositiveAmount(items[j].subtotal);
1058
+ itemsCount += Math.max(0, Math.floor(items[j].quantity ?? 0));
1057
1059
  }
1058
1060
  }
1059
- return (0, common_1.parseNum)(subtotal);
1061
+ return { subtotal: (0, common_1.parseNum)(subtotal), itemsCount };
1060
1062
  }
1061
1063
  function getOrderLevelDiscountBaseAmountForOfferStats({ categories, itemsPrice, totalDiscountApplied, orderDiscountDetails, }) {
1062
- const subtotalFromCategories = getOrderSubtotalFromCategoriesForOfferStats(categories);
1063
- if (subtotalFromCategories != null) {
1064
- return subtotalFromCategories;
1064
+ const categoriesResult = getOrderSubtotalFromCategoriesForOfferStats(categories);
1065
+ if (categoriesResult != null) {
1066
+ return { baseAmount: categoriesResult.subtotal, itemsCount: categoriesResult.itemsCount };
1065
1067
  }
1066
- return getNetItemsValueAfterItemDiscount({
1067
- itemsPrice,
1068
- totalDiscountApplied,
1069
- orderLevelDiscount: orderDiscountDetails?.total_discount,
1070
- });
1068
+ return {
1069
+ baseAmount: getNetItemsValueAfterItemDiscount({
1070
+ itemsPrice,
1071
+ totalDiscountApplied,
1072
+ orderLevelDiscount: orderDiscountDetails?.total_discount,
1073
+ }),
1074
+ itemsCount: 0,
1075
+ };
1071
1076
  }
1072
1077
  function getOrderLevelDiscountAmountForOfferStats({ orderDiscountDetails, orderLevelDiscountBaseAmount, }) {
1073
1078
  return (0, common_1.parseNum)(Math.min(getPositiveAmount(orderDiscountDetails?.total_discount), getPositiveAmount(orderLevelDiscountBaseAmount)));
@@ -1494,13 +1499,14 @@ function getRewardPreviewHashKey(rewardPreview) {
1494
1499
  // Failing to do so will cause the frontend (OffersProvider) to miss state changes —
1495
1500
  // it compares hash key strings to decide whether to update React state / dispatch Redux actions.
1496
1501
  // The same rule applies to getUnlockedOffersApplicableTimesHashKey and getRewardPreviewHashKey.
1497
- function getOffersWithStatsHashKey(offersWithStats, totalClaimableOffers, isEdit) {
1502
+ function getOffersWithStatsHashKey({ offersWithStats, totalClaimableOffers, isEdit, itemsCount, itemsPrice, }) {
1498
1503
  const editSegment = isEdit ? 'edit:' : '';
1504
+ const cartSegment = `c:${itemsCount ?? 0}:${itemsPrice ?? 0}`;
1499
1505
  if (offersWithStats.length === 0) {
1500
- return `offers:v1:${editSegment}empty:${totalClaimableOffers.length}`;
1506
+ return `offers:v1:${editSegment}${cartSegment}:empty:${totalClaimableOffers.length}`;
1501
1507
  }
1502
1508
  const sortedOffers = [...offersWithStats].sort((a, b) => a.offer._id.localeCompare(b.offer._id));
1503
- let result = `offers:v1:${editSegment}${totalClaimableOffers.length}|`;
1509
+ let result = `offers:v1:${editSegment}${cartSegment}:${totalClaimableOffers.length}|`;
1504
1510
  for (let i = 0; i < sortedOffers.length; i++) {
1505
1511
  const { offer, stats } = sortedOffers[i];
1506
1512
  const eligibleItemIds = [...stats.eligible_item_ids].sort().join(',');
@@ -1512,7 +1518,7 @@ function getOffersWithStatsHashKey(offersWithStats, totalClaimableOffers, isEdit
1512
1518
  }
1513
1519
  function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOfferRedemptionCounts, redeemed_offers, platform, currency_symbol, currentItemsPrice, totalDiscountApplied, categories, order_discount_details, isEdit, }) {
1514
1520
  const offersWithStats = [];
1515
- const orderLevelDiscountBaseAmount = getOrderLevelDiscountBaseAmountForOfferStats({
1521
+ const { baseAmount: orderLevelDiscountBaseAmount, itemsCount } = getOrderLevelDiscountBaseAmountForOfferStats({
1516
1522
  categories,
1517
1523
  itemsPrice: currentItemsPrice,
1518
1524
  totalDiscountApplied,
@@ -3029,7 +3035,13 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
3029
3035
  offersWithStats,
3030
3036
  totalClaimableOffers,
3031
3037
  unlockedOffersApplicableTimes,
3032
- offersWithStatsHashKey: getOffersWithStatsHashKey(offersWithStats, totalClaimableOffers, isEdit),
3038
+ offersWithStatsHashKey: getOffersWithStatsHashKey({
3039
+ offersWithStats,
3040
+ totalClaimableOffers,
3041
+ isEdit,
3042
+ itemsCount,
3043
+ itemsPrice: currentItemsPrice,
3044
+ }),
3033
3045
  unlockedOffersApplicableTimesHashKey: getUnlockedOffersApplicableTimesHashKey(unlockedOffersApplicableTimes, isEdit),
3034
3046
  };
3035
3047
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.488",
3
+ "version": "0.2.489",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",