@managesome/eposmatic-toolkit 0.4.38 → 0.4.39

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.
@@ -11,22 +11,49 @@ const code_redemption_helpers_1 = require("../helpers/utils/code-redemption-help
11
11
  const now = new Date('2026-01-01T00:00:00.000Z');
12
12
  function voucher(overrides = {}) {
13
13
  return {
14
- _id: 'voucher-1', code: 'SAVE10', brand_id: 'b', tenants: ['t'], name: 'SAVE10', description: '',
15
- active: true, is_public: true, max_uses: null, max_uses_per_customer: null,
16
- max_assignments: null, max_assignments_per_customer: null,
17
- assigned_count: 0, last_assigned_at: null, used_count: 0, last_used_at: null, expire_at: null,
18
- updatedAt: now, createdAt: now, ...overrides,
14
+ _id: 'voucher-1',
15
+ code: 'SAVE10',
16
+ brand_id: 'b',
17
+ tenants: ['t'],
18
+ name: 'SAVE10',
19
+ description: '',
20
+ active: true,
21
+ is_public: true,
22
+ max_uses: null,
23
+ max_uses_per_customer: null,
24
+ max_assignments: null,
25
+ max_assignments_per_customer: null,
26
+ assigned_count: 0,
27
+ last_assigned_at: null,
28
+ used_count: 0,
29
+ last_used_at: null,
30
+ expire_at: null,
31
+ updatedAt: now,
32
+ createdAt: now,
33
+ ...overrides,
19
34
  };
20
35
  }
21
36
  function giftCard(overrides = {}) {
22
37
  return {
23
- _id: 'gc-1', code: 'GC100', brand_id: 'b', tenants: ['t'], name: 'Free Broast', description: '',
24
- active: true, max_uses: null, max_uses_per_customer: null,
25
- max_assignments: null, max_assignments_per_customer: null,
26
- assigned_count: 0, used_count: 0, expire_at: null,
38
+ _id: 'gc-1',
39
+ code: 'GC100',
40
+ brand_id: 'b',
41
+ tenants: ['t'],
42
+ name: 'Free Broast',
43
+ description: '',
44
+ active: true,
45
+ max_uses: null,
46
+ max_uses_per_customer: null,
47
+ max_assignments: null,
48
+ max_assignments_per_customer: null,
49
+ assigned_count: 0,
50
+ used_count: 0,
51
+ expire_at: null,
27
52
  reward_type: constants_1.GIFT_CARD_REWARD_TYPE_ENUM.ITEMS,
28
53
  reward_items: { num_of_items: 1, excluded_refs: [], exclusive_refs: [], item_ids: ['item-1'], allocation: null },
29
- updatedAt: now, createdAt: now, ...overrides,
54
+ updatedAt: now,
55
+ createdAt: now,
56
+ ...overrides,
30
57
  };
31
58
  }
32
59
  describe('getDirectWalletGiftCardUsageGate', () => {
@@ -154,19 +181,33 @@ describe('buildValidatedWalletGiftCardRedemptionClaimPatch', () => {
154
181
  });
155
182
  describe('getWalletRedemptionsSourceOfTruth', () => {
156
183
  const voucherPathwayOffer = {
157
- _id: 'offer-v', name: 'Voucher offer', active: true, auto_apply: false, sort_id: 1,
158
- included_platforms: ['WEB_DELIVERY'], offer_special_hours: null,
184
+ _id: 'offer-v',
185
+ name: 'Voucher offer',
186
+ active: true,
187
+ auto_apply: false,
188
+ sort_id: 1,
189
+ included_platforms: ['WEB_DELIVERY'],
190
+ offer_special_hours: null,
159
191
  pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER,
160
192
  pathway_to_unlock_type_vouchers: {
161
- total_amount: 0, max_amount: null, max_uses_per_customer: null, max_claims: null, voucher_ids: ['voucher-1'],
193
+ total_amount: 0,
194
+ max_amount: null,
195
+ max_uses_per_customer: null,
196
+ max_claims: null,
197
+ voucher_ids: ['voucher-1'],
162
198
  },
163
199
  reward_type: 'ITEMS',
164
200
  reward_items: { num_of_items: 1, excluded_refs: [], exclusive_refs: [], item_ids: ['item-1'], allocation: null },
165
201
  };
166
202
  const empty = {
167
- offers: [], vouchers: [], giftCards: [],
168
- redeemedOffers: null, walletRedemptions: null, orderDiscountDetails: null,
169
- customerGiftCardCodes: null, customerOfferRedemptionCounts: { ASSIGNED: {}, USED: {} },
203
+ offers: [],
204
+ vouchers: [],
205
+ giftCards: [],
206
+ redeemedOffers: null,
207
+ walletRedemptions: null,
208
+ orderDiscountDetails: null,
209
+ customerGiftCardCodes: null,
210
+ customerOfferRedemptionCounts: { ASSIGNED: {}, USED: {} },
170
211
  };
171
212
  it('reports the voucher code of a redeemed voucher-pathway offer', () => {
172
213
  const result = (0, code_redemption_helpers_1.getWalletRedemptionsSourceOfTruth)({
@@ -17,8 +17,30 @@ const categories = () => [
17
17
  category_id: 'c',
18
18
  name: 'C',
19
19
  items: [
20
- { _id: 'ci1', item_id: 'i1', name: 'A', quantity: 1, price: 1000, subtotal: 1000, tax_rate: 0, modifiers: [], special_instructions: '', item_discount_details: null },
21
- { _id: 'ci2', item_id: 'i2', name: 'B', quantity: 1, price: 500, subtotal: 500, tax_rate: 0, modifiers: [], special_instructions: '', item_discount_details: null },
20
+ {
21
+ _id: 'ci1',
22
+ item_id: 'i1',
23
+ name: 'A',
24
+ quantity: 1,
25
+ price: 1000,
26
+ subtotal: 1000,
27
+ tax_rate: 0,
28
+ modifiers: [],
29
+ special_instructions: '',
30
+ item_discount_details: null,
31
+ },
32
+ {
33
+ _id: 'ci2',
34
+ item_id: 'i2',
35
+ name: 'B',
36
+ quantity: 1,
37
+ price: 500,
38
+ subtotal: 500,
39
+ tax_rate: 0,
40
+ modifiers: [],
41
+ special_instructions: '',
42
+ item_discount_details: null,
43
+ },
22
44
  ],
23
45
  },
24
46
  ];
@@ -60,7 +82,15 @@ function statsFor(offerToCheck, extra = {}) {
60
82
  ...(extra.customer ?? {}),
61
83
  },
62
84
  vouchers: [
63
- { _id: 'v1', code: 'SAVE', active: true, max_uses: null, max_assignments: null, assigned_count: 0, used_count: 0 },
85
+ {
86
+ _id: 'v1',
87
+ code: 'SAVE',
88
+ active: true,
89
+ max_uses: null,
90
+ max_assignments: null,
91
+ assigned_count: 0,
92
+ used_count: 0,
93
+ },
64
94
  ],
65
95
  giftCards: [],
66
96
  customerOfferRedemptionCounts: { ASSIGNED: {}, USED: {} },
@@ -102,7 +132,12 @@ describe('F-02: a zero spend threshold must not multiply claims', () => {
102
132
  expect(statsFor(offer({
103
133
  ...ordersPathway(0),
104
134
  reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.CASHBACK,
105
- reward_cashback: { cashback_value: 5, is_cashback_percentage: false, max_cashback_amount: null, excluded_refs: [] },
135
+ reward_cashback: {
136
+ cashback_value: 5,
137
+ is_cashback_percentage: false,
138
+ max_cashback_amount: null,
139
+ excluded_refs: [],
140
+ },
106
141
  })).stats.applicable_quantity).toBe(1);
107
142
  });
108
143
  it('treats a negative or absent threshold as "no minimum spend"', () => {
@@ -113,8 +148,14 @@ describe('F-02: a zero spend threshold must not multiply claims', () => {
113
148
  const withMaxClaims = (maxClaims) => offer({
114
149
  pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS,
115
150
  pathway_to_unlock_type_orders: {
116
- scope: 'OVERALL', min_num_of_orders: 0, max_num_of_orders: null, total_amount: 100,
117
- max_amount: null, orders_between_unlocks: null, max_claims: maxClaims, max_uses_per_customer: null,
151
+ scope: 'OVERALL',
152
+ min_num_of_orders: 0,
153
+ max_num_of_orders: null,
154
+ total_amount: 100,
155
+ max_amount: null,
156
+ orders_between_unlocks: null,
157
+ max_claims: maxClaims,
158
+ max_uses_per_customer: null,
118
159
  },
119
160
  ...twoFreeItems,
120
161
  });
@@ -126,7 +167,12 @@ describe('F-02: a zero spend threshold must not multiply claims', () => {
126
167
  it('does not multiply claims when a POINTS reward costs zero points', () => {
127
168
  const { stats: statsResult } = statsFor(offer({
128
169
  pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS,
129
- pathway_to_unlock_type_points: { num_of_points: 0, total_amount: 0, max_uses_per_customer: null, max_claims: null },
170
+ pathway_to_unlock_type_points: {
171
+ num_of_points: 0,
172
+ total_amount: 0,
173
+ max_uses_per_customer: null,
174
+ max_claims: null,
175
+ },
130
176
  reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
131
177
  reward_items: { num_of_items: 1, excluded_refs: [], exclusive_refs: [], item_ids: ['i1'], allocation: null },
132
178
  }));
@@ -152,14 +198,32 @@ describe('F-02: a zero spend threshold must not multiply claims', () => {
152
198
  });
153
199
  describe('F-05: removing a reward item releases its claim', () => {
154
200
  const paid = (id) => ({
155
- _id: id, item_id: 'i1', name: 'A', quantity: 1, price: 1000, subtotal: 1000, tax_rate: 0,
156
- modifiers: [], special_instructions: '', item_discount_details: null,
201
+ _id: id,
202
+ item_id: 'i1',
203
+ name: 'A',
204
+ quantity: 1,
205
+ price: 1000,
206
+ subtotal: 1000,
207
+ tax_rate: 0,
208
+ modifiers: [],
209
+ special_instructions: '',
210
+ item_discount_details: null,
157
211
  });
158
212
  const rewardItem = (id) => ({
159
- _id: id, item_id: 'i1', name: 'A', quantity: 1, price: 1000, subtotal: 0, tax_rate: 0,
160
- modifiers: [], special_instructions: '',
213
+ _id: id,
214
+ item_id: 'i1',
215
+ name: 'A',
216
+ quantity: 1,
217
+ price: 1000,
218
+ subtotal: 0,
219
+ tax_rate: 0,
220
+ modifiers: [],
221
+ special_instructions: '',
161
222
  item_discount_details: {
162
- loyalty_discount: 100, loyalty_discount_id: 'o2', is_loyalty_discount_percentage: true, total_discount: 1000,
223
+ loyalty_discount: 100,
224
+ loyalty_discount_id: 'o2',
225
+ is_loyalty_discount_percentage: true,
226
+ total_discount: 1000,
163
227
  },
164
228
  });
165
229
  const cartWith = (items) => ({
@@ -188,7 +252,11 @@ describe('F-01 (voucher slot): the same rule must hold there', () => {
188
252
  const voucherOffer = (excludedRefs, discountValue = 10) => offer({
189
253
  pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER,
190
254
  pathway_to_unlock_type_vouchers: {
191
- total_amount: 0, max_amount: null, max_uses_per_customer: null, max_claims: null, voucher_ids: ['v1'],
255
+ total_amount: 0,
256
+ max_amount: null,
257
+ max_uses_per_customer: null,
258
+ max_claims: null,
259
+ voucher_ids: ['v1'],
192
260
  },
193
261
  reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT,
194
262
  reward_discount: {
@@ -362,7 +362,7 @@ describe('getOffersWithStats spend qualification', () => {
362
362
  expect(stats.message).toContain('redeemed');
363
363
  expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
364
364
  });
365
- it('applies discount-items and cashback exclusion refs to the spend threshold', () => {
365
+ it('applies cashback item exclusions to the spend threshold but not DISCOUNT_ITEMS option refs', () => {
366
366
  const categories = [
367
367
  {
368
368
  category_id: 'category-salads',
@@ -387,7 +387,25 @@ describe('getOffersWithStats spend qualification', () => {
387
387
  name: 'Deal',
388
388
  quantity: 1,
389
389
  subtotal: 1799,
390
- modifiers: [],
390
+ // DISCOUNT_ITEMS exclusion refs are modifier/option refs ("Hide
391
+ // Options"), so the fixture carries a real option to point at.
392
+ modifiers: [
393
+ {
394
+ _id: 'modifier-deal-1',
395
+ ref: 'modifier-deal-extra',
396
+ name: 'Extra',
397
+ options: [
398
+ {
399
+ _id: 'option-deal-1',
400
+ ref: 'option-deal-sauce',
401
+ name: 'Sauce',
402
+ price: 100,
403
+ quantity: 1,
404
+ modifiers: [],
405
+ },
406
+ ],
407
+ },
408
+ ],
391
409
  item_discount_details: null,
392
410
  },
393
411
  ],
@@ -410,7 +428,7 @@ describe('getOffersWithStats spend qualification', () => {
410
428
  reward_discount: null,
411
429
  reward_discount_items: {
412
430
  num_of_items: 1,
413
- excluded_refs: ['item-deal'],
431
+ excluded_refs: ['option-deal-sauce'],
414
432
  exclusive_refs: [],
415
433
  item_ids: ['item-salad'],
416
434
  discount_value: 10,
@@ -422,7 +440,9 @@ describe('getOffersWithStats spend qualification', () => {
422
440
  currentItemsPrice: 3197,
423
441
  totalDiscountApplied: 0,
424
442
  });
425
- expect(discountItemsResult.offersWithStats[0].stats.message).toContain(`${constants_1.CURRENCY_SYMBOL_ENUM.$}1102`);
443
+ // DISCOUNT_ITEMS refs are option-level ("Hide Options"), so they do not touch
444
+ // the spend-qualification base: the threshold is met by the full cart.
445
+ expect(discountItemsResult.offersWithStats[0].stats.message).toContain('unlocked');
426
446
  const cashbackResult = getSingleOfferStats({
427
447
  offer: makeOrdersDiscountOffer({
428
448
  pathway_to_unlock_type_orders: ordersThreshold,
@@ -32,8 +32,8 @@ const PAID_ITEM = {
32
32
  special_instructions: '',
33
33
  item_discount_details: null,
34
34
  };
35
- function makeCategories() {
36
- return [{ category_id: CATEGORY_ID, name: 'BROAST', items: [PAID_ITEM] }];
35
+ function makeCategories(items = [PAID_ITEM]) {
36
+ return [{ category_id: CATEGORY_ID, name: 'BROAST', items }];
37
37
  }
38
38
  function makeVoucher() {
39
39
  const now = new Date('2026-01-01T00:00:00.000Z');
@@ -213,7 +213,7 @@ function makeOffer(pathway, reward, unlocked) {
213
213
  ...rewardPayload(reward),
214
214
  };
215
215
  }
216
- function statsFor(offer, { redeemedTimes = 0, totalOrders = 5 } = {}) {
216
+ function statsFor(offer, { redeemedTimes = 0, totalOrders = 5, vouchers = [makeVoucher()], voucherCodes = { [VOUCHER_CODE]: 2 }, categories = makeCategories(), } = {}) {
217
217
  const result = (0, offers_helpers_1.getOffersWithStats)({
218
218
  offers: [offer],
219
219
  customer: {
@@ -222,10 +222,10 @@ function statsFor(offer, { redeemedTimes = 0, totalOrders = 5 } = {}) {
222
222
  cashback_balance: 0,
223
223
  total_orders: totalOrders,
224
224
  total_orders_per_platform_type: { [constants_1.PLATFORMS_ENUM.WEB_DELIVERY]: totalOrders },
225
- voucher_codes: { [VOUCHER_CODE]: 2 },
225
+ voucher_codes: voucherCodes,
226
226
  gift_cards: {},
227
227
  },
228
- vouchers: [makeVoucher()],
228
+ vouchers,
229
229
  giftCards: [makeGiftCard()],
230
230
  customerOfferRedemptionCounts: { ASSIGNED: {}, USED: {} },
231
231
  redeemed_offers: redeemedTimes > 0 ? { [offer._id]: redeemedTimes } : null,
@@ -233,7 +233,7 @@ function statsFor(offer, { redeemedTimes = 0, totalOrders = 5 } = {}) {
233
233
  currency_symbol: constants_1.CURRENCY_SYMBOL_ENUM.Rs,
234
234
  currentItemsPrice: 1000,
235
235
  totalDiscountApplied: 0,
236
- categories: makeCategories(),
236
+ categories,
237
237
  order_discount_details: null,
238
238
  });
239
239
  return {
@@ -297,6 +297,195 @@ describe('pathway x reward matrix', () => {
297
297
  expect(stats.action_hint).not.toBeNull();
298
298
  });
299
299
  });
300
+ describe('fixed discount with exclusions applies instead of failing (T2)', () => {
301
+ const SIDE_ITEM = {
302
+ _id: 'cart-item-side',
303
+ item_id: 'item-side',
304
+ name: 'Fries',
305
+ quantity: 1,
306
+ price: 500,
307
+ subtotal: 500,
308
+ tax_rate: 0,
309
+ modifiers: [],
310
+ special_instructions: '',
311
+ item_discount_details: null,
312
+ };
313
+ const twoItemCart = () => makeCategories([PAID_ITEM, SIDE_ITEM]);
314
+ const discountOffer = (discount) => ({
315
+ ...makeOffer(constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS, constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT, true),
316
+ reward_discount: {
317
+ discount_value: 10,
318
+ is_discount_percentage: false,
319
+ max_discount_amount: null,
320
+ max_discount_percentage: null,
321
+ excluded_refs: [],
322
+ ...discount,
323
+ },
324
+ });
325
+ it('applies a fixed amount against the eligible subtotal', () => {
326
+ const result = (0, offer_apply_helpers_1.buildOfferDiscountUpdate)({
327
+ offer: discountOffer({ excluded_refs: ['item-side'] }),
328
+ orderDiscountDetails: null,
329
+ categories: twoItemCart(),
330
+ });
331
+ expect(result.success).toBe(true);
332
+ expect(result.discountDetails.loyalty_discount).toBe(10);
333
+ expect(result.discountDetails.is_loyalty_discount_percentage).toBe(false);
334
+ expect(result.errorMessage).toBeNull();
335
+ });
336
+ it('never discounts more than the eligible subtotal', () => {
337
+ const result = (0, offer_apply_helpers_1.buildOfferDiscountUpdate)({
338
+ offer: discountOffer({ discount_value: 5000, excluded_refs: ['item-side'] }),
339
+ orderDiscountDetails: null,
340
+ categories: twoItemCart(),
341
+ });
342
+ expect(result.success).toBe(true);
343
+ expect(result.discountDetails.loyalty_discount).toBe(1000);
344
+ });
345
+ it('still refuses when nothing is eligible', () => {
346
+ const result = (0, offer_apply_helpers_1.buildOfferDiscountUpdate)({
347
+ offer: discountOffer({ excluded_refs: ['item-broast', 'item-side'] }),
348
+ orderDiscountDetails: null,
349
+ categories: twoItemCart(),
350
+ });
351
+ expect(result.success).toBe(false);
352
+ expect(result.discountDetails).toBeNull();
353
+ });
354
+ it('leaves the percentage path unchanged', () => {
355
+ const result = (0, offer_apply_helpers_1.buildOfferDiscountUpdate)({
356
+ offer: discountOffer({ discount_value: 10, is_discount_percentage: true, excluded_refs: ['item-side'] }),
357
+ orderDiscountDetails: null,
358
+ categories: twoItemCart(),
359
+ });
360
+ expect(result.success).toBe(true);
361
+ expect(result.discountDetails.loyalty_discount).toBe(100);
362
+ });
363
+ it('keeps engine and builder in agreement for this class of offer', () => {
364
+ const offer = discountOffer({ excluded_refs: ['item-side'] });
365
+ const { stats } = statsFor(offer, { categories: twoItemCart() });
366
+ const result = (0, offer_apply_helpers_1.buildOfferDiscountUpdate)({
367
+ offer,
368
+ orderDiscountDetails: null,
369
+ categories: twoItemCart(),
370
+ });
371
+ // The original defect: claimable in the list, refused on click.
372
+ expect(stats.claimable).toBe(true);
373
+ expect(result.success).toBe(true);
374
+ });
375
+ });
376
+ describe('voucher-pathway availability messaging (G1)', () => {
377
+ const voucherOffer = () => makeOffer(constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER, constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS, true);
378
+ it('asks for a code when the offer voucher is usable but not held', () => {
379
+ const { stats } = statsFor(voucherOffer(), { voucherCodes: {} });
380
+ expect(stats.claimable).toBe(false);
381
+ expect(stats.message).toBe('Enter a matching voucher code to unlock this offer');
382
+ });
383
+ it('reports the voucher as unavailable when it is inactive', () => {
384
+ const inactive = { ...makeVoucher(), active: false };
385
+ const { stats } = statsFor(voucherOffer(), { vouchers: [inactive] });
386
+ expect(stats.claimable).toBe(false);
387
+ expect(stats.message).toContain('no longer available');
388
+ });
389
+ it('reports the voucher as unavailable when it is used up', () => {
390
+ const exhausted = { ...makeVoucher(), max_uses: 1, used_count: 1 };
391
+ const { stats } = statsFor(voucherOffer(), { vouchers: [exhausted] });
392
+ expect(stats.claimable).toBe(false);
393
+ expect(stats.message).toContain('no longer available');
394
+ });
395
+ it('still claims normally when the voucher is healthy and held', () => {
396
+ const { stats } = statsFor(voucherOffer());
397
+ expect(stats.claimable).toBe(true);
398
+ });
399
+ });
400
+ describe('admin-blocked cells are refused defensively (G2)', () => {
401
+ const blocked = [
402
+ [constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.POINTS, constants_1.OFFER_REWARD_TYPE_ENUM.POINTS],
403
+ [constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER, constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER],
404
+ ];
405
+ it.each(blocked)('%s + %s is rejected instead of counted twice', (pathway, reward) => {
406
+ const offer = makeOffer(pathway, reward, true);
407
+ expect((0, offers_helpers_1.isOfferPathwayAndRewardSharingResource)(offer)).toBe(true);
408
+ const { stats, unlockedTimes } = statsFor(offer);
409
+ expect(stats.invalid_offer).toBe(true);
410
+ expect(stats.claimable).toBe(false);
411
+ expect(unlockedTimes).toBeUndefined();
412
+ expect(stats.message).toContain('misconfigured');
413
+ });
414
+ it('does not flag any of the 26 valid cells', () => {
415
+ for (const [pathway, reward] of VALID_CELLS) {
416
+ expect((0, offers_helpers_1.isOfferPathwayAndRewardSharingResource)(makeOffer(pathway, reward, true))).toBe(false);
417
+ }
418
+ });
419
+ });
420
+ describe('voucher pathway honours max_claims (G4)', () => {
421
+ it('caps the session claims by max_claims even when more codes are held', () => {
422
+ const offer = makeOffer(constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.VOUCHER, constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS, true);
423
+ const capped = {
424
+ ...offer,
425
+ pathway_to_unlock_type_vouchers: {
426
+ ...offer.pathway_to_unlock_type_vouchers,
427
+ max_claims: 2,
428
+ },
429
+ };
430
+ // wallet holds 2 codes, max_claims is 2 -> 2; then tighten to 1
431
+ expect(statsFor(capped).stats.applicable_quantity).toBe(2);
432
+ const tighter = {
433
+ ...capped,
434
+ pathway_to_unlock_type_vouchers: { ...capped.pathway_to_unlock_type_vouchers, max_claims: 1 },
435
+ };
436
+ expect(statsFor(tighter).stats.applicable_quantity).toBe(1);
437
+ });
438
+ });
439
+ describe('item-reward progress uses floor, the ledger uses ceil (G3)', () => {
440
+ const rewardItem = (offerId) => ({
441
+ _id: 'cart-item-reward',
442
+ item_id: ITEM_ID,
443
+ name: 'Half Broast',
444
+ quantity: 1,
445
+ price: 1000,
446
+ subtotal: 0,
447
+ tax_rate: 0,
448
+ modifiers: [],
449
+ special_instructions: '',
450
+ item_discount_details: {
451
+ loyalty_discount: 100,
452
+ loyalty_discount_id: offerId,
453
+ is_loyalty_discount_percentage: true,
454
+ total_discount: 1000,
455
+ },
456
+ });
457
+ it('does not block selection when a 2-item reward has only 1 item picked', () => {
458
+ const offer = makeOffer(constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS, constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS, true);
459
+ const twoItemOffer = {
460
+ ...offer,
461
+ reward_items: { ...offer.reward_items, num_of_items: 2 },
462
+ };
463
+ const { stats } = statsFor(twoItemOffer, {
464
+ categories: makeCategories([PAID_ITEM, rewardItem(offer._id)]),
465
+ });
466
+ // floor(1 / 2) = 0 -> the second item is still selectable.
467
+ expect(stats.redeemed_times).toBe(0);
468
+ expect(stats.claimable).toBe(true);
469
+ });
470
+ it('records a full claim slot in the ledger for a partially used reward', () => {
471
+ const offer = makeOffer(constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS, constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS, true);
472
+ const twoItemOffer = {
473
+ ...offer,
474
+ reward_items: { ...offer.reward_items, num_of_items: 2 },
475
+ };
476
+ const synced = (0, offer_apply_helpers_1.getSyncOffersWithCartData)({
477
+ categories: makeCategories([PAID_ITEM, rewardItem(offer._id)]),
478
+ redeemed_offers: null,
479
+ order_discount_details: null,
480
+ price: 1000,
481
+ total_discount_applied: 0,
482
+ wallet_redemptions: null,
483
+ platform: constants_1.PLATFORMS_ENUM.WEB_DELIVERY,
484
+ }, [], {}, { [offer._id]: 1 }, [twoItemOffer]);
485
+ // ceil(1 / 2) = 1 -> the partially used reward still consumes its slot.
486
+ expect(synced.redeemed_offers).toEqual({ [offer._id]: 1 });
487
+ });
488
+ });
300
489
  describe('pathway gating modifiers', () => {
301
490
  const ordersOffer = (orders, totalOrders = 5) => {
302
491
  const offer = makeOffer(constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS, constants_1.OFFER_REWARD_TYPE_ENUM.POINTS, true);
@@ -38,8 +38,26 @@ const categories = [
38
38
  name: 'BROAST',
39
39
  items: [
40
40
  item(),
41
- { _id: 'c2', item_id: 'special-1', item_type: 'SPECIAL', name: 'Deal', quantity: 1, subtotal: 800, modifiers: [], item_discount_details: null },
42
- { _id: 'c3', item_id: 'halfhalf-1', item_type: 'HALF_HALF', name: 'Half & Half', quantity: 1, subtotal: 600, modifiers: [], item_discount_details: null },
41
+ {
42
+ _id: 'c2',
43
+ item_id: 'special-1',
44
+ item_type: 'SPECIAL',
45
+ name: 'Deal',
46
+ quantity: 1,
47
+ subtotal: 800,
48
+ modifiers: [],
49
+ item_discount_details: null,
50
+ },
51
+ {
52
+ _id: 'c3',
53
+ item_id: 'halfhalf-1',
54
+ item_type: 'HALF_HALF',
55
+ name: 'Half & Half',
56
+ quantity: 1,
57
+ subtotal: 600,
58
+ modifiers: [],
59
+ item_discount_details: null,
60
+ },
43
61
  ],
44
62
  },
45
63
  ];
@@ -22,11 +22,21 @@ describe('getMaxApplicableWalletCashback', () => {
22
22
  // item-level: 1000 - 200 = 800
23
23
  expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, totalDiscountApplied: 200, orderLevelDiscount: 0 })).toBe(500);
24
24
  // item-level discount bigger than the balance -> balance is the cap
25
- expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, cashbackBalance: 900, totalDiscountApplied: 300, orderLevelDiscount: 0 })).toBe(700);
25
+ expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({
26
+ ...base,
27
+ cashbackBalance: 900,
28
+ totalDiscountApplied: 300,
29
+ orderLevelDiscount: 0,
30
+ })).toBe(700);
26
31
  });
27
32
  it('never goes negative or NaN', () => {
28
33
  expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, cashbackBalance: -50 })).toBe(0);
29
- expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ cashbackBalance: NaN, itemsPrice: NaN, totalDiscountApplied: NaN, orderLevelDiscount: NaN })).toBe(0);
34
+ expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({
35
+ cashbackBalance: NaN,
36
+ itemsPrice: NaN,
37
+ totalDiscountApplied: NaN,
38
+ orderLevelDiscount: NaN,
39
+ })).toBe(0);
30
40
  expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, cashbackBalance: 5000, itemsPrice: 0 })).toBe(0);
31
41
  });
32
42
  });
@@ -80,7 +90,10 @@ describe('wallet redemption patches', () => {
80
90
  });
81
91
  it('clears everything when clearAll is set', () => {
82
92
  const patch = (0, offers_helpers_1.buildClearWalletRedemptionsPatch)({
83
- currentWalletRedemptions: { cashback_amount: 25, gift_cards: [{ gift_card_id: 'gc-1', redeemed_times: 1 }] },
93
+ currentWalletRedemptions: {
94
+ cashback_amount: 25,
95
+ gift_cards: [{ gift_card_id: 'gc-1', redeemed_times: 1 }],
96
+ },
84
97
  clearAll: true,
85
98
  });
86
99
  expect(patch.wallet_redemptions).toBeNull();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.38",
3
+ "version": "0.4.39",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",