@managesome/eposmatic-toolkit 0.4.37 → 0.4.38
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/esm/constants/accounting-templates.js +431 -0
- package/esm/constants/index.js +1 -0
- package/lib/constants/accounting-templates.d.ts +8 -0
- package/lib/constants/accounting-templates.js +434 -0
- package/lib/constants/index.d.ts +1 -0
- package/lib/constants/index.js +1 -0
- package/lib/schemas/models/expense_categories.d.ts +3 -3
- package/lib/schemas/models/expense_details.d.ts +3 -3
- package/lib/schemas/models/gift_cards.d.ts +3 -3
- package/lib/schemas/models/halfhalf_setup.d.ts +3 -3
- package/lib/schemas/models/internal_invoices.d.ts +3 -3
- package/lib/schemas/models/items.d.ts +12 -12
- package/lib/schemas/models/marketing_fingerprints.d.ts +3 -3
- package/lib/schemas/models/modifier_extras.d.ts +3 -3
- package/lib/schemas/models/offers.d.ts +3 -3
- package/lib/schemas/models/public_holidays.d.ts +3 -3
- package/lib/schemas/models/specials.d.ts +3 -3
- package/lib/schemas/models/tenant_chart_of_accounts.d.ts +3 -3
- package/lib/schemas/models/tenant_hr_departments.d.ts +3 -3
- package/lib/schemas/models/tenant_hr_documents.d.ts +3 -3
- package/lib/schemas/models/tenant_hr_policies.d.ts +3 -3
- package/lib/schemas/models/tenant_hr_positions.d.ts +3 -3
- package/lib/schemas/models/tenant_journal_entries.d.ts +3 -3
- package/lib/schemas/models/void_order_items.d.ts +3 -3
- package/lib/schemas/models/vouchers.d.ts +3 -3
- package/lib/schemas/models/warehouse_communications.d.ts +3 -3
- package/lib/schemas/models/warehouse_items.d.ts +3 -3
- package/lib/schemas/models/warehouse_void_purchase_order_items.d.ts +3 -3
- package/lib/schemas/models/warehouse_void_sale_order_items.d.ts +3 -3
- package/lib/schemas/types/tenant_chart_of_accounts.d.ts +7 -0
- package/lib/tests/cart-helpers-edit-item-refs.test.d.ts +1 -0
- package/lib/tests/cart-helpers-edit-item-refs.test.js +96 -0
- package/lib/tests/code-redemption-helpers.test.d.ts +1 -0
- package/lib/tests/code-redemption-helpers.test.js +205 -0
- package/lib/tests/gift-card-helpers.test.d.ts +1 -0
- package/lib/tests/gift-card-helpers.test.js +100 -0
- package/lib/tests/offer-claim-regressions.test.d.ts +1 -0
- package/lib/tests/offer-claim-regressions.test.js +299 -0
- package/lib/tests/offers-matrix.test.js +53 -14
- package/lib/tests/ref-exclusion-matching.test.d.ts +1 -0
- package/lib/tests/ref-exclusion-matching.test.js +150 -0
- package/lib/tests/wallet-cashback-helpers.test.d.ts +1 -0
- package/lib/tests/wallet-cashback-helpers.test.js +95 -0
- package/package.json +1 -1
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../constants");
|
|
4
|
+
const offer_apply_helpers_1 = require("../helpers/utils/offer-apply-helpers");
|
|
5
|
+
const offers_helpers_1 = require("../helpers/utils/offers-helpers");
|
|
6
|
+
const cart_actions_1 = require("../helpers/store/cart-actions");
|
|
7
|
+
/**
|
|
8
|
+
* Regressions for two defects found in an external audit:
|
|
9
|
+
*
|
|
10
|
+
* F-02 An ORDERS-pathway offer with `total_amount: 0` ("no minimum spend") was
|
|
11
|
+
* granted `floor(cartValue / 1)` claims - thousands of free items.
|
|
12
|
+
* F-01 A FIXED discount with `excluded_refs` was accepted by the builder and then
|
|
13
|
+
* silently deleted, together with its claim, by the cart sync.
|
|
14
|
+
*/
|
|
15
|
+
const categories = () => [
|
|
16
|
+
{
|
|
17
|
+
category_id: 'c',
|
|
18
|
+
name: 'C',
|
|
19
|
+
items: [
|
|
20
|
+
{ _id: 'ci1', item_id: 'i1', name: 'A', quantity: 1, price: 1000, subtotal: 1000, tax_rate: 0, modifiers: [], special_instructions: '', item_discount_details: null },
|
|
21
|
+
{ _id: 'ci2', item_id: 'i2', name: 'B', quantity: 1, price: 500, subtotal: 500, tax_rate: 0, modifiers: [], special_instructions: '', item_discount_details: null },
|
|
22
|
+
],
|
|
23
|
+
},
|
|
24
|
+
];
|
|
25
|
+
function offer(overrides) {
|
|
26
|
+
return {
|
|
27
|
+
_id: 'o',
|
|
28
|
+
name: 'Offer',
|
|
29
|
+
active: true,
|
|
30
|
+
auto_apply: false,
|
|
31
|
+
sort_id: 1,
|
|
32
|
+
included_platforms: [constants_1.PLATFORMS_ENUM.WEB_DELIVERY],
|
|
33
|
+
offer_special_hours: null,
|
|
34
|
+
...overrides,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function ordersPathway(totalAmount) {
|
|
38
|
+
return {
|
|
39
|
+
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS,
|
|
40
|
+
pathway_to_unlock_type_orders: {
|
|
41
|
+
scope: 'OVERALL',
|
|
42
|
+
min_num_of_orders: 0,
|
|
43
|
+
max_num_of_orders: null,
|
|
44
|
+
total_amount: totalAmount,
|
|
45
|
+
max_amount: null,
|
|
46
|
+
orders_between_unlocks: null,
|
|
47
|
+
max_claims: null,
|
|
48
|
+
max_uses_per_customer: null,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function statsFor(offerToCheck, extra = {}) {
|
|
53
|
+
const result = (0, offers_helpers_1.getOffersWithStats)({
|
|
54
|
+
offers: [offerToCheck],
|
|
55
|
+
customer: {
|
|
56
|
+
total_orders: 3,
|
|
57
|
+
loyalty_points: 50,
|
|
58
|
+
total_orders_per_platform_type: { [constants_1.PLATFORMS_ENUM.WEB_DELIVERY]: 3 },
|
|
59
|
+
voucher_codes: { SAVE: 2 },
|
|
60
|
+
...(extra.customer ?? {}),
|
|
61
|
+
},
|
|
62
|
+
vouchers: [
|
|
63
|
+
{ _id: 'v1', code: 'SAVE', active: true, max_uses: null, max_assignments: null, assigned_count: 0, used_count: 0 },
|
|
64
|
+
],
|
|
65
|
+
giftCards: [],
|
|
66
|
+
customerOfferRedemptionCounts: { ASSIGNED: {}, USED: {} },
|
|
67
|
+
redeemed_offers: extra.redeemed ?? null,
|
|
68
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_DELIVERY,
|
|
69
|
+
currency_symbol: constants_1.CURRENCY_SYMBOL_ENUM.Rs,
|
|
70
|
+
currentItemsPrice: 1500,
|
|
71
|
+
totalDiscountApplied: extra.discount ?? 0,
|
|
72
|
+
categories: extra.categories ?? categories(),
|
|
73
|
+
order_discount_details: extra.orderDiscountDetails ?? null,
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
stats: result.offersWithStats[0].stats,
|
|
77
|
+
unlocked: result.unlockedOffersApplicableTimes.o,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const twoFreeItems = {
|
|
81
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
|
|
82
|
+
reward_items: { num_of_items: 2, excluded_refs: [], exclusive_refs: [], item_ids: ['i1', 'i2'], allocation: null },
|
|
83
|
+
};
|
|
84
|
+
describe('F-02: a zero spend threshold must not multiply claims', () => {
|
|
85
|
+
it('grants one claim (not one per currency unit) for an item reward', () => {
|
|
86
|
+
const { stats } = statsFor(offer({ ...ordersPathway(0), ...twoFreeItems }));
|
|
87
|
+
// One claim x 2 items per claim.
|
|
88
|
+
expect(stats.applicable_quantity).toBe(2);
|
|
89
|
+
expect(stats.claimable).toBe(true);
|
|
90
|
+
});
|
|
91
|
+
it('keeps chunking when the threshold is real', () => {
|
|
92
|
+
const { stats } = statsFor(offer({ ...ordersPathway(100), ...twoFreeItems }));
|
|
93
|
+
// floor(1500 / 100) = 15 claims x 2 items.
|
|
94
|
+
expect(stats.applicable_quantity).toBe(30);
|
|
95
|
+
});
|
|
96
|
+
it('grants one claim for points and cashback rewards', () => {
|
|
97
|
+
expect(statsFor(offer({
|
|
98
|
+
...ordersPathway(0),
|
|
99
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.POINTS,
|
|
100
|
+
reward_points: { num_of_points: 1, once_per_order: false },
|
|
101
|
+
})).stats.applicable_quantity).toBe(1);
|
|
102
|
+
expect(statsFor(offer({
|
|
103
|
+
...ordersPathway(0),
|
|
104
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK,
|
|
105
|
+
reward_cashback: { cashback_value: 5, is_cashback_percentage: false, max_cashback_amount: null, excluded_refs: [] },
|
|
106
|
+
})).stats.applicable_quantity).toBe(1);
|
|
107
|
+
});
|
|
108
|
+
it('treats a negative or absent threshold as "no minimum spend"', () => {
|
|
109
|
+
expect(statsFor(offer({ ...ordersPathway(-50), ...twoFreeItems })).stats.applicable_quantity).toBe(2);
|
|
110
|
+
expect(statsFor(offer({ ...ordersPathway(null), ...twoFreeItems })).stats.applicable_quantity).toBe(2);
|
|
111
|
+
});
|
|
112
|
+
it('still honours max_claims and the lifetime budget', () => {
|
|
113
|
+
const withMaxClaims = (maxClaims) => offer({
|
|
114
|
+
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS,
|
|
115
|
+
pathway_to_unlock_type_orders: {
|
|
116
|
+
scope: 'OVERALL', min_num_of_orders: 0, max_num_of_orders: null, total_amount: 100,
|
|
117
|
+
max_amount: null, orders_between_unlocks: null, max_claims: maxClaims, max_uses_per_customer: null,
|
|
118
|
+
},
|
|
119
|
+
...twoFreeItems,
|
|
120
|
+
});
|
|
121
|
+
// threshold 100 on a 1500 cart = 15 chunks, capped by max_claims (x2 items per claim)
|
|
122
|
+
expect(statsFor(withMaxClaims(1)).stats.applicable_quantity).toBe(2);
|
|
123
|
+
expect(statsFor(withMaxClaims(3)).stats.applicable_quantity).toBe(6);
|
|
124
|
+
expect(statsFor(withMaxClaims(null)).stats.applicable_quantity).toBe(30);
|
|
125
|
+
});
|
|
126
|
+
it('does not multiply claims when a POINTS reward costs zero points', () => {
|
|
127
|
+
const { stats: statsResult } = statsFor(offer({
|
|
128
|
+
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS,
|
|
129
|
+
pathway_to_unlock_type_points: { num_of_points: 0, total_amount: 0, max_uses_per_customer: null, max_claims: null },
|
|
130
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
|
|
131
|
+
reward_items: { num_of_items: 1, excluded_refs: [], exclusive_refs: [], item_ids: ['i1'], allocation: null },
|
|
132
|
+
}));
|
|
133
|
+
// Was floor(points / 1) = every point the customer holds.
|
|
134
|
+
expect(statsResult.applicable_quantity).toBe(1);
|
|
135
|
+
});
|
|
136
|
+
it('still lets the vouchers held drive a voucher-pathway claim', () => {
|
|
137
|
+
const { stats } = statsFor(offer({
|
|
138
|
+
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER,
|
|
139
|
+
pathway_to_unlock_type_vouchers: {
|
|
140
|
+
total_amount: 0,
|
|
141
|
+
max_amount: null,
|
|
142
|
+
max_uses_per_customer: null,
|
|
143
|
+
max_claims: null,
|
|
144
|
+
voucher_ids: ['v1'],
|
|
145
|
+
},
|
|
146
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
|
|
147
|
+
reward_items: { num_of_items: 1, excluded_refs: [], exclusive_refs: [], item_ids: ['i1'], allocation: null },
|
|
148
|
+
}));
|
|
149
|
+
// Two codes held -> two claims -> two items.
|
|
150
|
+
expect(stats.applicable_quantity).toBe(2);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
describe('F-05: removing a reward item releases its claim', () => {
|
|
154
|
+
const paid = (id) => ({
|
|
155
|
+
_id: id, item_id: 'i1', name: 'A', quantity: 1, price: 1000, subtotal: 1000, tax_rate: 0,
|
|
156
|
+
modifiers: [], special_instructions: '', item_discount_details: null,
|
|
157
|
+
});
|
|
158
|
+
const rewardItem = (id) => ({
|
|
159
|
+
_id: id, item_id: 'i1', name: 'A', quantity: 1, price: 1000, subtotal: 0, tax_rate: 0,
|
|
160
|
+
modifiers: [], special_instructions: '',
|
|
161
|
+
item_discount_details: {
|
|
162
|
+
loyalty_discount: 100, loyalty_discount_id: 'o2', is_loyalty_discount_percentage: true, total_discount: 1000,
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
const cartWith = (items) => ({
|
|
166
|
+
categories: [{ category_id: 'c', name: 'C', items }],
|
|
167
|
+
order_discount_details: null,
|
|
168
|
+
redeemed_offers: { o2: 1 },
|
|
169
|
+
wallet_redemptions: null,
|
|
170
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_DELIVERY,
|
|
171
|
+
price: 1000,
|
|
172
|
+
total_discount_applied: 0,
|
|
173
|
+
});
|
|
174
|
+
it('clears the claim when the last stamped item goes', () => {
|
|
175
|
+
const result = (0, cart_actions_1.getRemoveItemFromCartData)(cartWith([paid('p1'), rewardItem('rw')]), { _id: 'rw' }, [], {});
|
|
176
|
+
expect(result.redeemed_offers ?? null).toBeNull();
|
|
177
|
+
});
|
|
178
|
+
it('keeps the claim while another reward item is still stamped', () => {
|
|
179
|
+
const result = (0, cart_actions_1.getRemoveItemFromCartData)(cartWith([paid('p1'), rewardItem('rw'), rewardItem('rw2')]), { _id: 'rw' }, [], {});
|
|
180
|
+
expect(result.redeemed_offers).toEqual({ o2: 1 });
|
|
181
|
+
});
|
|
182
|
+
it('never touches the claim when a non-reward item is removed', () => {
|
|
183
|
+
const result = (0, cart_actions_1.getRemoveItemFromCartData)(cartWith([paid('p1'), paid('p2'), rewardItem('rw')]), { _id: 'p2' }, [], {});
|
|
184
|
+
expect(result.redeemed_offers).toEqual({ o2: 1 });
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
describe('F-01 (voucher slot): the same rule must hold there', () => {
|
|
188
|
+
const voucherOffer = (excludedRefs, discountValue = 10) => offer({
|
|
189
|
+
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER,
|
|
190
|
+
pathway_to_unlock_type_vouchers: {
|
|
191
|
+
total_amount: 0, max_amount: null, max_uses_per_customer: null, max_claims: null, voucher_ids: ['v1'],
|
|
192
|
+
},
|
|
193
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT,
|
|
194
|
+
reward_discount: {
|
|
195
|
+
discount_value: discountValue,
|
|
196
|
+
is_discount_percentage: false,
|
|
197
|
+
max_discount_amount: null,
|
|
198
|
+
max_discount_percentage: null,
|
|
199
|
+
excluded_refs: excludedRefs,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
const applyAndSync = (offerToCheck) => {
|
|
203
|
+
const built = (0, offer_apply_helpers_1.buildOfferDiscountUpdate)({
|
|
204
|
+
offer: offerToCheck,
|
|
205
|
+
orderDiscountDetails: null,
|
|
206
|
+
categories: categories(),
|
|
207
|
+
});
|
|
208
|
+
const withReference = { ...built.discountDetails, voucher_reference_number: 'SAVE' };
|
|
209
|
+
const applied = (0, cart_actions_1.calculateCartOrderInfoAndSyncData)({
|
|
210
|
+
categories: categories(),
|
|
211
|
+
order_discount_details: withReference,
|
|
212
|
+
redeemed_offers: { o: 1 },
|
|
213
|
+
wallet_redemptions: null,
|
|
214
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_DELIVERY,
|
|
215
|
+
price: 1500,
|
|
216
|
+
total_discount_applied: 0,
|
|
217
|
+
}, [], {});
|
|
218
|
+
const { unlocked } = statsFor(offerToCheck, {
|
|
219
|
+
redeemed: applied.redeemed_offers,
|
|
220
|
+
orderDiscountDetails: applied.order_discount_details,
|
|
221
|
+
categories: applied.categories,
|
|
222
|
+
discount: applied.total_discount_applied,
|
|
223
|
+
});
|
|
224
|
+
return {
|
|
225
|
+
applied,
|
|
226
|
+
synced: (0, offer_apply_helpers_1.getSyncOffersWithCartData)(applied, [], {}, { o: unlocked ?? 0 }, [offerToCheck]),
|
|
227
|
+
};
|
|
228
|
+
};
|
|
229
|
+
it('keeps a fixed discount with exclusions in the voucher slot', () => {
|
|
230
|
+
const { synced } = applyAndSync(voucherOffer(['i2']));
|
|
231
|
+
expect(synced.order_discount_details?.voucher_discount).toBe(10);
|
|
232
|
+
expect(synced.redeemed_offers).toEqual({ o: 1 });
|
|
233
|
+
});
|
|
234
|
+
it('clamps it to the eligible subtotal', () => {
|
|
235
|
+
const { synced } = applyAndSync(voucherOffer(['i2'], 5000));
|
|
236
|
+
expect(synced.order_discount_details?.voucher_discount).toBe(1000);
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
describe('F-01: a fixed discount with exclusions survives the cart sync', () => {
|
|
240
|
+
const discountOffer = (excludedRefs, discountValue = 10) => offer({
|
|
241
|
+
...ordersPathway(1000),
|
|
242
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT,
|
|
243
|
+
reward_discount: {
|
|
244
|
+
discount_value: discountValue,
|
|
245
|
+
is_discount_percentage: false,
|
|
246
|
+
max_discount_amount: null,
|
|
247
|
+
max_discount_percentage: null,
|
|
248
|
+
excluded_refs: excludedRefs,
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
const applyAndSync = (offerToCheck) => {
|
|
252
|
+
const built = (0, offer_apply_helpers_1.buildOfferDiscountUpdate)({
|
|
253
|
+
offer: offerToCheck,
|
|
254
|
+
orderDiscountDetails: null,
|
|
255
|
+
categories: categories(),
|
|
256
|
+
});
|
|
257
|
+
if (!built.success)
|
|
258
|
+
return { built, synced: null };
|
|
259
|
+
const applied = (0, cart_actions_1.calculateCartOrderInfoAndSyncData)({
|
|
260
|
+
categories: categories(),
|
|
261
|
+
order_discount_details: built.discountDetails,
|
|
262
|
+
redeemed_offers: { o: 1 },
|
|
263
|
+
wallet_redemptions: null,
|
|
264
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_DELIVERY,
|
|
265
|
+
price: 1500,
|
|
266
|
+
total_discount_applied: 0,
|
|
267
|
+
}, [], {});
|
|
268
|
+
const { unlocked } = statsFor(offerToCheck, {
|
|
269
|
+
redeemed: applied.redeemed_offers,
|
|
270
|
+
orderDiscountDetails: applied.order_discount_details,
|
|
271
|
+
categories: applied.categories,
|
|
272
|
+
discount: applied.total_discount_applied,
|
|
273
|
+
});
|
|
274
|
+
return {
|
|
275
|
+
built,
|
|
276
|
+
applied,
|
|
277
|
+
synced: (0, offer_apply_helpers_1.getSyncOffersWithCartData)(applied, [], {}, { o: unlocked ?? 0 }, [offerToCheck]),
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
it('keeps the discount and its claim', () => {
|
|
281
|
+
const { synced } = applyAndSync(discountOffer(['i2']));
|
|
282
|
+
expect(synced.order_discount_details?.loyalty_discount).toBe(10);
|
|
283
|
+
expect(synced.redeemed_offers).toEqual({ o: 1 });
|
|
284
|
+
});
|
|
285
|
+
it('clamps a fixed value larger than the eligible subtotal', () => {
|
|
286
|
+
const { synced } = applyAndSync(discountOffer(['i2'], 5000));
|
|
287
|
+
expect(synced.order_discount_details?.loyalty_discount).toBe(1000);
|
|
288
|
+
expect(synced.redeemed_offers).toEqual({ o: 1 });
|
|
289
|
+
});
|
|
290
|
+
it('still refuses when every item is excluded', () => {
|
|
291
|
+
const { built } = applyAndSync(discountOffer(['i1', 'i2']));
|
|
292
|
+
expect(built.success).toBe(false);
|
|
293
|
+
});
|
|
294
|
+
it('is unchanged without exclusions', () => {
|
|
295
|
+
const { synced } = applyAndSync(discountOffer([]));
|
|
296
|
+
expect(synced.order_discount_details?.loyalty_discount).toBe(10);
|
|
297
|
+
expect(synced.redeemed_offers).toEqual({ o: 1 });
|
|
298
|
+
});
|
|
299
|
+
});
|
|
@@ -38,23 +38,48 @@ function makeCategories() {
|
|
|
38
38
|
function makeVoucher() {
|
|
39
39
|
const now = new Date('2026-01-01T00:00:00.000Z');
|
|
40
40
|
return {
|
|
41
|
-
_id: VOUCHER_ID,
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
_id: VOUCHER_ID,
|
|
42
|
+
code: VOUCHER_CODE,
|
|
43
|
+
brand_id: 'brand_1',
|
|
44
|
+
tenants: ['tenant_1'],
|
|
45
|
+
name: 'SAVE10',
|
|
46
|
+
description: '',
|
|
47
|
+
active: true,
|
|
48
|
+
is_public: true,
|
|
49
|
+
max_uses: null,
|
|
50
|
+
max_uses_per_customer: null,
|
|
51
|
+
max_assignments: null,
|
|
52
|
+
max_assignments_per_customer: null,
|
|
53
|
+
assigned_count: 0,
|
|
54
|
+
last_assigned_at: null,
|
|
55
|
+
used_count: 0,
|
|
56
|
+
last_used_at: null,
|
|
57
|
+
expire_at: null,
|
|
58
|
+
updatedAt: now,
|
|
59
|
+
createdAt: now,
|
|
46
60
|
};
|
|
47
61
|
}
|
|
48
62
|
function makeGiftCard() {
|
|
49
63
|
const now = new Date('2026-01-01T00:00:00.000Z');
|
|
50
64
|
return {
|
|
51
|
-
_id: GIFT_CARD_ID,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
65
|
+
_id: GIFT_CARD_ID,
|
|
66
|
+
code: 'GC100',
|
|
67
|
+
brand_id: 'brand_1',
|
|
68
|
+
tenants: ['tenant_1'],
|
|
69
|
+
name: 'Free Broast',
|
|
70
|
+
description: '',
|
|
71
|
+
active: true,
|
|
72
|
+
max_uses: null,
|
|
73
|
+
max_uses_per_customer: null,
|
|
74
|
+
max_assignments: null,
|
|
75
|
+
max_assignments_per_customer: null,
|
|
76
|
+
assigned_count: 0,
|
|
77
|
+
used_count: 0,
|
|
78
|
+
expire_at: null,
|
|
55
79
|
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
|
|
56
80
|
reward_items: { num_of_items: 1, excluded_refs: [], exclusive_refs: [], item_ids: [ITEM_ID], allocation: null },
|
|
57
|
-
updatedAt: now,
|
|
81
|
+
updatedAt: now,
|
|
82
|
+
createdAt: now,
|
|
58
83
|
};
|
|
59
84
|
}
|
|
60
85
|
function pathwayPayload(pathway, unlocked) {
|
|
@@ -148,7 +173,11 @@ function rewardPayload(reward) {
|
|
|
148
173
|
return {
|
|
149
174
|
reward_type: reward,
|
|
150
175
|
reward_items: {
|
|
151
|
-
num_of_items: 1,
|
|
176
|
+
num_of_items: 1,
|
|
177
|
+
excluded_refs: [],
|
|
178
|
+
exclusive_refs: [],
|
|
179
|
+
item_ids: [ITEM_ID],
|
|
180
|
+
allocation: null,
|
|
152
181
|
},
|
|
153
182
|
};
|
|
154
183
|
case constants_1.OFFER_REWARD_TYPE_ENUM.POINTS:
|
|
@@ -157,7 +186,10 @@ function rewardPayload(reward) {
|
|
|
157
186
|
return {
|
|
158
187
|
reward_type: reward,
|
|
159
188
|
reward_cashback: {
|
|
160
|
-
cashback_value: 5,
|
|
189
|
+
cashback_value: 5,
|
|
190
|
+
is_cashback_percentage: false,
|
|
191
|
+
max_cashback_amount: null,
|
|
192
|
+
excluded_refs: [],
|
|
161
193
|
},
|
|
162
194
|
};
|
|
163
195
|
case constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS:
|
|
@@ -335,13 +367,20 @@ describe('pathway x reward matrix', () => {
|
|
|
335
367
|
...pct,
|
|
336
368
|
pathway_to_unlock_type_orders: { ...pct.pathway_to_unlock_type_orders, total_amount: 100 },
|
|
337
369
|
reward_cashback: {
|
|
338
|
-
cashback_value: 5,
|
|
370
|
+
cashback_value: 5,
|
|
371
|
+
is_cashback_percentage: true,
|
|
372
|
+
max_cashback_amount: null,
|
|
373
|
+
excluded_refs: [],
|
|
339
374
|
},
|
|
340
375
|
};
|
|
341
376
|
expect(statsFor(pctOffer).stats.applicable_quantity).toBe(1);
|
|
342
377
|
});
|
|
343
378
|
it('once-per-order rewards never exceed one claim', () => {
|
|
344
|
-
for (const reward of [
|
|
379
|
+
for (const reward of [
|
|
380
|
+
constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT,
|
|
381
|
+
constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS,
|
|
382
|
+
constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER,
|
|
383
|
+
]) {
|
|
345
384
|
expect(statsFor(ordersOffer(reward)).stats.applicable_quantity).toBe(1);
|
|
346
385
|
}
|
|
347
386
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../constants");
|
|
4
|
+
const common_1 = require("../helpers/utils/common");
|
|
5
|
+
const offers_helpers_1 = require("../helpers/utils/offers-helpers");
|
|
6
|
+
/**
|
|
7
|
+
* Two different things are called "excluded_refs":
|
|
8
|
+
*
|
|
9
|
+
* - On a plain DISCOUNT / CASHBACK reward it is an **item-level** list, collected
|
|
10
|
+
* by the "Excluded Items" picker. A ref is an item id - and, because a SPECIAL
|
|
11
|
+
* or HALF_HALF order line stores its own document id in `item_id`, a special or
|
|
12
|
+
* half-half id too. These refs reduce the spend-qualification base.
|
|
13
|
+
* - On a DISCOUNT_ITEMS reward it behaves exactly like IRewardTypeItems: an
|
|
14
|
+
* **option-level** hide list, acting on the modifier tree at selection time.
|
|
15
|
+
* Which items the reward may be applied to is decided by `item_ids`. It does
|
|
16
|
+
* NOT reduce the spend-qualification base.
|
|
17
|
+
*/
|
|
18
|
+
const item = (overrides = {}) => ({
|
|
19
|
+
_id: 'cart-1',
|
|
20
|
+
item_id: 'item-broast',
|
|
21
|
+
name: 'Broast',
|
|
22
|
+
quantity: 1,
|
|
23
|
+
subtotal: 1000,
|
|
24
|
+
modifiers: [
|
|
25
|
+
{
|
|
26
|
+
_id: 'm-1',
|
|
27
|
+
ref: 'modifier-dips',
|
|
28
|
+
name: 'Dips',
|
|
29
|
+
options: [{ _id: 'o-1', ref: 'option-garlic', name: 'Garlic', price: 100, quantity: 1, modifiers: [] }],
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
item_discount_details: null,
|
|
33
|
+
...overrides,
|
|
34
|
+
});
|
|
35
|
+
const categories = [
|
|
36
|
+
{
|
|
37
|
+
category_id: 'category-broast',
|
|
38
|
+
name: 'BROAST',
|
|
39
|
+
items: [
|
|
40
|
+
item(),
|
|
41
|
+
{ _id: 'c2', item_id: 'special-1', item_type: 'SPECIAL', name: 'Deal', quantity: 1, subtotal: 800, modifiers: [], item_discount_details: null },
|
|
42
|
+
{ _id: 'c3', item_id: 'halfhalf-1', item_type: 'HALF_HALF', name: 'Half & Half', quantity: 1, subtotal: 600, modifiers: [], item_discount_details: null },
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
];
|
|
46
|
+
describe('doesOrderItemMatchRefs', () => {
|
|
47
|
+
it('matches item-level ids only', () => {
|
|
48
|
+
expect((0, common_1.doesOrderItemMatchRefs)(item(), ['item-broast'])).toBe(true);
|
|
49
|
+
expect((0, common_1.doesOrderItemMatchRefs)(item(), ['special-1'])).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
it('does not treat a modifier or option ref as an item ref', () => {
|
|
52
|
+
expect((0, common_1.doesOrderItemMatchRefs)(item(), ['modifier-dips'])).toBe(false);
|
|
53
|
+
expect((0, common_1.doesOrderItemMatchRefs)(item(), ['option-garlic'])).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
it('is false for an empty list', () => {
|
|
56
|
+
expect((0, common_1.doesOrderItemMatchRefs)(item(), [])).toBe(false);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
describe('calculateEligibleOfferDiscountDetails (item-level refs)', () => {
|
|
60
|
+
it('excludes by item id, special id and half-half id', () => {
|
|
61
|
+
expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['item-broast']).subtotal).toBe(1400);
|
|
62
|
+
expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['special-1']).subtotal).toBe(1600);
|
|
63
|
+
expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['halfhalf-1']).subtotal).toBe(1800);
|
|
64
|
+
expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['special-1', 'halfhalf-1']).subtotal).toBe(1000);
|
|
65
|
+
});
|
|
66
|
+
it('ignores modifier/option refs entirely', () => {
|
|
67
|
+
expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['modifier-dips']).subtotal).toBe(2400);
|
|
68
|
+
expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['modifier-dips']).hasExcludedRefs).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
it('excludes a whole category', () => {
|
|
71
|
+
expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['category-broast']).subtotal).toBe(0);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
describe('DISCOUNT_ITEMS option refs do not shrink the qualification base', () => {
|
|
75
|
+
const makeOffer = (rewardType, excludedRefs) => ({
|
|
76
|
+
_id: 'offer-1',
|
|
77
|
+
name: 'Offer',
|
|
78
|
+
active: true,
|
|
79
|
+
auto_apply: false,
|
|
80
|
+
sort_id: 1,
|
|
81
|
+
included_platforms: [constants_1.PLATFORMS_ENUM.WEB_DELIVERY],
|
|
82
|
+
offer_special_hours: null,
|
|
83
|
+
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS,
|
|
84
|
+
pathway_to_unlock_type_orders: {
|
|
85
|
+
scope: 'OVERALL',
|
|
86
|
+
max_uses_per_customer: null,
|
|
87
|
+
max_claims: null,
|
|
88
|
+
min_num_of_orders: 0,
|
|
89
|
+
max_num_of_orders: null,
|
|
90
|
+
total_amount: 1200,
|
|
91
|
+
max_amount: null,
|
|
92
|
+
orders_between_unlocks: null,
|
|
93
|
+
},
|
|
94
|
+
reward_type: rewardType,
|
|
95
|
+
...(rewardType === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS
|
|
96
|
+
? {
|
|
97
|
+
reward_discount_items: {
|
|
98
|
+
num_of_items: 1,
|
|
99
|
+
excluded_refs: excludedRefs,
|
|
100
|
+
exclusive_refs: [],
|
|
101
|
+
item_ids: ['item-broast'],
|
|
102
|
+
discount_value: 50,
|
|
103
|
+
is_discount_percentage: true,
|
|
104
|
+
allocation: null,
|
|
105
|
+
},
|
|
106
|
+
}
|
|
107
|
+
: {
|
|
108
|
+
reward_discount: {
|
|
109
|
+
discount_value: 50,
|
|
110
|
+
is_discount_percentage: true,
|
|
111
|
+
max_discount_amount: null,
|
|
112
|
+
max_discount_percentage: null,
|
|
113
|
+
excluded_refs: excludedRefs,
|
|
114
|
+
},
|
|
115
|
+
}),
|
|
116
|
+
});
|
|
117
|
+
const stats = (rewardType, excludedRefs) => (0, offers_helpers_1.getOffersWithStats)({
|
|
118
|
+
offers: [makeOffer(rewardType, excludedRefs)],
|
|
119
|
+
customer: {
|
|
120
|
+
phone_number: '+9200000000',
|
|
121
|
+
loyalty_points: 0,
|
|
122
|
+
cashback_balance: 0,
|
|
123
|
+
total_orders: 5,
|
|
124
|
+
total_orders_per_platform_type: { [constants_1.PLATFORMS_ENUM.WEB_DELIVERY]: 5 },
|
|
125
|
+
voucher_codes: {},
|
|
126
|
+
gift_cards: {},
|
|
127
|
+
},
|
|
128
|
+
vouchers: [],
|
|
129
|
+
giftCards: [],
|
|
130
|
+
customerOfferRedemptionCounts: { ASSIGNED: {}, USED: {} },
|
|
131
|
+
redeemed_offers: null,
|
|
132
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_DELIVERY,
|
|
133
|
+
currency_symbol: constants_1.CURRENCY_SYMBOL_ENUM.Rs,
|
|
134
|
+
currentItemsPrice: 2400,
|
|
135
|
+
totalDiscountApplied: 0,
|
|
136
|
+
categories,
|
|
137
|
+
order_discount_details: null,
|
|
138
|
+
}).offersWithStats[0].stats;
|
|
139
|
+
it('leaves the base untouched when an option ref is listed on a DISCOUNT_ITEMS reward', () => {
|
|
140
|
+
// Cart is 2400 and the threshold is 1200: still unlocked, even though an
|
|
141
|
+
// option inside the 1000 item is listed in excluded_refs.
|
|
142
|
+
expect(stats(constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS, ['modifier-dips']).progress).toBe(100);
|
|
143
|
+
});
|
|
144
|
+
it('does shrink the base when an item id is listed on a plain DISCOUNT reward', () => {
|
|
145
|
+
// Cart 2400, threshold 1200. Excluding the 1000 item leaves 1400 -> still unlocked.
|
|
146
|
+
expect(stats(constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT, ['item-broast']).progress).toBe(100);
|
|
147
|
+
// Excluding the 1000 item and the 800 special leaves 600 -> locked.
|
|
148
|
+
expect(stats(constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT, ['item-broast', 'special-1']).progress).toBeLessThan(100);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const offers_helpers_1 = require("../helpers/utils/offers-helpers");
|
|
4
|
+
/**
|
|
5
|
+
* The cashback half of the wallet, which had no tests at all.
|
|
6
|
+
*
|
|
7
|
+
* `getMaxApplicableWalletCashback` is what the cashback slider is bounded by, so
|
|
8
|
+
* an off-by-one here either lets the customer spend more than the order is worth
|
|
9
|
+
* or hides cashback they own.
|
|
10
|
+
*/
|
|
11
|
+
describe('getMaxApplicableWalletCashback', () => {
|
|
12
|
+
const base = { cashbackBalance: 500, itemsPrice: 1000, totalDiscountApplied: 0, orderLevelDiscount: 0 };
|
|
13
|
+
it('is capped by the wallet balance', () => {
|
|
14
|
+
expect((0, offers_helpers_1.getMaxApplicableWalletCashback)(base)).toBe(500);
|
|
15
|
+
});
|
|
16
|
+
it('is capped by the net items value when cashback is larger', () => {
|
|
17
|
+
expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, cashbackBalance: 5000 })).toBe(1000);
|
|
18
|
+
});
|
|
19
|
+
it('ignores order-level discounts but subtracts item-level ones', () => {
|
|
20
|
+
// order-level only: net value is still the full 1000
|
|
21
|
+
expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, totalDiscountApplied: 200, orderLevelDiscount: 200 })).toBe(500);
|
|
22
|
+
// item-level: 1000 - 200 = 800
|
|
23
|
+
expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, totalDiscountApplied: 200, orderLevelDiscount: 0 })).toBe(500);
|
|
24
|
+
// item-level discount bigger than the balance -> balance is the cap
|
|
25
|
+
expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, cashbackBalance: 900, totalDiscountApplied: 300, orderLevelDiscount: 0 })).toBe(700);
|
|
26
|
+
});
|
|
27
|
+
it('never goes negative or NaN', () => {
|
|
28
|
+
expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, cashbackBalance: -50 })).toBe(0);
|
|
29
|
+
expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ cashbackBalance: NaN, itemsPrice: NaN, totalDiscountApplied: NaN, orderLevelDiscount: NaN })).toBe(0);
|
|
30
|
+
expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, cashbackBalance: 5000, itemsPrice: 0 })).toBe(0);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
describe('cashback recommended breakpoints', () => {
|
|
34
|
+
it('splits the applicable amount into the default 25/50/75/100 steps', () => {
|
|
35
|
+
expect((0, offers_helpers_1.getWalletCashbackRecommendedValues)({ maxApplicableCashback: 100 })).toEqual([25, 50, 75, 100]);
|
|
36
|
+
});
|
|
37
|
+
it('keeps the percentage when the amount rounds', () => {
|
|
38
|
+
const breakpoints = (0, offers_helpers_1.getWalletCashbackRecommendedBreakpoints)({ maxApplicableCashback: 10 });
|
|
39
|
+
expect(breakpoints).toEqual([
|
|
40
|
+
{ value: 2.5, percentage: 25 },
|
|
41
|
+
{ value: 5, percentage: 50 },
|
|
42
|
+
{ value: 7.5, percentage: 75 },
|
|
43
|
+
{ value: 10, percentage: 100 },
|
|
44
|
+
]);
|
|
45
|
+
});
|
|
46
|
+
it('returns nothing when there is nothing applicable', () => {
|
|
47
|
+
expect((0, offers_helpers_1.getWalletCashbackRecommendedValues)({ maxApplicableCashback: 0 })).toEqual([]);
|
|
48
|
+
expect((0, offers_helpers_1.getWalletCashbackRecommendedValues)({ maxApplicableCashback: null })).toEqual([]);
|
|
49
|
+
});
|
|
50
|
+
it('accepts custom steps and clamps percentages to 0..100', () => {
|
|
51
|
+
expect((0, offers_helpers_1.getWalletCashbackRecommendedBreakpoints)({ maxApplicableCashback: 100, percentageSteps: [10, 200] })).toEqual([
|
|
52
|
+
{ value: 10, percentage: 10 },
|
|
53
|
+
{ value: 100, percentage: 100 },
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
describe('wallet redemption patches', () => {
|
|
58
|
+
it('sets the cashback amount and keeps gift cards', () => {
|
|
59
|
+
const patch = (0, offers_helpers_1.buildWalletCashbackRedemptionPatch)({
|
|
60
|
+
currentWalletRedemptions: { gift_cards: [{ gift_card_id: 'gc-1', redeemed_times: 1 }] },
|
|
61
|
+
cashbackAmount: 25,
|
|
62
|
+
});
|
|
63
|
+
expect(patch.wallet_redemptions.cashback_amount).toBe(25);
|
|
64
|
+
expect(patch.wallet_redemptions.gift_cards).toEqual([{ gift_card_id: 'gc-1', redeemed_times: 1 }]);
|
|
65
|
+
});
|
|
66
|
+
it('drops a zero cashback amount instead of writing 0', () => {
|
|
67
|
+
const patch = (0, offers_helpers_1.buildWalletCashbackRedemptionPatch)({ currentWalletRedemptions: null, cashbackAmount: 0 });
|
|
68
|
+
expect(patch.wallet_redemptions).toBeNull();
|
|
69
|
+
});
|
|
70
|
+
it('clears only cashback when asked, leaving gift cards intact', () => {
|
|
71
|
+
const patch = (0, offers_helpers_1.buildClearWalletRedemptionsPatch)({
|
|
72
|
+
currentWalletRedemptions: {
|
|
73
|
+
cashback_amount: 25,
|
|
74
|
+
gift_cards: [{ gift_card_id: 'gc-1', redeemed_times: 1 }],
|
|
75
|
+
},
|
|
76
|
+
clearCashbackAmount: true,
|
|
77
|
+
});
|
|
78
|
+
expect(patch.wallet_redemptions.cashback_amount).toBeUndefined();
|
|
79
|
+
expect(patch.wallet_redemptions.gift_cards).toEqual([{ gift_card_id: 'gc-1', redeemed_times: 1 }]);
|
|
80
|
+
});
|
|
81
|
+
it('clears everything when clearAll is set', () => {
|
|
82
|
+
const patch = (0, offers_helpers_1.buildClearWalletRedemptionsPatch)({
|
|
83
|
+
currentWalletRedemptions: { cashback_amount: 25, gift_cards: [{ gift_card_id: 'gc-1', redeemed_times: 1 }] },
|
|
84
|
+
clearAll: true,
|
|
85
|
+
});
|
|
86
|
+
expect(patch.wallet_redemptions).toBeNull();
|
|
87
|
+
});
|
|
88
|
+
it('merges a partial patch over the existing redemptions', () => {
|
|
89
|
+
const patch = (0, offers_helpers_1.buildWalletRedemptionsMergePatch)({
|
|
90
|
+
currentWalletRedemptions: { cashback_amount: 10 },
|
|
91
|
+
walletRedemptionsPatch: { cashback_amount: 40 },
|
|
92
|
+
});
|
|
93
|
+
expect(patch.wallet_redemptions.cashback_amount).toBe(40);
|
|
94
|
+
});
|
|
95
|
+
});
|