@managesome/eposmatic-toolkit 0.4.41 → 0.4.43
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/esm/helpers/utils/index.js +3 -0
- package/esm/helpers/utils/offer-claim-sequence.js +61 -0
- package/esm/helpers/utils/offer-item-resolution.js +47 -0
- package/esm/helpers/utils/offer-reward-claim.js +33 -0
- package/lib/helpers/store/cart-actions.d.ts +24 -0
- package/lib/helpers/store/cart-actions.js +59 -0
- package/lib/helpers/utils/index.d.ts +3 -0
- package/lib/helpers/utils/index.js +3 -0
- package/lib/helpers/utils/offer-claim-sequence.d.ts +42 -0
- package/lib/helpers/utils/offer-claim-sequence.js +66 -0
- package/lib/helpers/utils/offer-item-resolution.d.ts +51 -0
- package/lib/helpers/utils/offer-item-resolution.js +51 -0
- package/lib/helpers/utils/offer-reward-claim.d.ts +46 -0
- package/lib/helpers/utils/offer-reward-claim.js +36 -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 +107 -4
- package/lib/tests/offer-claim-sequence.test.d.ts +1 -0
- package/lib/tests/offer-claim-sequence.test.js +110 -0
- package/lib/tests/offer-item-resolution.test.d.ts +1 -0
- package/lib/tests/offer-item-resolution.test.js +99 -0
- package/lib/tests/offer-reward-claim.test.d.ts +1 -0
- package/lib/tests/offer-reward-claim.test.js +78 -0
- package/lib/tests/offers-matrix.test.js +31 -11
- 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
|
|
@@ -36,6 +36,9 @@ export * from './location-helpers';
|
|
|
36
36
|
export * from './menu-helpers';
|
|
37
37
|
export * from './minified-catalog';
|
|
38
38
|
export * from './offer-apply-helpers';
|
|
39
|
+
export * from './offer-item-resolution';
|
|
40
|
+
export * from './offer-claim-sequence';
|
|
41
|
+
export * from './offer-reward-claim';
|
|
39
42
|
export * from './offers-helpers';
|
|
40
43
|
export * from './operational-roles-helpers';
|
|
41
44
|
export * from './order-engine';
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { OFFER_REWARD_TYPE_ENUM } from '../../constants';
|
|
2
|
+
/**
|
|
3
|
+
* The voucher-linked claim sequence, in one place.
|
|
4
|
+
*
|
|
5
|
+
* It was copied into six apps (drawer, hook, promo banner, offers screen). One
|
|
6
|
+
* of those copies wrote the voucher code BEFORE resolving the reward item, which
|
|
7
|
+
* is how a sold-out reward left the customer with an applied code and nothing to
|
|
8
|
+
* claim. The order is the contract, and it lives here now.
|
|
9
|
+
*/
|
|
10
|
+
/** Trim and upper-case a code; an empty or whitespace-only code becomes null. */
|
|
11
|
+
export function normalizeVoucherCode(voucherCode) {
|
|
12
|
+
const normalized = voucherCode?.trim().toUpperCase() ?? '';
|
|
13
|
+
return normalized.length > 0 ? normalized : null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Does this reward need items already in the cart before it can be claimed?
|
|
17
|
+
*
|
|
18
|
+
* Order-level rewards (a discount, cashback, points, a gift card, a voucher)
|
|
19
|
+
* apply to a cart; item rewards bring their own item.
|
|
20
|
+
*/
|
|
21
|
+
export function offerNeedsExistingCartItems(offer) {
|
|
22
|
+
return (offer.reward_type !== OFFER_REWARD_TYPE_ENUM.ITEMS && offer.reward_type !== OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* One ordered claim. Resolve the reward BEFORE writing the code, and roll the
|
|
26
|
+
* code back if the later apply fails.
|
|
27
|
+
*
|
|
28
|
+
* Effects are injected so this stays pure: the caller owns dispatch, navigation
|
|
29
|
+
* and snackbars.
|
|
30
|
+
*/
|
|
31
|
+
export function claimVoucherLinkedOffer(steps) {
|
|
32
|
+
const code = normalizeVoucherCode(steps.code);
|
|
33
|
+
if (code == null) {
|
|
34
|
+
return { status: 'blank' };
|
|
35
|
+
}
|
|
36
|
+
if (steps.rewardItemIds.length > 0) {
|
|
37
|
+
const item = steps.resolveRewardItem();
|
|
38
|
+
if (item == null) {
|
|
39
|
+
// Nothing written: this is the whole point.
|
|
40
|
+
return { status: 'unavailable' };
|
|
41
|
+
}
|
|
42
|
+
if (!steps.applyVoucherCode(code)) {
|
|
43
|
+
return { status: 'write-failed' };
|
|
44
|
+
}
|
|
45
|
+
if (steps.rewardItemIds.length > 1) {
|
|
46
|
+
steps.openSelectionScreen();
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
steps.openItemDrawer(item);
|
|
50
|
+
}
|
|
51
|
+
return { status: 'opened' };
|
|
52
|
+
}
|
|
53
|
+
if (!steps.applyVoucherCode(code)) {
|
|
54
|
+
return { status: 'write-failed' };
|
|
55
|
+
}
|
|
56
|
+
if (!steps.applyLinkedOffer()) {
|
|
57
|
+
steps.rollback(steps.offerId);
|
|
58
|
+
return { status: 'failed' };
|
|
59
|
+
}
|
|
60
|
+
return { status: 'applied' };
|
|
61
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { OFFER_REWARD_TYPE_ENUM } from '../../constants';
|
|
2
|
+
const defaultIsSoldOut = (item) => item?.sold_out === true;
|
|
3
|
+
/**
|
|
4
|
+
* The first reward item the customer can actually receive, or null.
|
|
5
|
+
*
|
|
6
|
+
* Missing entries and sold-out items are skipped. Without a platform or a tenant
|
|
7
|
+
* there is nothing to resolve against, so the answer is null - which is what
|
|
8
|
+
* makes "don't write the voucher code yet" work (C7).
|
|
9
|
+
*/
|
|
10
|
+
export function resolveFirstAvailableRewardItem({ itemIds, mappedItems, platform, tenantId, convert, isSoldOut = defaultIsSoldOut, }) {
|
|
11
|
+
if (platform == null || tenantId == null)
|
|
12
|
+
return null;
|
|
13
|
+
for (let i = 0; i < itemIds.length; i++) {
|
|
14
|
+
const raw = mappedItems[itemIds[i]];
|
|
15
|
+
if (raw == null)
|
|
16
|
+
continue;
|
|
17
|
+
if (isSoldOut(raw))
|
|
18
|
+
continue;
|
|
19
|
+
const converted = convert(raw, platform, tenantId);
|
|
20
|
+
if (converted != null)
|
|
21
|
+
return converted;
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* How many items the reward-selection screen may offer.
|
|
27
|
+
*
|
|
28
|
+
* Rule, in one place:
|
|
29
|
+
* - voucher-linked claim -> the reward's `num_of_items` (one claim was bought);
|
|
30
|
+
* - otherwise -> `stats.applicable_quantity` (every claim the cart can support);
|
|
31
|
+
* - never below 1 when the reward grants at least one item.
|
|
32
|
+
*/
|
|
33
|
+
export function getRewardSelectionQuantity({ offer, stats, isVoucherLinked = false, }) {
|
|
34
|
+
const rewardNumOfItems = offer.reward_type === OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS
|
|
35
|
+
? offer.reward_discount_items?.num_of_items
|
|
36
|
+
: offer.reward_type === OFFER_REWARD_TYPE_ENUM.ITEMS
|
|
37
|
+
? offer.reward_items?.num_of_items
|
|
38
|
+
: undefined;
|
|
39
|
+
if (isVoucherLinked) {
|
|
40
|
+
return Math.max(1, rewardNumOfItems ?? 1);
|
|
41
|
+
}
|
|
42
|
+
const applicable = stats?.applicable_quantity;
|
|
43
|
+
if (applicable != null && Number.isFinite(applicable) && applicable > 0) {
|
|
44
|
+
return Math.floor(applicable);
|
|
45
|
+
}
|
|
46
|
+
return Math.max(1, rewardNumOfItems ?? 1);
|
|
47
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { OFFER_REWARD_TYPE_ENUM, PATHWAY_TO_UNLOCK_TYPE_ENUM } from '../../constants';
|
|
2
|
+
function getRewardExclusiveRefs(offer) {
|
|
3
|
+
if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
|
|
4
|
+
return offer.reward_discount_items?.exclusive_refs ?? [];
|
|
5
|
+
}
|
|
6
|
+
if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.ITEMS) {
|
|
7
|
+
return offer.reward_items?.exclusive_refs ?? [];
|
|
8
|
+
}
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Build the claim patch for a reward item: what the item is stamped with, and
|
|
13
|
+
* optionally the loyalty entry it applies.
|
|
14
|
+
*/
|
|
15
|
+
export function buildRewardClaim({ offer, catalogItemId, applicableQuantity, isVoucherLinked = offer.pathway_to_unlock_type === PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER, }) {
|
|
16
|
+
const quantity = Math.max(1, Math.floor(applicableQuantity));
|
|
17
|
+
const isGiftCard = offer.reward_type === OFFER_REWARD_TYPE_ENUM.GIFT_CARDS;
|
|
18
|
+
const patch = {
|
|
19
|
+
rewardClaim: isGiftCard ? { type: 'gift-card', giftCardId: offer._id } : { type: 'offer', offerId: offer._id },
|
|
20
|
+
};
|
|
21
|
+
// A gift-card reward always applies its own loyalty entry; an offer claim only
|
|
22
|
+
// does when the discount slot is free (i.e. not bought with a voucher code).
|
|
23
|
+
if (!isVoucherLinked || isGiftCard) {
|
|
24
|
+
patch.applyLoyalty = {
|
|
25
|
+
loyalty_id: offer._id,
|
|
26
|
+
name: offer.name,
|
|
27
|
+
refs: [...getRewardExclusiveRefs(offer), catalogItemId],
|
|
28
|
+
discount_details: null,
|
|
29
|
+
applicable_quantity: quantity,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return patch;
|
|
33
|
+
}
|
|
@@ -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
|
|
@@ -36,6 +36,9 @@ export * from './location-helpers';
|
|
|
36
36
|
export * from './menu-helpers';
|
|
37
37
|
export * from './minified-catalog';
|
|
38
38
|
export * from './offer-apply-helpers';
|
|
39
|
+
export * from './offer-item-resolution';
|
|
40
|
+
export * from './offer-claim-sequence';
|
|
41
|
+
export * from './offer-reward-claim';
|
|
39
42
|
export * from './offers-helpers';
|
|
40
43
|
export * from './operational-roles-helpers';
|
|
41
44
|
export * from './order-engine';
|
|
@@ -52,6 +52,9 @@ __exportStar(require("./location-helpers"), exports);
|
|
|
52
52
|
__exportStar(require("./menu-helpers"), exports);
|
|
53
53
|
__exportStar(require("./minified-catalog"), exports);
|
|
54
54
|
__exportStar(require("./offer-apply-helpers"), exports);
|
|
55
|
+
__exportStar(require("./offer-item-resolution"), exports);
|
|
56
|
+
__exportStar(require("./offer-claim-sequence"), exports);
|
|
57
|
+
__exportStar(require("./offer-reward-claim"), exports);
|
|
55
58
|
__exportStar(require("./offers-helpers"), exports);
|
|
56
59
|
__exportStar(require("./operational-roles-helpers"), exports);
|
|
57
60
|
__exportStar(require("./order-engine"), exports);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { IOffer } from '../../schemas';
|
|
2
|
+
/**
|
|
3
|
+
* The voucher-linked claim sequence, in one place.
|
|
4
|
+
*
|
|
5
|
+
* It was copied into six apps (drawer, hook, promo banner, offers screen). One
|
|
6
|
+
* of those copies wrote the voucher code BEFORE resolving the reward item, which
|
|
7
|
+
* is how a sold-out reward left the customer with an applied code and nothing to
|
|
8
|
+
* claim. The order is the contract, and it lives here now.
|
|
9
|
+
*/
|
|
10
|
+
/** Trim and upper-case a code; an empty or whitespace-only code becomes null. */
|
|
11
|
+
export declare function normalizeVoucherCode(voucherCode: string | null | undefined): string | null;
|
|
12
|
+
/**
|
|
13
|
+
* Does this reward need items already in the cart before it can be claimed?
|
|
14
|
+
*
|
|
15
|
+
* Order-level rewards (a discount, cashback, points, a gift card, a voucher)
|
|
16
|
+
* apply to a cart; item rewards bring their own item.
|
|
17
|
+
*/
|
|
18
|
+
export declare function offerNeedsExistingCartItems(offer: Pick<IOffer, 'reward_type'>): boolean;
|
|
19
|
+
export type TClaimVoucherLinkedOfferStatus = 'blank' | 'unavailable' | 'write-failed' | 'opened' | 'applied' | 'failed';
|
|
20
|
+
export interface IClaimVoucherLinkedOfferSteps<TItem> {
|
|
21
|
+
offerId: string;
|
|
22
|
+
code: string | null | undefined;
|
|
23
|
+
/** The reward's item ids. Empty means the reward is order-level. */
|
|
24
|
+
rewardItemIds: readonly string[];
|
|
25
|
+
resolveRewardItem: () => TItem | null | undefined;
|
|
26
|
+
applyVoucherCode: (code: string) => boolean;
|
|
27
|
+
openSelectionScreen: () => void;
|
|
28
|
+
openItemDrawer: (item: TItem) => void;
|
|
29
|
+
applyLinkedOffer: () => boolean;
|
|
30
|
+
rollback: (offerId: string) => void;
|
|
31
|
+
}
|
|
32
|
+
export interface IClaimVoucherLinkedOfferResult {
|
|
33
|
+
status: TClaimVoucherLinkedOfferStatus;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* One ordered claim. Resolve the reward BEFORE writing the code, and roll the
|
|
37
|
+
* code back if the later apply fails.
|
|
38
|
+
*
|
|
39
|
+
* Effects are injected so this stays pure: the caller owns dispatch, navigation
|
|
40
|
+
* and snackbars.
|
|
41
|
+
*/
|
|
42
|
+
export declare function claimVoucherLinkedOffer<TItem>(steps: IClaimVoucherLinkedOfferSteps<TItem>): IClaimVoucherLinkedOfferResult;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeVoucherCode = normalizeVoucherCode;
|
|
4
|
+
exports.offerNeedsExistingCartItems = offerNeedsExistingCartItems;
|
|
5
|
+
exports.claimVoucherLinkedOffer = claimVoucherLinkedOffer;
|
|
6
|
+
const constants_1 = require("../../constants");
|
|
7
|
+
/**
|
|
8
|
+
* The voucher-linked claim sequence, in one place.
|
|
9
|
+
*
|
|
10
|
+
* It was copied into six apps (drawer, hook, promo banner, offers screen). One
|
|
11
|
+
* of those copies wrote the voucher code BEFORE resolving the reward item, which
|
|
12
|
+
* is how a sold-out reward left the customer with an applied code and nothing to
|
|
13
|
+
* claim. The order is the contract, and it lives here now.
|
|
14
|
+
*/
|
|
15
|
+
/** Trim and upper-case a code; an empty or whitespace-only code becomes null. */
|
|
16
|
+
function normalizeVoucherCode(voucherCode) {
|
|
17
|
+
const normalized = voucherCode?.trim().toUpperCase() ?? '';
|
|
18
|
+
return normalized.length > 0 ? normalized : null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Does this reward need items already in the cart before it can be claimed?
|
|
22
|
+
*
|
|
23
|
+
* Order-level rewards (a discount, cashback, points, a gift card, a voucher)
|
|
24
|
+
* apply to a cart; item rewards bring their own item.
|
|
25
|
+
*/
|
|
26
|
+
function offerNeedsExistingCartItems(offer) {
|
|
27
|
+
return (offer.reward_type !== constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS && offer.reward_type !== constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* One ordered claim. Resolve the reward BEFORE writing the code, and roll the
|
|
31
|
+
* code back if the later apply fails.
|
|
32
|
+
*
|
|
33
|
+
* Effects are injected so this stays pure: the caller owns dispatch, navigation
|
|
34
|
+
* and snackbars.
|
|
35
|
+
*/
|
|
36
|
+
function claimVoucherLinkedOffer(steps) {
|
|
37
|
+
const code = normalizeVoucherCode(steps.code);
|
|
38
|
+
if (code == null) {
|
|
39
|
+
return { status: 'blank' };
|
|
40
|
+
}
|
|
41
|
+
if (steps.rewardItemIds.length > 0) {
|
|
42
|
+
const item = steps.resolveRewardItem();
|
|
43
|
+
if (item == null) {
|
|
44
|
+
// Nothing written: this is the whole point.
|
|
45
|
+
return { status: 'unavailable' };
|
|
46
|
+
}
|
|
47
|
+
if (!steps.applyVoucherCode(code)) {
|
|
48
|
+
return { status: 'write-failed' };
|
|
49
|
+
}
|
|
50
|
+
if (steps.rewardItemIds.length > 1) {
|
|
51
|
+
steps.openSelectionScreen();
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
steps.openItemDrawer(item);
|
|
55
|
+
}
|
|
56
|
+
return { status: 'opened' };
|
|
57
|
+
}
|
|
58
|
+
if (!steps.applyVoucherCode(code)) {
|
|
59
|
+
return { status: 'write-failed' };
|
|
60
|
+
}
|
|
61
|
+
if (!steps.applyLinkedOffer()) {
|
|
62
|
+
steps.rollback(steps.offerId);
|
|
63
|
+
return { status: 'failed' };
|
|
64
|
+
}
|
|
65
|
+
return { status: 'applied' };
|
|
66
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { IOffer } from '../../schemas';
|
|
2
|
+
import type { IOfferWithStats } from './offers-helpers';
|
|
3
|
+
/**
|
|
4
|
+
* Single source for the questions every frontend asks before it can claim a
|
|
5
|
+
* voucher-linked reward. They were answered in six drawers, five hooks and one
|
|
6
|
+
* mobile screen; when the answer changed, five of those places did not.
|
|
7
|
+
*
|
|
8
|
+
* Everything here is pure: callers pass the catalog, the platform and the tenant
|
|
9
|
+
* in. Nothing reads a store, a hook or a global.
|
|
10
|
+
*/
|
|
11
|
+
export interface IResolveRewardItemParams<TItem> {
|
|
12
|
+
/** The reward's item ids, in priority order. */
|
|
13
|
+
itemIds: readonly string[];
|
|
14
|
+
/** The app's mapped simplified catalog, keyed by item id. */
|
|
15
|
+
mappedItems: Record<string, TItem | undefined>;
|
|
16
|
+
/** Platform the order is on; resolution is impossible without it. */
|
|
17
|
+
platform: string | null | undefined;
|
|
18
|
+
/** Tenant the order belongs to. */
|
|
19
|
+
tenantId: string | null | undefined;
|
|
20
|
+
/** Convert a mapped catalog entry into the app's workable item. */
|
|
21
|
+
convert: (item: TItem, platform: string, tenantId: string) => unknown | null | undefined;
|
|
22
|
+
/** True when the entry cannot be sold. Defaults to `item.sold_out`. */
|
|
23
|
+
isSoldOut?: (item: TItem) => boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The first reward item the customer can actually receive, or null.
|
|
27
|
+
*
|
|
28
|
+
* Missing entries and sold-out items are skipped. Without a platform or a tenant
|
|
29
|
+
* there is nothing to resolve against, so the answer is null - which is what
|
|
30
|
+
* makes "don't write the voucher code yet" work (C7).
|
|
31
|
+
*/
|
|
32
|
+
export declare function resolveFirstAvailableRewardItem<TItem>({ itemIds, mappedItems, platform, tenantId, convert, isSoldOut, }: IResolveRewardItemParams<TItem>): unknown | null;
|
|
33
|
+
export interface IGetRewardSelectionQuantityParams {
|
|
34
|
+
offer: IOffer;
|
|
35
|
+
stats: IOfferWithStats['stats'] | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* True when one voucher code buys ONE claim. A voucher-linked claim is still
|
|
38
|
+
* worth the reward's `num_of_items` items, but never the total of several
|
|
39
|
+
* claims - the customer only paid for one.
|
|
40
|
+
*/
|
|
41
|
+
isVoucherLinked?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* How many items the reward-selection screen may offer.
|
|
45
|
+
*
|
|
46
|
+
* Rule, in one place:
|
|
47
|
+
* - voucher-linked claim -> the reward's `num_of_items` (one claim was bought);
|
|
48
|
+
* - otherwise -> `stats.applicable_quantity` (every claim the cart can support);
|
|
49
|
+
* - never below 1 when the reward grants at least one item.
|
|
50
|
+
*/
|
|
51
|
+
export declare function getRewardSelectionQuantity({ offer, stats, isVoucherLinked, }: IGetRewardSelectionQuantityParams): number;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveFirstAvailableRewardItem = resolveFirstAvailableRewardItem;
|
|
4
|
+
exports.getRewardSelectionQuantity = getRewardSelectionQuantity;
|
|
5
|
+
const constants_1 = require("../../constants");
|
|
6
|
+
const defaultIsSoldOut = (item) => item?.sold_out === true;
|
|
7
|
+
/**
|
|
8
|
+
* The first reward item the customer can actually receive, or null.
|
|
9
|
+
*
|
|
10
|
+
* Missing entries and sold-out items are skipped. Without a platform or a tenant
|
|
11
|
+
* there is nothing to resolve against, so the answer is null - which is what
|
|
12
|
+
* makes "don't write the voucher code yet" work (C7).
|
|
13
|
+
*/
|
|
14
|
+
function resolveFirstAvailableRewardItem({ itemIds, mappedItems, platform, tenantId, convert, isSoldOut = defaultIsSoldOut, }) {
|
|
15
|
+
if (platform == null || tenantId == null)
|
|
16
|
+
return null;
|
|
17
|
+
for (let i = 0; i < itemIds.length; i++) {
|
|
18
|
+
const raw = mappedItems[itemIds[i]];
|
|
19
|
+
if (raw == null)
|
|
20
|
+
continue;
|
|
21
|
+
if (isSoldOut(raw))
|
|
22
|
+
continue;
|
|
23
|
+
const converted = convert(raw, platform, tenantId);
|
|
24
|
+
if (converted != null)
|
|
25
|
+
return converted;
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* How many items the reward-selection screen may offer.
|
|
31
|
+
*
|
|
32
|
+
* Rule, in one place:
|
|
33
|
+
* - voucher-linked claim -> the reward's `num_of_items` (one claim was bought);
|
|
34
|
+
* - otherwise -> `stats.applicable_quantity` (every claim the cart can support);
|
|
35
|
+
* - never below 1 when the reward grants at least one item.
|
|
36
|
+
*/
|
|
37
|
+
function getRewardSelectionQuantity({ offer, stats, isVoucherLinked = false, }) {
|
|
38
|
+
const rewardNumOfItems = offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS
|
|
39
|
+
? offer.reward_discount_items?.num_of_items
|
|
40
|
+
: offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS
|
|
41
|
+
? offer.reward_items?.num_of_items
|
|
42
|
+
: undefined;
|
|
43
|
+
if (isVoucherLinked) {
|
|
44
|
+
return Math.max(1, rewardNumOfItems ?? 1);
|
|
45
|
+
}
|
|
46
|
+
const applicable = stats?.applicable_quantity;
|
|
47
|
+
if (applicable != null && Number.isFinite(applicable) && applicable > 0) {
|
|
48
|
+
return Math.floor(applicable);
|
|
49
|
+
}
|
|
50
|
+
return Math.max(1, rewardNumOfItems ?? 1);
|
|
51
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { IOffer } from '../../schemas';
|
|
2
|
+
/**
|
|
3
|
+
* The claim a free reward item carries when it is added to the cart.
|
|
4
|
+
*
|
|
5
|
+
* Every drawer and the mobile offers screen built this object by hand, and the
|
|
6
|
+
* two of them disagreed: a voucher-linked claim set `applyLoyalty` on one screen
|
|
7
|
+
* and not on another. What the item carries decides which offer the item is
|
|
8
|
+
* stamped with, so it is the same money path as the ledger entry.
|
|
9
|
+
*/
|
|
10
|
+
export type TRewardClaim = {
|
|
11
|
+
type: 'offer';
|
|
12
|
+
offerId: string;
|
|
13
|
+
} | {
|
|
14
|
+
type: 'gift-card';
|
|
15
|
+
giftCardId: string;
|
|
16
|
+
};
|
|
17
|
+
export interface IRewardClaimLoyalty {
|
|
18
|
+
loyalty_id: string;
|
|
19
|
+
name: string;
|
|
20
|
+
/** Options/items the reward may not discount, plus the item itself. */
|
|
21
|
+
refs: string[];
|
|
22
|
+
discount_details: null;
|
|
23
|
+
applicable_quantity: number;
|
|
24
|
+
}
|
|
25
|
+
export interface IBuildRewardClaimParams {
|
|
26
|
+
offer: IOffer;
|
|
27
|
+
/** The catalog item being added as the reward. */
|
|
28
|
+
catalogItemId: string;
|
|
29
|
+
/** How many items one claim is worth. */
|
|
30
|
+
applicableQuantity: number;
|
|
31
|
+
/**
|
|
32
|
+
* Set when the claim was bought with a voucher code. Voucher-linked claims do
|
|
33
|
+
* not set `applyLoyalty` - the code already occupies the discount slot.
|
|
34
|
+
* Defaults to the offer's own pathway.
|
|
35
|
+
*/
|
|
36
|
+
isVoucherLinked?: boolean;
|
|
37
|
+
}
|
|
38
|
+
export interface IRewardClaimPatch {
|
|
39
|
+
rewardClaim: TRewardClaim;
|
|
40
|
+
applyLoyalty?: IRewardClaimLoyalty;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Build the claim patch for a reward item: what the item is stamped with, and
|
|
44
|
+
* optionally the loyalty entry it applies.
|
|
45
|
+
*/
|
|
46
|
+
export declare function buildRewardClaim({ offer, catalogItemId, applicableQuantity, isVoucherLinked, }: IBuildRewardClaimParams): IRewardClaimPatch;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildRewardClaim = buildRewardClaim;
|
|
4
|
+
const constants_1 = require("../../constants");
|
|
5
|
+
function getRewardExclusiveRefs(offer) {
|
|
6
|
+
if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
|
|
7
|
+
return offer.reward_discount_items?.exclusive_refs ?? [];
|
|
8
|
+
}
|
|
9
|
+
if (offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS) {
|
|
10
|
+
return offer.reward_items?.exclusive_refs ?? [];
|
|
11
|
+
}
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Build the claim patch for a reward item: what the item is stamped with, and
|
|
16
|
+
* optionally the loyalty entry it applies.
|
|
17
|
+
*/
|
|
18
|
+
function buildRewardClaim({ offer, catalogItemId, applicableQuantity, isVoucherLinked = offer.pathway_to_unlock_type === constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER, }) {
|
|
19
|
+
const quantity = Math.max(1, Math.floor(applicableQuantity));
|
|
20
|
+
const isGiftCard = offer.reward_type === constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS;
|
|
21
|
+
const patch = {
|
|
22
|
+
rewardClaim: isGiftCard ? { type: 'gift-card', giftCardId: offer._id } : { type: 'offer', offerId: offer._id },
|
|
23
|
+
};
|
|
24
|
+
// A gift-card reward always applies its own loyalty entry; an offer claim only
|
|
25
|
+
// does when the discount slot is free (i.e. not bought with a voucher code).
|
|
26
|
+
if (!isVoucherLinked || isGiftCard) {
|
|
27
|
+
patch.applyLoyalty = {
|
|
28
|
+
loyalty_id: offer._id,
|
|
29
|
+
name: offer.name,
|
|
30
|
+
refs: [...getRewardExclusiveRefs(offer), catalogItemId],
|
|
31
|
+
discount_details: null,
|
|
32
|
+
applicable_quantity: quantity,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return patch;
|
|
36
|
+
}
|
|
@@ -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,83 @@ 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
|
+
{
|
|
236
|
+
_id: 'ci1',
|
|
237
|
+
item_id: 'i1',
|
|
238
|
+
name: 'A',
|
|
239
|
+
quantity: 1,
|
|
240
|
+
price: 1000,
|
|
241
|
+
subtotal: 1000,
|
|
242
|
+
tax_rate: 0,
|
|
243
|
+
modifiers: [],
|
|
244
|
+
special_instructions: '',
|
|
245
|
+
item_discount_details: null,
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
_id: 'ci2',
|
|
249
|
+
item_id: 'i1',
|
|
250
|
+
name: 'A',
|
|
251
|
+
quantity: 1,
|
|
252
|
+
price: 1000,
|
|
253
|
+
subtotal: 0,
|
|
254
|
+
tax_rate: 0,
|
|
255
|
+
modifiers: [],
|
|
256
|
+
special_instructions: '',
|
|
257
|
+
item_discount_details: { loyalty_discount_id: 'o', loyalty_discount: 0, total_discount: 0 },
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
},
|
|
261
|
+
];
|
|
262
|
+
it('counts an order-level claim from the ledger', () => {
|
|
263
|
+
const { stats } = statsFor(orderLevelOffer(), { redeemed: { o: 1 } });
|
|
264
|
+
expect(stats.redeemed_times).toBe(1);
|
|
265
|
+
expect(stats.claimable).toBe(false);
|
|
266
|
+
});
|
|
267
|
+
it('counts an item reward claim from stamped cart items', () => {
|
|
268
|
+
const { stats } = statsFor(itemOffer(), { redeemed: { o: 1 }, categories: cartWithStampedItem() });
|
|
269
|
+
expect(stats.redeemed_times).toBe(1);
|
|
270
|
+
expect(stats.claimable).toBe(false);
|
|
271
|
+
});
|
|
272
|
+
it('ignores a ledger-only claim for an item reward', () => {
|
|
273
|
+
// Deliberate: item rewards trust stamped items, not the ledger. This is the
|
|
274
|
+
// #882 trade-off - pin it so nobody "fixes" it back into reading the ledger.
|
|
275
|
+
const { stats } = statsFor(itemOffer(), { redeemed: { o: 1 } });
|
|
276
|
+
expect(stats.redeemed_times).toBe(0);
|
|
277
|
+
expect(stats.claimable).toBe(true);
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
describe("clearing one offer must not wipe another offer's voucher slot", () => {
|
|
205
281
|
it('keeps the slot when a different offer holds it', () => {
|
|
206
282
|
const data = {
|
|
207
283
|
categories: [
|
|
@@ -209,7 +285,18 @@ describe('clearing one offer must not wipe another offer\'s voucher slot', () =>
|
|
|
209
285
|
category_id: 'c',
|
|
210
286
|
name: 'C',
|
|
211
287
|
items: [
|
|
212
|
-
{
|
|
288
|
+
{
|
|
289
|
+
_id: 'p1',
|
|
290
|
+
item_id: 'i1',
|
|
291
|
+
name: 'A',
|
|
292
|
+
quantity: 1,
|
|
293
|
+
price: 1000,
|
|
294
|
+
subtotal: 1000,
|
|
295
|
+
tax_rate: 0,
|
|
296
|
+
modifiers: [],
|
|
297
|
+
special_instructions: '',
|
|
298
|
+
item_discount_details: null,
|
|
299
|
+
},
|
|
213
300
|
],
|
|
214
301
|
},
|
|
215
302
|
],
|
|
@@ -227,9 +314,11 @@ describe('clearing one offer must not wipe another offer\'s voucher slot', () =>
|
|
|
227
314
|
};
|
|
228
315
|
const after = (0, cart_actions_1.getClearVoucherCodeAndOffersFromCartData)(data, [], {}, ['A']);
|
|
229
316
|
const details = (after.order_discount_details ?? {});
|
|
230
|
-
// B is untouched: its slot, its reference and its claim
|
|
317
|
+
// B is untouched: its slot, its amount, its reference and its claim survive.
|
|
231
318
|
expect(details.voucher_discount_id).toBe('B');
|
|
232
319
|
expect(details.voucher_reference_number).toBe('SAVE-B');
|
|
320
|
+
expect(details.voucher_discount).toBe(10);
|
|
321
|
+
expect(details.total_discount).toBe(10);
|
|
233
322
|
expect(after.redeemed_offers).toEqual({ B: 1 });
|
|
234
323
|
});
|
|
235
324
|
it('still clears the slot when the target offer is the one holding it', () => {
|
|
@@ -239,7 +328,18 @@ describe('clearing one offer must not wipe another offer\'s voucher slot', () =>
|
|
|
239
328
|
category_id: 'c',
|
|
240
329
|
name: 'C',
|
|
241
330
|
items: [
|
|
242
|
-
{
|
|
331
|
+
{
|
|
332
|
+
_id: 'p1',
|
|
333
|
+
item_id: 'i1',
|
|
334
|
+
name: 'A',
|
|
335
|
+
quantity: 1,
|
|
336
|
+
price: 1000,
|
|
337
|
+
subtotal: 1000,
|
|
338
|
+
tax_rate: 0,
|
|
339
|
+
modifiers: [],
|
|
340
|
+
special_instructions: '',
|
|
341
|
+
item_discount_details: null,
|
|
342
|
+
},
|
|
243
343
|
],
|
|
244
344
|
},
|
|
245
345
|
],
|
|
@@ -259,6 +359,9 @@ describe('clearing one offer must not wipe another offer\'s voucher slot', () =>
|
|
|
259
359
|
const details = (after.order_discount_details ?? {});
|
|
260
360
|
expect(details.voucher_discount_id).toBeUndefined();
|
|
261
361
|
expect(details.voucher_reference_number).toBeUndefined();
|
|
362
|
+
// the money goes with the slot, not just the ids
|
|
363
|
+
expect(details.voucher_discount).toBeUndefined();
|
|
364
|
+
expect(details.total_discount).toBe(0);
|
|
262
365
|
});
|
|
263
366
|
});
|
|
264
367
|
describe('F-05: removing a reward item releases its claim', () => {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../constants");
|
|
4
|
+
const offer_claim_sequence_1 = require("../helpers/utils/offer-claim-sequence");
|
|
5
|
+
/**
|
|
6
|
+
* The claim sequence, tested once for all six frontends.
|
|
7
|
+
*
|
|
8
|
+
* The defect this encodes: the code used to be written before the reward item
|
|
9
|
+
* was resolved, so a sold-out reward left the voucher applied with nothing to
|
|
10
|
+
* claim.
|
|
11
|
+
*/
|
|
12
|
+
function makeSteps(overrides = {}) {
|
|
13
|
+
const calls = [];
|
|
14
|
+
const steps = {
|
|
15
|
+
offerId: 'offer-1',
|
|
16
|
+
code: 'save10',
|
|
17
|
+
rewardItemIds: ['item-1'],
|
|
18
|
+
resolveRewardItem: jest.fn(() => {
|
|
19
|
+
calls.push('resolve');
|
|
20
|
+
return { _id: 'item-1' };
|
|
21
|
+
}),
|
|
22
|
+
applyVoucherCode: jest.fn(() => {
|
|
23
|
+
calls.push('write');
|
|
24
|
+
return true;
|
|
25
|
+
}),
|
|
26
|
+
openSelectionScreen: jest.fn(() => calls.push('selection')),
|
|
27
|
+
openItemDrawer: jest.fn(() => calls.push('drawer')),
|
|
28
|
+
applyLinkedOffer: jest.fn(() => {
|
|
29
|
+
calls.push('apply');
|
|
30
|
+
return true;
|
|
31
|
+
}),
|
|
32
|
+
rollback: jest.fn(() => calls.push('rollback')),
|
|
33
|
+
...overrides,
|
|
34
|
+
};
|
|
35
|
+
return { steps: steps, calls };
|
|
36
|
+
}
|
|
37
|
+
describe('normalizeVoucherCode', () => {
|
|
38
|
+
it('trims and upper-cases', () => {
|
|
39
|
+
expect((0, offer_claim_sequence_1.normalizeVoucherCode)(' save10 ')).toBe('SAVE10');
|
|
40
|
+
});
|
|
41
|
+
it('is null for blank input', () => {
|
|
42
|
+
expect((0, offer_claim_sequence_1.normalizeVoucherCode)('')).toBeNull();
|
|
43
|
+
expect((0, offer_claim_sequence_1.normalizeVoucherCode)(' ')).toBeNull();
|
|
44
|
+
expect((0, offer_claim_sequence_1.normalizeVoucherCode)(null)).toBeNull();
|
|
45
|
+
expect((0, offer_claim_sequence_1.normalizeVoucherCode)(undefined)).toBeNull();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
describe('offerNeedsExistingCartItems', () => {
|
|
49
|
+
it('is false for rewards that bring their own item', () => {
|
|
50
|
+
expect((0, offer_claim_sequence_1.offerNeedsExistingCartItems)({ reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS })).toBe(false);
|
|
51
|
+
expect((0, offer_claim_sequence_1.offerNeedsExistingCartItems)({ reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS })).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
it('is true for order-level rewards', () => {
|
|
54
|
+
expect((0, offer_claim_sequence_1.offerNeedsExistingCartItems)({ reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT })).toBe(true);
|
|
55
|
+
expect((0, offer_claim_sequence_1.offerNeedsExistingCartItems)({ reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK })).toBe(true);
|
|
56
|
+
expect((0, offer_claim_sequence_1.offerNeedsExistingCartItems)({ reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.POINTS })).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
describe('claimVoucherLinkedOffer - item rewards', () => {
|
|
60
|
+
it('writes nothing when the reward item is unavailable', () => {
|
|
61
|
+
const { steps } = makeSteps({ resolveRewardItem: jest.fn(() => null) });
|
|
62
|
+
expect((0, offer_claim_sequence_1.claimVoucherLinkedOffer)(steps)).toEqual({ status: 'unavailable' });
|
|
63
|
+
expect(steps.applyVoucherCode).not.toHaveBeenCalled();
|
|
64
|
+
expect(steps.openItemDrawer).not.toHaveBeenCalled();
|
|
65
|
+
expect(steps.openSelectionScreen).not.toHaveBeenCalled();
|
|
66
|
+
});
|
|
67
|
+
it('resolves before writing', () => {
|
|
68
|
+
const { steps, calls } = makeSteps();
|
|
69
|
+
(0, offer_claim_sequence_1.claimVoucherLinkedOffer)(steps);
|
|
70
|
+
expect(calls).toEqual(['resolve', 'write', 'drawer']);
|
|
71
|
+
});
|
|
72
|
+
it('opens the selection screen for a multi-item reward', () => {
|
|
73
|
+
const { steps } = makeSteps({ rewardItemIds: ['item-1', 'item-2'] });
|
|
74
|
+
expect((0, offer_claim_sequence_1.claimVoucherLinkedOffer)(steps)).toEqual({ status: 'opened' });
|
|
75
|
+
expect(steps.openSelectionScreen).toHaveBeenCalledTimes(1);
|
|
76
|
+
expect(steps.openItemDrawer).not.toHaveBeenCalled();
|
|
77
|
+
});
|
|
78
|
+
it('opens the drawer with the resolved item', () => {
|
|
79
|
+
const { steps } = makeSteps();
|
|
80
|
+
(0, offer_claim_sequence_1.claimVoucherLinkedOffer)(steps);
|
|
81
|
+
expect(steps.openItemDrawer).toHaveBeenCalledWith({ _id: 'item-1' });
|
|
82
|
+
});
|
|
83
|
+
it('stops when the code write fails', () => {
|
|
84
|
+
const { steps } = makeSteps({ applyVoucherCode: jest.fn(() => false) });
|
|
85
|
+
expect((0, offer_claim_sequence_1.claimVoucherLinkedOffer)(steps)).toEqual({ status: 'write-failed' });
|
|
86
|
+
expect(steps.openItemDrawer).not.toHaveBeenCalled();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
describe('claimVoucherLinkedOffer - order-level rewards', () => {
|
|
90
|
+
it('writes, applies and reports success', () => {
|
|
91
|
+
const { steps, calls } = makeSteps({ rewardItemIds: [] });
|
|
92
|
+
expect((0, offer_claim_sequence_1.claimVoucherLinkedOffer)(steps)).toEqual({ status: 'applied' });
|
|
93
|
+
expect(calls).toEqual(['write', 'apply']);
|
|
94
|
+
expect(steps.rollback).not.toHaveBeenCalled();
|
|
95
|
+
});
|
|
96
|
+
it('rolls the code back when the apply fails', () => {
|
|
97
|
+
const { steps } = makeSteps({
|
|
98
|
+
rewardItemIds: [],
|
|
99
|
+
applyLinkedOffer: jest.fn(() => false),
|
|
100
|
+
});
|
|
101
|
+
expect((0, offer_claim_sequence_1.claimVoucherLinkedOffer)(steps)).toEqual({ status: 'failed' });
|
|
102
|
+
expect(steps.rollback).toHaveBeenCalledWith('offer-1');
|
|
103
|
+
});
|
|
104
|
+
it('never applies an offer for a blank code', () => {
|
|
105
|
+
const { steps } = makeSteps({ rewardItemIds: [], code: ' ' });
|
|
106
|
+
expect((0, offer_claim_sequence_1.claimVoucherLinkedOffer)(steps)).toEqual({ status: 'blank' });
|
|
107
|
+
expect(steps.applyVoucherCode).not.toHaveBeenCalled();
|
|
108
|
+
expect(steps.applyLinkedOffer).not.toHaveBeenCalled();
|
|
109
|
+
});
|
|
110
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../constants");
|
|
4
|
+
const offer_item_resolution_1 = require("../helpers/utils/offer-item-resolution");
|
|
5
|
+
/**
|
|
6
|
+
* Phase 0-1 of the offers unification: the two questions every frontend was
|
|
7
|
+
* answering privately.
|
|
8
|
+
*
|
|
9
|
+
* They are pure on purpose - the catalog, platform and tenant are arguments, so
|
|
10
|
+
* the same answer is available to a hook, a drawer and a mobile screen.
|
|
11
|
+
*/
|
|
12
|
+
const mapped = (entries) => entries;
|
|
13
|
+
const convert = (item) => ({ converted: item._id });
|
|
14
|
+
const baseOffer = {
|
|
15
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
|
|
16
|
+
reward_items: { num_of_items: 3 },
|
|
17
|
+
};
|
|
18
|
+
describe('resolveFirstAvailableRewardItem', () => {
|
|
19
|
+
it('returns null without a platform or a tenant', () => {
|
|
20
|
+
expect((0, offer_item_resolution_1.resolveFirstAvailableRewardItem)({
|
|
21
|
+
itemIds: ['a'],
|
|
22
|
+
mappedItems: mapped({ a: { _id: 'a' } }),
|
|
23
|
+
platform: undefined,
|
|
24
|
+
tenantId: 't1',
|
|
25
|
+
convert,
|
|
26
|
+
})).toBeNull();
|
|
27
|
+
expect((0, offer_item_resolution_1.resolveFirstAvailableRewardItem)({
|
|
28
|
+
itemIds: ['a'],
|
|
29
|
+
mappedItems: mapped({ a: { _id: 'a' } }),
|
|
30
|
+
platform: 'WEB_DELIVERY',
|
|
31
|
+
tenantId: undefined,
|
|
32
|
+
convert,
|
|
33
|
+
})).toBeNull();
|
|
34
|
+
});
|
|
35
|
+
it('skips missing and sold-out entries', () => {
|
|
36
|
+
const resolved = (0, offer_item_resolution_1.resolveFirstAvailableRewardItem)({
|
|
37
|
+
itemIds: ['missing', 'sold-out', 'ok'],
|
|
38
|
+
mappedItems: mapped({
|
|
39
|
+
'sold-out': { _id: 'sold-out', sold_out: true },
|
|
40
|
+
ok: { _id: 'ok', sold_out: false },
|
|
41
|
+
}),
|
|
42
|
+
platform: 'WEB_DELIVERY',
|
|
43
|
+
tenantId: 't1',
|
|
44
|
+
convert,
|
|
45
|
+
});
|
|
46
|
+
expect(resolved).toEqual({ converted: 'ok' });
|
|
47
|
+
});
|
|
48
|
+
it('returns null when the converter refuses every item', () => {
|
|
49
|
+
const resolved = (0, offer_item_resolution_1.resolveFirstAvailableRewardItem)({
|
|
50
|
+
itemIds: ['a', 'b'],
|
|
51
|
+
mappedItems: mapped({ a: { _id: 'a' }, b: { _id: 'b' } }),
|
|
52
|
+
platform: 'WEB_DELIVERY',
|
|
53
|
+
tenantId: 't1',
|
|
54
|
+
convert: () => null,
|
|
55
|
+
});
|
|
56
|
+
expect(resolved).toBeNull();
|
|
57
|
+
});
|
|
58
|
+
it('honours a custom sold-out predicate', () => {
|
|
59
|
+
const resolved = (0, offer_item_resolution_1.resolveFirstAvailableRewardItem)({
|
|
60
|
+
itemIds: ['a'],
|
|
61
|
+
mappedItems: mapped({ a: { _id: 'a', unavailable: true } }),
|
|
62
|
+
platform: 'WEB_DELIVERY',
|
|
63
|
+
tenantId: 't1',
|
|
64
|
+
convert,
|
|
65
|
+
isSoldOut: (item) => item.unavailable === true,
|
|
66
|
+
});
|
|
67
|
+
expect(resolved).toBeNull();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
describe('getRewardSelectionQuantity', () => {
|
|
71
|
+
const stats = (applicable) => ({ applicable_quantity: applicable });
|
|
72
|
+
it('lets a voucher-linked claim buy only the reward itself', () => {
|
|
73
|
+
// one code = one claim, worth num_of_items items - never the total of several
|
|
74
|
+
expect((0, offer_item_resolution_1.getRewardSelectionQuantity)({ offer: baseOffer, stats: stats(9), isVoucherLinked: true })).toBe(3);
|
|
75
|
+
});
|
|
76
|
+
it('uses every applicable claim when the customer bought them', () => {
|
|
77
|
+
expect((0, offer_item_resolution_1.getRewardSelectionQuantity)({ offer: baseOffer, stats: stats(9) })).toBe(9);
|
|
78
|
+
});
|
|
79
|
+
it('falls back to the reward when stats are missing or empty', () => {
|
|
80
|
+
expect((0, offer_item_resolution_1.getRewardSelectionQuantity)({ offer: baseOffer, stats: undefined })).toBe(3);
|
|
81
|
+
expect((0, offer_item_resolution_1.getRewardSelectionQuantity)({ offer: baseOffer, stats: stats(0) })).toBe(3);
|
|
82
|
+
});
|
|
83
|
+
it('reads DISCOUNT_ITEMS rewards too', () => {
|
|
84
|
+
const discountItemsOffer = {
|
|
85
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS,
|
|
86
|
+
reward_discount_items: { num_of_items: 2 },
|
|
87
|
+
};
|
|
88
|
+
expect((0, offer_item_resolution_1.getRewardSelectionQuantity)({ offer: discountItemsOffer, stats: stats(7) })).toBe(7);
|
|
89
|
+
expect((0, offer_item_resolution_1.getRewardSelectionQuantity)({
|
|
90
|
+
offer: discountItemsOffer,
|
|
91
|
+
stats: stats(7),
|
|
92
|
+
isVoucherLinked: true,
|
|
93
|
+
})).toBe(2);
|
|
94
|
+
});
|
|
95
|
+
it('never returns less than one item', () => {
|
|
96
|
+
const noItems = { reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS, reward_items: { num_of_items: 0 } };
|
|
97
|
+
expect((0, offer_item_resolution_1.getRewardSelectionQuantity)({ offer: noItems, stats: undefined })).toBe(1);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../constants");
|
|
4
|
+
const offer_reward_claim_1 = require("../helpers/utils/offer-reward-claim");
|
|
5
|
+
/**
|
|
6
|
+
* What a free reward item carries into the cart.
|
|
7
|
+
*
|
|
8
|
+
* Two screens built this by hand and disagreed about `applyLoyalty` for a
|
|
9
|
+
* voucher-linked claim, so the rule is pinned here once.
|
|
10
|
+
*/
|
|
11
|
+
const offer = (overrides = {}) => ({
|
|
12
|
+
_id: 'offer-1',
|
|
13
|
+
name: 'Free item',
|
|
14
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
|
|
15
|
+
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS,
|
|
16
|
+
reward_items: { num_of_items: 2, exclusive_refs: ['option-x'] },
|
|
17
|
+
...overrides,
|
|
18
|
+
});
|
|
19
|
+
describe('buildRewardClaim', () => {
|
|
20
|
+
it('stamps the offer and applies its loyalty entry', () => {
|
|
21
|
+
const patch = (0, offer_reward_claim_1.buildRewardClaim)({ offer: offer(), catalogItemId: 'item-1', applicableQuantity: 2 });
|
|
22
|
+
expect(patch.rewardClaim).toEqual({ type: 'offer', offerId: 'offer-1' });
|
|
23
|
+
expect(patch.applyLoyalty).toEqual({
|
|
24
|
+
loyalty_id: 'offer-1',
|
|
25
|
+
name: 'Free item',
|
|
26
|
+
refs: ['option-x', 'item-1'],
|
|
27
|
+
discount_details: null,
|
|
28
|
+
applicable_quantity: 2,
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
it('does not apply loyalty for a voucher-linked claim', () => {
|
|
32
|
+
// the voucher code already occupies the discount slot
|
|
33
|
+
const patch = (0, offer_reward_claim_1.buildRewardClaim)({
|
|
34
|
+
offer: offer({ pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER }),
|
|
35
|
+
catalogItemId: 'item-1',
|
|
36
|
+
applicableQuantity: 2,
|
|
37
|
+
});
|
|
38
|
+
expect(patch.rewardClaim).toEqual({ type: 'offer', offerId: 'offer-1' });
|
|
39
|
+
expect(patch.applyLoyalty).toBeUndefined();
|
|
40
|
+
});
|
|
41
|
+
it('reads the exclusive refs of a DISCOUNT_ITEMS reward', () => {
|
|
42
|
+
const patch = (0, offer_reward_claim_1.buildRewardClaim)({
|
|
43
|
+
offer: offer({
|
|
44
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS,
|
|
45
|
+
reward_items: undefined,
|
|
46
|
+
reward_discount_items: { num_of_items: 1, exclusive_refs: ['option-y'] },
|
|
47
|
+
}),
|
|
48
|
+
catalogItemId: 'item-9',
|
|
49
|
+
applicableQuantity: 1,
|
|
50
|
+
});
|
|
51
|
+
expect(patch.applyLoyalty?.refs).toEqual(['option-y', 'item-9']);
|
|
52
|
+
});
|
|
53
|
+
it('stamps a gift-card reward with the gift card id and always applies', () => {
|
|
54
|
+
const patch = (0, offer_reward_claim_1.buildRewardClaim)({
|
|
55
|
+
offer: offer({
|
|
56
|
+
reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS,
|
|
57
|
+
pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER,
|
|
58
|
+
}),
|
|
59
|
+
catalogItemId: 'item-1',
|
|
60
|
+
applicableQuantity: 1,
|
|
61
|
+
});
|
|
62
|
+
expect(patch.rewardClaim).toEqual({ type: 'gift-card', giftCardId: 'offer-1' });
|
|
63
|
+
expect(patch.applyLoyalty?.loyalty_id).toBe('offer-1');
|
|
64
|
+
});
|
|
65
|
+
it('honours an explicit voucher flag over the pathway', () => {
|
|
66
|
+
const patch = (0, offer_reward_claim_1.buildRewardClaim)({
|
|
67
|
+
offer: offer(),
|
|
68
|
+
catalogItemId: 'item-1',
|
|
69
|
+
applicableQuantity: 1,
|
|
70
|
+
isVoucherLinked: true,
|
|
71
|
+
});
|
|
72
|
+
expect(patch.applyLoyalty).toBeUndefined();
|
|
73
|
+
});
|
|
74
|
+
it('never carries a quantity below one', () => {
|
|
75
|
+
const patch = (0, offer_reward_claim_1.buildRewardClaim)({ offer: offer(), catalogItemId: 'item-1', applicableQuantity: 0 });
|
|
76
|
+
expect(patch.applyLoyalty?.applicable_quantity).toBe(1);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -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,24 @@ 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
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
expect(stats.claimable).toBe(false);
|
|
625
|
-
expect(stats.message).toMatch(/redeemed/);
|
|
634
|
+
const itemRewards = new Set([constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS, constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS]);
|
|
635
|
+
if (itemRewards.has(reward)) {
|
|
636
|
+
// Item rewards are claimed by stamping cart items. Eligibility reads that
|
|
637
|
+
// evidence, not the ledger, so a ledger-only claim is invisible and the
|
|
638
|
+
// offer stays claimable - the deliberate half of the #882 fix.
|
|
639
|
+
expect(stats.redeemed_times).toBe(0);
|
|
640
|
+
expect(stats.claimable).toBe(true);
|
|
641
|
+
return;
|
|
626
642
|
}
|
|
643
|
+
// Order-level rewards read the ledger: the claim is reported and the
|
|
644
|
+
// lifetime cap is spent.
|
|
645
|
+
expect(stats.redeemed_times).toBeGreaterThanOrEqual(1);
|
|
646
|
+
expect(stats.claimable).toBe(false);
|
|
627
647
|
});
|
|
628
648
|
});
|
|
629
649
|
});
|