@managesome/eposmatic-toolkit 0.4.37 → 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.
Files changed (57) hide show
  1. package/esm/constants/accounting-templates.js +431 -0
  2. package/esm/constants/index.js +1 -0
  3. package/esm/helpers/store/cart-actions.js +54 -6
  4. package/esm/helpers/utils/cart-helpers.js +19 -5
  5. package/esm/helpers/utils/common.js +18 -1
  6. package/esm/helpers/utils/offer-apply-helpers.js +5 -3
  7. package/esm/helpers/utils/offers-helpers.js +92 -63
  8. package/lib/constants/accounting-templates.d.ts +8 -0
  9. package/lib/constants/accounting-templates.js +434 -0
  10. package/lib/constants/index.d.ts +1 -0
  11. package/lib/constants/index.js +1 -0
  12. package/lib/helpers/store/cart-actions.js +54 -6
  13. package/lib/helpers/utils/cart-helpers.js +19 -5
  14. package/lib/helpers/utils/common.d.ts +15 -0
  15. package/lib/helpers/utils/common.js +19 -1
  16. package/lib/helpers/utils/offer-apply-helpers.js +5 -3
  17. package/lib/helpers/utils/offers-helpers.d.ts +15 -4
  18. package/lib/helpers/utils/offers-helpers.js +92 -62
  19. package/lib/schemas/models/expense_categories.d.ts +3 -3
  20. package/lib/schemas/models/expense_details.d.ts +3 -3
  21. package/lib/schemas/models/gift_cards.d.ts +3 -3
  22. package/lib/schemas/models/halfhalf_setup.d.ts +3 -3
  23. package/lib/schemas/models/internal_invoices.d.ts +3 -3
  24. package/lib/schemas/models/items.d.ts +12 -12
  25. package/lib/schemas/models/marketing_fingerprints.d.ts +3 -3
  26. package/lib/schemas/models/modifier_extras.d.ts +3 -3
  27. package/lib/schemas/models/offers.d.ts +3 -3
  28. package/lib/schemas/models/public_holidays.d.ts +3 -3
  29. package/lib/schemas/models/specials.d.ts +3 -3
  30. package/lib/schemas/models/tenant_chart_of_accounts.d.ts +3 -3
  31. package/lib/schemas/models/tenant_hr_departments.d.ts +3 -3
  32. package/lib/schemas/models/tenant_hr_documents.d.ts +3 -3
  33. package/lib/schemas/models/tenant_hr_policies.d.ts +3 -3
  34. package/lib/schemas/models/tenant_hr_positions.d.ts +3 -3
  35. package/lib/schemas/models/tenant_journal_entries.d.ts +3 -3
  36. package/lib/schemas/models/void_order_items.d.ts +3 -3
  37. package/lib/schemas/models/vouchers.d.ts +3 -3
  38. package/lib/schemas/models/warehouse_communications.d.ts +3 -3
  39. package/lib/schemas/models/warehouse_items.d.ts +3 -3
  40. package/lib/schemas/models/warehouse_void_purchase_order_items.d.ts +3 -3
  41. package/lib/schemas/models/warehouse_void_sale_order_items.d.ts +3 -3
  42. package/lib/schemas/types/tenant_chart_of_accounts.d.ts +7 -0
  43. package/lib/tests/cart-helpers-edit-item-refs.test.d.ts +1 -0
  44. package/lib/tests/cart-helpers-edit-item-refs.test.js +99 -0
  45. package/lib/tests/code-redemption-helpers.test.d.ts +1 -0
  46. package/lib/tests/code-redemption-helpers.test.js +246 -0
  47. package/lib/tests/gift-card-helpers.test.d.ts +1 -0
  48. package/lib/tests/gift-card-helpers.test.js +100 -0
  49. package/lib/tests/offer-claim-regressions.test.d.ts +1 -0
  50. package/lib/tests/offer-claim-regressions.test.js +367 -0
  51. package/lib/tests/offers-helpers.test.js +24 -4
  52. package/lib/tests/offers-matrix.test.js +248 -20
  53. package/lib/tests/ref-exclusion-matching.test.d.ts +1 -0
  54. package/lib/tests/ref-exclusion-matching.test.js +168 -0
  55. package/lib/tests/wallet-cashback-helpers.test.d.ts +1 -0
  56. package/lib/tests/wallet-cashback-helpers.test.js +108 -0
  57. package/package.json +1 -1
