@managesome/eposmatic-toolkit 0.4.27 → 0.4.28

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.
@@ -0,0 +1,676 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const globals_1 = require("@jest/globals");
4
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
5
+ const materialized_discount_compile_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-compile-helpers");
6
+ const materialized_discount_source_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-source-helpers");
7
+ const materialized_integer_contract_1 = require("../../helpers/materialized-reports/materialized-integer-contract");
8
+ /**
9
+ * The Discounts scenarios that the real-order fixture and the classifier tests
10
+ * do not cover, driven through the REAL pipeline: a raw order document ->
11
+ * `compileTenantDiscountReports` -> `buildDiscountSourcesForScope`.
12
+ *
13
+ * The orders below are shaped like the captured production orders
14
+ * (`src/tests/real-discount-orders-2026-10-23.json`): money in integer cents,
15
+ * a percentage RATE in integer BASIS POINTS (`5000` = 50%), the item's money in
16
+ * `_net_amount` / `_gross_amount`.
17
+ *
18
+ * Every case asserts the EXACT money, level, source key, order count, free
19
+ * count and complimentary flag — not the shape.
20
+ */
21
+ const TENANT = 'thailemon.uae';
22
+ const BRAND = 'thailemon.co.nz';
23
+ const DAY = '2026-10-23';
24
+ const MONTH = '2026-10';
25
+ const NOW = new Date('2026-10-23T23:59:00.000Z');
26
+ /** A raw item in the stored shape (money in cents). */
27
+ const rawItem = (over = {}) => ({
28
+ _id: 'item-1',
29
+ item_id: 'catalog-item-1',
30
+ menu_id: 'menu-1',
31
+ item_type: 'ITEM',
32
+ name: 'Pizza',
33
+ price: 10000,
34
+ subtotal: 10000,
35
+ quantity: 1,
36
+ tax_rate: 0,
37
+ _gst_amount: 0,
38
+ _net_amount: 10000,
39
+ _gross_amount: 10000,
40
+ _discount_amount: 0,
41
+ special_instructions: '',
42
+ item_discount_details: null,
43
+ modifiers: [],
44
+ ...over,
45
+ });
46
+ /** A raw order in the stored shape. */
47
+ const rawOrder = (over = {}) => ({
48
+ _id: 'order-1',
49
+ tenant_id: TENANT,
50
+ platform: 'POS_WALK-IN',
51
+ order_status: 'COMPLETED',
52
+ order_number: 1,
53
+ localDate: DAY,
54
+ localHour: 15,
55
+ localMinute: 0,
56
+ categories: [{ category_id: 'cat-1', name: 'FOOD', items: [rawItem()] }],
57
+ payments: [
58
+ {
59
+ _id: 'pay-1',
60
+ payment_type: 'CASH',
61
+ payment_status: 'SETTLED',
62
+ amount_due: 10000,
63
+ amount_paid: 10000,
64
+ amount_refunded: 0,
65
+ card_details: null,
66
+ payment_id: 'pay-1',
67
+ },
68
+ ],
69
+ grand_total: 10000,
70
+ price: 10000,
71
+ amount_earned: 10000,
72
+ amount_collected: 10000,
73
+ total_discount_applied: 0,
74
+ order_discount_details: null,
75
+ is_deleted: false,
76
+ createdAt: new Date('2026-10-23T10:00:00.000Z'),
77
+ updatedAt: new Date('2026-10-23T10:00:00.000Z'),
78
+ order_settled_at: new Date('2026-10-23T10:00:00.000Z'),
79
+ staff_details: null,
80
+ order_taker_details: null,
81
+ wallet_redemptions: null,
82
+ redeemed_offers: null,
83
+ ...over,
84
+ });
85
+ /** Compiles the orders and returns the month scope's source rows. */
86
+ const compile = (orders) => (0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
87
+ tenantId: TENANT,
88
+ brandId: BRAND,
89
+ localDate: DAY,
90
+ orders: orders,
91
+ gmtOffset: 5,
92
+ computedAt: NOW,
93
+ });
94
+ const sourcesOf = (orders) => {
95
+ const compiled = compile(orders);
96
+ const entry = compiled.entries.find((candidate) => candidate.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
97
+ return {
98
+ compiled,
99
+ sources: (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)({
100
+ tenantId: TENANT,
101
+ brandId: BRAND,
102
+ scopeContext: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
103
+ scopeField: MONTH,
104
+ dayIndex: null,
105
+ sections: entry.discount_entries,
106
+ computedAt: NOW,
107
+ sourceWatermark: null,
108
+ }),
109
+ };
110
+ };
111
+ const byKey = (sources) => new Map(sources.map((source) => [`${source.source_key}|${source.level}`, source]));
112
+ (0, globals_1.describe)('discount scenarios: voucher', () => {
113
+ (0, globals_1.it)('books an ORDER-level voucher PERCENTAGE as a voucher source in basis points', () => {
114
+ const { compiled, sources } = sourcesOf([
115
+ rawOrder({
116
+ _id: 'order-voucher-pct',
117
+ order_number: 1,
118
+ grand_total: 10000,
119
+ amount_earned: 10000,
120
+ amount_collected: 8000,
121
+ total_discount_applied: 2000,
122
+ order_discount_details: {
123
+ voucher_discount: 2000, // 20% in BASIS POINTS
124
+ voucher_discount_id: 'voucher-offer-1',
125
+ voucher_discount_name: '20% Voucher',
126
+ is_voucher_discount_percentage: true,
127
+ total_discount: 2000,
128
+ },
129
+ }),
130
+ ]);
131
+ expect(compiled.contributingOrders).toBe(1);
132
+ const source = byKey(sources).get(`voucher:voucher-offer-1|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
133
+ expect(source).toBeDefined();
134
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.VOUCHER);
135
+ expect(source.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER);
136
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER);
137
+ expect(source.kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT);
138
+ expect(source.rate_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE);
139
+ // 2000 bps = 20%, stored as the exact integer sum and count.
140
+ expect(source.percentage_rate_sum).toBe(2000);
141
+ expect(source.percentage_row_count).toBe(1);
142
+ expect(source.fixed_row_count).toBe(0);
143
+ expect(source.discount_amount).toBe(2000);
144
+ expect(source.order_count).toBe(1);
145
+ expect(source.row_count).toBe(1);
146
+ // NO stored average rate.
147
+ expect('rate' in source).toBe(false);
148
+ });
149
+ });
150
+ (0, globals_1.describe)('discount scenarios: cancelled orders', () => {
151
+ (0, globals_1.it)('keeps a cancelled order OUT of the active totals and IN the cancelled section', () => {
152
+ const { compiled, sources } = sourcesOf([
153
+ rawOrder({
154
+ _id: 'order-active',
155
+ order_number: 1,
156
+ total_discount_applied: 1000,
157
+ order_discount_details: {
158
+ special_discount: 1000,
159
+ special_discount_id: 'MANUAL_Rs 10',
160
+ special_discount_name: 'Apply Discount',
161
+ is_special_discount_percentage: false,
162
+ total_discount: 1000,
163
+ },
164
+ }),
165
+ rawOrder({
166
+ _id: 'order-cancelled',
167
+ order_number: 2,
168
+ order_status: 'CANCELLED',
169
+ total_discount_applied: 2000,
170
+ order_discount_details: {
171
+ special_discount: 2000,
172
+ special_discount_id: 'MANUAL_Rs 20',
173
+ special_discount_name: 'Apply Discount',
174
+ is_special_discount_percentage: false,
175
+ total_discount: 2000,
176
+ },
177
+ }),
178
+ ]);
179
+ // The active (COMPLETED) key carries only the active order's money.
180
+ const report = compiled.reports.find((candidate) => candidate.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
181
+ const counters = report.discount_counters;
182
+ expect(counters['COMPLETED:POS_WALK-IN'].amount).toBe(1000);
183
+ expect(counters['COMPLETED:POS_WALK-IN'].count).toBe(1);
184
+ // The cancelled order is present, in its own section.
185
+ expect(counters['CANCELLED:POS_WALK-IN'].amount).toBe(2000);
186
+ expect(counters['CANCELLED:POS_WALK-IN'].count).toBe(1);
187
+ // The source row carries BOTH orders, split per status key — the read
188
+ // filters the per-key metrics, so the cancelled money never reaches the
189
+ // active total (see the operations-service source aggregation).
190
+ const source = byKey(sources).get(`manual:order|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
191
+ expect(source.order_count).toBe(2);
192
+ expect(source.row_count).toBe(2);
193
+ expect(source.discount_amount).toBe(3000);
194
+ const perKey = new Map(source.key_metrics.map((metric) => [metric.key, metric]));
195
+ expect(perKey.get('COMPLETED:POS_WALK-IN')?.discount_amount).toBe(1000);
196
+ expect(perKey.get('CANCELLED:POS_WALK-IN')?.discount_amount).toBe(2000);
197
+ });
198
+ });
199
+ (0, globals_1.describe)('discount scenarios: refunds', () => {
200
+ (0, globals_1.it)('documents the CURRENT contract: a refunded discount is still counted', () => {
201
+ // The materialized discount counters are built from the order's discount
202
+ // rows; a payment refund does NOT net them out. This test pins that
203
+ // behaviour so a future change is deliberate (see the report gap).
204
+ const { sources } = sourcesOf([
205
+ rawOrder({
206
+ _id: 'order-refunded',
207
+ order_number: 1,
208
+ total_discount_applied: 1000,
209
+ order_discount_details: {
210
+ special_discount: 1000,
211
+ special_discount_id: 'MANUAL_Rs 10',
212
+ special_discount_name: 'Apply Discount',
213
+ is_special_discount_percentage: false,
214
+ total_discount: 1000,
215
+ },
216
+ payments: [
217
+ {
218
+ _id: 'pay-1',
219
+ payment_type: 'CARD_TERMINAL',
220
+ payment_status: 'SETTLED',
221
+ amount_due: 10000,
222
+ amount_paid: 10000,
223
+ amount_refunded: 5000, // a PARTIAL refund
224
+ card_details: null,
225
+ payment_id: 'pay-1',
226
+ },
227
+ ],
228
+ }),
229
+ ]);
230
+ const source = byKey(sources).get(`manual:order|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
231
+ // CURRENT contract: the discount is reported in full (no refund netting).
232
+ expect(source.discount_amount).toBe(1000);
233
+ expect(source.order_count).toBe(1);
234
+ });
235
+ });
236
+ (0, globals_1.describe)('discount scenarios: free modifiers', () => {
237
+ (0, globals_1.it)('does NOT create a discount entry or source row for a free modifier', () => {
238
+ const compiled = compile([
239
+ rawOrder({
240
+ _id: 'order-free-modifier',
241
+ order_number: 1,
242
+ categories: [
243
+ {
244
+ category_id: 'cat-1',
245
+ name: 'FOOD',
246
+ items: [
247
+ rawItem({
248
+ modifiers: [
249
+ {
250
+ _id: 'mod-1',
251
+ name: 'Extra Cheese',
252
+ price: 0,
253
+ is_free: true,
254
+ quantity: 1,
255
+ },
256
+ ],
257
+ }),
258
+ ],
259
+ },
260
+ ],
261
+ }),
262
+ ]);
263
+ // No discount slot anywhere -> the order contributes nothing, and no entry
264
+ // document is written for the scope at all.
265
+ expect(compiled.contributingOrders).toBe(0);
266
+ const monthEntry = compiled.entries.find((candidate) => candidate.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
267
+ expect(monthEntry).toBeUndefined();
268
+ // And therefore there is no source row either.
269
+ const sections = Object.values((compiled.entries.find((candidate) => candidate.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH)
270
+ ?.discount_entries ?? {}));
271
+ expect(sections.every((section) => (section.rows ?? []).length === 0)).toBe(true);
272
+ });
273
+ });
274
+ (0, globals_1.describe)('discount scenarios: item-level manual', () => {
275
+ (0, globals_1.it)('books a PLAIN FIXED item manual discount as manual:item', () => {
276
+ const { sources } = sourcesOf([
277
+ rawOrder({
278
+ _id: 'order-item-manual',
279
+ order_number: 1,
280
+ categories: [
281
+ {
282
+ category_id: 'cat-1',
283
+ name: 'FOOD',
284
+ items: [
285
+ rawItem({
286
+ item_discount_details: {
287
+ special_discount: 1500, // a FIXED amount in cents
288
+ special_discount_id: 'MANUAL_Rs 15',
289
+ special_discount_name: 'Apply Discount',
290
+ is_special_discount_percentage: false,
291
+ total_discount: 1500,
292
+ },
293
+ }),
294
+ ],
295
+ },
296
+ ],
297
+ }),
298
+ ]);
299
+ const source = byKey(sources).get(`manual:item|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM}`);
300
+ expect(source).toBeDefined();
301
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.MANUAL);
302
+ expect(source.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.MANUAL);
303
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM);
304
+ expect(source.kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT);
305
+ expect(source.rate_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.FIXED);
306
+ expect(source.fixed_row_count).toBe(1);
307
+ expect(source.percentage_row_count).toBe(0);
308
+ expect(source.discount_amount).toBe(1500);
309
+ expect(source.order_count).toBe(1);
310
+ expect(source.row_count).toBe(1);
311
+ // A fixed row stores no rate sum.
312
+ expect(source.percentage_rate_sum).toBe(0);
313
+ });
314
+ });
315
+ (0, globals_1.describe)('discount scenarios: collections', () => {
316
+ (0, globals_1.it)('books an ITEM collection FIXED amount as discount:<id>', () => {
317
+ const collectionId = '6ad9fa14ae19ab3c25c18a8a';
318
+ const { sources } = sourcesOf([
319
+ rawOrder({
320
+ _id: 'order-item-collection-fixed',
321
+ order_number: 1,
322
+ categories: [
323
+ {
324
+ category_id: 'cat-1',
325
+ name: 'FOOD',
326
+ items: [
327
+ rawItem({
328
+ item_discount_details: {
329
+ special_discount: 2000,
330
+ special_discount_id: collectionId,
331
+ special_discount_name: 'Bank Discount',
332
+ is_special_discount_percentage: false,
333
+ total_discount: 2000,
334
+ },
335
+ }),
336
+ ],
337
+ },
338
+ ],
339
+ }),
340
+ ]);
341
+ const source = byKey(sources).get(`discount:${collectionId}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM}`);
342
+ expect(source).toBeDefined();
343
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COLLECTION);
344
+ expect(source.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION);
345
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM);
346
+ expect(source.rate_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.FIXED);
347
+ expect(source.discount_amount).toBe(2000);
348
+ expect(source.order_count).toBe(1);
349
+ });
350
+ (0, globals_1.it)('books an ORDER collection PERCENTAGE as discount:<id> with the rate in basis points', () => {
351
+ const collectionId = '6ad9f91eae19ab3c25c18a89';
352
+ const { sources } = sourcesOf([
353
+ rawOrder({
354
+ _id: 'order-order-collection-pct',
355
+ order_number: 1,
356
+ grand_total: 10000,
357
+ amount_earned: 10000,
358
+ amount_collected: 9000,
359
+ total_discount_applied: 1000,
360
+ order_discount_details: {
361
+ special_discount: 1000, // 10% in BASIS POINTS
362
+ special_discount_id: collectionId,
363
+ special_discount_name: 'Bank Discount',
364
+ is_special_discount_percentage: true,
365
+ total_discount: 1000,
366
+ },
367
+ }),
368
+ ]);
369
+ const source = byKey(sources).get(`discount:${collectionId}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
370
+ expect(source).toBeDefined();
371
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COLLECTION);
372
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER);
373
+ expect(source.rate_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE);
374
+ expect(source.percentage_rate_sum).toBe(1000);
375
+ expect(source.percentage_row_count).toBe(1);
376
+ expect(source.discount_amount).toBe(1000);
377
+ expect(source.order_count).toBe(1);
378
+ });
379
+ });
380
+ (0, globals_1.describe)('discount scenarios: several order-level slots', () => {
381
+ (0, globals_1.it)('keeps special + loyalty on one order as INDEPENDENT source rows', () => {
382
+ const loyaltyId = '6ad9fa14ae19ab3c25c18a8a';
383
+ const { compiled, sources } = sourcesOf([
384
+ rawOrder({
385
+ _id: 'order-two-slots',
386
+ order_number: 1,
387
+ grand_total: 10000,
388
+ amount_earned: 10000,
389
+ amount_collected: 7000,
390
+ total_discount_applied: 3000,
391
+ order_discount_details: {
392
+ special_discount: 1000,
393
+ special_discount_id: 'MANUAL_Rs 10',
394
+ special_discount_name: 'Apply Discount',
395
+ is_special_discount_percentage: false,
396
+ loyalty_discount: 2000,
397
+ loyalty_discount_id: loyaltyId,
398
+ loyalty_discount_name: 'Loyalty Offer',
399
+ is_loyalty_discount_percentage: false,
400
+ total_discount: 3000,
401
+ },
402
+ }),
403
+ ]);
404
+ expect(compiled.contributingOrders).toBe(1);
405
+ const map = byKey(sources);
406
+ const manual = map.get(`manual:order|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
407
+ expect(manual).toBeDefined();
408
+ expect(manual.discount_amount).toBe(1000);
409
+ expect(manual.order_count).toBe(1);
410
+ const loyalty = map.get(`loyalty:offer:${loyaltyId}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
411
+ expect(loyalty).toBeDefined();
412
+ expect(loyalty.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY);
413
+ expect(loyalty.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER);
414
+ expect(loyalty.discount_amount).toBe(2000);
415
+ expect(loyalty.order_count).toBe(1);
416
+ // Two independent source rows for ONE order.
417
+ expect(sources).toHaveLength(2);
418
+ });
419
+ });
420
+ (0, globals_1.describe)('discount scenarios: discount and complimentary together', () => {
421
+ (0, globals_1.it)('books the discount and the complimentary tender as separate rows', () => {
422
+ const { sources } = sourcesOf([
423
+ rawOrder({
424
+ _id: 'order-discount-and-comp',
425
+ order_number: 1,
426
+ grand_total: 10000,
427
+ amount_earned: 10000,
428
+ amount_collected: 0,
429
+ total_discount_applied: 1000,
430
+ order_discount_details: {
431
+ special_discount: 1000,
432
+ special_discount_id: 'MANUAL_Rs 10',
433
+ special_discount_name: 'Apply Discount',
434
+ is_special_discount_percentage: false,
435
+ total_discount: 1000,
436
+ },
437
+ payments: [
438
+ {
439
+ _id: 'pay-comp',
440
+ payment_type: 'COMPLIMENTARY',
441
+ payment_status: 'SETTLED',
442
+ amount_due: 10000,
443
+ amount_paid: 10000,
444
+ amount_refunded: 0,
445
+ card_details: null,
446
+ payment_id: 'pay-comp',
447
+ },
448
+ ],
449
+ }),
450
+ ]);
451
+ const map = byKey(sources);
452
+ const discount = map.get(`manual:order|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
453
+ const comp = map.get(`${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_FULL}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT}`);
454
+ expect(discount).toBeDefined();
455
+ expect(discount.discount_amount).toBe(1000);
456
+ expect(discount.kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT);
457
+ expect(comp).toBeDefined();
458
+ expect(comp.kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY);
459
+ expect(comp.complimentary_full).toBe(true);
460
+ expect(comp.covered_amount).toBe(10000);
461
+ // A complimentary tender is NOT a discount: it carries no discount money.
462
+ expect(comp.discount_amount).toBe(0);
463
+ });
464
+ });
465
+ (0, globals_1.describe)('discount scenarios: complimentary with no discount', () => {
466
+ (0, globals_1.it)('books a FULL comp with no discount at all', () => {
467
+ const { sources } = sourcesOf([
468
+ rawOrder({
469
+ _id: 'order-full-comp',
470
+ order_number: 1,
471
+ grand_total: 10000,
472
+ amount_earned: 10000,
473
+ amount_collected: 0,
474
+ total_discount_applied: 0,
475
+ payments: [
476
+ {
477
+ _id: 'pay-comp',
478
+ payment_type: 'COMPLIMENTARY',
479
+ payment_status: 'SETTLED',
480
+ amount_due: 10000,
481
+ amount_paid: 10000,
482
+ amount_refunded: 0,
483
+ card_details: null,
484
+ payment_id: 'pay-comp',
485
+ },
486
+ ],
487
+ }),
488
+ ]);
489
+ const comp = byKey(sources).get(`${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_FULL}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT}`);
490
+ expect(comp).toBeDefined();
491
+ expect(comp.complimentary_full).toBe(true);
492
+ expect(comp.covered_amount).toBe(10000);
493
+ expect(comp.discount_amount).toBe(0);
494
+ expect(comp.order_count).toBe(1);
495
+ });
496
+ (0, globals_1.it)('books a PARTIAL comp (a comp tender beside another tender) with no discount', () => {
497
+ const { sources } = sourcesOf([
498
+ rawOrder({
499
+ _id: 'order-partial-comp',
500
+ order_number: 1,
501
+ grand_total: 10000,
502
+ amount_earned: 10000,
503
+ amount_collected: 5000,
504
+ total_discount_applied: 0,
505
+ payments: [
506
+ {
507
+ _id: 'pay-comp',
508
+ payment_type: 'COMPLIMENTARY',
509
+ payment_status: 'SETTLED',
510
+ amount_due: 5000,
511
+ amount_paid: 5000,
512
+ amount_refunded: 0,
513
+ card_details: null,
514
+ payment_id: 'pay-comp',
515
+ },
516
+ {
517
+ _id: 'pay-cash',
518
+ payment_type: 'CASH',
519
+ payment_status: 'SETTLED',
520
+ amount_due: 5000,
521
+ amount_paid: 5000,
522
+ amount_refunded: 0,
523
+ card_details: null,
524
+ payment_id: 'pay-cash',
525
+ },
526
+ ],
527
+ }),
528
+ ]);
529
+ const comp = byKey(sources).get(`${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.COMPLIMENTARY_PARTIAL}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.PAYMENT}`);
530
+ expect(comp).toBeDefined();
531
+ expect(comp.complimentary_full).toBe(false);
532
+ expect(comp.covered_amount).toBe(5000);
533
+ expect(comp.discount_amount).toBe(0);
534
+ expect(comp.order_count).toBe(1);
535
+ });
536
+ });
537
+ (0, globals_1.describe)('discount scenarios: loyalty and voucher at order level', () => {
538
+ (0, globals_1.it)('books an ORDER-level loyalty OFFER as loyalty:offer:<id>', () => {
539
+ const offerId = '6ad9fa14ae19ab3c25c18a8a';
540
+ const { sources } = sourcesOf([
541
+ rawOrder({
542
+ _id: 'order-loyalty-offer',
543
+ order_number: 1,
544
+ grand_total: 10000,
545
+ amount_earned: 10000,
546
+ amount_collected: 7000,
547
+ total_discount_applied: 3000,
548
+ order_discount_details: {
549
+ loyalty_discount: 3000,
550
+ loyalty_discount_id: offerId,
551
+ loyalty_discount_name: 'Loyalty Offer',
552
+ is_loyalty_discount_percentage: false,
553
+ total_discount: 3000,
554
+ },
555
+ }),
556
+ ]);
557
+ const source = byKey(sources).get(`loyalty:offer:${offerId}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
558
+ expect(source).toBeDefined();
559
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY);
560
+ expect(source.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER);
561
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER);
562
+ expect(source.discount_amount).toBe(3000);
563
+ expect(source.order_count).toBe(1);
564
+ });
565
+ (0, globals_1.it)('books an ITEM-level voucher as voucher:<id>', () => {
566
+ const voucherId = '6abf432e5e03cc49e2462a62';
567
+ const { sources } = sourcesOf([
568
+ rawOrder({
569
+ _id: 'order-item-voucher',
570
+ order_number: 1,
571
+ categories: [
572
+ {
573
+ category_id: 'cat-1',
574
+ name: 'FOOD',
575
+ items: [
576
+ rawItem({
577
+ item_discount_details: {
578
+ voucher_discount: 800,
579
+ voucher_discount_id: voucherId,
580
+ voucher_discount_name: 'Item Voucher',
581
+ is_voucher_discount_percentage: false,
582
+ total_discount: 800,
583
+ },
584
+ }),
585
+ ],
586
+ },
587
+ ],
588
+ }),
589
+ ]);
590
+ const source = byKey(sources).get(`voucher:${voucherId}|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM}`);
591
+ expect(source).toBeDefined();
592
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.VOUCHER);
593
+ expect(source.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM);
594
+ expect(source.discount_amount).toBe(800);
595
+ expect(source.order_count).toBe(1);
596
+ });
597
+ (0, globals_1.it)('books a GIFT CARD as loyalty:gift_card:<id>', () => {
598
+ const { sources } = sourcesOf([
599
+ rawOrder({
600
+ _id: 'order-gift-card',
601
+ order_number: 1,
602
+ grand_total: 10000,
603
+ amount_earned: 10000,
604
+ amount_collected: 8800,
605
+ total_discount_applied: 1200,
606
+ order_discount_details: {
607
+ loyalty_discount: 1200,
608
+ loyalty_discount_id: 'gift-card-1',
609
+ loyalty_discount_name: 'Gift Card',
610
+ loyalty_reference_number: 'GIFT-CODE-1',
611
+ is_loyalty_discount_percentage: false,
612
+ total_discount: 1200,
613
+ },
614
+ }),
615
+ ]);
616
+ const source = byKey(sources).get(`loyalty:gift_card:gift-card-1|${MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER}`);
617
+ expect(source).toBeDefined();
618
+ expect(source.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY);
619
+ expect(source.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD);
620
+ expect(source.discount_amount).toBe(1200);
621
+ expect(source.order_count).toBe(1);
622
+ });
623
+ });
624
+ (0, globals_1.describe)('discount scenarios: the compiled documents stay integer-only', () => {
625
+ (0, globals_1.it)('passes the recursive integer contract for every scenario above', () => {
626
+ const orders = [
627
+ rawOrder({
628
+ _id: 'order-voucher-pct',
629
+ order_number: 1,
630
+ total_discount_applied: 2000,
631
+ order_discount_details: {
632
+ voucher_discount: 2000,
633
+ voucher_discount_id: 'voucher-offer-1',
634
+ is_voucher_discount_percentage: true,
635
+ total_discount: 2000,
636
+ },
637
+ }),
638
+ rawOrder({
639
+ _id: 'order-cancelled',
640
+ order_number: 2,
641
+ order_status: 'CANCELLED',
642
+ total_discount_applied: 2000,
643
+ order_discount_details: {
644
+ special_discount: 2000,
645
+ special_discount_id: 'MANUAL_Rs 20',
646
+ is_special_discount_percentage: false,
647
+ total_discount: 2000,
648
+ },
649
+ }),
650
+ rawOrder({
651
+ _id: 'order-item-manual',
652
+ order_number: 3,
653
+ categories: [
654
+ {
655
+ category_id: 'cat-1',
656
+ name: 'FOOD',
657
+ items: [
658
+ rawItem({
659
+ item_discount_details: {
660
+ special_discount: 1500,
661
+ special_discount_id: 'MANUAL_Rs 15',
662
+ is_special_discount_percentage: false,
663
+ total_discount: 1500,
664
+ },
665
+ }),
666
+ ],
667
+ },
668
+ ],
669
+ }),
670
+ ];
671
+ const { compiled, sources } = sourcesOf(orders);
672
+ (0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(compiled.reports, { collection: 'reports' });
673
+ (0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(compiled.entries, { collection: 'entries' });
674
+ (0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(sources, { collection: 'sources' });
675
+ });
676
+ });
@@ -485,3 +485,137 @@ describe('complimentary full/partial accumulator', () => {
485
485
  expect(comp.complimentary_full).toBe(true);
486
486
  });
487
487
  });
488
+ describe('missing discount mechanisms (synthetic coverage)', () => {
489
+ it('classifies an order-level loyalty offer', () => {
490
+ const identity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
491
+ slot: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY,
492
+ sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER,
493
+ level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER,
494
+ discount_id: 'offer-order-1',
495
+ name: 'Order Loyalty Offer',
496
+ amount: 500,
497
+ }));
498
+ expect(identity.source_key).toBe('loyalty:offer:offer-order-1');
499
+ expect(identity.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY);
500
+ expect(identity.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER);
501
+ });
502
+ it('classifies an item-level voucher and keeps its reference', () => {
503
+ const identity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
504
+ slot: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.VOUCHER,
505
+ sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER,
506
+ level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
507
+ discount_id: 'voucher-item-1',
508
+ reference_number: 'ITEM-CODE',
509
+ name: 'Item Voucher',
510
+ amount: 500,
511
+ }));
512
+ expect(identity.source_key).toBe('voucher:voucher-item-1');
513
+ expect(identity.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.VOUCHER);
514
+ expect(identity.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM);
515
+ });
516
+ it('classifies a gift card from loyalty evidence/reference', () => {
517
+ const identity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
518
+ slot: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY,
519
+ sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER,
520
+ level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER,
521
+ discount_id: 'gift-card-1',
522
+ reference_number: 'GIFT-CODE-1',
523
+ name: 'Gift Card',
524
+ amount: 500,
525
+ }));
526
+ expect(identity.source_key).toBe('loyalty:gift_card:gift-card-1');
527
+ expect(identity.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY);
528
+ expect(identity.sub_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.GIFT_CARD);
529
+ });
530
+ it('classifies order and item collection discounts at either level', () => {
531
+ const id = '69f1c56ef9c86581b0ae9e36';
532
+ const order = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
533
+ sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION,
534
+ level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER,
535
+ discount_id: id,
536
+ rate: 1500,
537
+ name: 'Order Bank Discount',
538
+ }));
539
+ const item = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
540
+ sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.COLLECTION,
541
+ level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
542
+ item_id: 'item-1',
543
+ discount_id: id,
544
+ rate: 500,
545
+ is_percentage: false,
546
+ name: 'Item Bank Discount',
547
+ }));
548
+ expect(order.source_key).toBe(`discount:${id}`);
549
+ expect(item.source_key).toBe(`discount:${id}`);
550
+ expect(order.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER);
551
+ expect(item.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM);
552
+ });
553
+ });
554
+ describe('synthetic multi-source and comp combinations', () => {
555
+ const baseOrder = (over = {}) => ({
556
+ _id: 'order-combined',
557
+ tenant_id: 'tenant-a',
558
+ platform: 'POS_WALK-IN',
559
+ order_status: 'COMPLETED',
560
+ order_number: 99,
561
+ categories: [],
562
+ payments: [],
563
+ order_discount_details: null,
564
+ total_discount_applied: 0,
565
+ grand_total: 1000,
566
+ amount_earned: 1000,
567
+ amount_collected: 0,
568
+ createdAt: new Date('2026-09-15T13:00:00.000Z'),
569
+ updatedAt: new Date('2026-09-15T13:00:00.000Z'),
570
+ staff_details: null,
571
+ order_taker_details: null,
572
+ wallet_redemptions: null,
573
+ ...over,
574
+ });
575
+ it('keeps an order-level discount and a complimentary tender together', () => {
576
+ const result = (0, materialized_discount_accumulator_helpers_1.accumulateOrderDiscounts)(baseOrder({
577
+ order_discount_details: {
578
+ special_discount: 500,
579
+ special_discount_id: 'MANUAL_Rs 5',
580
+ special_discount_name: 'Apply Discount',
581
+ is_special_discount_percentage: false,
582
+ total_discount: 500,
583
+ },
584
+ payments: [
585
+ {
586
+ _id: 'p1',
587
+ payment_type: 'COMPLIMENTARY',
588
+ payment_status: 'SETTLED',
589
+ amount_due: 500,
590
+ amount_paid: 500,
591
+ amount_refunded: 0,
592
+ card_details: null,
593
+ payment_id: 'pay-comp',
594
+ },
595
+ ],
596
+ }), CONTEXT);
597
+ expect(result.entries.some((row) => row.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT)).toBe(true);
598
+ expect(result.entries.some((row) => row.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY)).toBe(true);
599
+ expect(result.counters.complimentary.payment_count).toBe(1);
600
+ });
601
+ it('keeps multiple order-level slots as independent source rows', () => {
602
+ const result = (0, materialized_discount_accumulator_helpers_1.accumulateOrderDiscounts)(baseOrder({
603
+ total_discount_applied: 800,
604
+ order_discount_details: {
605
+ special_discount: 500,
606
+ special_discount_id: 'MANUAL_Rs 5',
607
+ special_discount_name: 'Apply Discount',
608
+ is_special_discount_percentage: false,
609
+ loyalty_discount: 300,
610
+ loyalty_discount_id: 'loyalty-offer-1',
611
+ loyalty_discount_name: 'Loyalty Offer',
612
+ is_loyalty_discount_percentage: false,
613
+ total_discount: 800,
614
+ },
615
+ }), CONTEXT);
616
+ const orderRows = result.entries.filter((row) => row.level === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER &&
617
+ row.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT);
618
+ expect(orderRows).toHaveLength(2);
619
+ expect(new Set(orderRows.map((row) => row.discount_id))).toEqual(new Set(['MANUAL_Rs 5', 'loyalty-offer-1']));
620
+ });
621
+ });
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const strict_1 = __importDefault(require("node:assert/strict"));
7
+ const node_fs_1 = __importDefault(require("node:fs"));
8
+ const node_path_1 = __importDefault(require("node:path"));
9
+ const globals_1 = require("@jest/globals");
10
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
11
+ const materialized_discount_compile_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-compile-helpers");
12
+ const materialized_discount_source_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-source-helpers");
13
+ const materialized_integer_contract_1 = require("../../helpers/materialized-reports/materialized-integer-contract");
14
+ const ORDERS = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(__dirname, '..', 'real-discount-orders-2026-10-23.json'), 'utf8'));
15
+ const TENANT = 'thailemon.uae';
16
+ const BRAND = 'thailemon.co.nz';
17
+ const DAY = '2026-10-23';
18
+ const MONTH = '2026-10';
19
+ const compile = () => (0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
20
+ tenantId: TENANT,
21
+ brandId: BRAND,
22
+ localDate: DAY,
23
+ orders: ORDERS,
24
+ gmtOffset: 5,
25
+ computedAt: new Date('2026-10-23T23:59:00.000Z'),
26
+ });
27
+ const sourcesForMonth = () => {
28
+ const compiled = compile();
29
+ const entry = compiled.entries.find((e) => e.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
30
+ return {
31
+ compiled,
32
+ sources: (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)({
33
+ tenantId: TENANT,
34
+ brandId: BRAND,
35
+ scopeContext: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
36
+ scopeField: MONTH,
37
+ dayIndex: null,
38
+ sections: entry.discount_entries,
39
+ computedAt: new Date('2026-10-23T23:59:00.000Z'),
40
+ sourceWatermark: null,
41
+ }),
42
+ };
43
+ };
44
+ (0, globals_1.describe)('real 2026-10-23 discount orders', () => {
45
+ (0, globals_1.it)('contains all 19 reference orders, including the no-discount control', () => {
46
+ strict_1.default.equal(ORDERS.length, 19);
47
+ strict_1.default.deepEqual(ORDERS.map((order) => order.order_number).sort((a, b) => a - b), Array.from({ length: 19 }, (_, index) => 55 + index));
48
+ });
49
+ (0, globals_1.it)('compiles 18 contributing orders and reconciles source money to report money', () => {
50
+ const { compiled, sources } = sourcesForMonth();
51
+ strict_1.default.equal(compiled.contributingOrders, 18);
52
+ const monthReport = compiled.reports.find((report) => report.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
53
+ const reportTotal = Object.values(monthReport.discount_counters ?? {}).reduce((sum, counters) => sum + (counters.amount ?? 0), 0);
54
+ const sourceTotal = sources.reduce((sum, source) => sum + source.discount_amount, 0);
55
+ strict_1.default.equal(reportTotal, 311490);
56
+ strict_1.default.equal(sourceTotal, 311490);
57
+ strict_1.default.equal(sources.filter((source) => source.source_class === 'COMPLIMENTARY').length, 2);
58
+ strict_1.default.equal(sources.filter((source) => source.kind === MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.FREE_ITEM).length, 2);
59
+ strict_1.default.equal(sources.reduce((sum, source) => sum + source.free_item_count, 0), 4);
60
+ });
61
+ (0, globals_1.it)('keeps the expected source rows and distinct order counts', () => {
62
+ const { sources } = sourcesForMonth();
63
+ const byKey = new Map(sources.map((source) => [`${source.source_key}|${source.level}`, source]));
64
+ const expected = [
65
+ ['manual:order', MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER, 3, 90000],
66
+ ['manual:item', MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM, 4, 43490],
67
+ ['price_override:item', MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM, 1, 25000],
68
+ ['catalog:auto', MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM, 3, 70000],
69
+ ['loyalty:wallet_cashback', MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER, 1, 50000],
70
+ ['discount:68cbaf10f365eb45bdbee96d', MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM, 1, 15000],
71
+ ['discount:6adb1adfa5fa4b8c498e65ec', MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER, 1, 10000],
72
+ ['voucher:6abf432e5e03cc49e2462a62', MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER, 1, 5000],
73
+ ['loyalty:offer:6ad9fa14ae19ab3c25c18a8a', MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM, 1, 2000],
74
+ ['loyalty:offer:6ad9f91eae19ab3c25c18a89', MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM, 1, 1000],
75
+ ];
76
+ for (const [key, level, orderCount, amount] of expected) {
77
+ const source = byKey.get(`${key}|${level}`);
78
+ strict_1.default.ok(source, `missing ${key}`);
79
+ strict_1.default.equal(source.order_count, orderCount, String(key));
80
+ strict_1.default.equal(source.discount_amount, amount, String(key));
81
+ }
82
+ strict_1.default.equal(byKey.get('complimentary:full|PAYMENT')?.covered_amount, 50000);
83
+ strict_1.default.equal(byKey.get('complimentary:partial|PAYMENT')?.covered_amount, 25000);
84
+ strict_1.default.equal(byKey.get('complimentary:full|PAYMENT')?.complimentary_full, true);
85
+ strict_1.default.equal(byKey.get('complimentary:partial|PAYMENT')?.complimentary_full, false);
86
+ });
87
+ (0, globals_1.it)('derives rates from integer sum/count only and never stores an average', () => {
88
+ const { sources } = sourcesForMonth();
89
+ for (const source of sources) {
90
+ strict_1.default.equal('rate' in source, false, `${source.source_key} stored rate`);
91
+ strict_1.default.ok(Number.isInteger(source.percentage_rate_sum));
92
+ strict_1.default.ok(Number.isInteger(source.percentage_row_count));
93
+ strict_1.default.ok(Number.isInteger(source.discount_amount));
94
+ }
95
+ const manualItem = sources.find((source) => source.source_key === 'manual:item');
96
+ strict_1.default.equal(manualItem.percentage_rate_sum, 18000);
97
+ strict_1.default.equal(manualItem.percentage_row_count, 4);
98
+ strict_1.default.equal(manualItem.rate_kind, 'PERCENTAGE');
99
+ const manualOrder = sources.find((source) => source.source_key === 'manual:order');
100
+ strict_1.default.equal(manualOrder.rate_kind, 'MIXED');
101
+ strict_1.default.equal(manualOrder.percentage_rate_sum, 10000);
102
+ strict_1.default.equal(manualOrder.percentage_row_count, 2);
103
+ });
104
+ (0, globals_1.it)('passes the recursive integer-only contract for reports, entries and sources', () => {
105
+ const { compiled, sources } = sourcesForMonth();
106
+ (0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(compiled.reports, { collection: 'reports' });
107
+ (0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(compiled.entries, { collection: 'entries' });
108
+ (0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(sources, { collection: 'sources' });
109
+ });
110
+ });
@@ -85,7 +85,11 @@ describe('getFormattedTenantDiscountSources', () => {
85
85
  it('renders rate labels: percentage, mixed and fixed', () => {
86
86
  const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
87
87
  report([
88
- row({ percentage_rate_sum: 15000, percentage_row_count: 10, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE }),
88
+ row({
89
+ percentage_rate_sum: 15000,
90
+ percentage_row_count: 10,
91
+ rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE,
92
+ }),
89
93
  row({
90
94
  source_key: 'manual:item',
91
95
  percentage_rate_sum: null,
@@ -157,7 +161,12 @@ describe('getFormattedTenantDiscountSources', () => {
157
161
  it('keeps "mixed" and "—" when normalising (the rate kind wins)', () => {
158
162
  const formatted = (0, tenant_discount_source_formatter_1.getFormattedTenantDiscountSources)([
159
163
  report([
160
- row({ source_key: 'discount:a', percentage_rate_sum: null, percentage_row_count: null, rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED }),
164
+ row({
165
+ source_key: 'discount:a',
166
+ percentage_rate_sum: null,
167
+ percentage_row_count: null,
168
+ rate_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED,
169
+ }),
161
170
  row({
162
171
  source_key: 'discount:b',
163
172
  percentage_rate_sum: null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.27",
3
+ "version": "0.4.28",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",