@managesome/eposmatic-toolkit 0.2.371 → 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.
|
@@ -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 {
|
|
@@ -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,9 +68,17 @@ 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
|
-
export
|
|
73
|
-
|
|
71
|
+
export declare function getAddItemsToCartData(data: Partial<IOrder>, payload: ISetAddItemToCartActionPayload[], courses: ICourse[], listnerSettings: ICartListnerSettings, appliedCustomDiscounts?: IAppliedCustomDiscount[]): Partial<IOrder>;
|
|
72
|
+
export 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;
|
|
@@ -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
|
-
|
|
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,12 +2005,59 @@ 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
|
}
|
|
2000
|
-
|
|
2001
|
-
|
|
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
|
+
}
|
|
2060
|
+
function getUpdateItemInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
|
|
2002
2061
|
const orderItemIdsMap = {};
|
|
2003
2062
|
let categories;
|
|
2004
2063
|
if (data.categories != null) {
|
|
@@ -2014,6 +2073,49 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings) {
|
|
|
2014
2073
|
...item,
|
|
2015
2074
|
...payload,
|
|
2016
2075
|
};
|
|
2076
|
+
// Recalculate item-level custom discount from the original discount definition
|
|
2077
|
+
// when appliedCustomDiscounts are provided. This ensures quantity changes
|
|
2078
|
+
// do not incorrectly linearly scale fixed discounts or bypass caps.
|
|
2079
|
+
if (appliedCustomDiscounts &&
|
|
2080
|
+
appliedCustomDiscounts.length > 0 &&
|
|
2081
|
+
newItem.item_discount_details?.special_discount_id) {
|
|
2082
|
+
const matchedDiscount = appliedCustomDiscounts.find((d) => d._id === newItem.item_discount_details.special_discount_id &&
|
|
2083
|
+
d.applicability === constants_1.DISCOUNT_APPLICABILITY_ENUM.ITEM_SPECIFIC);
|
|
2084
|
+
if (matchedDiscount) {
|
|
2085
|
+
const modifiersTotal = calculateItemModifiersPrice(newItem);
|
|
2086
|
+
const itemTotalWithoutTax = (newItem.price + modifiersTotal) * newItem.quantity;
|
|
2087
|
+
const platform = data.platform;
|
|
2088
|
+
const discountValue = matchedDiscount.value[platform] ?? matchedDiscount.value.BASE_PRICE;
|
|
2089
|
+
let recalculatedSpecialDiscount;
|
|
2090
|
+
if (matchedDiscount.is_percentage) {
|
|
2091
|
+
// Percentage discount — keep the raw percentage value.
|
|
2092
|
+
// getDiscountDetails will compute the absolute amount against the current subtotal.
|
|
2093
|
+
recalculatedSpecialDiscount = discountValue;
|
|
2094
|
+
}
|
|
2095
|
+
else {
|
|
2096
|
+
// Fixed discount — the amount should NOT scale with quantity.
|
|
2097
|
+
recalculatedSpecialDiscount = discountValue;
|
|
2098
|
+
if (matchedDiscount.max_amount) {
|
|
2099
|
+
const maxAmount = matchedDiscount.max_amount[platform] ?? matchedDiscount.max_amount.BASE_PRICE;
|
|
2100
|
+
if (maxAmount > 0 && recalculatedSpecialDiscount > maxAmount) {
|
|
2101
|
+
recalculatedSpecialDiscount = maxAmount;
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
if (recalculatedSpecialDiscount > itemTotalWithoutTax) {
|
|
2105
|
+
recalculatedSpecialDiscount = itemTotalWithoutTax;
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
newItem = {
|
|
2109
|
+
...newItem,
|
|
2110
|
+
item_discount_details: {
|
|
2111
|
+
...newItem.item_discount_details,
|
|
2112
|
+
special_discount: (0, utils_1.parseNum)(recalculatedSpecialDiscount),
|
|
2113
|
+
is_special_discount_percentage: matchedDiscount.is_percentage,
|
|
2114
|
+
total_discount: 0,
|
|
2115
|
+
},
|
|
2116
|
+
};
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2017
2119
|
}
|
|
2018
2120
|
if (newItem.quantity > 0) {
|
|
2019
2121
|
items.push(newItem);
|
|
@@ -2028,6 +2130,16 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings) {
|
|
|
2028
2130
|
}
|
|
2029
2131
|
}
|
|
2030
2132
|
}
|
|
2133
|
+
// Recalculate order-wide custom discount from the original discount definition
|
|
2134
|
+
// when appliedCustomDiscounts are provided and an order-wide discount is active.
|
|
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;
|
|
2031
2143
|
const iterations = [];
|
|
2032
2144
|
if (payload.quantity != null && data.iterations != null && data.iterations.length > 0) {
|
|
2033
2145
|
const [firstIteration, ...restIterations] = data.iterations;
|
|
@@ -2078,6 +2190,7 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings) {
|
|
|
2078
2190
|
}
|
|
2079
2191
|
const order = {
|
|
2080
2192
|
...data,
|
|
2193
|
+
order_discount_details: syncedOrderDiscountDetails,
|
|
2081
2194
|
...(iterations.length > 0 && { iterations }),
|
|
2082
2195
|
categories,
|
|
2083
2196
|
};
|
|
@@ -2085,8 +2198,8 @@ function getUpdateItemInCartData(data, payload, courses, listnerSettings) {
|
|
|
2085
2198
|
return newData;
|
|
2086
2199
|
}
|
|
2087
2200
|
/* ======================><====================== */
|
|
2088
|
-
function getUpdateItemsInCartData(data, payload, courses, listnerSettings) {
|
|
2089
|
-
const newData = payload.reduce((acc, item) => getUpdateItemInCartData(acc, item, courses, listnerSettings), data);
|
|
2201
|
+
function getUpdateItemsInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
|
|
2202
|
+
const newData = payload.reduce((acc, item) => getUpdateItemInCartData(acc, item, courses, listnerSettings, appliedCustomDiscounts), data);
|
|
2090
2203
|
return newData;
|
|
2091
2204
|
}
|
|
2092
2205
|
/* ======================><====================== */
|