@@ -32,29 +32,54 @@ 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');
40
40
  return {
41
- _id: VOUCHER_ID, code: VOUCHER_CODE, brand_id: 'brand_1', tenants: ['tenant_1'],
42
- name: 'SAVE10', description: '', active: true, is_public: true,
43
- max_uses: null, max_uses_per_customer: null, max_assignments: null, max_assignments_per_customer: null,
44
- assigned_count: 0, last_assigned_at: null, used_count: 0, last_used_at: null, expire_at: null,
45
- updatedAt: now, createdAt: now,
41
+ _id: VOUCHER_ID,
42
+ code: VOUCHER_CODE,
43
+ brand_id: 'brand_1',
44
+ tenants: ['tenant_1'],
45
+ name: 'SAVE10',
46
+ description: '',
47
+ active: true,
48
+ is_public: true,
49
+ max_uses: null,
50
+ max_uses_per_customer: null,
51
+ max_assignments: null,
52
+ max_assignments_per_customer: null,
53
+ assigned_count: 0,
54
+ last_assigned_at: null,
55
+ used_count: 0,
56
+ last_used_at: null,
57
+ expire_at: null,
58
+ updatedAt: now,
59
+ createdAt: now,
46
60
  };
47
61
  }
48
62
  function makeGiftCard() {
49
63
  const now = new Date('2026-01-01T00:00:00.000Z');
50
64
  return {
51
- _id: GIFT_CARD_ID, code: 'GC100', brand_id: 'brand_1', tenants: ['tenant_1'],
52
- name: 'Free Broast', description: '', active: true,
53
- max_uses: null, max_uses_per_customer: null, max_assignments: null, max_assignments_per_customer: null,
54
- assigned_count: 0, used_count: 0, expire_at: null,
65
+ _id: GIFT_CARD_ID,
66
+ code: 'GC100',
67
+ brand_id: 'brand_1',
68
+ tenants: ['tenant_1'],
69
+ name: 'Free Broast',
70
+ description: '',
71
+ active: true,
72
+ max_uses: null,
73
+ max_uses_per_customer: null,
74
+ max_assignments: null,
75
+ max_assignments_per_customer: null,
76
+ assigned_count: 0,
77
+ used_count: 0,
78
+ expire_at: null,
55
79
  reward_type: constants_1.OFFER_REWARD_TYPE_ENUM.ITEMS,
56
80
  reward_items: { num_of_items: 1, excluded_refs: [], exclusive_refs: [], item_ids: [ITEM_ID], allocation: null },
57
- updatedAt: now, createdAt: now,
81
+ updatedAt: now,
82
+ createdAt: now,
58
83
  };
59
84
  }
