@managesome/eposmatic-toolkit 0.4.40 → 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 +66 -1
- package/esm/helpers/utils/offers-helpers.js +5 -2
- package/lib/helpers/store/cart-actions.d.ts +24 -0
- package/lib/helpers/store/cart-actions.js +68 -1
- package/lib/helpers/utils/offers-helpers.js +5 -2
- 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 +152 -0
- package/lib/tests/offers-matrix.test.js +31 -8
- package/package.json +1 -1
|
@@ -3566,9 +3566,74 @@ 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) {
|
|
3627
|
+
// The voucher slot belongs to ONE offer. Clearing a specific offer must not
|
|
3628
|
+
// wipe a different offer that currently holds that slot (it would leave that
|
|
3629
|
+
// offer's claim in the ledger with no discount on the cart). With no offerIds
|
|
3630
|
+
// the caller means "clear the voucher code entirely", which still clears it.
|
|
3631
|
+
const voucherSlotOfferId = data.order_discount_details?.voucher_discount_id;
|
|
3632
|
+
const shouldClearVoucherSlot = offerIds === undefined || voucherSlotOfferId == null || offerIds.includes(String(voucherSlotOfferId));
|
|
3570
3633
|
const orderWithoutOffers = getRemoveAllOffersFromCartData(data, courses, listnerSettings, offerIds);
|
|
3571
|
-
const nextOrderDiscountDetails =
|
|
3634
|
+
const nextOrderDiscountDetails = shouldClearVoucherSlot
|
|
3635
|
+
? buildClearVoucherDiscountDetails(orderWithoutOffers.order_discount_details)
|
|
3636
|
+
: orderWithoutOffers.order_discount_details;
|
|
3572
3637
|
return getUpdateCartInfoData(orderWithoutOffers, {
|
|
3573
3638
|
order_discount_details: nextOrderDiscountDetails,
|
|
3574
3639
|
}, courses, listnerSettings);
|
|
@@ -219,8 +219,11 @@ function calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categor
|
|
|
219
219
|
// must NOT be read as "one claim per currency unit", which would multiply the
|
|
220
220
|
// reward by the cart total.
|
|
221
221
|
const getUnlockTimesByOrderAmount = (orderValue, totalAmount) => {
|
|
222
|
+
// Below one currency unit a threshold is not a meaningful chunk size: it
|
|
223
|
+
// multiplies claims by the cart value (0.5 on a 1500 cart = 3000 claims).
|
|
224
|
+
// Treat anything < 1 as "no threshold".
|
|
222
225
|
const requiredAmount = Number(totalAmount);
|
|
223
|
-
if (!Number.isFinite(requiredAmount) || requiredAmount
|
|
226
|
+
if (!Number.isFinite(requiredAmount) || requiredAmount < 1)
|
|
224
227
|
return 1;
|
|
225
228
|
return Math.max(1, Math.floor(orderValue / requiredAmount));
|
|
226
229
|
};
|
|
@@ -1942,7 +1945,7 @@ export function getOffersWithStats({ offers, customer, vouchers, giftCards, cust
|
|
|
1942
1945
|
// `floor(orderValue / 1)` claims.
|
|
1943
1946
|
const getOrderValueClaimTimes = (orderValue, minAmount) => {
|
|
1944
1947
|
const requiredAmount = Number(minAmount);
|
|
1945
|
-
if (!Number.isFinite(requiredAmount) || requiredAmount
|
|
1948
|
+
if (!Number.isFinite(requiredAmount) || requiredAmount < 1)
|
|
1946
1949
|
return 1;
|
|
1947
1950
|
return Math.max(1, Math.floor(orderValue / requiredAmount));
|
|
1948
1951
|
};
|
|
@@ -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,9 +3603,74 @@ 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) {
|
|
3664
|
+
// The voucher slot belongs to ONE offer. Clearing a specific offer must not
|
|
3665
|
+
// wipe a different offer that currently holds that slot (it would leave that
|
|
3666
|
+
// offer's claim in the ledger with no discount on the cart). With no offerIds
|
|
3667
|
+
// the caller means "clear the voucher code entirely", which still clears it.
|
|
3668
|
+
const voucherSlotOfferId = data.order_discount_details?.voucher_discount_id;
|
|
3669
|
+
const shouldClearVoucherSlot = offerIds === undefined || voucherSlotOfferId == null || offerIds.includes(String(voucherSlotOfferId));
|
|
3605
3670
|
const orderWithoutOffers = getRemoveAllOffersFromCartData(data, courses, listnerSettings, offerIds);
|
|
3606
|
-
const nextOrderDiscountDetails =
|
|
3671
|
+
const nextOrderDiscountDetails = shouldClearVoucherSlot
|
|
3672
|
+
? (0, offers_helpers_1.buildClearVoucherDiscountDetails)(orderWithoutOffers.order_discount_details)
|
|
3673
|
+
: orderWithoutOffers.order_discount_details;
|
|
3607
3674
|
return getUpdateCartInfoData(orderWithoutOffers, {
|
|
3608
3675
|
order_discount_details: nextOrderDiscountDetails,
|
|
3609
3676
|
}, courses, listnerSettings);
|
|
@@ -256,8 +256,11 @@ function calculatePointsRedeemedForOffer(redeemedOffer, totalOrderPrice, categor
|
|
|
256
256
|
// must NOT be read as "one claim per currency unit", which would multiply the
|
|
257
257
|
// reward by the cart total.
|
|
258
258
|
const getUnlockTimesByOrderAmount = (orderValue, totalAmount) => {
|
|
259
|
+
// Below one currency unit a threshold is not a meaningful chunk size: it
|
|
260
|
+
// multiplies claims by the cart value (0.5 on a 1500 cart = 3000 claims).
|
|
261
|
+
// Treat anything < 1 as "no threshold".
|
|
259
262
|
const requiredAmount = Number(totalAmount);
|
|
260
|
-
if (!Number.isFinite(requiredAmount) || requiredAmount
|
|
263
|
+
if (!Number.isFinite(requiredAmount) || requiredAmount < 1)
|
|
261
264
|
return 1;
|
|
262
265
|
return Math.max(1, Math.floor(orderValue / requiredAmount));
|
|
263
266
|
};
|
|
@@ -1980,7 +1983,7 @@ function getOffersWithStats({ offers, customer, vouchers, giftCards, customerOff
|
|
|
1980
1983
|
// `floor(orderValue / 1)` claims.
|
|
1981
1984
|
const getOrderValueClaimTimes = (orderValue, minAmount) => {
|
|
1982
1985
|
const requiredAmount = Number(minAmount);
|
|
1983
|
-
if (!Number.isFinite(requiredAmount) || requiredAmount
|
|
1986
|
+
if (!Number.isFinite(requiredAmount) || requiredAmount < 1)
|
|
1984
1987
|
return 1;
|
|
1985
1988
|
return Math.max(1, Math.floor(orderValue / requiredAmount));
|
|
1986
1989
|
};
|
|
@@ -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
|
+
});
|
|
@@ -179,6 +179,11 @@ describe('F-02: a zero spend threshold must not multiply claims', () => {
|
|
|
179
179
|
// Was floor(points / 1) = every point the customer holds.
|
|
180
180
|
expect(statsResult.applicable_quantity).toBe(1);
|
|
181
181
|
});
|
|
182
|
+
it('treats a sub-unit threshold as "no threshold" instead of multiplying claims', () => {
|
|
183
|
+
// 0.5 used to mean floor(1500 / 0.5) = 3000 claims.
|
|
184
|
+
expect(statsFor(offer({ ...ordersPathway(0.5), ...twoFreeItems })).stats.applicable_quantity).toBe(2);
|
|
185
|
+
expect(statsFor(offer({ ...ordersPathway(0.1), ...twoFreeItems })).stats.applicable_quantity).toBe(2);
|
|
186
|
+
});
|
|
182
187
|
it('still lets the vouchers held drive a voucher-pathway claim', () => {
|
|
183
188
|
const { stats } = statsFor(offer({
|
|
184
189
|
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER,
|
|
@@ -196,6 +201,147 @@ describe('F-02: a zero spend threshold must not multiply claims', () => {
|
|
|
196
201
|
expect(stats.applicable_quantity).toBe(2);
|
|
197
202
|
});
|
|
198
203
|
});
|
|
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", () => {
|
|
259
|
+
it('keeps the slot when a different offer holds it', () => {
|
|
260
|
+
const data = {
|
|
261
|
+
categories: [
|
|
262
|
+
{
|
|
263
|
+
category_id: 'c',
|
|
264
|
+
name: 'C',
|
|
265
|
+
items: [
|
|
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
|
+
},
|
|
278
|
+
],
|
|
279
|
+
},
|
|
280
|
+
],
|
|
281
|
+
order_discount_details: {
|
|
282
|
+
voucher_discount_id: 'B',
|
|
283
|
+
voucher_reference_number: 'SAVE-B',
|
|
284
|
+
voucher_discount: 10,
|
|
285
|
+
total_discount: 10,
|
|
286
|
+
},
|
|
287
|
+
redeemed_offers: { A: 1, B: 1 },
|
|
288
|
+
wallet_redemptions: null,
|
|
289
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_DELIVERY,
|
|
290
|
+
price: 1000,
|
|
291
|
+
total_discount_applied: 10,
|
|
292
|
+
};
|
|
293
|
+
const after = (0, cart_actions_1.getClearVoucherCodeAndOffersFromCartData)(data, [], {}, ['A']);
|
|
294
|
+
const details = (after.order_discount_details ?? {});
|
|
295
|
+
// B is untouched: its slot, its amount, its reference and its claim survive.
|
|
296
|
+
expect(details.voucher_discount_id).toBe('B');
|
|
297
|
+
expect(details.voucher_reference_number).toBe('SAVE-B');
|
|
298
|
+
expect(details.voucher_discount).toBe(10);
|
|
299
|
+
expect(details.total_discount).toBe(10);
|
|
300
|
+
expect(after.redeemed_offers).toEqual({ B: 1 });
|
|
301
|
+
});
|
|
302
|
+
it('still clears the slot when the target offer is the one holding it', () => {
|
|
303
|
+
const data = {
|
|
304
|
+
categories: [
|
|
305
|
+
{
|
|
306
|
+
category_id: 'c',
|
|
307
|
+
name: 'C',
|
|
308
|
+
items: [
|
|
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
|
+
},
|
|
321
|
+
],
|
|
322
|
+
},
|
|
323
|
+
],
|
|
324
|
+
order_discount_details: {
|
|
325
|
+
voucher_discount_id: 'A',
|
|
326
|
+
voucher_reference_number: 'SAVE-A',
|
|
327
|
+
voucher_discount: 10,
|
|
328
|
+
total_discount: 10,
|
|
329
|
+
},
|
|
330
|
+
redeemed_offers: { A: 1 },
|
|
331
|
+
wallet_redemptions: null,
|
|
332
|
+
platform: constants_1.PLATFORMS_ENUM.WEB_DELIVERY,
|
|
333
|
+
price: 1000,
|
|
334
|
+
total_discount_applied: 10,
|
|
335
|
+
};
|
|
336
|
+
const after = (0, cart_actions_1.getClearVoucherCodeAndOffersFromCartData)(data, [], {}, ['A']);
|
|
337
|
+
const details = (after.order_discount_details ?? {});
|
|
338
|
+
expect(details.voucher_discount_id).toBeUndefined();
|
|
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);
|
|
343
|
+
});
|
|
344
|
+
});
|
|
199
345
|
describe('F-05: removing a reward item releases its claim', () => {
|
|
200
346
|
const paid = (id) => ({
|
|
201
347
|
_id: id,
|
|
@@ -243,6 +389,12 @@ describe('F-05: removing a reward item releases its claim', () => {
|
|
|
243
389
|
const result = (0, cart_actions_1.getRemoveItemFromCartData)(cartWith([paid('p1'), rewardItem('rw'), rewardItem('rw2')]), { _id: 'rw' }, [], {});
|
|
244
390
|
expect(result.redeemed_offers).toEqual({ o2: 1 });
|
|
245
391
|
});
|
|
392
|
+
it('also releases the claim on the update-item path', () => {
|
|
393
|
+
// The same removal can arrive as "update this item's quantity to 0" rather
|
|
394
|
+
// than as an explicit remove; both paths must reconcile the ledger.
|
|
395
|
+
const result = (0, cart_actions_1.getUpdateItemInCartData)(cartWith([paid('p1'), rewardItem('rw')]), { _id: 'rw', quantity: 0 }, [], {});
|
|
396
|
+
expect(result.redeemed_offers ?? null).toBeNull();
|
|
397
|
+
});
|
|
246
398
|
it('never touches the claim when a non-reward item is removed', () => {
|
|
247
399
|
const result = (0, cart_actions_1.getRemoveItemFromCartData)(cartWith([paid('p1'), paid('p2'), rewardItem('rw')]), { _id: 'p2' }, [], {});
|
|
248
400
|
expect(result.redeemed_offers).toEqual({ o2: 1 });
|
|
@@ -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
|
});
|