@managesome/eposmatic-toolkit 0.3.3 → 0.3.6
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.
- package/lib/helpers/utils/offers-helpers.d.ts +11 -1
- package/lib/helpers/utils/offers-helpers.js +74 -20
- package/lib/helpers/utils/order-engine.d.ts +1 -1
- package/lib/helpers/utils/order-engine.js +2 -2
- package/lib/tests/offer-apply-helpers.test.js +64 -0
- package/lib/tests/offers-helpers-edit-baseline.test.d.ts +1 -0
- package/lib/tests/offers-helpers-edit-baseline.test.js +394 -0
- package/package.json +1 -1
|
@@ -225,8 +225,18 @@ interface IGetOffersWithStats {
|
|
|
225
225
|
* inflating eligibility thresholds (e.g. ORDER_FROM max_num_of_orders).
|
|
226
226
|
*/
|
|
227
227
|
isEdit?: boolean;
|
|
228
|
+
/**
|
|
229
|
+
* The edited order's `redeemed_offers` EXACTLY as loaded from the server
|
|
230
|
+
* (frozen snapshot taken when the edit session started). Together with
|
|
231
|
+
* `isEdit`, it keeps the edited order's own settled claims from counting
|
|
232
|
+
* against per-customer limits (`max_uses_per_customer`): the USED
|
|
233
|
+
* redemption fingerprints still include this order until its update
|
|
234
|
+
* settles, so the raw count must not mark the offer exhausted for the
|
|
235
|
+
* order that consumed it. Omit for new orders / flows without an edit.
|
|
236
|
+
*/
|
|
237
|
+
editedOrderRedeemedOffers?: IOrder['redeemed_offers'] | null | undefined;
|
|
228
238
|
}
|
|
229
|
-
export declare function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOfferRedemptionCounts, redeemed_offers, platform, currency_symbol, currentItemsPrice, totalDiscountApplied, categories, order_discount_details, isEdit, }: IGetOffersWithStats): {
|
|
239
|
+
export declare function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOfferRedemptionCounts, redeemed_offers, platform, currency_symbol, currentItemsPrice, totalDiscountApplied, categories, order_discount_details, isEdit, editedOrderRedeemedOffers, }: IGetOffersWithStats): {
|
|
230
240
|
offersWithStats: IOfferWithStats[];
|
|
231
241
|
totalClaimableOffers: string[];
|
|
232
242
|
unlockedOffersApplicableTimes: Record<string, number>;
|
|
@@ -1571,7 +1571,7 @@ function getOffersWithStatsHashKey({ offersWithStats, totalClaimableOffers, isEd
|
|
|
1571
1571
|
}
|
|
1572
1572
|
return result;
|
|
1573
1573
|
}
|
|
1574
|
-
function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOfferRedemptionCounts, redeemed_offers, platform, currency_symbol, currentItemsPrice, totalDiscountApplied, categories, order_discount_details, isEdit, }) {
|
|
1574
|
+
function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOfferRedemptionCounts, redeemed_offers, platform, currency_symbol, currentItemsPrice, totalDiscountApplied, categories, order_discount_details, isEdit, editedOrderRedeemedOffers, }) {
|
|
1575
1575
|
const offersWithStats = [];
|
|
1576
1576
|
const { baseAmount: orderLevelDiscountBaseAmount, itemsCount } = getOrderLevelDiscountBaseAmountForOfferStats({
|
|
1577
1577
|
categories,
|
|
@@ -1625,6 +1625,31 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
1625
1625
|
orderDiscountDetails: order_discount_details,
|
|
1626
1626
|
});
|
|
1627
1627
|
const redeemedOffersById = redeemed_offers ?? null;
|
|
1628
|
+
// Frozen baseline of the edited order's own claims (captured when the edit
|
|
1629
|
+
// session started). Normalized once here so the per-offer evaluation below
|
|
1630
|
+
// can subtract them from the customer's lifetime USED counters without the
|
|
1631
|
+
// edited order counting itself out of `max_uses_per_customer`.
|
|
1632
|
+
const editedOrderBaselineClaims = new Map();
|
|
1633
|
+
if (isEdit && editedOrderRedeemedOffers != null) {
|
|
1634
|
+
const baselineOfferIds = Object.keys(editedOrderRedeemedOffers);
|
|
1635
|
+
for (let i = 0; i < baselineOfferIds.length; i++) {
|
|
1636
|
+
const baselineOfferId = baselineOfferIds[i];
|
|
1637
|
+
const normalizedBaselineOfferId = normalizeOfferId(baselineOfferId);
|
|
1638
|
+
const rawBaselineCount = Number(editedOrderRedeemedOffers[baselineOfferId]);
|
|
1639
|
+
if (normalizedBaselineOfferId == null || !Number.isFinite(rawBaselineCount) || rawBaselineCount <= 0) {
|
|
1640
|
+
continue;
|
|
1641
|
+
}
|
|
1642
|
+
editedOrderBaselineClaims.set(normalizedBaselineOfferId, Math.floor(rawBaselineCount));
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
const getBaselineRedeemedClaimsForOffer = (offerId) => {
|
|
1646
|
+
const normalizedOfferId = normalizeOfferId(offerId);
|
|
1647
|
+
if (normalizedOfferId == null)
|
|
1648
|
+
return 0;
|
|
1649
|
+
return editedOrderBaselineClaims.get(normalizedOfferId) ?? 0;
|
|
1650
|
+
};
|
|
1651
|
+
const getEffectiveUsedRedemptionCount = (offerId) => Math.max(0, getCustomerUsedRedemptionCount(customerOfferRedemptionCounts, offerId) -
|
|
1652
|
+
getBaselineRedeemedClaimsForOffer(offerId));
|
|
1628
1653
|
const vouchersById = new Map();
|
|
1629
1654
|
const allVouchersById = new Map();
|
|
1630
1655
|
if (vouchers != null) {
|
|
@@ -1794,9 +1819,37 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
1794
1819
|
const maxUsesPerCustomer = getOfferMaxUsesPerCustomer(offerToCheck);
|
|
1795
1820
|
if (maxUsesPerCustomer == null)
|
|
1796
1821
|
return false;
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1822
|
+
const baselineRedeemedClaims = getBaselineRedeemedClaimsForOffer(offerToCheck._id);
|
|
1823
|
+
// Unknown counts must never destroy claims the edited order verifiably
|
|
1824
|
+
// carried at load; for everything else keep the conservative
|
|
1825
|
+
// "counts unknown = exhausted" behavior so new claims stay blocked.
|
|
1826
|
+
if (customerOfferRedemptionCounts == null) {
|
|
1827
|
+
return baselineRedeemedClaims <= 0;
|
|
1828
|
+
}
|
|
1829
|
+
return getEffectiveUsedRedemptionCount(offerToCheck._id) >= maxUsesPerCustomer;
|
|
1830
|
+
};
|
|
1831
|
+
// Remaining lifetime budget for this offer expressed in CLAIMS:
|
|
1832
|
+
// max_uses_per_customer minus the customer's effective (baseline-adjusted)
|
|
1833
|
+
// settled usage. Null when the offer has no per-customer limit.
|
|
1834
|
+
const getRemainingClaimBudgetForOffer = (offerToCheck) => {
|
|
1835
|
+
const maxUsesPerCustomer = getOfferMaxUsesPerCustomer(offerToCheck);
|
|
1836
|
+
if (maxUsesPerCustomer == null)
|
|
1837
|
+
return null;
|
|
1838
|
+
// Unknown counts must not grant new claims: preserve only when the
|
|
1839
|
+
// edited order verifiably carried claims (mirrors
|
|
1840
|
+
// isOfferUsesPerCustomerExhausted). With a baseline the full limit stays
|
|
1841
|
+
// available because the customer's true used count is unknown.
|
|
1842
|
+
if (customerOfferRedemptionCounts == null) {
|
|
1843
|
+
return getBaselineRedeemedClaimsForOffer(offerToCheck._id) > 0 ? maxUsesPerCustomer : 0;
|
|
1844
|
+
}
|
|
1845
|
+
return Math.max(0, maxUsesPerCustomer - getEffectiveUsedRedemptionCount(offerToCheck._id));
|
|
1846
|
+
};
|
|
1847
|
+
// Per-order claim counts (value chunks / max_claims) must never exceed the
|
|
1848
|
+
// customer's remaining budget, otherwise a multi-claim reward could be
|
|
1849
|
+
// claimed more times in one order than max_uses_per_customer allows.
|
|
1850
|
+
const clampClaimsToRemainingClaimBudget = (offerToCheck, perOrderClaims) => {
|
|
1851
|
+
const remainingBudget = getRemainingClaimBudgetForOffer(offerToCheck);
|
|
1852
|
+
return remainingBudget == null ? perOrderClaims : Math.min(perOrderClaims, remainingBudget);
|
|
1800
1853
|
};
|
|
1801
1854
|
const getOrderValueClaimTimes = (orderValue, minAmount) => {
|
|
1802
1855
|
const safeRequiredAmount = Math.max(1, minAmount || 1);
|
|
@@ -1833,10 +1886,10 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
1833
1886
|
if (normalizedUnlockableClaims === 0) {
|
|
1834
1887
|
return 0;
|
|
1835
1888
|
}
|
|
1836
|
-
//
|
|
1837
|
-
//
|
|
1838
|
-
//
|
|
1839
|
-
//
|
|
1889
|
+
// The incoming claim count is already clamped to the customer's
|
|
1890
|
+
// remaining lifetime budget (max_uses_per_customer − effectiveUsed, via
|
|
1891
|
+
// clampClaimsToRemainingClaimBudget at the call site); the reward's own
|
|
1892
|
+
// max_claims governs the ongoing session only.
|
|
1840
1893
|
return normalizedUnlockableClaims;
|
|
1841
1894
|
};
|
|
1842
1895
|
const getPathwayMaxRedeemableClaims = (pathwayUnlockableClaims) => {
|
|
@@ -1844,8 +1897,9 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
1844
1897
|
if (normalizedUnlockableClaims === 0) {
|
|
1845
1898
|
return 0;
|
|
1846
1899
|
}
|
|
1847
|
-
// Same
|
|
1848
|
-
//
|
|
1900
|
+
// Same as getPointsMaxRedeemableClaims: the incoming claim count is
|
|
1901
|
+
// already clamped to the customer's remaining lifetime budget at the
|
|
1902
|
+
// call site.
|
|
1849
1903
|
return normalizedUnlockableClaims;
|
|
1850
1904
|
};
|
|
1851
1905
|
const isDiscountSlotOccupiedForOffer = (offerToCheck) => {
|
|
@@ -2044,7 +2098,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
2044
2098
|
? Infinity
|
|
2045
2099
|
: Math.max(1, offer.pathway_to_unlock_type_orders.max_claims);
|
|
2046
2100
|
const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, offer.pathway_to_unlock_type_orders?.total_amount);
|
|
2047
|
-
const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
|
|
2101
|
+
const totalUnlockableClaims = clampClaimsToRemainingClaimBudget(offer, Math.min(unlockTimesForAmount, maxClaimsPerOrder));
|
|
2048
2102
|
const discountSlotOccupied = isDiscountSlotOccupiedForOffer(offer);
|
|
2049
2103
|
const isClaimable = redeemedTimes < totalUnlockableClaims && !discountSlotOccupied;
|
|
2050
2104
|
stats.redeemed_times = redeemedTimes;
|
|
@@ -2073,7 +2127,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
2073
2127
|
? Infinity
|
|
2074
2128
|
: Math.max(1, offer.pathway_to_unlock_type_orders.max_claims);
|
|
2075
2129
|
const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, offer.pathway_to_unlock_type_orders?.total_amount);
|
|
2076
|
-
const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
|
|
2130
|
+
const totalUnlockableClaims = clampClaimsToRemainingClaimBudget(offer, Math.min(unlockTimesForAmount, maxClaimsPerOrder));
|
|
2077
2131
|
const maxRedeemableClaims = getPointsMaxRedeemableClaims({
|
|
2078
2132
|
offerToCheck: offer,
|
|
2079
2133
|
pathwayUnlockableClaims: totalUnlockableClaims,
|
|
@@ -2152,7 +2206,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
2152
2206
|
? Infinity
|
|
2153
2207
|
: Math.max(1, offer.pathway_to_unlock_type_orders.max_claims);
|
|
2154
2208
|
const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, offer.pathway_to_unlock_type_orders?.total_amount);
|
|
2155
|
-
const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
|
|
2209
|
+
const totalUnlockableClaims = clampClaimsToRemainingClaimBudget(offer, Math.min(unlockTimesForAmount, maxClaimsPerOrder));
|
|
2156
2210
|
const maxRedeemableClaims = getPathwayMaxRedeemableClaims(totalUnlockableClaims);
|
|
2157
2211
|
const isClaimable = redeemedTimes < maxRedeemableClaims;
|
|
2158
2212
|
stats.redeemed_times = redeemedTimes;
|
|
@@ -2208,7 +2262,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
2208
2262
|
? Infinity
|
|
2209
2263
|
: Math.max(1, offer.pathway_to_unlock_type_orders.max_claims);
|
|
2210
2264
|
const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, offer.pathway_to_unlock_type_orders?.total_amount);
|
|
2211
|
-
const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder);
|
|
2265
|
+
const totalUnlockableClaims = clampClaimsToRemainingClaimBudget(offer, Math.min(unlockTimesForAmount, maxClaimsPerOrder));
|
|
2212
2266
|
const isClaimable = redeemedTimes < totalUnlockableClaims;
|
|
2213
2267
|
stats.redeemed_times = redeemedTimes;
|
|
2214
2268
|
stats.applicable_quantity = applicableQuantity * Math.max(1, totalUnlockableClaims);
|
|
@@ -2273,7 +2327,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
2273
2327
|
const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
|
|
2274
2328
|
const unlockTimesForItems = Math.floor(totalItemsSelected / (num_of_items || 1));
|
|
2275
2329
|
const unlockTimesForAmount = total_amount != null && total_amount > 0 ? Math.floor(orderValue / total_amount) : unlockTimesForItems;
|
|
2276
|
-
const totalUnlockableClaims = Math.min(unlockTimesForItems, unlockTimesForAmount, maxClaimsPerOrder);
|
|
2330
|
+
const totalUnlockableClaims = clampClaimsToRemainingClaimBudget(offer, Math.min(unlockTimesForItems, unlockTimesForAmount, maxClaimsPerOrder));
|
|
2277
2331
|
const itemsProgress = getPreUnlockProgressPercentage(bachelorItems, num_of_items);
|
|
2278
2332
|
const amountProgress = getPreUnlockProgressPercentage(orderValue, total_amount);
|
|
2279
2333
|
const combinedProgress = Math.min(itemsProgress, amountProgress);
|
|
@@ -2576,7 +2630,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
2576
2630
|
if (stats.progress >= 100) {
|
|
2577
2631
|
const pointBurnTimes = Math.floor(effectiveLoyaltyPointsForCurrentOffer / (num_of_points || 1));
|
|
2578
2632
|
const amountTimes = total_amount != null && total_amount > 0 ? Math.floor(orderValue / total_amount) : pointBurnTimes;
|
|
2579
|
-
const totalUnlockableClaims = Math.min(pointBurnTimes, amountTimes, maxClaimsPerOrder);
|
|
2633
|
+
const totalUnlockableClaims = clampClaimsToRemainingClaimBudget(offer, Math.min(pointBurnTimes, amountTimes, maxClaimsPerOrder));
|
|
2580
2634
|
if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT) {
|
|
2581
2635
|
if (offer.reward_discount == null)
|
|
2582
2636
|
continue;
|
|
@@ -2931,7 +2985,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
2931
2985
|
const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
|
|
2932
2986
|
const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
|
|
2933
2987
|
const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
|
|
2934
|
-
const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
|
|
2988
|
+
const totalUnlockableClaims = clampClaimsToRemainingClaimBudget(offer, Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap));
|
|
2935
2989
|
const isClaimable = redeemedTimes < totalUnlockableClaims;
|
|
2936
2990
|
stats.redeemed_times = redeemedTimes;
|
|
2937
2991
|
stats.applicable_quantity = applicableQuantity * Math.max(1, totalUnlockableClaims);
|
|
@@ -2952,7 +3006,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
2952
3006
|
const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
|
|
2953
3007
|
const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
|
|
2954
3008
|
const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
|
|
2955
|
-
const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
|
|
3009
|
+
const totalUnlockableClaims = clampClaimsToRemainingClaimBudget(offer, Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap));
|
|
2956
3010
|
const maxRedeemableClaims = getPointsMaxRedeemableClaims({
|
|
2957
3011
|
offerToCheck: offer,
|
|
2958
3012
|
pathwayUnlockableClaims: totalUnlockableClaims,
|
|
@@ -3028,7 +3082,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
3028
3082
|
const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
|
|
3029
3083
|
const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
|
|
3030
3084
|
const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
|
|
3031
|
-
const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
|
|
3085
|
+
const totalUnlockableClaims = clampClaimsToRemainingClaimBudget(offer, Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap));
|
|
3032
3086
|
const maxRedeemableClaims = getPathwayMaxRedeemableClaims(totalUnlockableClaims);
|
|
3033
3087
|
const isClaimable = redeemedTimes < maxRedeemableClaims;
|
|
3034
3088
|
stats.redeemed_times = redeemedTimes;
|
|
@@ -3080,7 +3134,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
3080
3134
|
const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
|
|
3081
3135
|
const unlockTimesForAmount = getOrderValueClaimTimes(orderValue, total_amount);
|
|
3082
3136
|
const voucherInventoryClaimCap = matchingVoucherContext != null ? Math.max(0, spendableVoucherCount + redeemedTimesForOffer) : Infinity;
|
|
3083
|
-
const totalUnlockableClaims = Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap);
|
|
3137
|
+
const totalUnlockableClaims = clampClaimsToRemainingClaimBudget(offer, Math.min(unlockTimesForAmount, maxClaimsPerOrder, voucherInventoryClaimCap));
|
|
3084
3138
|
const isClaimable = redeemedTimes < totalUnlockableClaims;
|
|
3085
3139
|
stats.redeemed_times = redeemedTimes;
|
|
3086
3140
|
stats.applicable_quantity = applicableQuantity * Math.max(1, totalUnlockableClaims);
|
|
@@ -160,7 +160,7 @@ export declare function convertCatalogItemToOrderEngineItem({ item, orderItem, p
|
|
|
160
160
|
export declare function hasRequiredModifier<T extends IModifier | IOrderEngineModifier>(currentModifiers: T[] | null): boolean;
|
|
161
161
|
export declare function convertItemToOrderItem(item: IItem | ISimplifiedCatalogItem, menu_id: string | null, platform: PLATFORMS_ENUM | undefined, tenantId: string, orderItemType?: ORDER_ITEM_TYPE_ENUM): IOrderItem;
|
|
162
162
|
export declare function setCheckFieldInOrderEngineItem(orderEngineItem: IOrderEngineItem, identifier: string, shouldCheck: boolean, chooseExpensiveSegmentPrice?: boolean): IOrderEngineItem;
|
|
163
|
-
export declare function setIncrementOrderEngineItemQuantity(orderEngineItem: IOrderEngineItem, increment: number): IOrderEngineItem;
|
|
163
|
+
export declare function setIncrementOrderEngineItemQuantity(orderEngineItem: IOrderEngineItem, increment: number, isAbs?: boolean): IOrderEngineItem;
|
|
164
164
|
export declare function setUpdateSelectedOrderEngineOptionQuantity(orderEngineItem: IOrderEngineItem, identifier: string, newQuantity: number, isIncrement?: boolean): IOrderEngineItem;
|
|
165
165
|
export declare function getFlattenedOrderEngineItemModifiers(modifiers: IOrderEngineModifier[], keepOptionalModifiersAtBottom?: boolean): IOrderEngineModifier[];
|
|
166
166
|
export {};
|
|
@@ -1298,8 +1298,8 @@ function setCheckFieldInOrderEngineItem(orderEngineItem, identifier, shouldCheck
|
|
|
1298
1298
|
void chooseExpensiveSegmentPrice;
|
|
1299
1299
|
return updatedOrderEngineItem;
|
|
1300
1300
|
}
|
|
1301
|
-
function setIncrementOrderEngineItemQuantity(orderEngineItem, increment) {
|
|
1302
|
-
const newQuantity = increment + orderEngineItem.quantity;
|
|
1301
|
+
function setIncrementOrderEngineItemQuantity(orderEngineItem, increment, isAbs) {
|
|
1302
|
+
const newQuantity = isAbs ? Math.abs(increment) || 1 : increment + orderEngineItem.quantity;
|
|
1303
1303
|
const item_discount_details = (0, store_1.getItemQuantityChangeResults)({
|
|
1304
1304
|
newQuantity,
|
|
1305
1305
|
prevQuantity: orderEngineItem.quantity,
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const constants_1 = require("../constants");
|
|
4
4
|
const cart_actions_1 = require("../helpers/store/cart-actions");
|
|
5
5
|
const offer_apply_helpers_1 = require("../helpers/utils/offer-apply-helpers");
|
|
6
|
+
const offers_helpers_1 = require("../helpers/utils/offers-helpers");
|
|
6
7
|
const OFFER_ID = 'offer-reward-threshold';
|
|
7
8
|
const POST_ORDER_REWARD_TYPES = ['POINTS', 'CASHBACK', 'VOUCHER', 'GIFT_CARDS'];
|
|
8
9
|
function makeCategories({ itemDiscountDetails = null, quantity = 1, subtotal = 699, itemId = 'cart-item-1', voidReason = undefined, } = {}) {
|
|
@@ -110,6 +111,69 @@ describe('offer redeemed_offers sync', () => {
|
|
|
110
111
|
expect(result.redeemed_offers?.[OFFER_ID]).toBe(1);
|
|
111
112
|
});
|
|
112
113
|
});
|
|
114
|
+
describe('edit-order baseline keeps the applied item reward through the full stats → sync chain', () => {
|
|
115
|
+
const getFlatItems = (categories) => (categories ?? []).flatMap((category) => category.items ?? []);
|
|
116
|
+
const itemDiscountDetails = { loyalty_discount_id: OFFER_ID };
|
|
117
|
+
const makeLimitedItemOffer = () => makeOffer('ITEMS', {
|
|
118
|
+
included_platforms: [constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY],
|
|
119
|
+
pathway_to_unlock_type_orders: {
|
|
120
|
+
scope: 'OVERALL',
|
|
121
|
+
min_num_of_orders: 0,
|
|
122
|
+
max_num_of_orders: null,
|
|
123
|
+
total_amount: 1000,
|
|
124
|
+
max_amount: null,
|
|
125
|
+
orders_between_unlocks: null,
|
|
126
|
+
max_claims: null,
|
|
127
|
+
max_uses_per_customer: 1,
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
const makeEditOrderStats = ({ isEdit, editedOrderRedeemedOffers, }) => (0, offers_helpers_1.getOffersWithStats)({
|
|
131
|
+
offers: [makeLimitedItemOffer()],
|
|
132
|
+
customer: { total_orders: 1, loyalty_points: 0 },
|
|
133
|
+
vouchers: [],
|
|
134
|
+
giftCards: [],
|
|
135
|
+
customerOfferRedemptionCounts: {
|
|
136
|
+
USED: { [OFFER_ID]: 1 },
|
|
137
|
+
ASSIGNED: {},
|
|
138
|
+
},
|
|
139
|
+
redeemed_offers: { [OFFER_ID]: 1 },
|
|
140
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY,
|
|
141
|
+
currency_symbol: constants_1.CURRENCY_SYMBOL_ENUM.$,
|
|
142
|
+
categories: makeCategories({ itemDiscountDetails, subtotal: 2000 }),
|
|
143
|
+
currentItemsPrice: 2000,
|
|
144
|
+
totalDiscountApplied: 0,
|
|
145
|
+
order_discount_details: null,
|
|
146
|
+
isEdit,
|
|
147
|
+
editedOrderRedeemedOffers,
|
|
148
|
+
});
|
|
149
|
+
it('keeps the applied reward item and stamps no void reason when editing the consuming order', () => {
|
|
150
|
+
// The edited order's own claim must not exhaust the customer's limit.
|
|
151
|
+
const stats = makeEditOrderStats({
|
|
152
|
+
isEdit: true,
|
|
153
|
+
editedOrderRedeemedOffers: { [OFFER_ID]: 1 },
|
|
154
|
+
});
|
|
155
|
+
expect(stats.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
156
|
+
const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder({
|
|
157
|
+
categories: makeCategories({ itemDiscountDetails }),
|
|
158
|
+
iterations: [{ categories: makeCategories({ itemDiscountDetails }) }],
|
|
159
|
+
}), [], {}, stats.unlockedOffersApplicableTimes, [makeLimitedItemOffer()]);
|
|
160
|
+
expect(getFlatItems(result.categories)).toHaveLength(1);
|
|
161
|
+
expect(getFlatItems(result.iterations?.[0]?.categories)[0]?.void_reason).toBeUndefined();
|
|
162
|
+
});
|
|
163
|
+
it('removes the item with the auto void reason when the limit is genuinely exhausted (regression guard)', () => {
|
|
164
|
+
const stats = makeEditOrderStats({
|
|
165
|
+
isEdit: false,
|
|
166
|
+
editedOrderRedeemedOffers: null,
|
|
167
|
+
});
|
|
168
|
+
expect(stats.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
|
|
169
|
+
const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder({
|
|
170
|
+
categories: makeCategories({ itemDiscountDetails }),
|
|
171
|
+
iterations: [{ categories: makeCategories({ itemDiscountDetails }) }],
|
|
172
|
+
}), [], {}, stats.unlockedOffersApplicableTimes, [makeLimitedItemOffer()]);
|
|
173
|
+
expect(getFlatItems(result.categories)).toHaveLength(0);
|
|
174
|
+
expect(getFlatItems(result.iterations?.[0]?.categories)[0]?.void_reason).toBe(constants_1.OFFER_NO_LONGER_QUALIFIED_VOID_REASON);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
113
177
|
describe('offer sync auto-removed items void reason', () => {
|
|
114
178
|
const getFlatItems = (categories) => (categories ?? []).flatMap((category) => category.items ?? []);
|
|
115
179
|
const makeIteration = (categories) => [{ categories }];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../constants");
|
|
4
|
+
const offers_helpers_1 = require("../helpers/utils/offers-helpers");
|
|
5
|
+
const OFFER_ID = 'offer-free-burger';
|
|
6
|
+
const BURGER_ITEM_ID = 'item-burger';
|
|
7
|
+
function makeCategories(subtotal = 2000) {
|
|
8
|
+
return [
|
|
9
|
+
{
|
|
10
|
+
category_id: 'category-pizza',
|
|
11
|
+
items: [
|
|
12
|
+
{
|
|
13
|
+
_id: 'cart-item-1',
|
|
14
|
+
item_id: 'item-classic',
|
|
15
|
+
name: 'Classic',
|
|
16
|
+
quantity: 1,
|
|
17
|
+
subtotal,
|
|
18
|
+
modifiers: [],
|
|
19
|
+
item_discount_details: null,
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
category_id: 'category-burger',
|
|
25
|
+
items: [
|
|
26
|
+
{
|
|
27
|
+
_id: 'cart-item-burger',
|
|
28
|
+
item_id: BURGER_ITEM_ID,
|
|
29
|
+
name: 'Chicken Burger',
|
|
30
|
+
quantity: 1,
|
|
31
|
+
subtotal: 0,
|
|
32
|
+
modifiers: [],
|
|
33
|
+
item_discount_details: {
|
|
34
|
+
loyalty_discount: 100,
|
|
35
|
+
loyalty_discount_id: OFFER_ID,
|
|
36
|
+
loyalty_discount_name: 'Free Chicken Burger on First order over 1500',
|
|
37
|
+
is_loyalty_discount_percentage: true,
|
|
38
|
+
loyalty_discount_reason: 'Free Chicken Burger on First order over 1500',
|
|
39
|
+
total_discount: 360,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
function makeFreeBurgerOffer(maxUsesPerCustomer = 1) {
|
|
47
|
+
return {
|
|
48
|
+
_id: OFFER_ID,
|
|
49
|
+
name: 'Free Chicken Burger on First order over 1500',
|
|
50
|
+
active: true,
|
|
51
|
+
auto_apply: true,
|
|
52
|
+
sort_id: 1,
|
|
53
|
+
included_platforms: [constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY],
|
|
54
|
+
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS,
|
|
55
|
+
pathway_to_unlock_type_orders: {
|
|
56
|
+
scope: constants_1.PATHWAY_TO_UNLOCK_TYPE_ORDERS_SCOPE_ENUM.OVERALL,
|
|
57
|
+
min_num_of_orders: 0,
|
|
58
|
+
max_num_of_orders: null,
|
|
59
|
+
total_amount: 1500,
|
|
60
|
+
max_amount: null,
|
|
61
|
+
orders_between_unlocks: null,
|
|
62
|
+
max_claims: 10000,
|
|
63
|
+
max_uses_per_customer: maxUsesPerCustomer,
|
|
64
|
+
},
|
|
65
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
|
|
66
|
+
reward_items: {
|
|
67
|
+
num_of_items: 1,
|
|
68
|
+
excluded_refs: [],
|
|
69
|
+
exclusive_refs: [],
|
|
70
|
+
item_ids: [BURGER_ITEM_ID],
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function makeUsedCounts(usedTimes) {
|
|
75
|
+
return {
|
|
76
|
+
[constants_1.CUSTOMER_REDEMPTION_TYPE_ENUM.USED]: { [OFFER_ID]: usedTimes },
|
|
77
|
+
[constants_1.CUSTOMER_REDEMPTION_TYPE_ENUM.ASSIGNED]: {},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function makeOrderLevelDiscountOffer(maxUsesPerCustomer = 1) {
|
|
81
|
+
return {
|
|
82
|
+
...makeFreeBurgerOffer(maxUsesPerCustomer),
|
|
83
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT,
|
|
84
|
+
reward_discount: {
|
|
85
|
+
discount_value: 10,
|
|
86
|
+
is_discount_percentage: true,
|
|
87
|
+
max_discount_amount: null,
|
|
88
|
+
max_discount_percentage: null,
|
|
89
|
+
excluded_refs: [],
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function runStats(options = {}) {
|
|
94
|
+
return (0, offers_helpers_1.getOffersWithStats)({
|
|
95
|
+
offers: [options.offer ?? makeFreeBurgerOffer(options.maxUsesPerCustomer ?? 1)],
|
|
96
|
+
customer: { total_orders: 1, loyalty_points: 0 },
|
|
97
|
+
vouchers: [],
|
|
98
|
+
giftCards: [],
|
|
99
|
+
customerOfferRedemptionCounts: options.counts === undefined ? makeUsedCounts(1) : options.counts,
|
|
100
|
+
redeemed_offers: { [OFFER_ID]: 1 },
|
|
101
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY,
|
|
102
|
+
currency_symbol: constants_1.CURRENCY_SYMBOL_ENUM.$,
|
|
103
|
+
categories: makeCategories(options.orderValue),
|
|
104
|
+
currentItemsPrice: options.orderValue ?? 2000,
|
|
105
|
+
totalDiscountApplied: 0,
|
|
106
|
+
order_discount_details: null,
|
|
107
|
+
isEdit: options.isEdit,
|
|
108
|
+
editedOrderRedeemedOffers: options.editedOrderRedeemedOffers,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
describe('getOffersWithStats — edited-order redemption baseline', () => {
|
|
112
|
+
it('keeps an applied item reward when editing the order that consumed it', () => {
|
|
113
|
+
const result = runStats({
|
|
114
|
+
counts: makeUsedCounts(1),
|
|
115
|
+
isEdit: true,
|
|
116
|
+
editedOrderRedeemedOffers: { [OFFER_ID]: 1 },
|
|
117
|
+
});
|
|
118
|
+
const { stats } = result.offersWithStats[0];
|
|
119
|
+
expect(stats.redeemed_times).toBe(1);
|
|
120
|
+
expect(stats.applicable_quantity).toBe(1);
|
|
121
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
122
|
+
expect(stats.message).toContain('redeemed');
|
|
123
|
+
expect(stats.message).not.toContain('personal limit');
|
|
124
|
+
});
|
|
125
|
+
it('still exhausts the offer on a new order when the customer already used it', () => {
|
|
126
|
+
const result = runStats({ counts: makeUsedCounts(1) });
|
|
127
|
+
const { stats } = result.offersWithStats[0];
|
|
128
|
+
expect(stats.applicable_quantity).toBe(0);
|
|
129
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
|
|
130
|
+
expect(stats.message).toContain('personal limit');
|
|
131
|
+
});
|
|
132
|
+
it('still exhausts the offer when editing an order with no baseline claim for it', () => {
|
|
133
|
+
const result = runStats({
|
|
134
|
+
counts: makeUsedCounts(1),
|
|
135
|
+
isEdit: true,
|
|
136
|
+
editedOrderRedeemedOffers: {},
|
|
137
|
+
});
|
|
138
|
+
const { stats } = result.offersWithStats[0];
|
|
139
|
+
expect(stats.applicable_quantity).toBe(0);
|
|
140
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
|
|
141
|
+
expect(stats.message).toContain('personal limit');
|
|
142
|
+
});
|
|
143
|
+
it('keeps the previous behavior when isEdit is set but no baseline snapshot is provided', () => {
|
|
144
|
+
const result = runStats({ counts: makeUsedCounts(1), isEdit: true });
|
|
145
|
+
const { stats } = result.offersWithStats[0];
|
|
146
|
+
expect(stats.applicable_quantity).toBe(0);
|
|
147
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
|
|
148
|
+
expect(stats.message).toContain('personal limit');
|
|
149
|
+
});
|
|
150
|
+
it('preserves the claim while counts are unknown if the edited order carried it', () => {
|
|
151
|
+
const result = runStats({
|
|
152
|
+
counts: null,
|
|
153
|
+
isEdit: true,
|
|
154
|
+
editedOrderRedeemedOffers: { [OFFER_ID]: 1 },
|
|
155
|
+
});
|
|
156
|
+
const { stats } = result.offersWithStats[0];
|
|
157
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
158
|
+
expect(stats.message).not.toContain('personal limit');
|
|
159
|
+
});
|
|
160
|
+
it('stays fail-closed on unknown counts without baseline evidence', () => {
|
|
161
|
+
const result = runStats({
|
|
162
|
+
counts: null,
|
|
163
|
+
isEdit: true,
|
|
164
|
+
editedOrderRedeemedOffers: {},
|
|
165
|
+
});
|
|
166
|
+
const { stats } = result.offersWithStats[0];
|
|
167
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
|
|
168
|
+
expect(stats.message).toContain('personal limit');
|
|
169
|
+
});
|
|
170
|
+
it("subtracts only the edited order's own claims from the lifetime count", () => {
|
|
171
|
+
const result = runStats({
|
|
172
|
+
counts: makeUsedCounts(2),
|
|
173
|
+
isEdit: true,
|
|
174
|
+
editedOrderRedeemedOffers: { [OFFER_ID]: 1 },
|
|
175
|
+
maxUsesPerCustomer: 2,
|
|
176
|
+
});
|
|
177
|
+
const { stats } = result.offersWithStats[0];
|
|
178
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
179
|
+
expect(stats.message).not.toContain('personal limit');
|
|
180
|
+
});
|
|
181
|
+
it('still exhausts when other orders consumed the full lifetime budget', () => {
|
|
182
|
+
const result = runStats({
|
|
183
|
+
counts: makeUsedCounts(2),
|
|
184
|
+
isEdit: true,
|
|
185
|
+
editedOrderRedeemedOffers: {},
|
|
186
|
+
maxUsesPerCustomer: 2,
|
|
187
|
+
});
|
|
188
|
+
const { stats } = result.offersWithStats[0];
|
|
189
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
|
|
190
|
+
expect(stats.message).toContain('personal limit');
|
|
191
|
+
});
|
|
192
|
+
it('keeps an order-level reward (discount) when editing the order that consumed it', () => {
|
|
193
|
+
const offer = makeOrderLevelDiscountOffer(1);
|
|
194
|
+
const edited = runStats({
|
|
195
|
+
counts: makeUsedCounts(1),
|
|
196
|
+
isEdit: true,
|
|
197
|
+
editedOrderRedeemedOffers: { [OFFER_ID]: 1 },
|
|
198
|
+
offer,
|
|
199
|
+
});
|
|
200
|
+
expect(edited.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
201
|
+
expect(edited.offersWithStats[0].stats.message).not.toContain('personal limit');
|
|
202
|
+
const fresh = runStats({ counts: makeUsedCounts(1), offer });
|
|
203
|
+
expect(fresh.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
|
|
204
|
+
expect(fresh.offersWithStats[0].stats.message).toContain('personal limit');
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
describe('getOffersWithStats — remaining-budget clamp (Q1)', () => {
|
|
208
|
+
it('caps multi-chunk session claims at the remaining budget (counts known)', () => {
|
|
209
|
+
const result = runStats({
|
|
210
|
+
counts: makeUsedCounts(1),
|
|
211
|
+
isEdit: true,
|
|
212
|
+
editedOrderRedeemedOffers: { [OFFER_ID]: 1 },
|
|
213
|
+
orderValue: 4000,
|
|
214
|
+
maxUsesPerCustomer: 1,
|
|
215
|
+
});
|
|
216
|
+
// Two spend chunks unlock, but the customer's budget is a single use.
|
|
217
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
218
|
+
expect(result.offersWithStats[0].stats.message).not.toContain('personal limit');
|
|
219
|
+
});
|
|
220
|
+
it('caps multi-chunk session claims while counts are unknown (hold window)', () => {
|
|
221
|
+
const result = runStats({
|
|
222
|
+
counts: null,
|
|
223
|
+
isEdit: true,
|
|
224
|
+
editedOrderRedeemedOffers: { [OFFER_ID]: 1 },
|
|
225
|
+
orderValue: 4000,
|
|
226
|
+
maxUsesPerCustomer: 1,
|
|
227
|
+
});
|
|
228
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
229
|
+
expect(result.offersWithStats[0].stats.message).not.toContain('personal limit');
|
|
230
|
+
});
|
|
231
|
+
it('reduces multi-chunk claims to the remaining budget', () => {
|
|
232
|
+
const result = runStats({
|
|
233
|
+
counts: makeUsedCounts(1),
|
|
234
|
+
orderValue: 4000,
|
|
235
|
+
maxUsesPerCustomer: 2,
|
|
236
|
+
});
|
|
237
|
+
// Two chunks available, one lifetime use left.
|
|
238
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
239
|
+
});
|
|
240
|
+
it('does not clamp below the per-order claims while budget remains', () => {
|
|
241
|
+
const result = runStats({
|
|
242
|
+
counts: makeUsedCounts(0),
|
|
243
|
+
orderValue: 4000,
|
|
244
|
+
maxUsesPerCustomer: 3,
|
|
245
|
+
});
|
|
246
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(2);
|
|
247
|
+
});
|
|
248
|
+
it('ignores non-numeric baseline counts and stays fail-closed', () => {
|
|
249
|
+
const result = runStats({
|
|
250
|
+
counts: makeUsedCounts(2),
|
|
251
|
+
isEdit: true,
|
|
252
|
+
editedOrderRedeemedOffers: { [OFFER_ID]: 'abc' },
|
|
253
|
+
maxUsesPerCustomer: 2,
|
|
254
|
+
});
|
|
255
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
|
|
256
|
+
expect(result.offersWithStats[0].stats.message).toContain('personal limit');
|
|
257
|
+
});
|
|
258
|
+
it('normalizes whitespace-padded baseline keys and string counts', () => {
|
|
259
|
+
const result = runStats({
|
|
260
|
+
counts: makeUsedCounts(2),
|
|
261
|
+
isEdit: true,
|
|
262
|
+
editedOrderRedeemedOffers: { ' offer-free-burger ': '1' },
|
|
263
|
+
maxUsesPerCustomer: 2,
|
|
264
|
+
});
|
|
265
|
+
// Baseline subtracts one use: budget = 2 − (2 − 1) = 1.
|
|
266
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
function makePathwayOffer(pathway) {
|
|
270
|
+
const base = {
|
|
271
|
+
_id: OFFER_ID,
|
|
272
|
+
name: 'Pathway reward',
|
|
273
|
+
active: true,
|
|
274
|
+
auto_apply: true,
|
|
275
|
+
sort_id: 1,
|
|
276
|
+
included_platforms: [constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY],
|
|
277
|
+
pathway_to_unlock_type: pathway,
|
|
278
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
|
|
279
|
+
reward_items: {
|
|
280
|
+
num_of_items: 1,
|
|
281
|
+
excluded_refs: [],
|
|
282
|
+
exclusive_refs: [],
|
|
283
|
+
item_ids: [BURGER_ITEM_ID],
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
if (pathway === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS) {
|
|
287
|
+
return {
|
|
288
|
+
...base,
|
|
289
|
+
pathway_to_unlock_type_orders: {
|
|
290
|
+
scope: constants_1.PATHWAY_TO_UNLOCK_TYPE_ORDERS_SCOPE_ENUM.OVERALL,
|
|
291
|
+
min_num_of_orders: 0,
|
|
292
|
+
max_num_of_orders: null,
|
|
293
|
+
total_amount: 1500,
|
|
294
|
+
max_amount: null,
|
|
295
|
+
orders_between_unlocks: null,
|
|
296
|
+
max_claims: 2,
|
|
297
|
+
max_uses_per_customer: 1,
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
if (pathway === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ITEMS) {
|
|
302
|
+
return {
|
|
303
|
+
...base,
|
|
304
|
+
pathway_to_unlock_type_orders: null,
|
|
305
|
+
pathway_to_unlock_type_items: {
|
|
306
|
+
num_of_items: 1,
|
|
307
|
+
max_uses_per_customer: 1,
|
|
308
|
+
max_claims: 2,
|
|
309
|
+
min_same_item_quantity: null,
|
|
310
|
+
max_same_item_quantity: null,
|
|
311
|
+
total_amount: null,
|
|
312
|
+
excluded_refs: [],
|
|
313
|
+
exclusive_refs: [],
|
|
314
|
+
item_ids: ['item-classic'],
|
|
315
|
+
category_ids: [],
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
if (pathway === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS) {
|
|
320
|
+
return {
|
|
321
|
+
...base,
|
|
322
|
+
pathway_to_unlock_type_orders: null,
|
|
323
|
+
pathway_to_unlock_type_points: {
|
|
324
|
+
num_of_points: 100,
|
|
325
|
+
total_amount: null,
|
|
326
|
+
max_uses_per_customer: 1,
|
|
327
|
+
max_claims: 2,
|
|
328
|
+
},
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
...base,
|
|
333
|
+
pathway_to_unlock_type_orders: null,
|
|
334
|
+
pathway_to_unlock_type_vouchers: {
|
|
335
|
+
total_amount: 1500,
|
|
336
|
+
max_amount: null,
|
|
337
|
+
max_uses_per_customer: 1,
|
|
338
|
+
max_claims: 2,
|
|
339
|
+
voucher_ids: ['voucher-v'],
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
const PATHWAY_CASES = [
|
|
344
|
+
{ pathway: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS },
|
|
345
|
+
{ pathway: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ITEMS },
|
|
346
|
+
{ pathway: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS, customer: { total_orders: 1, loyalty_points: 500 } },
|
|
347
|
+
{
|
|
348
|
+
pathway: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER,
|
|
349
|
+
customer: { total_orders: 1, loyalty_points: 0, voucher_codes: { VV: 1 } },
|
|
350
|
+
vouchers: [
|
|
351
|
+
{
|
|
352
|
+
_id: 'voucher-v',
|
|
353
|
+
code: 'VV',
|
|
354
|
+
active: true,
|
|
355
|
+
max_uses: 100,
|
|
356
|
+
used_count: 0,
|
|
357
|
+
max_uses_per_customer: null,
|
|
358
|
+
expire_at: null,
|
|
359
|
+
},
|
|
360
|
+
],
|
|
361
|
+
},
|
|
362
|
+
];
|
|
363
|
+
function runPathwayStats({ pathwayCase, isEdit, baseline, }) {
|
|
364
|
+
return (0, offers_helpers_1.getOffersWithStats)({
|
|
365
|
+
offers: [makePathwayOffer(pathwayCase.pathway)],
|
|
366
|
+
customer: pathwayCase.customer ?? { total_orders: 1, loyalty_points: 0 },
|
|
367
|
+
vouchers: (pathwayCase.vouchers ?? []),
|
|
368
|
+
giftCards: [],
|
|
369
|
+
customerOfferRedemptionCounts: makeUsedCounts(1),
|
|
370
|
+
redeemed_offers: { [OFFER_ID]: 1 },
|
|
371
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY,
|
|
372
|
+
currency_symbol: constants_1.CURRENCY_SYMBOL_ENUM.$,
|
|
373
|
+
categories: makeCategories(),
|
|
374
|
+
currentItemsPrice: 2000,
|
|
375
|
+
totalDiscountApplied: 0,
|
|
376
|
+
order_discount_details: null,
|
|
377
|
+
isEdit,
|
|
378
|
+
editedOrderRedeemedOffers: baseline,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
describe('getOffersWithStats — baseline exemption across all pathway gates', () => {
|
|
382
|
+
test.each(PATHWAY_CASES.map((pathwayCase) => [pathwayCase.pathway, pathwayCase]))('%s pathway keeps the edited order claim with a baseline and blocks it on a new order', (_pathway, pathwayCase) => {
|
|
383
|
+
const edited = runPathwayStats({
|
|
384
|
+
pathwayCase,
|
|
385
|
+
isEdit: true,
|
|
386
|
+
baseline: { [OFFER_ID]: 1 },
|
|
387
|
+
});
|
|
388
|
+
expect(edited.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
389
|
+
expect(edited.offersWithStats[0].stats.message).not.toContain('personal limit');
|
|
390
|
+
const fresh = runPathwayStats({ pathwayCase });
|
|
391
|
+
expect(fresh.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
|
|
392
|
+
expect(fresh.offersWithStats[0].stats.message).toContain('personal limit');
|
|
393
|
+
});
|
|
394
|
+
});
|