@managesome/eposmatic-toolkit 0.2.375 → 0.2.376

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.
@@ -1932,19 +1932,10 @@ function getAddItemToCartData(data, payload, courses, listnerSettings, appliedCu
1932
1932
  itemDiscountDetails: itemWithValidatedRewardClaim.item_discount_details,
1933
1933
  rewardClaim: validatedRewardClaim,
1934
1934
  });
1935
- // Recalculate order-wide custom discount with caps before final sync
1936
- const syncedOrderDiscountDetails = appliedCustomDiscounts && appliedCustomDiscounts.length > 0
1937
- ? getSyncedOrderDiscountDetailsWithCaps({
1938
- orderDiscountDetails: orderWithSyncedItemRewardEvidence.order_discount_details,
1939
- appliedCustomDiscounts,
1940
- categories: orderWithSyncedItemRewardEvidence.categories,
1941
- platform: (orderWithSyncedItemRewardEvidence.platform ?? data.platform),
1942
- })
1943
- : orderWithSyncedItemRewardEvidence.order_discount_details;
1944
- const orderForSync = syncedOrderDiscountDetails !== orderWithSyncedItemRewardEvidence.order_discount_details
1945
- ? { ...orderWithSyncedItemRewardEvidence, order_discount_details: syncedOrderDiscountDetails }
1946
- : orderWithSyncedItemRewardEvidence;
1947
- const newData = calculateCartOrderInfoAndSyncData(orderForSync, courses, listnerSettings);
1935
+ const newData = calculateCartOrderInfoAndSyncData(orderWithSyncedItemRewardEvidence, courses, listnerSettings);
1936
+ if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0) {
1937
+ return applyCustomDiscountCapsPostSync(newData, appliedCustomDiscounts, (orderWithSyncedItemRewardEvidence.platform ?? data.platform), courses, listnerSettings);
1938
+ }
1948
1939
  return newData;
1949
1940
  }
