@managesome/eposmatic-toolkit 0.2.372 → 0.2.373

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,58 @@ 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
+ if (matchedOrderDiscount.is_percentage) {
2036
+ // Percentage discount — keep the raw percentage value.
2037
+ // getDiscountDetails will compute the absolute amount against the current subtotal.
2038
+ recalculatedDiscount = discountValue;
2039
+ }
2040
+ else {
2041
+ // Fixed discount — amount stays as-is, but still subject to max_amount cap
2042
+ recalculatedDiscount = discountValue;
2043
+ if (matchedOrderDiscount.max_amount) {
2044
+ const maxAmount = matchedOrderDiscount.max_amount[platform] ?? matchedOrderDiscount.max_amount.BASE_PRICE;
2045
+ if (maxAmount > 0 && recalculatedDiscount > maxAmount) {
2046
+ recalculatedDiscount = maxAmount;
2047
+ }
2048
+ }
2049
+ if (recalculatedDiscount > currentSubtotal) {
2050
+ recalculatedDiscount = currentSubtotal;
2051
+ }
2052
+ }
2053
+ return {
2054
+ ...orderDiscountDetails,
2055
+ special_discount: (0, utils_1.parseNum)(recalculatedDiscount),
2056
+ is_special_discount_percentage: matchedOrderDiscount.is_percentage,
2057
+ total_discount: 0,
2058
+ };
2059
+ }
2000
2060
  function getUpdateItemInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
2001
2061
  const orderItemIdsMap = {};
2002
2062
  let categories;
@@ -2072,51 +2132,14 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings, applie
2072
2132
  }
2073
2133
  // Recalculate order-wide custom discount from the original discount definition
2074
2134
  // 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
- }
2135
+ const syncedOrderDiscountDetails = appliedCustomDiscounts && appliedCustomDiscounts.length > 0
2136
+ ? getSyncedOrderDiscountDetailsWithCaps({
2137
+ orderDiscountDetails: data.order_discount_details,
2138
+ appliedCustomDiscounts,
2139
+ categories,
2140
+ platform: data.platform,
2141
+ })
2142
+ : data.order_discount_details;
2120
2143
  const iterations = [];
2121
2144
  if (payload.quantity != null && data.iterations != null && data.iterations.length > 0) {
2122
2145
  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.373",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",