@managesome/eposmatic-toolkit 0.2.372 → 0.2.374

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.
@@ -60,7 +60,7 @@ export interface ISetAddItemToCartActionPayload {
60
60
  rewardClaim?: IRewardClaim;
61
61
  }
62
62
  export declare const getOrderItemComaparableData: (item: IOrderItem) => Partial<IOrderItem>;
63
- export declare function getAddItemToCartData(data: Partial<IOrder>, payload: ISetAddItemToCartActionPayload, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
63
+ export declare function getAddItemToCartData(data: Partial<IOrder>, payload: ISetAddItemToCartActionPayload, courses: ICourse[], listnerSettings: ICartListnerSettings, appliedCustomDiscounts?: IAppliedCustomDiscount[]): Partial<IOrder>;
64
64
  interface IGetSplitCartItemToTwo {
65
65
  data: Partial<IOrder>;
66
66
  orderItemId: string;
@@ -68,8 +68,8 @@ interface IGetSplitCartItemToTwo {
68
68
  listnerSettings: ICartListnerSettings;
69
69
  }
70
70
  export declare function getSplitCartItemToTwo({ data, orderItemId, courses, listnerSettings }: IGetSplitCartItemToTwo): Partial<IOrder>;
71
- export declare function getAddItemsToCartData(data: Partial<IOrder>, payload: ISetAddItemToCartActionPayload[], courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
72
- interface IAppliedCustomDiscount {
71
+ export declare function getAddItemsToCartData(data: Partial<IOrder>, payload: ISetAddItemToCartActionPayload[], courses: ICourse[], listnerSettings: ICartListnerSettings, appliedCustomDiscounts?: IAppliedCustomDiscount[]): Partial<IOrder>;
72
+ export interface IAppliedCustomDiscount {
73
73
  _id: string;
74
74
  applicability: DISCOUNT_APPLICABILITY_ENUM;
75
75
  value: IPrice;
@@ -1736,7 +1736,7 @@ const getOrderItemComaparableData = (item) => ({
1736
1736
  quantity: item.quantity,
1737
1737
  });
1738
1738
  exports.getOrderItemComaparableData = getOrderItemComaparableData;
1739
- function getAddItemToCartData(data, payload, courses, listnerSettings) {
1739
+ function getAddItemToCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
1740
1740
  const getComparableItem = (item) => {
1741
1741
  return {
1742
1742
  item_id: item.item_id,
@@ -1932,7 +1932,19 @@ function getAddItemToCartData(data, payload, courses, listnerSettings) {
1932
1932
  itemDiscountDetails: itemWithValidatedRewardClaim.item_discount_details,
1933
1933
  rewardClaim: validatedRewardClaim,
1934
1934
  });
1935
- const newData = calculateCartOrderInfoAndSyncData(orderWithSyncedItemRewardEvidence, courses, listnerSettings);
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);
1936
1948
  return newData;
1937
1949
  }
1938
1950
  function getSplitCartItemToTwo({ data, orderItemId, courses, listnerSettings }) {
@@ -1993,10 +2005,82 @@ function getSplitCartItemToTwo({ data, orderItemId, courses, listnerSettings })
1993
2005
  return newData;
1994
2006
  }
1995
2007
  /* ======================><====================== */
1996
- function getAddItemsToCartData(data, payload, courses, listnerSettings) {
1997
- const newData = payload.reduce((acc, item) => getAddItemToCartData(acc, item, courses, listnerSettings), data);
2008
+ function getAddItemsToCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
2009
+ const newData = payload.reduce((acc, item) => getAddItemToCartData(acc, item, courses, listnerSettings, appliedCustomDiscounts), data);
1998
2010
  return newData;
1999
2011
  }
2012
+ /**
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.
2016
+ */
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;
2056
+ }
2057
+ }
2058
+ else {
2059
+ // No cap configured — keep as percentage
2060
+ recalculatedDiscount = discountPercentage;
2061
+ isSpecialDiscountPercentage = true;
2062
+ }
2063
+ }
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;
2071
+ }
2072
+ }
2073
+ if (recalculatedDiscount > currentSubtotal) {
2074
+ recalculatedDiscount = currentSubtotal;
2075
+ }
2076
+ }
2077
+ return {
2078
+ ...orderDiscountDetails,
2079
+ special_discount: (0, utils_1.parseNum)(recalculatedDiscount),
2080
+ is_special_discount_percentage: isSpecialDiscountPercentage,
2081
+ total_discount: 0,
2082
+ };
2083
+ }
2000
2084
  function getUpdateItemInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
