@managesome/eposmatic-toolkit 0.2.374 → 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.
|
@@ -83,7 +83,7 @@ export declare function getUpdateCartInfoData(data: Partial<IOrder>, payload: Pa
|
|
|
83
83
|
export declare function getRemoveItemFromCartData(data: Partial<IOrder>, payload: {
|
|
84
84
|
_id: string;
|
|
85
85
|
void_reason?: string;
|
|
86
|
-
}, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
|
|
86
|
+
}, courses: ICourse[], listnerSettings: ICartListnerSettings, appliedCustomDiscounts?: IAppliedCustomDiscount[]): Partial<IOrder>;
|
|
87
87
|
export declare function getUpdatedCartInfoOnInit(data: Partial<IOrder>, order: Partial<IOrder>, courses: ICourse[], platform: PLATFORMS_ENUM | undefined, categories: IOrderCategory[] | undefined, mappedItems: Record<string, IItem>, tenantId: string | undefined, listnerSettings: ICartListnerSettings, offers: IOffer[] | undefined): Partial<IOrder>;
|
|
88
88
|
/**
|
|
89
89
|
* Drop entries from `redeemed_offers` (and any related discount/wallet state)
|
|
@@ -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
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
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
|
-
*
|
|
2014
|
-
*
|
|
2015
|
-
*
|
|
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
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
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
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
const
|
|
2069
|
-
|
|
2070
|
-
|
|
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
|
|
2246
|
-
|
|
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) {
|
|
@@ -2290,7 +2263,7 @@ function getUpdateCartInfoData(data, payload, courses, listnerSettings, syncCont
|
|
|
2290
2263
|
};
|
|
2291
2264
|
}
|
|
2292
2265
|
/* ======================><====================== */
|
|
2293
|
-
function getRemoveItemFromCartData(data, payload, courses, listnerSettings) {
|
|
2266
|
+
function getRemoveItemFromCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
|
|
2294
2267
|
const orderItemIdsMap = {};
|
|
2295
2268
|
let categories;
|
|
2296
2269
|
if (data.categories != null) {
|
|
@@ -2366,8 +2339,11 @@ function getRemoveItemFromCartData(data, payload, courses, listnerSettings) {
|
|
|
2366
2339
|
...(iterations.length > 0 && { iterations }),
|
|
2367
2340
|
categories,
|
|
2368
2341
|
};
|
|
2369
|
-
const
|
|
2370
|
-
|
|
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;
|
|
2371
2347
|
}
|
|
2372
2348
|
/* ======================>Order Object<====================== */
|
|
2373
2349
|
function getUpdatedCartInfoOnInit(data, order, courses, platform, categories, mappedItems, tenantId, listnerSettings, offers) {
|