@managesome/eposmatic-toolkit 0.2.342 → 0.2.344
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/helpers/store/cart-actions.d.ts +24 -0
- package/lib/helpers/store/cart-actions.js +54 -7
- package/lib/helpers/utils/offer-apply-helpers.js +6 -2
- package/lib/helpers/utils/offer-apply-helpers.test.d.ts +1 -0
- package/lib/helpers/utils/offer-apply-helpers.test.js +110 -0
- package/package.json +1 -1
|
@@ -77,6 +77,15 @@ export declare function getRemoveItemFromCartData(data: Partial<IOrder>, payload
|
|
|
77
77
|
void_reason?: string;
|
|
78
78
|
}, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
|
|
79
79
|
export declare function getUpdatedCartInfoOnInit(data: Partial<IOrder>, order: Partial<IOrder>, courses: ICourse[], platform: PLATFORMS_ENUM | undefined, categories: IOrderCategory[] | undefined, mappedItems: Record<string, IItem>, tenantId: string | undefined, listnerSettings: ICartListnerSettings, offers: IOffer[] | undefined): Partial<IOrder>;
|
|
80
|
+
/**
|
|
81
|
+
* Drop entries from `redeemed_offers` (and any related discount/wallet state)
|
|
82
|
+
* for offers whose `included_platforms` does not contain the supplied platform.
|
|
83
|
+
*
|
|
84
|
+
* Use this when the platform changes outside of `getUpdatedCartInfoOnInit` (for
|
|
85
|
+
* example, on platform switch in a long POS cashier session) to keep
|
|
86
|
+
* platform-incompatible rewards from being silently preserved.
|
|
87
|
+
*/
|
|
88
|
+
export declare function pruneInvalidRedeemedOffersForPlatform(data: Partial<IOrder>, courses: ICourse[], listnerSettings: ICartListnerSettings, offers: IOffer[] | undefined, platform: PLATFORMS_ENUM | undefined): Partial<IOrder>;
|
|
80
89
|
export declare function getChangeCourseForItemData(data: Partial<IOrder>, itemId: string, new_course_id: string, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
|
|
81
90
|
export declare function getChangeCourseStatusData(data: Partial<IOrder>, course_id: string, called: boolean, courses: ICourse[], listnerSettings: ICartListnerSettings): {
|
|
82
91
|
dinein_details: {
|
|
@@ -166,6 +175,21 @@ export declare function getCartItemsObj({ currentIterationCategories, currentDin
|
|
|
166
175
|
export declare function getRemoveOfferFromCartData(data: Partial<IOrder>, offer_id: string, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
|
|
167
176
|
export declare function getRemoveAllOffersFromCartData(data: Partial<IOrder>, courses: ICourse[], listnerSettings: ICartListnerSettings, offerIds: string[] | undefined): Partial<IOrder>;
|
|
168
177
|
export declare function getClearVoucherCodeAndOffersFromCartData(data: Partial<IOrder>, courses: ICourse[], listnerSettings: ICartListnerSettings, offerIds: string[] | undefined): Partial<IOrder>;
|
|
178
|
+
/**
|
|
179
|
+
* Wipe all reward metadata (`redeemed_offers`, `wallet_redemptions`) and any
|
|
180
|
+
* loyalty/voucher fields on `order_discount_details`, then resync the cart.
|
|
181
|
+
*
|
|
182
|
+
* Use this on hard reward-session boundaries — for example, in a POS cashier
|
|
183
|
+
* session when the customer phone changes — where every reward applied for the
|
|
184
|
+
* previous customer must be dropped before the next customer's eligibility is
|
|
185
|
+
* evaluated. Items in the cart are preserved.
|
|
186
|
+
*
|
|
187
|
+
* Unlike `getClearVoucherCodeAndOffersFromCartData`, this also clears
|
|
188
|
+
* `wallet_redemptions.gift_cards` and `wallet_redemptions.cashback_amount`,
|
|
189
|
+
* along with any wallet-cashback `loyalty_discount_*` mirror on
|
|
190
|
+
* `order_discount_details`.
|
|
191
|
+
*/
|
|
192
|
+
export declare function getResetRewardStateForNewCustomer(data: Partial<IOrder>, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
|
|
169
193
|
/**
|
|
170
194
|
* Calculate the eligible subtotal by excluding items/categories in excluded_refs
|
|
171
195
|
*/
|
|
@@ -22,12 +22,14 @@ exports.getUpdateItemsInCartData = getUpdateItemsInCartData;
|
|
|
22
22
|
exports.getUpdateCartInfoData = getUpdateCartInfoData;
|
|
23
23
|
exports.getRemoveItemFromCartData = getRemoveItemFromCartData;
|
|
24
24
|
exports.getUpdatedCartInfoOnInit = getUpdatedCartInfoOnInit;
|
|
25
|
+
exports.pruneInvalidRedeemedOffersForPlatform = pruneInvalidRedeemedOffersForPlatform;
|
|
25
26
|
exports.getChangeCourseForItemData = getChangeCourseForItemData;
|
|
26
27
|
exports.getChangeCourseStatusData = getChangeCourseStatusData;
|
|
27
28
|
exports.getCartItemsObj = getCartItemsObj;
|
|
28
29
|
exports.getRemoveOfferFromCartData = getRemoveOfferFromCartData;
|
|
29
30
|
exports.getRemoveAllOffersFromCartData = getRemoveAllOffersFromCartData;
|
|
30
31
|
exports.getClearVoucherCodeAndOffersFromCartData = getClearVoucherCodeAndOffersFromCartData;
|
|
32
|
+
exports.getResetRewardStateForNewCustomer = getResetRewardStateForNewCustomer;
|
|
31
33
|
exports.calculateEligibleOfferDiscountDetails = calculateEligibleOfferDiscountDetails;
|
|
32
34
|
const bson_objectid_1 = __importDefault(require("bson-objectid"));
|
|
33
35
|
const lodash_1 = require("lodash");
|
|
@@ -841,10 +843,14 @@ function getReconciledRedeemedOffersMapForOfferSync({ existingRedeemedOffers, ca
|
|
|
841
843
|
delete nextRedeemedOffers[normalizedOfferId];
|
|
842
844
|
continue;
|
|
843
845
|
}
|
|
846
|
+
const hasUnlockContext = unlockedOffersApplicableTimes != null;
|
|
844
847
|
const maxApplicableTimes = toPositiveInteger(unlockedOffersApplicableTimes?.[offer._id], 0);
|
|
845
848
|
if (maxApplicableTimes <= 0) {
|
|
846
|
-
|
|
847
|
-
|
|
849
|
+
if (!hasUnlockContext) {
|
|
850
|
+
nextRedeemedOffers[normalizedOfferId] = existingRedeemedTimes;
|
|
851
|
+
continue;
|
|
852
|
+
}
|
|
853
|
+
delete nextRedeemedOffers[normalizedOfferId];
|
|
848
854
|
continue;
|
|
849
855
|
}
|
|
850
856
|
nextRedeemedOffers[normalizedOfferId] = Math.min(existingRedeemedTimes, maxApplicableTimes);
|
|
@@ -2308,9 +2314,24 @@ function getUpdatedCartInfoOnInit(data, order, courses, platform, categories, ma
|
|
|
2308
2314
|
if (platform == null || data.platform === order.platform || offers == null || offers.length === 0) {
|
|
2309
2315
|
return orderData;
|
|
2310
2316
|
}
|
|
2311
|
-
|
|
2317
|
+
return pruneInvalidRedeemedOffersForPlatform(orderData, courses, listnerSettings, offers, platform);
|
|
2318
|
+
}
|
|
2319
|
+
/* =================================><================================= */
|
|
2320
|
+
/**
|
|
2321
|
+
* Drop entries from `redeemed_offers` (and any related discount/wallet state)
|
|
2322
|
+
* for offers whose `included_platforms` does not contain the supplied platform.
|
|
2323
|
+
*
|
|
2324
|
+
* Use this when the platform changes outside of `getUpdatedCartInfoOnInit` (for
|
|
2325
|
+
* example, on platform switch in a long POS cashier session) to keep
|
|
2326
|
+
* platform-incompatible rewards from being silently preserved.
|
|
2327
|
+
*/
|
|
2328
|
+
function pruneInvalidRedeemedOffersForPlatform(data, courses, listnerSettings, offers, platform) {
|
|
2329
|
+
if (platform == null || offers == null || offers.length === 0) {
|
|
2330
|
+
return data;
|
|
2331
|
+
}
|
|
2332
|
+
const redeemedOfferIds = Object.keys(data.redeemed_offers ?? {});
|
|
2312
2333
|
if (redeemedOfferIds.length === 0) {
|
|
2313
|
-
return
|
|
2334
|
+
return data;
|
|
2314
2335
|
}
|
|
2315
2336
|
const offersMap = new Map();
|
|
2316
2337
|
for (let i = 0; i < offers.length; i++) {
|
|
@@ -2325,11 +2346,10 @@ function getUpdatedCartInfoOnInit(data, order, courses, platform, categories, ma
|
|
|
2325
2346
|
}
|
|
2326
2347
|
}
|
|
2327
2348
|
if (invalidRedeemedOfferIds.length === 0) {
|
|
2328
|
-
return
|
|
2349
|
+
return data;
|
|
2329
2350
|
}
|
|
2330
|
-
return getRemoveAllOffersFromCartData(
|
|
2351
|
+
return getRemoveAllOffersFromCartData(data, courses, listnerSettings, invalidRedeemedOfferIds);
|
|
2331
2352
|
}
|
|
2332
|
-
/* =================================><================================= */
|
|
2333
2353
|
function getChangeCourseForItemData(data, itemId, new_course_id, courses, listnerSettings) {
|
|
2334
2354
|
if (data.dinein_details == null)
|
|
2335
2355
|
return data;
|
|
@@ -3095,6 +3115,33 @@ function getClearVoucherCodeAndOffersFromCartData(data, courses, listnerSettings
|
|
|
3095
3115
|
}, courses, listnerSettings);
|
|
3096
3116
|
}
|
|
3097
3117
|
// ================================><================================= //
|
|
3118
|
+
/**
|
|
3119
|
+
* Wipe all reward metadata (`redeemed_offers`, `wallet_redemptions`) and any
|
|
3120
|
+
* loyalty/voucher fields on `order_discount_details`, then resync the cart.
|
|
3121
|
+
*
|
|
3122
|
+
* Use this on hard reward-session boundaries — for example, in a POS cashier
|
|
3123
|
+
* session when the customer phone changes — where every reward applied for the
|
|
3124
|
+
* previous customer must be dropped before the next customer's eligibility is
|
|
3125
|
+
* evaluated. Items in the cart are preserved.
|
|
3126
|
+
*
|
|
3127
|
+
* Unlike `getClearVoucherCodeAndOffersFromCartData`, this also clears
|
|
3128
|
+
* `wallet_redemptions.gift_cards` and `wallet_redemptions.cashback_amount`,
|
|
3129
|
+
* along with any wallet-cashback `loyalty_discount_*` mirror on
|
|
3130
|
+
* `order_discount_details`.
|
|
3131
|
+
*/
|
|
3132
|
+
function getResetRewardStateForNewCustomer(data, courses, listnerSettings) {
|
|
3133
|
+
const orderWithoutOffers = getRemoveAllOffersFromCartData(data, courses, listnerSettings, undefined);
|
|
3134
|
+
const orderDiscountDetailsWithoutVoucher = (0, offers_helpers_1.buildClearVoucherDiscountDetails)(orderWithoutOffers.order_discount_details);
|
|
3135
|
+
const orderDiscountDetails = orderDiscountDetailsWithoutVoucher != null &&
|
|
3136
|
+
orderDiscountDetailsWithoutVoucher.loyalty_discount_id === constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID
|
|
3137
|
+
? clearLoyaltyDiscountFields(orderDiscountDetailsWithoutVoucher)
|
|
3138
|
+
: orderDiscountDetailsWithoutVoucher;
|
|
3139
|
+
return getUpdateCartInfoData(orderWithoutOffers, {
|
|
3140
|
+
order_discount_details: orderDiscountDetails,
|
|
3141
|
+
wallet_redemptions: null,
|
|
3142
|
+
}, courses, listnerSettings);
|
|
3143
|
+
}
|
|
3144
|
+
// ================================><================================= //
|
|
3098
3145
|
/**
|
|
3099
3146
|
* Calculate the eligible subtotal by excluding items/categories in excluded_refs
|
|
3100
3147
|
*/
|
|
@@ -498,10 +498,14 @@ function getReconciledRedeemedOffersMap({ existingRedeemedOffers, categories, of
|
|
|
498
498
|
delete nextRedeemedOffers[normalizedOfferId];
|
|
499
499
|
continue;
|
|
500
500
|
}
|
|
501
|
+
const hasUnlockContext = unlockedOffersApplicableTimes != null;
|
|
501
502
|
const maxApplicableTimes = toPositiveInteger(unlockedOffersApplicableTimes?.[offer._id], 0);
|
|
502
503
|
if (maxApplicableTimes <= 0) {
|
|
503
|
-
|
|
504
|
-
|
|
504
|
+
if (!hasUnlockContext) {
|
|
505
|
+
nextRedeemedOffers[normalizedOfferId] = existingRedeemedTimes;
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
delete nextRedeemedOffers[normalizedOfferId];
|
|
505
509
|
continue;
|
|
506
510
|
}
|
|
507
511
|
nextRedeemedOffers[normalizedOfferId] = Math.min(existingRedeemedTimes, maxApplicableTimes);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const cart_actions_1 = require("../store/cart-actions");
|
|
4
|
+
const offer_apply_helpers_1 = require("./offer-apply-helpers");
|
|
5
|
+
const OFFER_ID = 'offer-reward-threshold';
|
|
6
|
+
const POST_ORDER_REWARD_TYPES = ['POINTS', 'CASHBACK', 'VOUCHER', 'GIFT_CARDS'];
|
|
7
|
+
function makeCategories({ itemDiscountDetails = null, quantity = 1, subtotal = 699, } = {}) {
|
|
8
|
+
return [
|
|
9
|
+
{
|
|
10
|
+
category_id: 'category-pizza',
|
|
11
|
+
items: [
|
|
12
|
+
{
|
|
13
|
+
_id: 'cart-item-1',
|
|
14
|
+
item_id: 'item-classic-euro',
|
|
15
|
+
name: 'Classic Euro',
|
|
16
|
+
quantity,
|
|
17
|
+
subtotal,
|
|
18
|
+
modifiers: [],
|
|
19
|
+
item_discount_details: itemDiscountDetails,
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
},
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
function makeOrder(overrides = {}) {
|
|
26
|
+
return {
|
|
27
|
+
categories: makeCategories(),
|
|
28
|
+
order_discount_details: null,
|
|
29
|
+
redeemed_offers: {
|
|
30
|
+
[OFFER_ID]: 1,
|
|
31
|
+
},
|
|
32
|
+
wallet_redemptions: null,
|
|
33
|
+
platform: 'WEB_TAKE-AWAY',
|
|
34
|
+
...overrides,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function makeOffer(rewardType, overrides = {}) {
|
|
38
|
+
const offer = {
|
|
39
|
+
_id: OFFER_ID,
|
|
40
|
+
name: `${rewardType} threshold reward`,
|
|
41
|
+
active: true,
|
|
42
|
+
reward_type: rewardType,
|
|
43
|
+
pathway_to_unlock_type: 'ORDERS',
|
|
44
|
+
pathway_to_unlock_type_orders: {
|
|
45
|
+
total_amount: 1000,
|
|
46
|
+
max_claims: null,
|
|
47
|
+
max_uses_per_customer: null,
|
|
48
|
+
},
|
|
49
|
+
...overrides,
|
|
50
|
+
};
|
|
51
|
+
if (rewardType === 'POINTS') {
|
|
52
|
+
offer.reward_points = { points: 100, once_per_order: true };
|
|
53
|
+
}
|
|
54
|
+
if (rewardType === 'CASHBACK') {
|
|
55
|
+
offer.reward_cashback = { cashback_value: 10, is_cashback_percentage: false };
|
|
56
|
+
}
|
|
57
|
+
if (rewardType === 'VOUCHER') {
|
|
58
|
+
offer.reward_voucher = { voucher_ids: { 'voucher-april28': true } };
|
|
59
|
+
}
|
|
60
|
+
if (rewardType === 'GIFT_CARDS') {
|
|
61
|
+
offer.reward_gift_cards = { gift_card_ids: { 'gift-card-reward': true } };
|
|
62
|
+
}
|
|
63
|
+
if (rewardType === 'ITEMS') {
|
|
64
|
+
offer.reward_items = {
|
|
65
|
+
num_of_items: 1,
|
|
66
|
+
item_ids: ['item-classic-euro'],
|
|
67
|
+
excluded_refs: [],
|
|
68
|
+
exclusive_refs: [],
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return offer;
|
|
72
|
+
}
|
|
73
|
+
describe('offer redeemed_offers sync', () => {
|
|
74
|
+
test.each(POST_ORDER_REWARD_TYPES)('getSyncOffersWithCartData clears stale %s reward claims when unlock context reports zero availability', (rewardType) => {
|
|
75
|
+
const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder(), [], {}, {}, [makeOffer(rewardType)]);
|
|
76
|
+
expect(result.redeemed_offers?.[OFFER_ID]).toBeUndefined();
|
|
77
|
+
});
|
|
78
|
+
test.each(POST_ORDER_REWARD_TYPES)('calculateCartOrderInfoAndSyncData clears stale %s reward claims when unlock context reports zero availability', (rewardType) => {
|
|
79
|
+
const result = (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(makeOrder(), [], {}, undefined, {
|
|
80
|
+
unlockedOffersApplicableTimes: {},
|
|
81
|
+
offers: [makeOffer(rewardType)],
|
|
82
|
+
});
|
|
83
|
+
expect(result.redeemed_offers?.[OFFER_ID]).toBeUndefined();
|
|
84
|
+
});
|
|
85
|
+
it('getSyncOffersWithCartData preserves selected item reward evidence while the item reward remains unlocked', () => {
|
|
86
|
+
const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder({
|
|
87
|
+
categories: makeCategories({
|
|
88
|
+
itemDiscountDetails: {
|
|
89
|
+
loyalty_discount_id: OFFER_ID,
|
|
90
|
+
},
|
|
91
|
+
}),
|
|
92
|
+
redeemed_offers: null,
|
|
93
|
+
}), [], {}, { [OFFER_ID]: 1 }, [makeOffer('ITEMS')]);
|
|
94
|
+
expect(result.redeemed_offers?.[OFFER_ID]).toBe(1);
|
|
95
|
+
});
|
|
96
|
+
it('calculateCartOrderInfoAndSyncData preserves selected item reward evidence while the item reward remains unlocked', () => {
|
|
97
|
+
const result = (0, cart_actions_1.calculateCartOrderInfoAndSyncData)(makeOrder({
|
|
98
|
+
categories: makeCategories({
|
|
99
|
+
itemDiscountDetails: {
|
|
100
|
+
loyalty_discount_id: OFFER_ID,
|
|
101
|
+
},
|
|
102
|
+
}),
|
|
103
|
+
redeemed_offers: null,
|
|
104
|
+
}), [], {}, undefined, {
|
|
105
|
+
unlockedOffersApplicableTimes: { [OFFER_ID]: 1 },
|
|
106
|
+
offers: [makeOffer('ITEMS')],
|
|
107
|
+
});
|
|
108
|
+
expect(result.redeemed_offers?.[OFFER_ID]).toBe(1);
|
|
109
|
+
});
|
|
110
|
+
});
|