@managesome/eposmatic-toolkit 0.2.344 → 0.2.345
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.js +150 -7
- package/lib/helpers/utils/offers-helpers.test.d.ts +1 -0
- package/lib/helpers/utils/offers-helpers.test.js +157 -0
- package/lib/tests/offer-apply-helpers.test.d.ts +1 -0
- package/lib/tests/offer-apply-helpers.test.js +110 -0
- package/lib/tests/offers-helpers.test.d.ts +1 -0
- package/lib/tests/offers-helpers.test.js +157 -0
- package/package.json +1 -1
|
@@ -1039,6 +1039,122 @@ function getNetItemsValueAfterItemDiscount({ itemsPrice, totalDiscountApplied, o
|
|
|
1039
1039
|
const itemDiscount = (0, common_1.parseNum)(Math.max(0, safeTotalDiscountApplied - safeOrderLevelDiscount));
|
|
1040
1040
|
return (0, common_1.parseNum)(Math.max(0, safeItemsPrice - itemDiscount));
|
|
1041
1041
|
}
|
|
1042
|
+
function getPositiveAmount(value) {
|
|
1043
|
+
const parsedValue = Number(value);
|
|
1044
|
+
if (!Number.isFinite(parsedValue) || parsedValue <= 0) {
|
|
1045
|
+
return 0;
|
|
1046
|
+
}
|
|
1047
|
+
return (0, common_1.parseNum)(parsedValue);
|
|
1048
|
+
}
|
|
1049
|
+
function getOrderSubtotalFromCategoriesForOfferStats(categories) {
|
|
1050
|
+
if (categories == null)
|
|
1051
|
+
return null;
|
|
1052
|
+
let subtotal = 0;
|
|
1053
|
+
for (let i = 0; i < categories.length; i++) {
|
|
1054
|
+
const items = categories[i].items;
|
|
1055
|
+
for (let j = 0; j < items.length; j++) {
|
|
1056
|
+
subtotal += getPositiveAmount(items[j].subtotal);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
return (0, common_1.parseNum)(subtotal);
|
|
1060
|
+
}
|
|
1061
|
+
function getOrderLevelDiscountBaseAmountForOfferStats({ categories, itemsPrice, totalDiscountApplied, orderDiscountDetails, }) {
|
|
1062
|
+
const subtotalFromCategories = getOrderSubtotalFromCategoriesForOfferStats(categories);
|
|
1063
|
+
if (subtotalFromCategories != null) {
|
|
1064
|
+
return subtotalFromCategories;
|
|
1065
|
+
}
|
|
1066
|
+
return getNetItemsValueAfterItemDiscount({
|
|
1067
|
+
itemsPrice,
|
|
1068
|
+
totalDiscountApplied,
|
|
1069
|
+
orderLevelDiscount: orderDiscountDetails?.total_discount,
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
function getOrderLevelDiscountAmountForOfferStats({ orderDiscountDetails, orderLevelDiscountBaseAmount, }) {
|
|
1073
|
+
return (0, common_1.parseNum)(Math.min(getPositiveAmount(orderDiscountDetails?.total_discount), getPositiveAmount(orderLevelDiscountBaseAmount)));
|
|
1074
|
+
}
|
|
1075
|
+
function getOrderLevelDiscountSlotAmount({ orderLevelDiscountBaseAmount, discountValue, isDiscountPercentage, }) {
|
|
1076
|
+
const safeDiscountValue = getPositiveAmount(discountValue);
|
|
1077
|
+
if (safeDiscountValue <= 0) {
|
|
1078
|
+
return 0;
|
|
1079
|
+
}
|
|
1080
|
+
if (isDiscountPercentage) {
|
|
1081
|
+
return (0, common_1.parseNum)((getPositiveAmount(orderLevelDiscountBaseAmount) * safeDiscountValue) / 100);
|
|
1082
|
+
}
|
|
1083
|
+
return safeDiscountValue;
|
|
1084
|
+
}
|
|
1085
|
+
function getOrderLevelDiscountSlotAmounts({ orderDiscountDetails, orderLevelDiscountBaseAmount, }) {
|
|
1086
|
+
const discountSlotAmounts = {
|
|
1087
|
+
special: 0,
|
|
1088
|
+
loyalty: 0,
|
|
1089
|
+
voucher: 0,
|
|
1090
|
+
};
|
|
1091
|
+
if (orderDiscountDetails == null) {
|
|
1092
|
+
return discountSlotAmounts;
|
|
1093
|
+
}
|
|
1094
|
+
const safeOrderLevelDiscountBaseAmount = getPositiveAmount(orderLevelDiscountBaseAmount);
|
|
1095
|
+
let remainingDiscountableAmount = safeOrderLevelDiscountBaseAmount;
|
|
1096
|
+
const applyDiscountSlot = ({ slot, discountId, discountValue, isDiscountPercentage, }) => {
|
|
1097
|
+
if (!discountId || !discountValue) {
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
const discountSlotAmount = getOrderLevelDiscountSlotAmount({
|
|
1101
|
+
orderLevelDiscountBaseAmount: safeOrderLevelDiscountBaseAmount,
|
|
1102
|
+
discountValue,
|
|
1103
|
+
isDiscountPercentage,
|
|
1104
|
+
});
|
|
1105
|
+
if (discountSlotAmount <= 0 || discountSlotAmount > remainingDiscountableAmount) {
|
|
1106
|
+
return;
|
|
1107
|
+
}
|
|
1108
|
+
discountSlotAmounts[slot] = discountSlotAmount;
|
|
1109
|
+
remainingDiscountableAmount = (0, common_1.parseNum)(Math.max(0, remainingDiscountableAmount - discountSlotAmount));
|
|
1110
|
+
};
|
|
1111
|
+
applyDiscountSlot({
|
|
1112
|
+
slot: 'special',
|
|
1113
|
+
discountId: orderDiscountDetails.special_discount_id,
|
|
1114
|
+
discountValue: orderDiscountDetails.special_discount,
|
|
1115
|
+
isDiscountPercentage: orderDiscountDetails.is_special_discount_percentage,
|
|
1116
|
+
});
|
|
1117
|
+
applyDiscountSlot({
|
|
1118
|
+
slot: 'loyalty',
|
|
1119
|
+
discountId: orderDiscountDetails.loyalty_discount_id,
|
|
1120
|
+
discountValue: orderDiscountDetails.loyalty_discount,
|
|
1121
|
+
isDiscountPercentage: orderDiscountDetails.is_loyalty_discount_percentage,
|
|
1122
|
+
});
|
|
1123
|
+
applyDiscountSlot({
|
|
1124
|
+
slot: 'voucher',
|
|
1125
|
+
discountId: orderDiscountDetails.voucher_discount_id,
|
|
1126
|
+
discountValue: orderDiscountDetails.voucher_discount,
|
|
1127
|
+
isDiscountPercentage: orderDiscountDetails.is_voucher_discount_percentage,
|
|
1128
|
+
});
|
|
1129
|
+
return discountSlotAmounts;
|
|
1130
|
+
}
|
|
1131
|
+
function getOfferOrderLevelDiscountSlot(offer) {
|
|
1132
|
+
return offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER ? 'voucher' : 'loyalty';
|
|
1133
|
+
}
|
|
1134
|
+
function getOwnOfferOrderLevelDiscountAmount({ offer, orderDiscountDetails, orderLevelDiscountBaseAmount, orderLevelDiscountAmount, }) {
|
|
1135
|
+
if (orderDiscountDetails == null || orderLevelDiscountAmount <= 0) {
|
|
1136
|
+
return 0;
|
|
1137
|
+
}
|
|
1138
|
+
const discountSlot = getOfferOrderLevelDiscountSlot(offer);
|
|
1139
|
+
const appliedOfferId = discountSlot === 'voucher' ? orderDiscountDetails.voucher_discount_id : orderDiscountDetails.loyalty_discount_id;
|
|
1140
|
+
if (appliedOfferId !== offer._id) {
|
|
1141
|
+
return 0;
|
|
1142
|
+
}
|
|
1143
|
+
const discountSlotAmounts = getOrderLevelDiscountSlotAmounts({
|
|
1144
|
+
orderDiscountDetails,
|
|
1145
|
+
orderLevelDiscountBaseAmount,
|
|
1146
|
+
});
|
|
1147
|
+
return (0, common_1.parseNum)(Math.min(orderLevelDiscountAmount, discountSlotAmounts[discountSlot]));
|
|
1148
|
+
}
|
|
1149
|
+
function getOfferQualifyingOrderValue({ offer, orderLevelDiscountBaseAmount, orderLevelDiscountAmount, orderDiscountDetails, }) {
|
|
1150
|
+
const ownOrderLevelDiscountAmount = getOwnOfferOrderLevelDiscountAmount({
|
|
1151
|
+
offer,
|
|
1152
|
+
orderDiscountDetails,
|
|
1153
|
+
orderLevelDiscountBaseAmount,
|
|
1154
|
+
orderLevelDiscountAmount,
|
|
1155
|
+
});
|
|
1156
|
+
return (0, common_1.parseNum)(Math.max(0, orderLevelDiscountBaseAmount - orderLevelDiscountAmount + ownOrderLevelDiscountAmount));
|
|
1157
|
+
}
|
|
1042
1158
|
function getMaxApplicableWalletCashback({ cashbackBalance, itemsPrice, totalDiscountApplied, orderLevelDiscount, }) {
|
|
1043
1159
|
const safeCashbackBalance = Number.isFinite(cashbackBalance) ? Math.max(0, Number(cashbackBalance)) : 0;
|
|
1044
1160
|
const netItemsValue = getNetItemsValueAfterItemDiscount({
|
|
@@ -1394,9 +1510,16 @@ function getOffersWithStatsHashKey(offersWithStats, totalClaimableOffers) {
|
|
|
1394
1510
|
}
|
|
1395
1511
|
function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOfferRedemptionCounts, redeemed_offers, platform, currency_symbol, currentItemsPrice, totalDiscountApplied, categories, order_discount_details, }) {
|
|
1396
1512
|
const offersWithStats = [];
|
|
1397
|
-
const
|
|
1398
|
-
|
|
1399
|
-
|
|
1513
|
+
const orderLevelDiscountBaseAmount = getOrderLevelDiscountBaseAmountForOfferStats({
|
|
1514
|
+
categories,
|
|
1515
|
+
itemsPrice: currentItemsPrice,
|
|
1516
|
+
totalDiscountApplied,
|
|
1517
|
+
orderDiscountDetails: order_discount_details,
|
|
1518
|
+
});
|
|
1519
|
+
const orderLevelDiscountAmount = getOrderLevelDiscountAmountForOfferStats({
|
|
1520
|
+
orderDiscountDetails: order_discount_details,
|
|
1521
|
+
orderLevelDiscountBaseAmount,
|
|
1522
|
+
});
|
|
1400
1523
|
const unlockedOffersApplicableTimes = {};
|
|
1401
1524
|
let totalClaimableOffers = [];
|
|
1402
1525
|
let totalLoyaltyPointsOfCustomer = null;
|
|
@@ -1728,7 +1851,12 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
1728
1851
|
offersWithStats.push({ offer, stats });
|
|
1729
1852
|
continue;
|
|
1730
1853
|
}
|
|
1731
|
-
const orderValue =
|
|
1854
|
+
const orderValue = getOfferQualifyingOrderValue({
|
|
1855
|
+
offer,
|
|
1856
|
+
orderLevelDiscountBaseAmount,
|
|
1857
|
+
orderLevelDiscountAmount,
|
|
1858
|
+
orderDiscountDetails: order_discount_details,
|
|
1859
|
+
});
|
|
1732
1860
|
// Need to check if offer is unlocked
|
|
1733
1861
|
const { scope = constants_1.PATHWAY_TO_UNLOCK_TYPE_ORDERS_SCOPE_ENUM.OVERALL, min_num_of_orders, max_num_of_orders, total_amount, max_amount, orders_between_unlocks, } = offer.pathway_to_unlock_type_orders;
|
|
1734
1862
|
const scopedStats = getOrdersScopedStats({
|
|
@@ -2024,7 +2152,12 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
2024
2152
|
offersWithStats.push({ offer, stats });
|
|
2025
2153
|
continue;
|
|
2026
2154
|
}
|
|
2027
|
-
const orderValue =
|
|
2155
|
+
const orderValue = getOfferQualifyingOrderValue({
|
|
2156
|
+
offer,
|
|
2157
|
+
orderLevelDiscountBaseAmount,
|
|
2158
|
+
orderLevelDiscountAmount,
|
|
2159
|
+
orderDiscountDetails: order_discount_details,
|
|
2160
|
+
});
|
|
2028
2161
|
const { num_of_items, max_claims, min_same_item_quantity, max_same_item_quantity, total_amount, excluded_refs, exclusive_refs, item_ids, category_ids, } = offer.pathway_to_unlock_type_items;
|
|
2029
2162
|
const bachelorItems = getCartItemsApplicableToOfferItemsQuantity({
|
|
2030
2163
|
categories,
|
|
@@ -2276,7 +2409,12 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
2276
2409
|
offersWithStats.push({ offer, stats });
|
|
2277
2410
|
continue;
|
|
2278
2411
|
}
|
|
2279
|
-
const orderValue =
|
|
2412
|
+
const orderValue = getOfferQualifyingOrderValue({
|
|
2413
|
+
offer,
|
|
2414
|
+
orderLevelDiscountBaseAmount,
|
|
2415
|
+
orderLevelDiscountAmount,
|
|
2416
|
+
orderDiscountDetails: order_discount_details,
|
|
2417
|
+
});
|
|
2280
2418
|
const { num_of_points, total_amount, max_claims } = offer.pathway_to_unlock_type_points;
|
|
2281
2419
|
const maxClaimsPerOrder = max_claims == null ? Infinity : Math.max(1, max_claims);
|
|
2282
2420
|
if (totalLoyaltyPointsOfCustomer == null) {
|
|
@@ -2533,7 +2671,12 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
2533
2671
|
});
|
|
2534
2672
|
continue;
|
|
2535
2673
|
}
|
|
2536
|
-
const orderValue =
|
|
2674
|
+
const orderValue = getOfferQualifyingOrderValue({
|
|
2675
|
+
offer,
|
|
2676
|
+
orderLevelDiscountBaseAmount,
|
|
2677
|
+
orderLevelDiscountAmount,
|
|
2678
|
+
orderDiscountDetails: order_discount_details,
|
|
2679
|
+
});
|
|
2537
2680
|
const { total_amount, max_amount, max_claims, voucher_ids } = offer.pathway_to_unlock_type_vouchers;
|
|
2538
2681
|
const voucherReferenceNumber = order_discount_details?.voucher_reference_number;
|
|
2539
2682
|
const normalizedVoucherRef = voucherReferenceNumber?.trim().toUpperCase();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../../constants");
|
|
4
|
+
const offers_helpers_1 = require("./offers-helpers");
|
|
5
|
+
const OFFER_ID = 'offer-orders-discount';
|
|
6
|
+
function makeCategories(subtotal) {
|
|
7
|
+
return [
|
|
8
|
+
{
|
|
9
|
+
category_id: 'category-pizza',
|
|
10
|
+
items: [
|
|
11
|
+
{
|
|
12
|
+
_id: 'cart-item-1',
|
|
13
|
+
item_id: 'item-classic',
|
|
14
|
+
name: 'Classic',
|
|
15
|
+
quantity: 1,
|
|
16
|
+
subtotal,
|
|
17
|
+
modifiers: [],
|
|
18
|
+
item_discount_details: null,
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
function makeOrdersDiscountOffer(overrides = {}) {
|
|
25
|
+
return {
|
|
26
|
+
_id: OFFER_ID,
|
|
27
|
+
name: 'Spend threshold discount',
|
|
28
|
+
active: true,
|
|
29
|
+
auto_apply: false,
|
|
30
|
+
sort_id: 1,
|
|
31
|
+
included_platforms: [constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY],
|
|
32
|
+
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS,
|
|
33
|
+
pathway_to_unlock_type_orders: {
|
|
34
|
+
scope: constants_1.PATHWAY_TO_UNLOCK_TYPE_ORDERS_SCOPE_ENUM.OVERALL,
|
|
35
|
+
min_num_of_orders: 0,
|
|
36
|
+
max_num_of_orders: null,
|
|
37
|
+
total_amount: 1000,
|
|
38
|
+
max_amount: null,
|
|
39
|
+
orders_between_unlocks: null,
|
|
40
|
+
max_claims: null,
|
|
41
|
+
max_uses_per_customer: null,
|
|
42
|
+
},
|
|
43
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT,
|
|
44
|
+
reward_discount: {
|
|
45
|
+
discount_value: 20,
|
|
46
|
+
is_discount_percentage: true,
|
|
47
|
+
max_discount_amount: null,
|
|
48
|
+
max_discount_percentage: null,
|
|
49
|
+
excluded_refs: [],
|
|
50
|
+
},
|
|
51
|
+
...overrides,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function getSingleOfferStats(options = {}) {
|
|
55
|
+
const offer = options.offer ?? makeOrdersDiscountOffer();
|
|
56
|
+
const categories = Object.prototype.hasOwnProperty.call(options, 'categories')
|
|
57
|
+
? options.categories
|
|
58
|
+
: makeCategories(1000);
|
|
59
|
+
const orderDiscountDetails = options.orderDiscountDetails ?? null;
|
|
60
|
+
const redeemedOffers = options.redeemedOffers ?? null;
|
|
61
|
+
const currentItemsPrice = options.currentItemsPrice ?? 1000;
|
|
62
|
+
const totalDiscountApplied = options.totalDiscountApplied ?? 0;
|
|
63
|
+
return (0, offers_helpers_1.getOffersWithStats)({
|
|
64
|
+
offers: [offer],
|
|
65
|
+
customer: {
|
|
66
|
+
total_orders: 1,
|
|
67
|
+
},
|
|
68
|
+
vouchers: undefined,
|
|
69
|
+
giftCards: undefined,
|
|
70
|
+
customerOfferRedemptionCounts: undefined,
|
|
71
|
+
redeemed_offers: redeemedOffers,
|
|
72
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY,
|
|
73
|
+
currency_symbol: constants_1.CURRENCY_SYMBOL_ENUM.$,
|
|
74
|
+
categories,
|
|
75
|
+
currentItemsPrice,
|
|
76
|
+
totalDiscountApplied,
|
|
77
|
+
order_discount_details: orderDiscountDetails,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
describe('getOffersWithStats spend qualification', () => {
|
|
81
|
+
it('uses item-net subtotal instead of original item price', () => {
|
|
82
|
+
const result = getSingleOfferStats({
|
|
83
|
+
categories: makeCategories(500),
|
|
84
|
+
currentItemsPrice: 1000,
|
|
85
|
+
totalDiscountApplied: 500,
|
|
86
|
+
});
|
|
87
|
+
const stats = result.offersWithStats[0].stats;
|
|
88
|
+
expect(stats.claimable).toBe(false);
|
|
89
|
+
expect(stats.progress).toBe(50);
|
|
90
|
+
expect(stats.message).toContain(`${constants_1.CURRENCY_SYMBOL_ENUM.$}500`);
|
|
91
|
+
expect(result.totalClaimableOffers).not.toContain(OFFER_ID);
|
|
92
|
+
});
|
|
93
|
+
it('uses item-discount fallback pricing when categories are not available', () => {
|
|
94
|
+
const result = getSingleOfferStats({
|
|
95
|
+
categories: undefined,
|
|
96
|
+
currentItemsPrice: 1000,
|
|
97
|
+
totalDiscountApplied: 500,
|
|
98
|
+
});
|
|
99
|
+
const stats = result.offersWithStats[0].stats;
|
|
100
|
+
expect(stats.claimable).toBe(false);
|
|
101
|
+
expect(stats.progress).toBe(50);
|
|
102
|
+
expect(result.totalClaimableOffers).not.toContain(OFFER_ID);
|
|
103
|
+
});
|
|
104
|
+
it('does not gross up an unclaimed orders discount reward threshold', () => {
|
|
105
|
+
const result = getSingleOfferStats({
|
|
106
|
+
categories: makeCategories(1000),
|
|
107
|
+
currentItemsPrice: 1000,
|
|
108
|
+
totalDiscountApplied: 0,
|
|
109
|
+
});
|
|
110
|
+
const stats = result.offersWithStats[0].stats;
|
|
111
|
+
expect(stats.claimable).toBe(true);
|
|
112
|
+
expect(stats.progress).toBe(100);
|
|
113
|
+
expect(result.totalClaimableOffers).toContain(OFFER_ID);
|
|
114
|
+
});
|
|
115
|
+
it('adds back the applied offer discount when re-checking its own orders threshold', () => {
|
|
116
|
+
const result = getSingleOfferStats({
|
|
117
|
+
categories: makeCategories(1000),
|
|
118
|
+
currentItemsPrice: 1000,
|
|
119
|
+
totalDiscountApplied: 200,
|
|
120
|
+
redeemedOffers: {
|
|
121
|
+
[OFFER_ID]: 1,
|
|
122
|
+
},
|
|
123
|
+
orderDiscountDetails: {
|
|
124
|
+
loyalty_discount_id: OFFER_ID,
|
|
125
|
+
loyalty_discount_name: 'Spend threshold discount',
|
|
126
|
+
loyalty_discount: 20,
|
|
127
|
+
is_loyalty_discount_percentage: true,
|
|
128
|
+
total_discount: 200,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
const stats = result.offersWithStats[0].stats;
|
|
132
|
+
expect(stats.progress).toBe(100);
|
|
133
|
+
expect(stats.claimable).toBe(false);
|
|
134
|
+
expect(stats.redeemed_times).toBe(1);
|
|
135
|
+
expect(stats.message).toBe('This offer is redeemed ✅');
|
|
136
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
137
|
+
});
|
|
138
|
+
it('counts unrelated order-level discounts against the orders threshold', () => {
|
|
139
|
+
const result = getSingleOfferStats({
|
|
140
|
+
categories: makeCategories(1000),
|
|
141
|
+
currentItemsPrice: 1000,
|
|
142
|
+
totalDiscountApplied: 200,
|
|
143
|
+
orderDiscountDetails: {
|
|
144
|
+
special_discount_id: 'manual-discount',
|
|
145
|
+
special_discount_name: 'Manual discount',
|
|
146
|
+
special_discount: 200,
|
|
147
|
+
is_special_discount_percentage: false,
|
|
148
|
+
total_discount: 200,
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
const stats = result.offersWithStats[0].stats;
|
|
152
|
+
expect(stats.claimable).toBe(false);
|
|
153
|
+
expect(stats.progress).toBe(80);
|
|
154
|
+
expect(stats.message).toContain(`${constants_1.CURRENCY_SYMBOL_ENUM.$}200`);
|
|
155
|
+
expect(result.totalClaimableOffers).not.toContain(OFFER_ID);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const cart_actions_1 = require("../helpers/store/cart-actions");
|
|
4
|
+
const offer_apply_helpers_1 = require("../helpers/utils/offer-apply-helpers");
|
|
5
|
+
const OFFER_ID = 'offer-reward-threshold';
|
|
6
|
+
const POST_ORDER_REWARD_TYPES = ['POINTS', 'CASHBACK', 'VOUCHER', 'GIFT_CARDS'];
|
|
7
|
+
function makeCategories({ itemDiscountDetails = null, quantity = 1, subtotal = 699, } = {}) {
|
|
8
|
+
return [
|
|
9
|
+
{
|
|
10
|
+
category_id: 'category-pizza',
|
|
11
|
+
items: [
|
|
12
|
+
{
|
|
13
|
+
_id: 'cart-item-1',
|
|
14
|
+
item_id: 'item-classic-euro',
|
|
15
|
+
name: 'Classic Euro',
|
|
16
|
+
quantity,
|
|
17
|
+
subtotal,
|
|
18
|
+
modifiers: [],
|
|
19
|
+
item_discount_details: itemDiscountDetails,
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
},
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
function makeOrder(overrides = {}) {
|
|
26
|
+
return {
|
|
27
|
+
categories: makeCategories(),
|
|
28
|
+
order_discount_details: null,
|
|
29
|
+
redeemed_offers: {
|
|
30
|
+
[OFFER_ID]: 1,
|
|
31
|
+
},
|
|
32
|
+
wallet_redemptions: null,
|
|
33
|
+
platform: 'WEB_TAKE-AWAY',
|
|
34
|
+
...overrides,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function makeOffer(rewardType, overrides = {}) {
|
|
38
|
+
const offer = {
|
|
39
|
+
_id: OFFER_ID,
|
|
40
|
+
name: `${rewardType} threshold reward`,
|
|
41
|
+
active: true,
|
|
42
|
+
reward_type: rewardType,
|
|
43
|
+
pathway_to_unlock_type: 'ORDERS',
|
|
44
|
+
pathway_to_unlock_type_orders: {
|
|
45
|
+
total_amount: 1000,
|
|
46
|
+
max_claims: null,
|
|
47
|
+
max_uses_per_customer: null,
|
|
48
|
+
},
|
|
49
|
+
...overrides,
|
|
50
|
+
};
|
|
51
|
+
if (rewardType === 'POINTS') {
|
|
52
|
+
offer.reward_points = { points: 100, once_per_order: true };
|
|
53
|
+
}
|
|
54
|
+
if (rewardType === 'CASHBACK') {
|
|
55
|
+
offer.reward_cashback = { cashback_value: 10, is_cashback_percentage: false };
|
|
56
|
+
}
|
|
57
|
+
if (rewardType === 'VOUCHER') {
|
|
58
|
+
offer.reward_voucher = { voucher_ids: { 'voucher-april28': true } };
|
|
59
|
+
}
|
|
60
|
+
if (rewardType === 'GIFT_CARDS') {
|
|
61
|
+
offer.reward_gift_cards = { gift_card_ids: { 'gift-card-reward': true } };
|
|
62
|
+
}
|
|
63
|
+
if (rewardType === 'ITEMS') {
|
|
64
|
+
offer.reward_items = {
|
|
65
|
+
num_of_items: 1,
|
|
66
|
+
item_ids: ['item-classic-euro'],
|
|
67
|
+
excluded_refs: [],
|
|
68
|
+
exclusive_refs: [],
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return offer;
|
|
72
|
+
}
|
|
73
|
+
describe('offer redeemed_offers sync', () => {
|
|
74
|
+
test.each(POST_ORDER_REWARD_TYPES)('getSyncOffersWithCartData clears stale %s reward claims when unlock context reports zero availability', (rewardType) => {
|
|
75
|
+
const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder(), [], {}, {}, [makeOffer(rewardType)]);
|
|
76
|
+
expect(result.redeemed_offers?.[OFFER_ID]).toBeUndefined();
|
|
77
|
+
});
|
|
78
|
+
test.each(POST_ORDER_REWARD_TYPES)('calculateCartOrderInfoAndSyncData clears stale %s reward claims when unlock context reports zero availability', (rewardType) => {
|
|
79
|
+
const result = (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(makeOrder(), [], {}, undefined, {
|
|
80
|
+
unlockedOffersApplicableTimes: {},
|
|
81
|
+
offers: [makeOffer(rewardType)],
|
|
82
|
+
});
|
|
83
|
+
expect(result.redeemed_offers?.[OFFER_ID]).toBeUndefined();
|
|
84
|
+
});
|
|
85
|
+
it('getSyncOffersWithCartData preserves selected item reward evidence while the item reward remains unlocked', () => {
|
|
86
|
+
const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder({
|
|
87
|
+
categories: makeCategories({
|
|
88
|
+
itemDiscountDetails: {
|
|
89
|
+
loyalty_discount_id: OFFER_ID,
|
|
90
|
+
},
|
|
91
|
+
}),
|
|
92
|
+
redeemed_offers: null,
|
|
93
|
+
}), [], {}, { [OFFER_ID]: 1 }, [makeOffer('ITEMS')]);
|
|
94
|
+
expect(result.redeemed_offers?.[OFFER_ID]).toBe(1);
|
|
95
|
+
});
|
|
96
|
+
it('calculateCartOrderInfoAndSyncData preserves selected item reward evidence while the item reward remains unlocked', () => {
|
|
97
|
+
const result = (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(makeOrder({
|
|
98
|
+
categories: makeCategories({
|
|
99
|
+
itemDiscountDetails: {
|
|
100
|
+
loyalty_discount_id: OFFER_ID,
|
|
101
|
+
},
|
|
102
|
+
}),
|
|
103
|
+
redeemed_offers: null,
|
|
104
|
+
}), [], {}, undefined, {
|
|
105
|
+
unlockedOffersApplicableTimes: { [OFFER_ID]: 1 },
|
|
106
|
+
offers: [makeOffer('ITEMS')],
|
|
107
|
+
});
|
|
108
|
+
expect(result.redeemed_offers?.[OFFER_ID]).toBe(1);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,157 @@
|
|
|
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-orders-discount';
|
|
6
|
+
function makeCategories(subtotal) {
|
|
7
|
+
return [
|
|
8
|
+
{
|
|
9
|
+
category_id: 'category-pizza',
|
|
10
|
+
items: [
|
|
11
|
+
{
|
|
12
|
+
_id: 'cart-item-1',
|
|
13
|
+
item_id: 'item-classic',
|
|
14
|
+
name: 'Classic',
|
|
15
|
+
quantity: 1,
|
|
16
|
+
subtotal,
|
|
17
|
+
modifiers: [],
|
|
18
|
+
item_discount_details: null,
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
function makeOrdersDiscountOffer(overrides = {}) {
|
|
25
|
+
return {
|
|
26
|
+
_id: OFFER_ID,
|
|
27
|
+
name: 'Spend threshold discount',
|
|
28
|
+
active: true,
|
|
29
|
+
auto_apply: false,
|
|
30
|
+
sort_id: 1,
|
|
31
|
+
included_platforms: [constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY],
|
|
32
|
+
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS,
|
|
33
|
+
pathway_to_unlock_type_orders: {
|
|
34
|
+
scope: constants_1.PATHWAY_TO_UNLOCK_TYPE_ORDERS_SCOPE_ENUM.OVERALL,
|
|
35
|
+
min_num_of_orders: 0,
|
|
36
|
+
max_num_of_orders: null,
|
|
37
|
+
total_amount: 1000,
|
|
38
|
+
max_amount: null,
|
|
39
|
+
orders_between_unlocks: null,
|
|
40
|
+
max_claims: null,
|
|
41
|
+
max_uses_per_customer: null,
|
|
42
|
+
},
|
|
43
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT,
|
|
44
|
+
reward_discount: {
|
|
45
|
+
discount_value: 20,
|
|
46
|
+
is_discount_percentage: true,
|
|
47
|
+
max_discount_amount: null,
|
|
48
|
+
max_discount_percentage: null,
|
|
49
|
+
excluded_refs: [],
|
|
50
|
+
},
|
|
51
|
+
...overrides,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function getSingleOfferStats(options = {}) {
|
|
55
|
+
const offer = options.offer ?? makeOrdersDiscountOffer();
|
|
56
|
+
const categories = Object.prototype.hasOwnProperty.call(options, 'categories')
|
|
57
|
+
? options.categories
|
|
58
|
+
: makeCategories(1000);
|
|
59
|
+
const orderDiscountDetails = options.orderDiscountDetails ?? null;
|
|
60
|
+
const redeemedOffers = options.redeemedOffers ?? null;
|
|
61
|
+
const currentItemsPrice = options.currentItemsPrice ?? 1000;
|
|
62
|
+
const totalDiscountApplied = options.totalDiscountApplied ?? 0;
|
|
63
|
+
return (0, offers_helpers_1.getOffersWithStats)({
|
|
64
|
+
offers: [offer],
|
|
65
|
+
customer: {
|
|
66
|
+
total_orders: 1,
|
|
67
|
+
},
|
|
68
|
+
vouchers: undefined,
|
|
69
|
+
giftCards: undefined,
|
|
70
|
+
customerOfferRedemptionCounts: undefined,
|
|
71
|
+
redeemed_offers: redeemedOffers,
|
|
72
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY,
|
|
73
|
+
currency_symbol: constants_1.CURRENCY_SYMBOL_ENUM.$,
|
|
74
|
+
categories,
|
|
75
|
+
currentItemsPrice,
|
|
76
|
+
totalDiscountApplied,
|
|
77
|
+
order_discount_details: orderDiscountDetails,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
describe('getOffersWithStats spend qualification', () => {
|
|
81
|
+
it('uses item-net subtotal instead of original item price', () => {
|
|
82
|
+
const result = getSingleOfferStats({
|
|
83
|
+
categories: makeCategories(500),
|
|
84
|
+
currentItemsPrice: 1000,
|
|
85
|
+
totalDiscountApplied: 500,
|
|
86
|
+
});
|
|
87
|
+
const stats = result.offersWithStats[0].stats;
|
|
88
|
+
expect(stats.claimable).toBe(false);
|
|
89
|
+
expect(stats.progress).toBe(50);
|
|
90
|
+
expect(stats.message).toContain(`${constants_1.CURRENCY_SYMBOL_ENUM.$}500`);
|
|
91
|
+
expect(result.totalClaimableOffers).not.toContain(OFFER_ID);
|
|
92
|
+
});
|
|
93
|
+
it('uses item-discount fallback pricing when categories are not available', () => {
|
|
94
|
+
const result = getSingleOfferStats({
|
|
95
|
+
categories: undefined,
|
|
96
|
+
currentItemsPrice: 1000,
|
|
97
|
+
totalDiscountApplied: 500,
|
|
98
|
+
});
|
|
99
|
+
const stats = result.offersWithStats[0].stats;
|
|
100
|
+
expect(stats.claimable).toBe(false);
|
|
101
|
+
expect(stats.progress).toBe(50);
|
|
102
|
+
expect(result.totalClaimableOffers).not.toContain(OFFER_ID);
|
|
103
|
+
});
|
|
104
|
+
it('does not gross up an unclaimed orders discount reward threshold', () => {
|
|
105
|
+
const result = getSingleOfferStats({
|
|
106
|
+
categories: makeCategories(1000),
|
|
107
|
+
currentItemsPrice: 1000,
|
|
108
|
+
totalDiscountApplied: 0,
|
|
109
|
+
});
|
|
110
|
+
const stats = result.offersWithStats[0].stats;
|
|
111
|
+
expect(stats.claimable).toBe(true);
|
|
112
|
+
expect(stats.progress).toBe(100);
|
|
113
|
+
expect(result.totalClaimableOffers).toContain(OFFER_ID);
|
|
114
|
+
});
|
|
115
|
+
it('adds back the applied offer discount when re-checking its own orders threshold', () => {
|
|
116
|
+
const result = getSingleOfferStats({
|
|
117
|
+
categories: makeCategories(1000),
|
|
118
|
+
currentItemsPrice: 1000,
|
|
119
|
+
totalDiscountApplied: 200,
|
|
120
|
+
redeemedOffers: {
|
|
121
|
+
[OFFER_ID]: 1,
|
|
122
|
+
},
|
|
123
|
+
orderDiscountDetails: {
|
|
124
|
+
loyalty_discount_id: OFFER_ID,
|
|
125
|
+
loyalty_discount_name: 'Spend threshold discount',
|
|
126
|
+
loyalty_discount: 20,
|
|
127
|
+
is_loyalty_discount_percentage: true,
|
|
128
|
+
total_discount: 200,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
const stats = result.offersWithStats[0].stats;
|
|
132
|
+
expect(stats.progress).toBe(100);
|
|
133
|
+
expect(stats.claimable).toBe(false);
|
|
134
|
+
expect(stats.redeemed_times).toBe(1);
|
|
135
|
+
expect(stats.message).toContain('redeemed');
|
|
136
|
+
expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
|
|
137
|
+
});
|
|
138
|
+
it('counts unrelated order-level discounts against the orders threshold', () => {
|
|
139
|
+
const result = getSingleOfferStats({
|
|
140
|
+
categories: makeCategories(1000),
|
|
141
|
+
currentItemsPrice: 1000,
|
|
142
|
+
totalDiscountApplied: 200,
|
|
143
|
+
orderDiscountDetails: {
|
|
144
|
+
special_discount_id: 'manual-discount',
|
|
145
|
+
special_discount_name: 'Manual discount',
|
|
146
|
+
special_discount: 200,
|
|
147
|
+
is_special_discount_percentage: false,
|
|
148
|
+
total_discount: 200,
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
const stats = result.offersWithStats[0].stats;
|
|
152
|
+
expect(stats.claimable).toBe(false);
|
|
153
|
+
expect(stats.progress).toBe(80);
|
|
154
|
+
expect(stats.message).toContain(`${constants_1.CURRENCY_SYMBOL_ENUM.$}200`);
|
|
155
|
+
expect(result.totalClaimableOffers).not.toContain(OFFER_ID);
|
|
156
|
+
});
|
|
157
|
+
});
|