@managesome/eposmatic-toolkit 0.2.370 → 0.2.372

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.
@@ -1,5 +1,5 @@
1
- import { GIFT_CARD_REWARD_TYPE_ENUM, ORDER_STATUS_ENUM, PLATFORMS_ENUM, SORT_TYPE_ENUM } from '../../constants';
2
- import { ICourse, IDiscountDetails, IGeneralItem, IItem, IModifier, IOffer, IOrder, IOrderCategory, IOrderCourse, IOrderDeliveryDetails, IOrderItem, IOrderIteration } from '../../schemas';
1
+ import { DISCOUNT_APPLICABILITY_ENUM, GIFT_CARD_REWARD_TYPE_ENUM, ORDER_STATUS_ENUM, PLATFORMS_ENUM, SORT_TYPE_ENUM } from '../../constants';
2
+ import { ICourse, IDiscountDetails, IGeneralItem, IItem, IModifier, IOffer, IOrder, IOrderCategory, IOrderCourse, IOrderDeliveryDetails, IOrderItem, IOrderIteration, IPrice } from '../../schemas';
3
3
  import { ICartListnerSettings } from '../types';
4
4
  import { ICartItemsObj } from '../types/cart-actions';
5
5
  interface IGetItemQuantityChangeResultsParams {
@@ -69,8 +69,16 @@ interface IGetSplitCartItemToTwo {
69
69
  }
70
70
  export declare function getSplitCartItemToTwo({ data, orderItemId, courses, listnerSettings }: IGetSplitCartItemToTwo): Partial<IOrder>;
71
71
  export declare function getAddItemsToCartData(data: Partial<IOrder>, payload: ISetAddItemToCartActionPayload[], courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
72
- export declare function getUpdateItemInCartData(data: Partial<IOrder>, payload: Partial<IOrderItem>, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
73
- export declare function getUpdateItemsInCartData(data: Partial<IOrder>, payload: Partial<IOrder>[], courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
72
+ interface IAppliedCustomDiscount {
73
+ _id: string;
74
+ applicability: DISCOUNT_APPLICABILITY_ENUM;
75
+ value: IPrice;
76
+ is_percentage: boolean;
77
+ max_percentage: IPrice | null | undefined;
78
+ max_amount: IPrice | null | undefined;
79
+ }
80
+ export declare function getUpdateItemInCartData(data: Partial<IOrder>, payload: Partial<IOrderItem>, courses: ICourse[], listnerSettings: ICartListnerSettings, appliedCustomDiscounts?: IAppliedCustomDiscount[]): Partial<IOrder>;
81
+ export declare function getUpdateItemsInCartData(data: Partial<IOrder>, payload: Partial<IOrder>[], courses: ICourse[], listnerSettings: ICartListnerSettings, appliedCustomDiscounts?: IAppliedCustomDiscount[]): Partial<IOrder>;
74
82
  export declare function getUpdateCartInfoData(data: Partial<IOrder>, payload: Partial<IOrder>, courses: ICourse[], listnerSettings: ICartListnerSettings, syncContext?: ICartOfferSyncContext): Partial<IOrder>;
75
83
  export declare function getRemoveItemFromCartData(data: Partial<IOrder>, payload: {
76
84
  _id: string;
@@ -1997,8 +1997,7 @@ function getAddItemsToCartData(data, payload, courses, listnerSettings) {
1997
1997
  const newData = payload.reduce((acc, item) => getAddItemToCartData(acc, item, courses, listnerSettings), data);
1998
1998
  return newData;
1999
1999
  }
2000
- /* ======================><====================== */
2001
- function getUpdateItemInCartData(data, payload, courses, listnerSettings) {
2000
+ function getUpdateItemInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
2002
2001
  const orderItemIdsMap = {};
2003
2002
  let categories;
2004
2003
  if (data.categories != null) {
@@ -2014,6 +2013,49 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings) {
2014
2013
  ...item,
2015
2014
  ...payload,
2016
2015
  };
2016
+ // Recalculate item-level custom discount from the original discount definition
2017
+ // when appliedCustomDiscounts are provided. This ensures quantity changes
2018
+ // do not incorrectly linearly scale fixed discounts or bypass caps.
2019
+ if (appliedCustomDiscounts &&
2020
+ appliedCustomDiscounts.length > 0 &&
2021
+ newItem.item_discount_details?.special_discount_id) {
2022
+ const matchedDiscount = appliedCustomDiscounts.find((d) => d._id === newItem.item_discount_details.special_discount_id &&
2023
+ d.applicability === constants_1.DISCOUNT_APPLICABILITY_ENUM.ITEM_SPECIFIC);
2024
+ if (matchedDiscount) {
2025
+ const modifiersTotal = calculateItemModifiersPrice(newItem);
2026
+ const itemTotalWithoutTax = (newItem.price + modifiersTotal) * newItem.quantity;
2027
+ const platform = data.platform;
2028
+ const discountValue = matchedDiscount.value[platform] ?? matchedDiscount.value.BASE_PRICE;
2029
+ let recalculatedSpecialDiscount;
2030
+ 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;
2034
+ }
2035
+ else {
2036
+ // Fixed discount — the amount should NOT scale with quantity.
2037
+ recalculatedSpecialDiscount = discountValue;
2038
+ if (matchedDiscount.max_amount) {
2039
+ const maxAmount = matchedDiscount.max_amount[platform] ?? matchedDiscount.max_amount.BASE_PRICE;
2040
+ if (maxAmount > 0 && recalculatedSpecialDiscount > maxAmount) {
2041
+ recalculatedSpecialDiscount = maxAmount;
2042
+ }
2043
+ }
2044
+ if (recalculatedSpecialDiscount > itemTotalWithoutTax) {
2045
+ recalculatedSpecialDiscount = itemTotalWithoutTax;
2046
+ }
2047
+ }
2048
+ newItem = {
2049
+ ...newItem,
2050
+ item_discount_details: {
2051
+ ...newItem.item_discount_details,
2052
+ special_discount: (0, utils_1.parseNum)(recalculatedSpecialDiscount),
2053
+ is_special_discount_percentage: matchedDiscount.is_percentage,
2054
+ total_discount: 0,
2055
+ },
2056
+ };
2057
+ }
2058
+ }
2017
2059
  }
2018
2060
  if (newItem.quantity > 0) {
2019
2061
  items.push(newItem);
@@ -2028,6 +2070,53 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings) {
2028
2070
  }
2029
2071
  }
2030
2072
  }
2073
+ // Recalculate order-wide custom discount from the original discount definition
2074
+ // 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
+ }
2031
2120
  const iterations = [];
2032
2121
  if (payload.quantity != null && data.iterations != null && data.iterations.length > 0) {
2033
2122
  const [firstIteration, ...restIterations] = data.iterations;
@@ -2078,6 +2167,7 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings) {
2078
2167
  }
2079
2168
  const order = {
2080
2169
  ...data,
2170
+ order_discount_details: syncedOrderDiscountDetails,
2081
2171
  ...(iterations.length > 0 && { iterations }),
2082
2172
  categories,
2083
2173
  };
@@ -2085,8 +2175,8 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings) {
2085
2175
  return newData;
2086
2176
  }
2087
2177
  /* ======================><====================== */
2088
- function getUpdateItemsInCartData(data, payload, courses, listnerSettings) {
2089
- const newData = payload.reduce((acc, item) => getUpdateItemInCartData(acc, item, courses, listnerSettings), data);
2178
+ function getUpdateItemsInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
2179
+ const newData = payload.reduce((acc, item) => getUpdateItemInCartData(acc, item, courses, listnerSettings, appliedCustomDiscounts), data);
2090
2180
  return newData;
2091
2181
  }
2092
2182
  /* ======================><====================== */
@@ -23,11 +23,11 @@ export interface IWarehouseItem {
23
23
  keywords: string[];
24
24
  image_url: string | null;
25
25
  tracking_mode: TRACKING_MODE_ENUM;
26
- expiry_tracked?: boolean;
27
- expiration_days?: number;
28
- best_before_days?: number;
29
- removal_days?: number;
30
- alert_days?: number;
26
+ expiry_tracked: boolean;
27
+ expiration_days: number | null;
28
+ best_before_days: number | null;
29
+ removal_days: number | null;
30
+ alert_days: number | null;
31
31
  createdAt: Date;
32
32
  updatedAt: Date;
33
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.370",
3
+ "version": "0.2.372",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",