1950
1941
  function getSplitCartItemToTwo({ data, orderItemId, courses, listnerSettings }) {
@@ -2010,76 +2001,133 @@ function getAddItemsToCartData(data, payload, courses, listnerSettings, appliedC
2010
2001
  return newData;
2011
2002
  }
2012
2003
  /**
2013
- * Recalculate an order-wide special_discount from the original discount definition.
2014
- * Returns the updated order_discount_details, or the original if no match is found.
2015
- * This ensures that quantity/cart changes correctly respect max_amount/max_percentage caps.
2004
+ * Post-sync cap enforcement for custom discounts.
2005
+ *
2006
+ * After `calculateCartOrderInfoAndSyncData` has computed correct `total_discount`
2007
+ * values (handling GST, modifiers, item totals, etc.), this function checks
2008
+ * whether any order-level or item-level percentage discount exceeds its configured
2009
+ * `max_amount` cap. If so, the discount is collapsed to a fixed amount equal to
2010
+ * the cap and the cart is re-synced.
2011
+ *
2012
+ * All manual subtotal / tax computation is eliminated — the sync result is the
2013
+ * single source of truth for the discountable base.
2016
2014
  */
2017
- function getSyncedOrderDiscountDetailsWithCaps({ orderDiscountDetails, appliedCustomDiscounts, categories, platform, }) {
2018
- if (!orderDiscountDetails?.special_discount_id)
2019
- return orderDiscountDetails;
2020
- const matchedOrderDiscount = appliedCustomDiscounts.find((d) => d._id === orderDiscountDetails.special_discount_id && d.applicability === constants_1.DISCOUNT_APPLICABILITY_ENUM.ORDER_WIDE);
2021
- if (!matchedOrderDiscount)
2022
- return orderDiscountDetails;
2023
- // Calculate current subtotal of all items in the categories
2024
- let currentSubtotal = 0;
2025
- if (categories) {
2026
- for (const category of categories) {
2027
- for (const item of category.items) {
2028
- const modifiersTotal = calculateItemModifiersPrice(item);
2029
- currentSubtotal += (item.price + modifiersTotal) * item.quantity;
2030
- }
2031
- }
2032
- }
2033
- const discountValue = matchedOrderDiscount.value[platform] ?? matchedOrderDiscount.value.BASE_PRICE;
2034
- let recalculatedDiscount;
2035
- let isSpecialDiscountPercentage = matchedOrderDiscount.is_percentage;
2036
- if (matchedOrderDiscount.is_percentage) {
2037
- // Clamp percentage to 100% to match apply-time logic in CustomDiscountItem.tsx
2038
- let discountPercentage = discountValue;
2039
- if (discountPercentage > 100)
2040
- discountPercentage = 100;
2041
- const maxAmount = matchedOrderDiscount.max_amount
2042
- ? (matchedOrderDiscount.max_amount[platform] ?? matchedOrderDiscount.max_amount.BASE_PRICE)
2043
- : null;
2044
- const hasCap = maxAmount != null && Number.isFinite(maxAmount) && maxAmount > 0;
2045
- if (hasCap) {
2046
- const computedDiscount = (discountPercentage / 100) * currentSubtotal;
2047
- if (computedDiscount > maxAmount && maxAmount <= currentSubtotal) {
2048
- // Cap exceeded — store as fixed amount equal to the cap
2049
- recalculatedDiscount = maxAmount;
2050
- isSpecialDiscountPercentage = false;
2051
- }
2052
- else {
2053
- // Within cap — keep as percentage
2054
- recalculatedDiscount = discountPercentage;
2055
- isSpecialDiscountPercentage = true;
2015
+ function applyCustomDiscountCapsPostSync(synced, appliedCustomDiscounts, platform, courses, listnerSettings) {
2016
+ let needsResync = false;
2017
+ let result = synced;
2018
+ // ── Order-level cap check ──────────────────────────────────────────
2019
+ const orderDetails = result.order_discount_details;
2020
+ if (orderDetails?.special_discount_id) {
2021
+ const matched = appliedCustomDiscounts.find((d) => d._id === orderDetails.special_discount_id && d.applicability === constants_1.DISCOUNT_APPLICABILITY_ENUM.ORDER_WIDE);
2022
+ if (matched?.is_percentage && matched.max_amount) {
2023
+ const maxCap = (0, utils_1.parseNum)(matched.max_amount[platform] ?? matched.max_amount.BASE_PRICE);
2024
+ if (Number.isFinite(maxCap) && maxCap > 0) {
2025
+ // Cap breach: total_discount exceeds maxCap → switch to fixed
2026
+ if ((orderDetails.total_discount ?? 0) > maxCap) {
2027
+ result = {
2028
+ ...result,
2029
+ order_discount_details: {
2030
+ ...orderDetails,
2031
+ special_discount: maxCap,
2032
+ is_special_discount_percentage: false,
2033
+ total_discount: 0,
2034
+ },
2035
+ };
2036
+ needsResync = true;
2037
+ }
2038
+ // Un-cap: currently fixed (capped) but percentage would fit under cap → revert
2039
+ else if (orderDetails.is_special_discount_percentage === false && orderDetails.special_discount != null) {
2040
+ const discountPct = (0, utils_1.parseNum)(matched.value[platform] ?? matched.value.BASE_PRICE);
2041
+ if (Number.isFinite(discountPct) && discountPct > 0 && discountPct <= 100) {
2042
+ const computedDiscount = (discountPct / 100) * (result.price ?? 0);
2043
+ if (computedDiscount <= maxCap) {
2044
+ result = {
2045
+ ...result,
2046
+ order_discount_details: {
2047
+ ...orderDetails,
2048
+ special_discount: discountPct,
2049
+ is_special_discount_percentage: true,
2050
+ total_discount: 0,
2051
+ },
2052
+ };
2053
+ needsResync = true;
2054
+ }
2055
+ }
2056
+ }
2056
2057
  }
2057
2058
  }
2058
- else {
2059
- // No cap configured — keep as percentage
2060
- recalculatedDiscount = discountPercentage;
2061
- isSpecialDiscountPercentage = true;
2062
- }
2063
2059
  }
2064
- else {
2065
- // Fixed discount — amount stays as-is, but still subject to max_amount cap
2066
- recalculatedDiscount = discountValue;
2067
- if (matchedOrderDiscount.max_amount) {
2068
- const maxAmount = matchedOrderDiscount.max_amount[platform] ?? matchedOrderDiscount.max_amount.BASE_PRICE;
2069
- if (maxAmount > 0 && recalculatedDiscount > maxAmount) {
2070
- recalculatedDiscount = maxAmount;
2060
+ // ── Item-level cap check ───────────────────────────────────────────
2061
+ const syncedCategories = result.categories;
2062
+ if (syncedCategories) {
2063
+ for (let ci = 0; ci < syncedCategories.length; ci++) {
2064
+ const category = syncedCategories[ci];
2065
+ let categoryChanged = false;
2066
+ const newItems = [];
2067
+ for (let ji = 0; ji < category.items.length; ji++) {
2068
+ const item = category.items[ji];
2069
+ const itemDetails = item.item_discount_details;
2070
+ if (!itemDetails?.special_discount_id) {
2071
+ newItems.push(item);
2072
+ continue;
2073
+ }
2074
+ const matched = appliedCustomDiscounts.find((d) => d._id === itemDetails.special_discount_id && d.applicability === constants_1.DISCOUNT_APPLICABILITY_ENUM.ITEM_SPECIFIC);
2075
+ if (!matched?.is_percentage || !matched.max_amount) {
2076
+ newItems.push(item);
2077
+ continue;
2078
+ }
2079
+ const maxCap = (0, utils_1.parseNum)(matched.max_amount[platform] ?? matched.max_amount.BASE_PRICE);
2080
+ if (!Number.isFinite(maxCap) || maxCap <= 0) {
2081
+ newItems.push(item);
2082
+ continue;
2083
+ }
2084
+ // Cap breach: total_discount exceeds maxCap → switch to fixed
2085
+ if ((itemDetails.total_discount ?? 0) > maxCap) {
2086
+ newItems.push({
2087
+ ...item,
2088
+ item_discount_details: {
2089
+ ...itemDetails,
2090
+ special_discount: maxCap,
2091
+ is_special_discount_percentage: false,
2092
+ total_discount: 0,
2093
+ },
2094
+ });
2095
+ needsResync = true;
2096
+ categoryChanged = true;
2097
+ continue;
2098
+ }
2099
+ // Un-cap: currently fixed (capped) but percentage would fit under cap → revert
2100
+ if (itemDetails.is_special_discount_percentage === false && itemDetails.special_discount != null) {
2101
+ const discountPct = (0, utils_1.parseNum)(matched.value[platform] ?? matched.value.BASE_PRICE);
2102
+ if (Number.isFinite(discountPct) && discountPct > 0 && discountPct <= 100) {
2103
+ const computedDiscount = (discountPct / 100) * (item.subtotal ?? 0);
2104
+ if (computedDiscount <= maxCap) {
2105
+ newItems.push({
2106
+ ...item,
2107
+ item_discount_details: {
2108
+ ...itemDetails,
2109
+ special_discount: discountPct,
2110
+ is_special_discount_percentage: true,
2111
+ total_discount: 0,
2112
+ },
2113
+ });
2114
+ needsResync = true;
2115
+ categoryChanged = true;
2116
+ continue;
2117
+ }
2118
+ }
2119
+ }
2120
+ newItems.push(item);
2121
+ }
2122
+ if (categoryChanged) {
2123
+ result = {
2124
+ ...result,
2125
+ categories: result.categories.map((c, idx) => (idx === ci ? { ...c, items: newItems } : c)),
2126
+ };
2071
2127
  }
2072
- }
2073
- if (recalculatedDiscount > currentSubtotal) {
2074
- recalculatedDiscount = currentSubtotal;
2075
2128
  }
2076
2129
  }
2077
- return {
2078
- ...orderDiscountDetails,
2079
- special_discount: (0, utils_1.parseNum)(recalculatedDiscount),
2080
- is_special_discount_percentage: isSpecialDiscountPercentage,
2081
- total_discount: 0,
2082
- };
2130
+ return needsResync ? calculateCartOrderInfoAndSyncData(result, courses, listnerSettings) : result;
2083
2131
  }
2084
2132
  function getUpdateItemInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
2085
2133
  const orderItemIdsMap = {};
@@ -2097,73 +2145,6 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings, applie
2097
2145
  ...item,
2098
2146
  ...payload,
2099
2147
  };
2100
- // Recalculate item-level custom discount from the original discount definition
2101
- // when appliedCustomDiscounts are provided. This ensures quantity changes
2102
- // do not incorrectly linearly scale fixed discounts or bypass caps.
2103
- if (appliedCustomDiscounts &&
2104
- appliedCustomDiscounts.length > 0 &&
2105
- newItem.item_discount_details?.special_discount_id) {
2106
- const matchedDiscount = appliedCustomDiscounts.find((d) => d._id === newItem.item_discount_details.special_discount_id &&
2107
- d.applicability === constants_1.DISCOUNT_APPLICABILITY_ENUM.ITEM_SPECIFIC);
2108
- if (matchedDiscount) {
2109
- const modifiersTotal = calculateItemModifiersPrice(newItem);
2110
- const itemTotalWithoutTax = (newItem.price + modifiersTotal) * newItem.quantity;
2111
- const platform = data.platform;
2112
- const discountValue = matchedDiscount.value[platform] ?? matchedDiscount.value.BASE_PRICE;
2113
- let recalculatedSpecialDiscount;
2114
- let isItemDiscountPercentage = matchedDiscount.is_percentage;
2115
- if (matchedDiscount.is_percentage) {
2116
- // Clamp percentage to 100% to match apply-time logic in CustomDiscountItem.tsx
2117
- let discountPercentage = discountValue;
2118
- if (discountPercentage > 100)
2119
- discountPercentage = 100;
2120
- const maxAmount = matchedDiscount.max_amount
2121
- ? (matchedDiscount.max_amount[platform] ?? matchedDiscount.max_amount.BASE_PRICE)
2122
- : null;
2123
- const hasCap = maxAmount != null && Number.isFinite(maxAmount) && maxAmount > 0;
2124
- if (hasCap) {
2125
- const computedDiscount = (discountPercentage / 100) * itemTotalWithoutTax;
2126
- if (computedDiscount > maxAmount && maxAmount <= itemTotalWithoutTax) {
2127
- // Cap exceeded — store as fixed amount equal to the cap
2128
- recalculatedSpecialDiscount = maxAmount;
2129
- isItemDiscountPercentage = false;
2130
- }
2131
- else {
2132
- // Within cap — keep as percentage
2133
- recalculatedSpecialDiscount = discountPercentage;
2134
- isItemDiscountPercentage = true;
2135
- }
2136
- }
2137
- else {
2138
- // No cap configured — keep as percentage
2139
- recalculatedSpecialDiscount = discountPercentage;
2140
- isItemDiscountPercentage = true;
2141
- }
2142
- }
2143
- else {
2144
- // Fixed discount — the amount should NOT scale with quantity.
2145
- recalculatedSpecialDiscount = discountValue;
2146
- if (matchedDiscount.max_amount) {
2147
- const maxAmount = matchedDiscount.max_amount[platform] ?? matchedDiscount.max_amount.BASE_PRICE;
2148
- if (maxAmount > 0 && recalculatedSpecialDiscount > maxAmount) {
2149
- recalculatedSpecialDiscount = maxAmount;
2150
- }
2151
- }
2152
- if (recalculatedSpecialDiscount > itemTotalWithoutTax) {
2153
- recalculatedSpecialDiscount = itemTotalWithoutTax;
2154
- }
2155
- }
2156
- newItem = {
2157
- ...newItem,
2158
- item_discount_details: {
2159
- ...newItem.item_discount_details,
2160
- special_discount: (0, utils_1.parseNum)(recalculatedSpecialDiscount),
2161
- is_special_discount_percentage: isItemDiscountPercentage,
2162
- total_discount: 0,
2163
- },
2164
- };
2165
- }
2166
- }
2167
2148
  }
2168
2149
  if (newItem.quantity > 0) {
2169
2150
  items.push(newItem);
@@ -2178,16 +2159,6 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings, applie
2178
2159
  }
2179
2160
  }
2180
2161
  }
2181
- // Recalculate order-wide custom discount from the original discount definition
2182
- // when appliedCustomDiscounts are provided and an order-wide discount is active.
2183
- const syncedOrderDiscountDetails = appliedCustomDiscounts && appliedCustomDiscounts.length > 0
2184
- ? getSyncedOrderDiscountDetailsWithCaps({
2185
- orderDiscountDetails: data.order_discount_details,
2186
- appliedCustomDiscounts,
2187
- categories,
2188
- platform: data.platform,
2189
- })
2190
- : data.order_discount_details;
2191
2162
  const iterations = [];
2192
2163
  if (payload.quantity != null && data.iterations != null && data.iterations.length > 0) {
2193
2164
  const [firstIteration, ...restIterations] = data.iterations;
@@ -2238,12 +2209,14 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings, applie
2238
2209
  }
2239
2210
  const order = {
2240
2211
  ...data,
2241
- order_discount_details: syncedOrderDiscountDetails,
2242
2212
  ...(iterations.length > 0 && { iterations }),
2243
2213
  categories,
2244
2214
  };
2245
- const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
2246
- return newData;
2215
+ const synced = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
2216
+ if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0) {
2217
+ return applyCustomDiscountCapsPostSync(synced, appliedCustomDiscounts, data.platform, courses, listnerSettings);
2218
+ }
2219
+ return synced;
2247
2220
  }
2248
2221
  /* ======================><====================== */
2249
2222
  function getUpdateItemsInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
@@ -2361,23 +2334,16 @@ function getRemoveItemFromCartData(data, payload, courses, listnerSettings, appl
2361
2334
  iterations.push(firstIterationData);
2362
2335
  iterations.push(...restIterations);
2363
2336
  }
2364
- // Re-evaluate order-wide custom discount cap after item removal
2365
- const syncedOrderDiscountDetails = appliedCustomDiscounts && appliedCustomDiscounts.length > 0
2366
- ? getSyncedOrderDiscountDetailsWithCaps({
2367
- orderDiscountDetails: data.order_discount_details,
2368
- appliedCustomDiscounts,
2369
- categories,
2370
- platform: data.platform,
2371
- })
2372
- : data.order_discount_details;
2373
2337
  const order = {
2374
2338
  ...data,
2375
- order_discount_details: syncedOrderDiscountDetails,
2376
2339
  ...(iterations.length > 0 && { iterations }),
2377
2340
  categories,
2378
2341
  };
2379
- const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
2380
- return newData;
2342
+ const synced = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
2343
+ if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0) {
2344
+ return applyCustomDiscountCapsPostSync(synced, appliedCustomDiscounts, data.platform, courses, listnerSettings);
2345
+ }
2346
+ return synced;
2381
2347
  }
2382
2348
  /* ======================>Order Object<====================== */
2383
2349
  function getUpdatedCartInfoOnInit(data, order, courses, platform, categories, mappedItems, tenantId, listnerSettings, offers) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.375",
3
+ "version": "0.2.376",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",