2001
2085
  const orderItemIdsMap = {};
2002
2086
  let categories;
@@ -2027,10 +2111,34 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings, applie
2027
2111
  const platform = data.platform;
2028
2112
  const discountValue = matchedDiscount.value[platform] ?? matchedDiscount.value.BASE_PRICE;
2029
2113
  let recalculatedSpecialDiscount;
2114
+ let isItemDiscountPercentage = matchedDiscount.is_percentage;
2030
2115
  if (matchedDiscount.is_percentage) {
2031
- // Percentage discount — keep the raw percentage value.
2032
- // getDiscountDetails will compute the absolute amount against the current subtotal.
2033
- recalculatedSpecialDiscount = discountValue;
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
+ }
2034
2142
  }
2035
2143
  else {
2036
2144
  // Fixed discount — the amount should NOT scale with quantity.
@@ -2050,7 +2158,7 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings, applie
2050
2158
  item_discount_details: {
2051
2159
  ...newItem.item_discount_details,
2052
2160
  special_discount: (0, utils_1.parseNum)(recalculatedSpecialDiscount),
2053
- is_special_discount_percentage: matchedDiscount.is_percentage,
2161
+ is_special_discount_percentage: isItemDiscountPercentage,
2054
2162
  total_discount: 0,
2055
2163
  },
2056
2164
  };
@@ -2072,51 +2180,14 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings, applie
2072
2180
  }
2073
2181
  // Recalculate order-wide custom discount from the original discount definition
2074
2182
  // when appliedCustomDiscounts are provided and an order-wide discount is active.
2075
- // This ensures quantity/cart changes correctly respect max_amount/max_percentage caps.
2076
- let syncedOrderDiscountDetails = data.order_discount_details;
2077
- if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0 && syncedOrderDiscountDetails?.special_discount_id) {
2078
- const matchedOrderDiscount = appliedCustomDiscounts.find((d) => d._id === syncedOrderDiscountDetails.special_discount_id &&
2079
- d.applicability === constants_1.DISCOUNT_APPLICABILITY_ENUM.ORDER_WIDE);
2080
- if (matchedOrderDiscount) {
2081
- // Calculate current subtotal of all items in the updated categories
2082
- let currentSubtotal = 0;
2083
- if (categories) {
2084
- for (const category of categories) {
2085
- for (const item of category.items) {
2086
- const modifiersTotal = calculateItemModifiersPrice(item);
2087
- currentSubtotal += (item.price + modifiersTotal) * item.quantity;
2088
- }
2089
- }
2090
- }
2091
- const platform = data.platform;
2092
- const discountValue = matchedOrderDiscount.value[platform] ?? matchedOrderDiscount.value.BASE_PRICE;
2093
- let recalculatedDiscount;
2094
- if (matchedOrderDiscount.is_percentage) {
2095
- // Percentage discount — keep the raw percentage value.
2096
- // getDiscountDetails will compute the absolute amount against the current subtotal.
2097
- recalculatedDiscount = discountValue;
2098
- }
2099
- else {
2100
- // Fixed discount — amount stays as-is, but still subject to max_amount cap
2101
- recalculatedDiscount = discountValue;
2102
- if (matchedOrderDiscount.max_amount) {
2103
- const maxAmount = matchedOrderDiscount.max_amount[platform] ?? matchedOrderDiscount.max_amount.BASE_PRICE;
2104
- if (maxAmount > 0 && recalculatedDiscount > maxAmount) {
2105
- recalculatedDiscount = maxAmount;
2106
- }
2107
- }
2108
- if (recalculatedDiscount > currentSubtotal) {
2109
- recalculatedDiscount = currentSubtotal;
2110
- }
2111
- }
2112
- syncedOrderDiscountDetails = {
2113
- ...syncedOrderDiscountDetails,
2114
- special_discount: (0, utils_1.parseNum)(recalculatedDiscount),
2115
- is_special_discount_percentage: matchedOrderDiscount.is_percentage,
2116
- total_discount: 0,
2117
- };
2118
- }
2119
- }
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;
2120
2191
  const iterations = [];
2121
2192
  if (payload.quantity != null && data.iterations != null && data.iterations.length > 0) {
2122
2193
  const [firstIteration, ...restIterations] = data.iterations;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.372",
3
+ "version": "0.2.374",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",