@managesome/eposmatic-toolkit 0.4.41 → 0.4.42
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/helpers/store/cart-actions.js +57 -0
- package/lib/helpers/store/cart-actions.d.ts +24 -0
- package/lib/helpers/store/cart-actions.js +59 -0
- package/lib/tests/edit-discount-only.test.d.ts +1 -0
- package/lib/tests/edit-discount-only.test.js +124 -0
- package/lib/tests/offer-claim-regressions.test.js +85 -4
- package/lib/tests/offers-matrix.test.js +31 -8
- package/package.json +1 -1
|
@@ -3566,6 +3566,63 @@ export function getRemoveAllOffersFromCartData(data, courses, listnerSettings, o
|
|
|
3566
3566
|
const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
|
|
3567
3567
|
return newData;
|
|
3568
3568
|
}
|
|
3569
|
+
/**
|
|
3570
|
+
* Apply ONLY the order-level discount slots, leaving the rest of the cart alone.
|
|
3571
|
+
*
|
|
3572
|
+
* Used while an order is being edited: the full offers sync stays parked so the
|
|
3573
|
+
* customer's reward claims cannot be rewritten from the live cart, but an
|
|
3574
|
+
* order-level discount still has to react to the cart shrinking (for example to
|
|
3575
|
+
* only-excluded items, where the eligible subtotal becomes 0 and the discount
|
|
3576
|
+
* must go).
|
|
3577
|
+
*
|
|
3578
|
+
* Item rewards live on the items themselves. Recalculating totals with a ledger
|
|
3579
|
+
* that lacks a claim for a stamped item would strip that reward, so a shadow
|
|
3580
|
+
* claim map fills in only the missing entries - and the caller's real ledger is
|
|
3581
|
+
* restored on the way out. Categories, claims and wallet redemptions are always
|
|
3582
|
+
* taken from the input, never from the recalculation.
|
|
3583
|
+
*/
|
|
3584
|
+
export function getUpdateOrderDiscountDetailsOnlyData(data, nextOrderDiscountDetails, courses, listnerSettings) {
|
|
3585
|
+
const shadowRedeemedOffers = { ...(data.redeemed_offers ?? {}) };
|
|
3586
|
+
for (let c = 0; c < (data.categories ?? []).length; c++) {
|
|
3587
|
+
const items = data.categories?.[c].items ?? [];
|
|
3588
|
+
for (let i = 0; i < items.length; i++) {
|
|
3589
|
+
const details = items[i].item_discount_details;
|
|
3590
|
+
const stampedOfferId = details?.loyalty_discount_id ?? details?.voucher_discount_id;
|
|
3591
|
+
if (stampedOfferId != null && shadowRedeemedOffers[stampedOfferId] == null) {
|
|
3592
|
+
shadowRedeemedOffers[stampedOfferId] = 1;
|
|
3593
|
+
}
|
|
3594
|
+
}
|
|
3595
|
+
}
|
|
3596
|
+
const recalculated = getUpdateCartInfoData({ ...data, redeemed_offers: shadowRedeemedOffers }, { order_discount_details: nextOrderDiscountDetails ?? null }, courses, listnerSettings);
|
|
3597
|
+
return {
|
|
3598
|
+
...recalculated,
|
|
3599
|
+
categories: data.categories,
|
|
3600
|
+
redeemed_offers: data.redeemed_offers,
|
|
3601
|
+
wallet_redemptions: data.wallet_redemptions,
|
|
3602
|
+
order_discount_details: nextOrderDiscountDetails ?? null,
|
|
3603
|
+
};
|
|
3604
|
+
}
|
|
3605
|
+
/**
|
|
3606
|
+
* Do two order_discount_details values carry the same discount slots?
|
|
3607
|
+
*
|
|
3608
|
+
* Compares only what an order-level discount is: the two slot ids, their amounts,
|
|
3609
|
+
* their percentage flags and the voucher reference. Used to avoid dispatching a
|
|
3610
|
+
* reconcile when nothing changed.
|
|
3611
|
+
*/
|
|
3612
|
+
export function haveSameDiscountSlots(current, next) {
|
|
3613
|
+
const normalize = (details) => [
|
|
3614
|
+
details?.loyalty_discount_id ?? null,
|
|
3615
|
+
details?.loyalty_discount ?? null,
|
|
3616
|
+
details?.is_loyalty_discount_percentage ?? false,
|
|
3617
|
+
details?.voucher_discount_id ?? null,
|
|
3618
|
+
details?.voucher_discount ?? null,
|
|
3619
|
+
details?.is_voucher_discount_percentage ?? false,
|
|
3620
|
+
details?.voucher_reference_number ?? null,
|
|
3621
|
+
];
|
|
3622
|
+
const left = normalize(current);
|
|
3623
|
+
const right = normalize(next);
|
|
3624
|
+
return left.every((value, index) => value === right[index]);
|
|
3625
|
+
}
|
|
3569
3626
|
export function getClearVoucherCodeAndOffersFromCartData(data, courses, listnerSettings, offerIds) {
|
|
3570
3627
|
// The voucher slot belongs to ONE offer. Clearing a specific offer must not
|
|
3571
3628
|
// wipe a different offer that currently holds that slot (it would leave that
|
|
@@ -236,6 +236,30 @@ export interface IGetCartItemsObjPayload {
|
|
|
236
236
|
export declare function getCartItemsObj({ currentIterationCategories, currentDineinCourses, previousIterationCategories, previousDineinCourses, mappedItems, shouldSortItemsViaId, platform, isAnEdit, sortType, }: IGetCartItemsObjPayload): ICartItemsObj;
|
|
237
237
|
export declare function getRemoveOfferFromCartData(data: Partial<IOrder>, offer_id: string, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
|
|
238
238
|
export declare function getRemoveAllOffersFromCartData(data: Partial<IOrder>, courses: ICourse[], listnerSettings: ICartListnerSettings, offerIds: string[] | undefined): Partial<IOrder>;
|
|
239
|
+
/**
|
|
240
|
+
* Apply ONLY the order-level discount slots, leaving the rest of the cart alone.
|
|
241
|
+
*
|
|
242
|
+
* Used while an order is being edited: the full offers sync stays parked so the
|
|
243
|
+
* customer's reward claims cannot be rewritten from the live cart, but an
|
|
244
|
+
* order-level discount still has to react to the cart shrinking (for example to
|
|
245
|
+
* only-excluded items, where the eligible subtotal becomes 0 and the discount
|
|
246
|
+
* must go).
|
|
247
|
+
*
|
|
248
|
+
* Item rewards live on the items themselves. Recalculating totals with a ledger
|
|
249
|
+
* that lacks a claim for a stamped item would strip that reward, so a shadow
|
|
250
|
+
* claim map fills in only the missing entries - and the caller's real ledger is
|
|
251
|
+
* restored on the way out. Categories, claims and wallet redemptions are always
|
|
252
|
+
* taken from the input, never from the recalculation.
|
|
253
|
+
*/
|
|
254
|
+
export declare function getUpdateOrderDiscountDetailsOnlyData(data: Partial<IOrder>, nextOrderDiscountDetails: IOrder['order_discount_details'] | null | undefined, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
|
|
255
|
+
/**
|
|
256
|
+
* Do two order_discount_details values carry the same discount slots?
|
|
257
|
+
*
|
|
258
|
+
* Compares only what an order-level discount is: the two slot ids, their amounts,
|
|
259
|
+
* their percentage flags and the voucher reference. Used to avoid dispatching a
|
|
260
|
+
* reconcile when nothing changed.
|
|
261
|
+
*/
|
|
262
|
+
export declare function haveSameDiscountSlots(current: IOrder['order_discount_details'] | null | undefined, next: IOrder['order_discount_details'] | null | undefined): boolean;
|
|
239
263
|
export declare function getClearVoucherCodeAndOffersFromCartData(data: Partial<IOrder>, courses: ICourse[], listnerSettings: ICartListnerSettings, offerIds: string[] | undefined): Partial<IOrder>;
|
|
240
264
|
/**
|
|
241
265
|
* Wipe all reward metadata (`redeemed_offers`, `wallet_redemptions`) and any
|
|
@@ -30,6 +30,8 @@ exports.getChangeCourseStatusData = getChangeCourseStatusData;
|
|
|
30
30
|
exports.getCartItemsObj = getCartItemsObj;
|
|
31
31
|
exports.getRemoveOfferFromCartData = getRemoveOfferFromCartData;
|
|
32
32
|
exports.getRemoveAllOffersFromCartData = getRemoveAllOffersFromCartData;
|
|
33
|
+
exports.getUpdateOrderDiscountDetailsOnlyData = getUpdateOrderDiscountDetailsOnlyData;
|
|
34
|
+
exports.haveSameDiscountSlots = haveSameDiscountSlots;
|
|
33
35
|
exports.getClearVoucherCodeAndOffersFromCartData = getClearVoucherCodeAndOffersFromCartData;
|
|
34
36
|
exports.getResetRewardStateForNewCustomer = getResetRewardStateForNewCustomer;
|
|
35
37
|
const bson_objectid_1 = __importDefault(require("bson-objectid"));
|
|
@@ -3601,6 +3603,63 @@ function getRemoveAllOffersFromCartData(data, courses, listnerSettings, offerIds
|
|
|
3601
3603
|
const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
|
|
3602
3604
|
return newData;
|
|
3603
3605
|
}
|
|
3606
|
+
/**
|
|
3607
|
+
* Apply ONLY the order-level discount slots, leaving the rest of the cart alone.
|
|
3608
|
+
*
|
|
3609
|
+
* Used while an order is being edited: the full offers sync stays parked so the
|
|
3610
|
+
* customer's reward claims cannot be rewritten from the live cart, but an
|
|
3611
|
+
* order-level discount still has to react to the cart shrinking (for example to
|
|
3612
|
+
* only-excluded items, where the eligible subtotal becomes 0 and the discount
|
|
3613
|
+
* must go).
|
|
3614
|
+
*
|
|
3615
|
+
* Item rewards live on the items themselves. Recalculating totals with a ledger
|
|
3616
|
+
* that lacks a claim for a stamped item would strip that reward, so a shadow
|
|
3617
|
+
* claim map fills in only the missing entries - and the caller's real ledger is
|
|
3618
|
+
* restored on the way out. Categories, claims and wallet redemptions are always
|
|
3619
|
+
* taken from the input, never from the recalculation.
|
|
3620
|
+
*/
|
|
3621
|
+
function getUpdateOrderDiscountDetailsOnlyData(data, nextOrderDiscountDetails, courses, listnerSettings) {
|
|
3622
|
+
const shadowRedeemedOffers = { ...(data.redeemed_offers ?? {}) };
|
|
3623
|
+
for (let c = 0; c < (data.categories ?? []).length; c++) {
|
|
3624
|
+
const items = data.categories?.[c].items ?? [];
|
|
3625
|
+
for (let i = 0; i < items.length; i++) {
|
|
3626
|
+
const details = items[i].item_discount_details;
|
|
3627
|
+
const stampedOfferId = details?.loyalty_discount_id ?? details?.voucher_discount_id;
|
|
3628
|
+
if (stampedOfferId != null && shadowRedeemedOffers[stampedOfferId] == null) {
|
|
3629
|
+
shadowRedeemedOffers[stampedOfferId] = 1;
|
|
3630
|
+
}
|
|
3631
|
+
}
|
|
3632
|
+
}
|
|
3633
|
+
const recalculated = getUpdateCartInfoData({ ...data, redeemed_offers: shadowRedeemedOffers }, { order_discount_details: nextOrderDiscountDetails ?? null }, courses, listnerSettings);
|
|
3634
|
+
return {
|
|
3635
|
+
...recalculated,
|
|
3636
|
+
categories: data.categories,
|
|
3637
|
+
redeemed_offers: data.redeemed_offers,
|
|
3638
|
+
wallet_redemptions: data.wallet_redemptions,
|
|
3639
|
+
order_discount_details: nextOrderDiscountDetails ?? null,
|
|
3640
|
+
};
|
|
3641
|
+
}
|
|
3642
|
+
/**
|
|
3643
|
+
* Do two order_discount_details values carry the same discount slots?
|
|
3644
|
+
*
|
|
3645
|
+
* Compares only what an order-level discount is: the two slot ids, their amounts,
|
|
3646
|
+
* their percentage flags and the voucher reference. Used to avoid dispatching a
|
|
3647
|
+
* reconcile when nothing changed.
|
|
3648
|
+
*/
|
|
3649
|
+
function haveSameDiscountSlots(current, next) {
|
|
3650
|
+
const normalize = (details) => [
|
|
3651
|
+
details?.loyalty_discount_id ?? null,
|
|
3652
|
+
details?.loyalty_discount ?? null,
|
|
3653
|
+
details?.is_loyalty_discount_percentage ?? false,
|
|
3654
|
+
details?.voucher_discount_id ?? null,
|
|
3655
|
+
details?.voucher_discount ?? null,
|
|
3656
|
+
details?.is_voucher_discount_percentage ?? false,
|
|
3657
|
+
details?.voucher_reference_number ?? null,
|
|
3658
|
+
];
|
|
3659
|
+
const left = normalize(current);
|
|
3660
|
+
const right = normalize(next);
|
|
3661
|
+
return left.every((value, index) => value === right[index]);
|
|
3662
|
+
}
|
|
3604
3663
|
function getClearVoucherCodeAndOffersFromCartData(data, courses, listnerSettings, offerIds) {
|
|
3605
3664
|
// The voucher slot belongs to ONE offer. Clearing a specific offer must not
|
|
3606
3665
|
// wipe a different offer that currently holds that slot (it would leave that
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const cart_actions_1 = require("../helpers/store/cart-actions");
|
|
4
|
+
/**
|
|
5
|
+
* F6: the edit-mode discount reconcile.
|
|
6
|
+
*
|
|
7
|
+
* While an order is edited the full offers sync is parked, so this is the only
|
|
8
|
+
* path that can react to the cart shrinking. Its riskiest part is the shadow
|
|
9
|
+
* claim map: recalculating totals with a ledger that lacks a claim for a stamped
|
|
10
|
+
* item would strip that item's reward, so missing entries are filled in - and the
|
|
11
|
+
* caller's real ledger is restored afterwards.
|
|
12
|
+
*/
|
|
13
|
+
const stampedItem = (overrides = {}) => ({
|
|
14
|
+
_id: 'ci-stamped',
|
|
15
|
+
item_id: 'i1',
|
|
16
|
+
item_type: 'ITEM',
|
|
17
|
+
name: 'Free item',
|
|
18
|
+
quantity: 1,
|
|
19
|
+
price: 1000,
|
|
20
|
+
subtotal: 0,
|
|
21
|
+
tax_rate: 0,
|
|
22
|
+
modifiers: [],
|
|
23
|
+
special_instructions: '',
|
|
24
|
+
item_discount_details: {
|
|
25
|
+
loyalty_discount: 100,
|
|
26
|
+
loyalty_discount_id: 'reward-offer',
|
|
27
|
+
is_loyalty_discount_percentage: true,
|
|
28
|
+
total_discount: 1000,
|
|
29
|
+
},
|
|
30
|
+
...overrides,
|
|
31
|
+
});
|
|
32
|
+
const paidItem = () => ({
|
|
33
|
+
_id: 'ci-paid',
|
|
34
|
+
item_id: 'i2',
|
|
35
|
+
item_type: 'ITEM',
|
|
36
|
+
name: 'Paid item',
|
|
37
|
+
quantity: 1,
|
|
38
|
+
price: 1000,
|
|
39
|
+
subtotal: 1000,
|
|
40
|
+
tax_rate: 0,
|
|
41
|
+
modifiers: [],
|
|
42
|
+
special_instructions: '',
|
|
43
|
+
item_discount_details: null,
|
|
44
|
+
});
|
|
45
|
+
function cart(overrides = {}) {
|
|
46
|
+
return {
|
|
47
|
+
platform: 'WEB_DELIVERY',
|
|
48
|
+
price: 1000,
|
|
49
|
+
total_discount_applied: 0,
|
|
50
|
+
categories: [{ category_id: 'c', name: 'C', items: [paidItem(), stampedItem()] }],
|
|
51
|
+
order_discount_details: null,
|
|
52
|
+
redeemed_offers: null,
|
|
53
|
+
wallet_redemptions: null,
|
|
54
|
+
...overrides,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
describe('getUpdateOrderDiscountDetailsOnlyData', () => {
|
|
58
|
+
it('applies the new discount slots', () => {
|
|
59
|
+
const next = {
|
|
60
|
+
loyalty_discount_id: 'offer-1',
|
|
61
|
+
loyalty_discount: 10,
|
|
62
|
+
is_loyalty_discount_percentage: false,
|
|
63
|
+
total_discount: 10,
|
|
64
|
+
};
|
|
65
|
+
const result = (0, cart_actions_1.getUpdateOrderDiscountDetailsOnlyData)(cart(), next, [], {});
|
|
66
|
+
expect(result.order_discount_details).toEqual(next);
|
|
67
|
+
});
|
|
68
|
+
it('clears the slots when nothing is left to discount', () => {
|
|
69
|
+
const result = (0, cart_actions_1.getUpdateOrderDiscountDetailsOnlyData)(cart({
|
|
70
|
+
order_discount_details: {
|
|
71
|
+
loyalty_discount_id: 'offer-1',
|
|
72
|
+
loyalty_discount: 10,
|
|
73
|
+
total_discount: 10,
|
|
74
|
+
},
|
|
75
|
+
}), null, [], {});
|
|
76
|
+
expect(result.order_discount_details?.loyalty_discount_id).toBeUndefined();
|
|
77
|
+
});
|
|
78
|
+
it('never touches categories, claims or wallet redemptions', () => {
|
|
79
|
+
const data = cart({
|
|
80
|
+
redeemed_offers: { 'reward-offer': 1 },
|
|
81
|
+
wallet_redemptions: { cashback_amount: 25, gift_cards: [{ gift_card_id: 'gc-1', redeemed_times: 1 }] },
|
|
82
|
+
});
|
|
83
|
+
const result = (0, cart_actions_1.getUpdateOrderDiscountDetailsOnlyData)(data, null, [], {});
|
|
84
|
+
expect(result.categories).toBe(data.categories);
|
|
85
|
+
expect(result.redeemed_offers).toEqual(data.redeemed_offers);
|
|
86
|
+
expect(result.wallet_redemptions).toEqual(data.wallet_redemptions);
|
|
87
|
+
});
|
|
88
|
+
it('keeps a stamped item reward whose claim is missing from the ledger', () => {
|
|
89
|
+
// The shadow map is the whole point: without it the recalculation would see a
|
|
90
|
+
// reward with no claim and strip the free item.
|
|
91
|
+
const data = cart({ redeemed_offers: null });
|
|
92
|
+
const result = (0, cart_actions_1.getUpdateOrderDiscountDetailsOnlyData)(data, null, [], {});
|
|
93
|
+
const items = (result.categories ?? [])[0].items ?? [];
|
|
94
|
+
const stamped = items.find((item) => item._id === 'ci-stamped');
|
|
95
|
+
expect(stamped?.item_discount_details).not.toBeNull();
|
|
96
|
+
expect(stamped?.item_discount_details?.loyalty_discount_id).toBe('reward-offer');
|
|
97
|
+
// and the invented claim is not written back
|
|
98
|
+
expect(result.redeemed_offers).toBeNull();
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
describe('haveSameDiscountSlots', () => {
|
|
102
|
+
const base = {
|
|
103
|
+
loyalty_discount_id: 'a',
|
|
104
|
+
loyalty_discount: 10,
|
|
105
|
+
is_loyalty_discount_percentage: false,
|
|
106
|
+
voucher_discount_id: null,
|
|
107
|
+
voucher_discount: null,
|
|
108
|
+
is_voucher_discount_percentage: false,
|
|
109
|
+
voucher_reference_number: null,
|
|
110
|
+
};
|
|
111
|
+
it('is true for equal slots', () => {
|
|
112
|
+
expect((0, cart_actions_1.haveSameDiscountSlots)(base, { ...base })).toBe(true);
|
|
113
|
+
});
|
|
114
|
+
it('is true for two empty values', () => {
|
|
115
|
+
expect((0, cart_actions_1.haveSameDiscountSlots)(null, undefined)).toBe(true);
|
|
116
|
+
expect((0, cart_actions_1.haveSameDiscountSlots)(null, {})).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
it('is false when an amount, an id or the reference differs', () => {
|
|
119
|
+
expect((0, cart_actions_1.haveSameDiscountSlots)(base, { ...base, loyalty_discount: 11 })).toBe(false);
|
|
120
|
+
expect((0, cart_actions_1.haveSameDiscountSlots)(base, { ...base, loyalty_discount_id: 'b' })).toBe(false);
|
|
121
|
+
expect((0, cart_actions_1.haveSameDiscountSlots)(base, { ...base, voucher_reference_number: 'SAVE' })).toBe(false);
|
|
122
|
+
expect((0, cart_actions_1.haveSameDiscountSlots)(base, null)).toBe(false);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
@@ -201,7 +201,61 @@ describe('F-02: a zero spend threshold must not multiply claims', () => {
|
|
|
201
201
|
expect(stats.applicable_quantity).toBe(2);
|
|
202
202
|
});
|
|
203
203
|
});
|
|
204
|
-
describe('
|
|
204
|
+
describe('claim accounting: ledger vs cart evidence', () => {
|
|
205
|
+
// The matrix suite has a per-cell "already claimed" block, but it asserts
|
|
206
|
+
// nothing for POINTS / CASHBACK / ITEMS / DISCOUNT_ITEMS. These three cases pin
|
|
207
|
+
// the rule those cells depend on.
|
|
208
|
+
// A lifetime cap of one makes "already claimed" observable: without it a
|
|
209
|
+
// repeatable reward stays claimable by design.
|
|
210
|
+
const cappedPathway = () => {
|
|
211
|
+
const pathway = ordersPathway(100);
|
|
212
|
+
return {
|
|
213
|
+
...pathway,
|
|
214
|
+
pathway_to_unlock_type_orders: {
|
|
215
|
+
...pathway.pathway_to_unlock_type_orders,
|
|
216
|
+
max_uses_per_customer: 1,
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
};
|
|
220
|
+
const orderLevelOffer = () => offer({
|
|
221
|
+
...cappedPathway(),
|
|
222
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.POINTS,
|
|
223
|
+
reward_points: { num_of_points: 1, once_per_order: false },
|
|
224
|
+
});
|
|
225
|
+
const itemOffer = () => offer({
|
|
226
|
+
...cappedPathway(),
|
|
227
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
|
|
228
|
+
reward_items: { num_of_items: 1, excluded_refs: [], exclusive_refs: [], item_ids: ['i1'], allocation: null },
|
|
229
|
+
});
|
|
230
|
+
const cartWithStampedItem = () => [
|
|
231
|
+
{
|
|
232
|
+
category_id: 'c',
|
|
233
|
+
name: 'C',
|
|
234
|
+
items: [
|
|
235
|
+
{ _id: 'ci1', item_id: 'i1', name: 'A', quantity: 1, price: 1000, subtotal: 1000, tax_rate: 0, modifiers: [], special_instructions: '', item_discount_details: null },
|
|
236
|
+
{ _id: 'ci2', item_id: 'i1', name: 'A', quantity: 1, price: 1000, subtotal: 0, tax_rate: 0, modifiers: [], special_instructions: '', item_discount_details: { loyalty_discount_id: 'o', loyalty_discount: 0, total_discount: 0 } },
|
|
237
|
+
],
|
|
238
|
+
},
|
|
239
|
+
];
|
|
240
|
+
it('counts an order-level claim from the ledger', () => {
|
|
241
|
+
const { stats } = statsFor(orderLevelOffer(), { redeemed: { o: 1 } });
|
|
242
|
+
expect(stats.redeemed_times).toBe(1);
|
|
243
|
+
expect(stats.claimable).toBe(false);
|
|
244
|
+
});
|
|
245
|
+
it('counts an item reward claim from stamped cart items', () => {
|
|
246
|
+
const { stats } = statsFor(itemOffer(), { redeemed: { o: 1 }, categories: cartWithStampedItem() });
|
|
247
|
+
expect(stats.redeemed_times).toBe(1);
|
|
248
|
+
expect(stats.claimable).toBe(false);
|
|
249
|
+
});
|
|
250
|
+
it('ignores a ledger-only claim for an item reward', () => {
|
|
251
|
+
// Deliberate: item rewards trust stamped items, not the ledger. This is the
|
|
252
|
+
// #882 trade-off - pin it so nobody "fixes" it back into reading the ledger.
|
|
253
|
+
const { stats } = statsFor(itemOffer(), { redeemed: { o: 1 } });
|
|
254
|
+
expect(stats.redeemed_times).toBe(0);
|
|
255
|
+
expect(stats.claimable).toBe(true);
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
describe("clearing one offer must not wipe another offer's voucher slot", () => {
|
|
205
259
|
it('keeps the slot when a different offer holds it', () => {
|
|
206
260
|
const data = {
|
|
207
261
|
categories: [
|
|
@@ -209,7 +263,18 @@ describe('clearing one offer must not wipe another offer\'s voucher slot', () =>
|
|
|
209
263
|
category_id: 'c',
|
|
210
264
|
name: 'C',
|
|
211
265
|
items: [
|
|
212
|
-
{
|
|
266
|
+
{
|
|
267
|
+
_id: 'p1',
|
|
268
|
+
item_id: 'i1',
|
|
269
|
+
name: 'A',
|
|
270
|
+
quantity: 1,
|
|
271
|
+
price: 1000,
|
|
272
|
+
subtotal: 1000,
|
|
273
|
+
tax_rate: 0,
|
|
274
|
+
modifiers: [],
|
|
275
|
+
special_instructions: '',
|
|
276
|
+
item_discount_details: null,
|
|
277
|
+
},
|
|
213
278
|
],
|
|
214
279
|
},
|
|
215
280
|
],
|
|
@@ -227,9 +292,11 @@ describe('clearing one offer must not wipe another offer\'s voucher slot', () =>
|
|
|
227
292
|
};
|
|
228
293
|
const after = (0, cart_actions_1.getClearVoucherCodeAndOffersFromCartData)(data, [], {}, ['A']);
|
|
229
294
|
const details = (after.order_discount_details ?? {});
|
|
230
|
-
// B is untouched: its slot, its reference and its claim
|
|
295
|
+
// B is untouched: its slot, its amount, its reference and its claim survive.
|
|
231
296
|
expect(details.voucher_discount_id).toBe('B');
|
|
232
297
|
expect(details.voucher_reference_number).toBe('SAVE-B');
|
|
298
|
+
expect(details.voucher_discount).toBe(10);
|
|
299
|
+
expect(details.total_discount).toBe(10);
|
|
233
300
|
expect(after.redeemed_offers).toEqual({ B: 1 });
|
|
234
301
|
});
|
|
235
302
|
it('still clears the slot when the target offer is the one holding it', () => {
|
|
@@ -239,7 +306,18 @@ describe('clearing one offer must not wipe another offer\'s voucher slot', () =>
|
|
|
239
306
|
category_id: 'c',
|
|
240
307
|
name: 'C',
|
|
241
308
|
items: [
|
|
242
|
-
{
|
|
309
|
+
{
|
|
310
|
+
_id: 'p1',
|
|
311
|
+
item_id: 'i1',
|
|
312
|
+
name: 'A',
|
|
313
|
+
quantity: 1,
|
|
314
|
+
price: 1000,
|
|
315
|
+
subtotal: 1000,
|
|
316
|
+
tax_rate: 0,
|
|
317
|
+
modifiers: [],
|
|
318
|
+
special_instructions: '',
|
|
319
|
+
item_discount_details: null,
|
|
320
|
+
},
|
|
243
321
|
],
|
|
244
322
|
},
|
|
245
323
|
],
|
|
@@ -259,6 +337,9 @@ describe('clearing one offer must not wipe another offer\'s voucher slot', () =>
|
|
|
259
337
|
const details = (after.order_discount_details ?? {});
|
|
260
338
|
expect(details.voucher_discount_id).toBeUndefined();
|
|
261
339
|
expect(details.voucher_reference_number).toBeUndefined();
|
|
340
|
+
// the money goes with the slot, not just the ids
|
|
341
|
+
expect(details.voucher_discount).toBeUndefined();
|
|
342
|
+
expect(details.total_discount).toBe(0);
|
|
262
343
|
});
|
|
263
344
|
});
|
|
264
345
|
describe('F-05: removing a reward item releases its claim', () => {
|
|
@@ -213,6 +213,21 @@ function makeOffer(pathway, reward, unlocked) {
|
|
|
213
213
|
...rewardPayload(reward),
|
|
214
214
|
};
|
|
215
215
|
}
|
|
216
|
+
/** Cap the lifetime uses on whichever pathway payload this offer carries. */
|
|
217
|
+
function withLifetimeCap(offer) {
|
|
218
|
+
const clone = { ...offer };
|
|
219
|
+
for (const key of [
|
|
220
|
+
'pathway_to_unlock_type_orders',
|
|
221
|
+
'pathway_to_unlock_type_items',
|
|
222
|
+
'pathway_to_unlock_type_points',
|
|
223
|
+
'pathway_to_unlock_type_vouchers',
|
|
224
|
+
]) {
|
|
225
|
+
if (clone[key] != null) {
|
|
226
|
+
clone[key] = { ...clone[key], max_uses_per_customer: 1 };
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return clone;
|
|
230
|
+
}
|
|
216
231
|
function statsFor(offer, { redeemedTimes = 0, totalOrders = 5, vouchers = [makeVoucher()], voucherCodes = { [VOUCHER_CODE]: 2 }, categories = makeCategories(), } = {}) {
|
|
217
232
|
const result = (0, offers_helpers_1.getOffersWithStats)({
|
|
218
233
|
offers: [offer],
|
|
@@ -611,19 +626,27 @@ describe('pathway x reward matrix', () => {
|
|
|
611
626
|
});
|
|
612
627
|
describe.each(VALID_CELLS)('%s + %s (already claimed)', (pathway, reward) => {
|
|
613
628
|
it('reports the claim and does not offer a duplicate', () => {
|
|
614
|
-
|
|
629
|
+
// The shared fixtures leave max_uses_per_customer unset for some pathways,
|
|
630
|
+
// which makes "already claimed" unobservable. Cap every cell so the claim
|
|
631
|
+
// and the block are both real.
|
|
632
|
+
const offer = withLifetimeCap(makeOffer(pathway, reward, true));
|
|
615
633
|
const { stats } = statsFor(offer, { redeemedTimes: 1 });
|
|
616
|
-
|
|
617
|
-
const repeatedRewards = new Set([
|
|
618
|
-
constants_1.OFFER_REWARD_TYPE_ENUM.POINTS,
|
|
619
|
-
constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK,
|
|
634
|
+
const itemRewards = new Set([
|
|
620
635
|
constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
|
|
621
636
|
constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS,
|
|
622
637
|
]);
|
|
623
|
-
if (
|
|
624
|
-
|
|
625
|
-
|
|
638
|
+
if (itemRewards.has(reward)) {
|
|
639
|
+
// Item rewards are claimed by stamping cart items. Eligibility reads that
|
|
640
|
+
// evidence, not the ledger, so a ledger-only claim is invisible and the
|
|
641
|
+
// offer stays claimable - the deliberate half of the #882 fix.
|
|
642
|
+
expect(stats.redeemed_times).toBe(0);
|
|
643
|
+
expect(stats.claimable).toBe(true);
|
|
644
|
+
return;
|
|
626
645
|
}
|
|
646
|
+
// Order-level rewards read the ledger: the claim is reported and the
|
|
647
|
+
// lifetime cap is spent.
|
|
648
|
+
expect(stats.redeemed_times).toBeGreaterThanOrEqual(1);
|
|
649
|
+
expect(stats.claimable).toBe(false);
|
|
627
650
|
});
|
|
628
651
|
});
|
|
629
652
|
});
|