60
85
  function pathwayPayload(pathway, unlocked) {
@@ -148,7 +173,11 @@ function rewardPayload(reward) {
148
173
  return {
149
174
  reward_type: reward,
150
175
  reward_items: {
151
- num_of_items: 1, excluded_refs: [], exclusive_refs: [], item_ids: [ITEM_ID], allocation: null,
176
+ num_of_items: 1,
177
+ excluded_refs: [],
178
+ exclusive_refs: [],
179
+ item_ids: [ITEM_ID],
180
+ allocation: null,
152
181
  },
153
182
  };
154
183
  case constants_1.OFFER_REWARD_TYPE_ENUM.POINTS:
@@ -157,7 +186,10 @@ function rewardPayload(reward) {
157
186
  return {
158
187
  reward_type: reward,
159
188
  reward_cashback: {
160
- cashback_value: 5, is_cashback_percentage: false, max_cashback_amount: null, excluded_refs: [],
189
+ cashback_value: 5,
190
+ is_cashback_percentage: false,
191
+ max_cashback_amount: null,
192
+ excluded_refs: [],
161
193
  },
162
194
  };
163
195
  case constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS:
@@ -181,7 +213,7 @@ function makeOffer(pathway, reward, unlocked) {
181
213
  ...rewardPayload(reward),
182
214
  };
183
215
  }
184
- function statsFor(offer, { redeemedTimes = 0, totalOrders = 5 } = {}) {
216
+ function statsFor(offer, { redeemedTimes = 0, totalOrders = 5, vouchers = [makeVoucher()], voucherCodes = { [VOUCHER_CODE]: 2 }, categories = makeCategories(), } = {}) {
185
217
  const result = (0, offers_helpers_1.getOffersWithStats)({
186
218
  offers: [offer],
187
219
  customer: {
@@ -190,10 +222,10 @@ function statsFor(offer, { redeemedTimes = 0, totalOrders = 5 } = {}) {
190
222
  cashback_balance: 0,
191
223
  total_orders: totalOrders,
192
224
  total_orders_per_platform_type: { [constants_1.PLATFORMS_ENUM.WEB_DELIVERY]: totalOrders },
193
- voucher_codes: { [VOUCHER_CODE]: 2 },
225
+ voucher_codes: voucherCodes,
194
226
  gift_cards: {},
195
227
  },
196
- vouchers: [makeVoucher()],
228
+ vouchers,
197
229
  giftCards: [makeGiftCard()],
198
230
  customerOfferRedemptionCounts: { ASSIGNED: {}, USED: {} },
199
231
  redeemed_offers: redeemedTimes > 0 ? { [offer._id]: redeemedTimes } : null,
@@ -201,7 +233,7 @@ function statsFor(offer, { redeemedTimes = 0, totalOrders = 5 } = {}) {
201
233
  currency_symbol: constants_1.CURRENCY_SYMBOL_ENUM.Rs,
202
234
  currentItemsPrice: 1000,
203
235
  totalDiscountApplied: 0,
204
- categories: makeCategories(),
236
+ categories,
205
237
  order_discount_details: null,
206
238
  });
207
239
  return {
@@ -265,6 +297,195 @@ describe('pathway x reward matrix', () => {
265
297
  expect(stats.action_hint).not.toBeNull();
266
298
  });
267
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
+ });
268
489
  describe('pathway gating modifiers', () => {
269
490
  const ordersOffer = (orders, totalOrders = 5) => {
270
491
  const offer = makeOffer(constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS, constants_1.OFFER_REWARD_TYPE_ENUM.POINTS, true);
@@ -335,13 +556,20 @@ describe('pathway x reward matrix', () => {
335
556
  ...pct,
336
557
  pathway_to_unlock_type_orders: { ...pct.pathway_to_unlock_type_orders, total_amount: 100 },
337
558
  reward_cashback: {
338
- cashback_value: 5, is_cashback_percentage: true, max_cashback_amount: null, excluded_refs: [],
559
+ cashback_value: 5,
560
+ is_cashback_percentage: true,
561
+ max_cashback_amount: null,
562
+ excluded_refs: [],
339
563
  },
340
564
  };
341
565
  expect(statsFor(pctOffer).stats.applicable_quantity).toBe(1);
342
566
  });
343
567
  it('once-per-order rewards never exceed one claim', () => {
344
- for (const reward of [constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT, constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS, constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER]) {
568
+ for (const reward of [
569
+ constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT,
570
+ constants_1.OFFER_REWARD_TYPE_ENUM.GIFT_CARDS,
571
+ constants_1.OFFER_REWARD_TYPE_ENUM.VOUCHER,
572
+ ]) {
345
573
  expect(statsFor(ordersOffer(reward)).stats.applicable_quantity).toBe(1);
346
574
  }
347
575
  });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("../constants");
4
+ const common_1 = require("../helpers/utils/common");
5
+ const offers_helpers_1 = require("../helpers/utils/offers-helpers");
6
+ /**
7
+ * Two different things are called "excluded_refs":
8
+ *
9
+ * - On a plain DISCOUNT / CASHBACK reward it is an **item-level** list, collected
10
+ * by the "Excluded Items" picker. A ref is an item id - and, because a SPECIAL
11
+ * or HALF_HALF order line stores its own document id in `item_id`, a special or
12
+ * half-half id too. These refs reduce the spend-qualification base.
13
+ * - On a DISCOUNT_ITEMS reward it behaves exactly like IRewardTypeItems: an
14
+ * **option-level** hide list, acting on the modifier tree at selection time.
15
+ * Which items the reward may be applied to is decided by `item_ids`. It does
16
+ * NOT reduce the spend-qualification base.
17
+ */
18
+ const item = (overrides = {}) => ({
19
+ _id: 'cart-1',
20
+ item_id: 'item-broast',
21
+ name: 'Broast',
22
+ quantity: 1,
23
+ subtotal: 1000,
24
+ modifiers: [
25
+ {
26
+ _id: 'm-1',
27
+ ref: 'modifier-dips',
28
+ name: 'Dips',
29
+ options: [{ _id: 'o-1', ref: 'option-garlic', name: 'Garlic', price: 100, quantity: 1, modifiers: [] }],
30
+ },
31
+ ],
32
+ item_discount_details: null,
33
+ ...overrides,
34
+ });
35
+ const categories = [
36
+ {
37
+ category_id: 'category-broast',
38
+ name: 'BROAST',
39
+ items: [
40
+ item(),
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
+ },
61
+ ],
62
+ },
63
+ ];
64
+ describe('doesOrderItemMatchRefs', () => {
65
+ it('matches item-level ids only', () => {
66
+ expect((0, common_1.doesOrderItemMatchRefs)(item(), ['item-broast'])).toBe(true);
67
+ expect((0, common_1.doesOrderItemMatchRefs)(item(), ['special-1'])).toBe(false);
68
+ });
69
+ it('does not treat a modifier or option ref as an item ref', () => {
70
+ expect((0, common_1.doesOrderItemMatchRefs)(item(), ['modifier-dips'])).toBe(false);
71
+ expect((0, common_1.doesOrderItemMatchRefs)(item(), ['option-garlic'])).toBe(false);
72
+ });
73
+ it('is false for an empty list', () => {
74
+ expect((0, common_1.doesOrderItemMatchRefs)(item(), [])).toBe(false);
75
+ });
76
+ });
77
+ describe('calculateEligibleOfferDiscountDetails (item-level refs)', () => {
78
+ it('excludes by item id, special id and half-half id', () => {
79
+ expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['item-broast']).subtotal).toBe(1400);
80
+ expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['special-1']).subtotal).toBe(1600);
81
+ expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['halfhalf-1']).subtotal).toBe(1800);
82
+ expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['special-1', 'halfhalf-1']).subtotal).toBe(1000);
83
+ });
84
+ it('ignores modifier/option refs entirely', () => {
85
+ expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['modifier-dips']).subtotal).toBe(2400);
86
+ expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['modifier-dips']).hasExcludedRefs).toBe(false);
87
+ });
88
+ it('excludes a whole category', () => {
89
+ expect((0, common_1.calculateEligibleOfferDiscountDetails)(categories, ['category-broast']).subtotal).toBe(0);
90
+ });
91
+ });
92
+ describe('DISCOUNT_ITEMS option refs do not shrink the qualification base', () => {
93
+ const makeOffer = (rewardType, excludedRefs) => ({
94
+ _id: 'offer-1',
95
+ name: 'Offer',
96
+ active: true,
97
+ auto_apply: false,
98
+ sort_id: 1,
99
+ included_platforms: [constants_1.PLATFORMS_ENUM.WEB_DELIVERY],
100
+ offer_special_hours: null,
101
+ pathway_to_unlock_type: constants_1.PATHWAY_TO_UNLOCK_TYPE_ENUM.ORDERS,
102
+ pathway_to_unlock_type_orders: {
103
+ scope: 'OVERALL',
104
+ max_uses_per_customer: null,
105
+ max_claims: null,
106
+ min_num_of_orders: 0,
107
+ max_num_of_orders: null,
108
+ total_amount: 1200,
109
+ max_amount: null,
110
+ orders_between_unlocks: null,
111
+ },
112
+ reward_type: rewardType,
113
+ ...(rewardType === constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS
114
+ ? {
115
+ reward_discount_items: {
116
+ num_of_items: 1,
117
+ excluded_refs: excludedRefs,
118
+ exclusive_refs: [],
119
+ item_ids: ['item-broast'],
120
+ discount_value: 50,
121
+ is_discount_percentage: true,
122
+ allocation: null,
123
+ },
124
+ }
125
+ : {
126
+ reward_discount: {
127
+ discount_value: 50,
128
+ is_discount_percentage: true,
129
+ max_discount_amount: null,
130
+ max_discount_percentage: null,
131
+ excluded_refs: excludedRefs,
132
+ },
133
+ }),
134
+ });
135
+ const stats = (rewardType, excludedRefs) => (0, offers_helpers_1.getOffersWithStats)({
136
+ offers: [makeOffer(rewardType, excludedRefs)],
137
+ customer: {
138
+ phone_number: '+9200000000',
139
+ loyalty_points: 0,
140
+ cashback_balance: 0,
141
+ total_orders: 5,
142
+ total_orders_per_platform_type: { [constants_1.PLATFORMS_ENUM.WEB_DELIVERY]: 5 },
143
+ voucher_codes: {},
144
+ gift_cards: {},
145
+ },
146
+ vouchers: [],
147
+ giftCards: [],
148
+ customerOfferRedemptionCounts: { ASSIGNED: {}, USED: {} },
149
+ redeemed_offers: null,
150
+ platform: constants_1.PLATFORMS_ENUM.WEB_DELIVERY,
151
+ currency_symbol: constants_1.CURRENCY_SYMBOL_ENUM.Rs,
152
+ currentItemsPrice: 2400,
153
+ totalDiscountApplied: 0,
154
+ categories,
155
+ order_discount_details: null,
156
+ }).offersWithStats[0].stats;
157
+ it('leaves the base untouched when an option ref is listed on a DISCOUNT_ITEMS reward', () => {
158
+ // Cart is 2400 and the threshold is 1200: still unlocked, even though an
159
+ // option inside the 1000 item is listed in excluded_refs.
160
+ expect(stats(constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS, ['modifier-dips']).progress).toBe(100);
161
+ });
162
+ it('does shrink the base when an item id is listed on a plain DISCOUNT reward', () => {
163
+ // Cart 2400, threshold 1200. Excluding the 1000 item leaves 1400 -> still unlocked.
164
+ expect(stats(constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT, ['item-broast']).progress).toBe(100);
165
+ // Excluding the 1000 item and the 800 special leaves 600 -> locked.
166
+ expect(stats(constants_1.OFFER_REWARD_TYPE_ENUM.DISCOUNT, ['item-broast', 'special-1']).progress).toBeLessThan(100);
167
+ });
168
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const offers_helpers_1 = require("../helpers/utils/offers-helpers");
4
+ /**
5
+ * The cashback half of the wallet, which had no tests at all.
6
+ *
7
+ * `getMaxApplicableWalletCashback` is what the cashback slider is bounded by, so
8
+ * an off-by-one here either lets the customer spend more than the order is worth
9
+ * or hides cashback they own.
10
+ */
11
+ describe('getMaxApplicableWalletCashback', () => {
12
+ const base = { cashbackBalance: 500, itemsPrice: 1000, totalDiscountApplied: 0, orderLevelDiscount: 0 };
13
+ it('is capped by the wallet balance', () => {
14
+ expect((0, offers_helpers_1.getMaxApplicableWalletCashback)(base)).toBe(500);
15
+ });
16
+ it('is capped by the net items value when cashback is larger', () => {
17
+ expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, cashbackBalance: 5000 })).toBe(1000);
18
+ });
19
+ it('ignores order-level discounts but subtracts item-level ones', () => {
20
+ // order-level only: net value is still the full 1000
21
+ expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, totalDiscountApplied: 200, orderLevelDiscount: 200 })).toBe(500);
22
+ // item-level: 1000 - 200 = 800
23
+ expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, totalDiscountApplied: 200, orderLevelDiscount: 0 })).toBe(500);
24
+ // item-level discount bigger than the balance -> balance is the cap
25
+ expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({
26
+ ...base,
27
+ cashbackBalance: 900,
28
+ totalDiscountApplied: 300,
29
+ orderLevelDiscount: 0,
30
+ })).toBe(700);
31
+ });
32
+ it('never goes negative or NaN', () => {
33
+ expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, cashbackBalance: -50 })).toBe(0);
34
+ expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({
35
+ cashbackBalance: NaN,
36
+ itemsPrice: NaN,
37
+ totalDiscountApplied: NaN,
38
+ orderLevelDiscount: NaN,
39
+ })).toBe(0);
40
+ expect((0, offers_helpers_1.getMaxApplicableWalletCashback)({ ...base, cashbackBalance: 5000, itemsPrice: 0 })).toBe(0);
41
+ });
42
+ });
43
+ describe('cashback recommended breakpoints', () => {
44
+ it('splits the applicable amount into the default 25/50/75/100 steps', () => {
45
+ expect((0, offers_helpers_1.getWalletCashbackRecommendedValues)({ maxApplicableCashback: 100 })).toEqual([25, 50, 75, 100]);
46
+ });
47
+ it('keeps the percentage when the amount rounds', () => {
48
+ const breakpoints = (0, offers_helpers_1.getWalletCashbackRecommendedBreakpoints)({ maxApplicableCashback: 10 });
49
+ expect(breakpoints).toEqual([
50
+ { value: 2.5, percentage: 25 },
51
+ { value: 5, percentage: 50 },
52
+ { value: 7.5, percentage: 75 },
53
+ { value: 10, percentage: 100 },
54
+ ]);
55
+ });
56
+ it('returns nothing when there is nothing applicable', () => {
57
+ expect((0, offers_helpers_1.getWalletCashbackRecommendedValues)({ maxApplicableCashback: 0 })).toEqual([]);
58
+ expect((0, offers_helpers_1.getWalletCashbackRecommendedValues)({ maxApplicableCashback: null })).toEqual([]);
59
+ });
60
+ it('accepts custom steps and clamps percentages to 0..100', () => {
61
+ expect((0, offers_helpers_1.getWalletCashbackRecommendedBreakpoints)({ maxApplicableCashback: 100, percentageSteps: [10, 200] })).toEqual([
62
+ { value: 10, percentage: 10 },
63
+ { value: 100, percentage: 100 },
64
+ ]);
65
+ });
66
+ });
67
+ describe('wallet redemption patches', () => {
68
+ it('sets the cashback amount and keeps gift cards', () => {
69
+ const patch = (0, offers_helpers_1.buildWalletCashbackRedemptionPatch)({
70
+ currentWalletRedemptions: { gift_cards: [{ gift_card_id: 'gc-1', redeemed_times: 1 }] },
71
+ cashbackAmount: 25,
72
+ });
73
+ expect(patch.wallet_redemptions.cashback_amount).toBe(25);
74
+ expect(patch.wallet_redemptions.gift_cards).toEqual([{ gift_card_id: 'gc-1', redeemed_times: 1 }]);
75
+ });
76
+ it('drops a zero cashback amount instead of writing 0', () => {
77
+ const patch = (0, offers_helpers_1.buildWalletCashbackRedemptionPatch)({ currentWalletRedemptions: null, cashbackAmount: 0 });
78
+ expect(patch.wallet_redemptions).toBeNull();
79
+ });
80
+ it('clears only cashback when asked, leaving gift cards intact', () => {
81
+ const patch = (0, offers_helpers_1.buildClearWalletRedemptionsPatch)({
82
+ currentWalletRedemptions: {
83
+ cashback_amount: 25,
84
+ gift_cards: [{ gift_card_id: 'gc-1', redeemed_times: 1 }],
85
+ },
86
+ clearCashbackAmount: true,
87
+ });
88
+ expect(patch.wallet_redemptions.cashback_amount).toBeUndefined();
89
+ expect(patch.wallet_redemptions.gift_cards).toEqual([{ gift_card_id: 'gc-1', redeemed_times: 1 }]);
90
+ });
91
+ it('clears everything when clearAll is set', () => {
92
+ const patch = (0, offers_helpers_1.buildClearWalletRedemptionsPatch)({
93
+ currentWalletRedemptions: {
94
+ cashback_amount: 25,
95
+ gift_cards: [{ gift_card_id: 'gc-1', redeemed_times: 1 }],
96
+ },
97
+ clearAll: true,
98
+ });
99
+ expect(patch.wallet_redemptions).toBeNull();
100
+ });
101
+ it('merges a partial patch over the existing redemptions', () => {
102
+ const patch = (0, offers_helpers_1.buildWalletRedemptionsMergePatch)({
103
+ currentWalletRedemptions: { cashback_amount: 10 },
104
+ walletRedemptionsPatch: { cashback_amount: 40 },
105
+ });
106
+ expect(patch.wallet_redemptions.cashback_amount).toBe(40);
107
+ });
108
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.37",
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",