@managesome/eposmatic-toolkit 0.4.26 → 0.4.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/helpers/materialized-reports/index.js +1 -0
- package/esm/helpers/materialized-reports/materialized-credit-bills-helpers.js +9 -4
- package/esm/helpers/materialized-reports/materialized-delivery-classification-helpers.js +25 -9
- package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +8 -5
- package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +13 -1
- package/esm/helpers/materialized-reports/materialized-discount-source-helpers.js +0 -3
- package/esm/helpers/materialized-reports/materialized-integer-contract.js +142 -0
- package/esm/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +7 -1
- package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +9 -2
- package/esm/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +17 -7
- package/esm/helpers/materialized-reports/tenant-discount-source-formatter.js +29 -29
- package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +4 -2
- package/esm/helpers/utils/duration-conversion.js +31 -0
- package/esm/helpers/utils/index.js +2 -0
- package/esm/helpers/utils/percentage-conversion.js +69 -0
- package/esm/schemas/models/materialized_tenant_discount_sources.js +5 -1
- package/lib/helpers/materialized-reports/index.d.ts +1 -0
- package/lib/helpers/materialized-reports/index.js +1 -0
- package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.js +9 -4
- package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +25 -9
- package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +8 -5
- package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.d.ts +18 -0
- package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +14 -2
- package/lib/helpers/materialized-reports/materialized-discount-source-helpers.js +0 -3
- package/lib/helpers/materialized-reports/materialized-integer-contract.d.ts +68 -0
- package/lib/helpers/materialized-reports/materialized-integer-contract.js +147 -0
- package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +7 -1
- package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +9 -2
- package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +17 -7
- package/lib/helpers/materialized-reports/tenant-discount-source-formatter.d.ts +12 -30
- package/lib/helpers/materialized-reports/tenant-discount-source-formatter.js +29 -30
- package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +4 -2
- package/lib/helpers/types/materialized-reports-apis.d.ts +11 -3
- package/lib/helpers/utils/duration-conversion.d.ts +19 -0
- package/lib/helpers/utils/duration-conversion.js +37 -0
- package/lib/helpers/utils/index.d.ts +2 -0
- package/lib/helpers/utils/index.js +2 -0
- package/lib/helpers/utils/percentage-conversion.d.ts +49 -0
- package/lib/helpers/utils/percentage-conversion.js +76 -0
- package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +0 -3
- package/lib/schemas/models/materialized_tenant_discount_sources.js +5 -1
- package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +11 -4
- package/lib/schemas/types/materialized_tenant_discount_sources.d.ts +23 -4
- package/lib/tests/materialized-reports/fixtures.d.ts +15 -1
- package/lib/tests/materialized-reports/fixtures.js +118 -8
- package/lib/tests/materialized-reports/materialized-discount-classification.test.js +34 -24
- package/lib/tests/materialized-reports/materialized-discount-sources.test.js +14 -10
- package/lib/tests/materialized-reports/materialized-integer-contract.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-integer-contract.test.js +223 -0
- package/lib/tests/materialized-reports/materialized-write-paths-inventory.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-write-paths-inventory.test.js +58 -0
- package/lib/tests/materialized-reports/materialized-writers-integer-only.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-writers-integer-only.test.js +150 -0
- package/lib/tests/materialized-reports/sales-overview-baseline.test.js +51 -51
- package/lib/tests/materialized-reports/tenant-discount-source-formatter.test.js +80 -41
- package/package.json +1 -1
|
@@ -22,23 +22,30 @@ const CONTEXT = {
|
|
|
22
22
|
local_hour: 18,
|
|
23
23
|
local_hour_label: '2026-09-15 18',
|
|
24
24
|
};
|
|
25
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Independently measured (analyze-discount-parity) — basha: 2931 orders.
|
|
27
|
+
*
|
|
28
|
+
* RE-MEASURED on the integer storage contract (2026-10-22): the fixtures are
|
|
29
|
+
* upgraded to the contract on load (money → integer cents, a percentage rate →
|
|
30
|
+
* integer basis points), so every money anchor is an integer and every RAW
|
|
31
|
+
* slot anchor is in basis points for a percentage row.
|
|
32
|
+
*/
|
|
26
33
|
const BASHA = {
|
|
27
34
|
totalCount: 28,
|
|
28
|
-
headline:
|
|
29
|
-
orderLevel:
|
|
35
|
+
headline: 68813,
|
|
36
|
+
orderLevel: 49487,
|
|
30
37
|
itemLevel: 19326,
|
|
31
38
|
itemDiscountRows: 22,
|
|
32
39
|
unattributed: 0,
|
|
33
|
-
orderSpecialRaw:
|
|
34
|
-
orderSpecialResolved:
|
|
40
|
+
orderSpecialRaw: 21269,
|
|
41
|
+
orderSpecialResolved: 49487,
|
|
35
42
|
orderSpecialCount: 11,
|
|
36
43
|
orderLoyaltyRaw: 0,
|
|
37
44
|
orderLoyaltyResolved: 0,
|
|
38
45
|
orderLoyaltyCount: 0,
|
|
39
46
|
orderVoucherRaw: 0,
|
|
40
47
|
orderVoucherResolved: 0,
|
|
41
|
-
itemSpecialRaw:
|
|
48
|
+
itemSpecialRaw: 130140,
|
|
42
49
|
itemSpecialResolved: 19326,
|
|
43
50
|
itemSpecialCount: 22,
|
|
44
51
|
itemSpecialCollectionRaw: 0,
|
|
@@ -51,24 +58,24 @@ const BASHA = {
|
|
|
51
58
|
complimentaryAmount: 2980,
|
|
52
59
|
complimentaryNotes: 0,
|
|
53
60
|
};
|
|
54
|
-
/** Independently measured — bruzo: 2166 orders. */
|
|
61
|
+
/** Independently measured — bruzo: 2166 orders. RE-MEASURED on the integer contract. */
|
|
55
62
|
const BRUZO = {
|
|
56
63
|
totalCount: 336,
|
|
57
|
-
headline:
|
|
58
|
-
orderLevel:
|
|
59
|
-
itemLevel: 100437
|
|
64
|
+
headline: 108581,
|
|
65
|
+
orderLevel: 8144,
|
|
66
|
+
itemLevel: 100437,
|
|
60
67
|
itemDiscountRows: 304,
|
|
61
68
|
unattributed: 0,
|
|
62
69
|
orderSpecialRaw: 0,
|
|
63
70
|
orderSpecialResolved: 0,
|
|
64
71
|
orderSpecialCount: 0,
|
|
65
|
-
orderLoyaltyRaw:
|
|
66
|
-
orderLoyaltyResolved:
|
|
72
|
+
orderLoyaltyRaw: 15914,
|
|
73
|
+
orderLoyaltyResolved: 7394,
|
|
67
74
|
orderLoyaltyCount: 22,
|
|
68
75
|
orderVoucherRaw: 750,
|
|
69
76
|
orderVoucherResolved: 750,
|
|
70
|
-
itemSpecialRaw:
|
|
71
|
-
itemSpecialResolved: 100437
|
|
77
|
+
itemSpecialRaw: 103793,
|
|
78
|
+
itemSpecialResolved: 100437,
|
|
72
79
|
itemSpecialCount: 304,
|
|
73
80
|
itemSpecialCollectionRaw: 0,
|
|
74
81
|
itemSpecialCollectionResolved: 0,
|
|
@@ -172,9 +179,9 @@ describe('materialized discounts — accumulator vs the independent analysis', (
|
|
|
172
179
|
// Anchor F: order + item == headline (holds exactly in both stores)
|
|
173
180
|
expect(round2(counters.order_level_amount + counters.item_level_amount)).toBe(round2(counters.amount));
|
|
174
181
|
// RAW slot counters — the overview-parity values (anchors B/C/D)
|
|
175
|
-
expect(
|
|
182
|
+
expect(counters.order_special.amount).toBe(expected.orderSpecialRaw);
|
|
176
183
|
expect(round2(counters.order_special.count)).toBe(expected.orderSpecialCount);
|
|
177
|
-
expect(
|
|
184
|
+
expect(counters.order_loyalty.amount).toBe(expected.orderLoyaltyRaw);
|
|
178
185
|
expect(round2(counters.order_loyalty.count)).toBe(expected.orderLoyaltyCount);
|
|
179
186
|
expect(round2(counters.order_voucher.amount)).toBe(expected.orderVoucherRaw);
|
|
180
187
|
// RESOLVED money — anchors H/J: the slots explain the order/item totals
|
|
@@ -186,15 +193,15 @@ describe('materialized discounts — accumulator vs the independent analysis', (
|
|
|
186
193
|
counters.order_voucher.resolved_amount +
|
|
187
194
|
counters.unattributed_amount)).toBe(round2(counters.order_level_amount));
|
|
188
195
|
// Item-level raw + resolved
|
|
189
|
-
expect(
|
|
196
|
+
expect(counters.item_special.amount).toBe(expected.itemSpecialRaw);
|
|
190
197
|
expect(round2(counters.item_special.resolved_amount)).toBe(expected.itemSpecialResolved);
|
|
191
198
|
expect(round2(counters.item_special.count)).toBe(expected.itemSpecialCount);
|
|
192
|
-
expect(
|
|
199
|
+
expect(counters.item_special.collection_amount).toBe(expected.itemSpecialCollectionRaw);
|
|
193
200
|
expect(round2(counters.item_special.collection_resolved_amount)).toBe(expected.itemSpecialCollectionResolved);
|
|
194
201
|
// Catalog-auto rows write the CATALOG ITEM id, which is a 24-hex ObjectId.
|
|
195
202
|
// They used to be booked as COLLECTION; the caller now supplies the item
|
|
196
203
|
// ids so the two are separated (bruzo: 302 rows / 100_293).
|
|
197
|
-
expect(
|
|
204
|
+
expect(counters.item_special.catalog_item_amount).toBe(expected.itemSpecialCatalogRaw);
|
|
198
205
|
expect(round2(counters.item_special.catalog_item_resolved_amount)).toBe(expected.itemSpecialCatalogResolved);
|
|
199
206
|
expect(round2(counters.item_special.resolved_amount)).toBe(round2(counters.item_level_amount));
|
|
200
207
|
// Free items are a SUBSET of the item rows (anchor G), never added to them
|
|
@@ -235,15 +242,17 @@ describe('materialized discounts — sub-kind classifier (synthesized coverage)'
|
|
|
235
242
|
})).toBe(K.GIFT_CARD);
|
|
236
243
|
});
|
|
237
244
|
it('resolves a PERCENTAGE slot to the row money, never to the percent', () => {
|
|
245
|
+
// A percentage RATE is stored in integer BASIS POINTS (1% = 100 bps) and the
|
|
246
|
+
// row money in integer CENTS.
|
|
238
247
|
const details = {
|
|
239
|
-
special_discount:
|
|
248
|
+
special_discount: 2500,
|
|
240
249
|
special_discount_id: 'MANUAL_25%',
|
|
241
250
|
special_discount_name: 'Discount',
|
|
242
251
|
is_special_discount_percentage: true,
|
|
243
|
-
total_discount:
|
|
252
|
+
total_discount: 8718,
|
|
244
253
|
};
|
|
245
|
-
const resolved = (0, materialized_discount_classification_helpers_1.resolveRowMoney)(details,
|
|
246
|
-
expect(resolved.perSlot.get(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL)).toBe(
|
|
254
|
+
const resolved = (0, materialized_discount_classification_helpers_1.resolveRowMoney)(details, 8718);
|
|
255
|
+
expect(resolved.perSlot.get(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL)).toBe(8718);
|
|
247
256
|
expect(resolved.unallocated).toBe(0);
|
|
248
257
|
});
|
|
249
258
|
it('resolves a FIXED slot to its own value', () => {
|
|
@@ -300,7 +309,8 @@ describe('materialized discounts — accumulator edge cases (synthesized)', () =
|
|
|
300
309
|
_net_amount: 0,
|
|
301
310
|
_gross_amount: 220,
|
|
302
311
|
item_discount_details: {
|
|
303
|
-
|
|
312
|
+
// 100% off = 10000 basis points.
|
|
313
|
+
loyalty_discount: 10000,
|
|
304
314
|
loyalty_discount_id: '6aa472763689e5f08dd0599f',
|
|
305
315
|
is_loyalty_discount_percentage: true,
|
|
306
316
|
total_discount: 220,
|
|
@@ -74,7 +74,7 @@ describe('discount source identity', () => {
|
|
|
74
74
|
expect(identity.level).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ORDER);
|
|
75
75
|
});
|
|
76
76
|
it('groups every manual order amount under ONE source', () => {
|
|
77
|
-
const a = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({ discount_id: 'MANUAL_50%', rate:
|
|
77
|
+
const a = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({ discount_id: 'MANUAL_50%', rate: 5000, name: 'Discount' }));
|
|
78
78
|
const b = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({ discount_id: 'MANUAL_Rs 500', rate: 500, name: 'Apply Discount', is_percentage: false }));
|
|
79
79
|
expect(a.source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.MANUAL_ORDER);
|
|
80
80
|
expect(b.source_key).toBe(a.source_key);
|
|
@@ -86,7 +86,7 @@ describe('discount source identity', () => {
|
|
|
86
86
|
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
87
87
|
item_id: 'item-1',
|
|
88
88
|
discount_id: 'MANUAL_25%',
|
|
89
|
-
rate:
|
|
89
|
+
rate: 2500,
|
|
90
90
|
}));
|
|
91
91
|
expect(identity.source_key).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_KEY.MANUAL_ITEM);
|
|
92
92
|
expect(identity.source_name).toBe('Manual item discount');
|
|
@@ -96,7 +96,7 @@ describe('discount source identity', () => {
|
|
|
96
96
|
expect((0, materialized_discount_source_helpers_1.normalizeDiscountSourceKeyPart)('VISA CARD')).toBe('visa_card');
|
|
97
97
|
const identity = (0, materialized_discount_source_helpers_1.classifyDiscountEntrySource)(entry({
|
|
98
98
|
discount_id: 'MANUAL_10%',
|
|
99
|
-
rate:
|
|
99
|
+
rate: 1000,
|
|
100
100
|
name: 'MANUAL_10%',
|
|
101
101
|
reason: 'Discount applied for VISA CARD',
|
|
102
102
|
}));
|
|
@@ -150,7 +150,7 @@ describe('discount source identity', () => {
|
|
|
150
150
|
sub_kind: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM.OFFER,
|
|
151
151
|
discount_id: offer,
|
|
152
152
|
name: 'Z2 Party 10% Offer',
|
|
153
|
-
rate:
|
|
153
|
+
rate: 1000,
|
|
154
154
|
}));
|
|
155
155
|
expect(offerIdentity.source_key).toBe(`loyalty:offer:${offer}`);
|
|
156
156
|
expect(offerIdentity.source_class).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.LOYALTY);
|
|
@@ -248,7 +248,7 @@ describe('discount source identity', () => {
|
|
|
248
248
|
describe('buildDiscountSourcesForScope', () => {
|
|
249
249
|
it('groups two manual order rows into one source with distinct orders', () => {
|
|
250
250
|
const docs = (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)(scopeInput([
|
|
251
|
-
entry({ order_id: 'o1', discount_id: 'MANUAL_50%', rate:
|
|
251
|
+
entry({ order_id: 'o1', discount_id: 'MANUAL_50%', rate: 5000, amount: 500 }),
|
|
252
252
|
entry({ order_id: 'o2', discount_id: 'MANUAL_Rs 200', rate: 200, is_percentage: false, amount: 200 }),
|
|
253
253
|
]));
|
|
254
254
|
expect(docs).toHaveLength(1);
|
|
@@ -257,7 +257,11 @@ describe('buildDiscountSourcesForScope', () => {
|
|
|
257
257
|
expect(docs[0].row_count).toBe(2);
|
|
258
258
|
expect(docs[0].discount_amount).toBe(700);
|
|
259
259
|
expect(docs[0].rate_kind).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED);
|
|
260
|
-
|
|
260
|
+
// NO stored average rate: the source keeps the integer sum/count and the
|
|
261
|
+
// average is derived at the read boundary.
|
|
262
|
+
expect(docs[0]).not.toHaveProperty('rate');
|
|
263
|
+
expect(docs[0].percentage_rate_sum).toBe(5000);
|
|
264
|
+
expect(docs[0].percentage_row_count).toBe(1);
|
|
261
265
|
});
|
|
262
266
|
it('counts an order ONCE per source even with several item rows', () => {
|
|
263
267
|
const docs = (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)(scopeInput([
|
|
@@ -267,7 +271,7 @@ describe('buildDiscountSourcesForScope', () => {
|
|
|
267
271
|
item_id: 'i1',
|
|
268
272
|
entry_key: 'item:i1:special',
|
|
269
273
|
discount_id: 'MANUAL_25%',
|
|
270
|
-
rate:
|
|
274
|
+
rate: 2500,
|
|
271
275
|
amount: 250,
|
|
272
276
|
}),
|
|
273
277
|
entry({
|
|
@@ -276,7 +280,7 @@ describe('buildDiscountSourcesForScope', () => {
|
|
|
276
280
|
item_id: 'i2',
|
|
277
281
|
entry_key: 'item:i2:special',
|
|
278
282
|
discount_id: 'MANUAL_25%',
|
|
279
|
-
rate:
|
|
283
|
+
rate: 2500,
|
|
280
284
|
amount: 250,
|
|
281
285
|
}),
|
|
282
286
|
]));
|
|
@@ -291,7 +295,7 @@ describe('buildDiscountSourcesForScope', () => {
|
|
|
291
295
|
item_id: 'i1',
|
|
292
296
|
entry_key: 'item:i1:special',
|
|
293
297
|
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
294
|
-
rate:
|
|
298
|
+
rate: 10000,
|
|
295
299
|
amount: 500,
|
|
296
300
|
item_value: 500,
|
|
297
301
|
});
|
|
@@ -308,7 +312,7 @@ describe('buildDiscountSourcesForScope', () => {
|
|
|
308
312
|
item_id: 'i2',
|
|
309
313
|
entry_key: 'item:i2:special',
|
|
310
314
|
level: MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_LEVEL_ENUM.ITEM,
|
|
311
|
-
rate:
|
|
315
|
+
rate: 5000,
|
|
312
316
|
amount: 100,
|
|
313
317
|
}),
|
|
314
318
|
]));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
4
|
+
const materialized_integer_contract_1 = require("../../helpers/materialized-reports/materialized-integer-contract");
|
|
5
|
+
const materialized_report_drilldown_rows_1 = require("../../helpers/materialized-reports/materialized-report-drilldown-rows");
|
|
6
|
+
const materialized_discount_compile_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-compile-helpers");
|
|
7
|
+
const materialized_discount_source_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-source-helpers");
|
|
8
|
+
const fixtures_1 = require("./fixtures");
|
|
9
|
+
/**
|
|
10
|
+
* THE integer-only storage contract.
|
|
11
|
+
*
|
|
12
|
+
* A materialized document may only hold integers:
|
|
13
|
+
* - money in CENTS,
|
|
14
|
+
* - percentage rates in BASIS POINTS (1% = 100 bps),
|
|
15
|
+
* - durations in whole MINUTES,
|
|
16
|
+
* - counts / iterations / schema versions as integers,
|
|
17
|
+
* - NO stored share, average or ratio (they are derived in the formatter).
|
|
18
|
+
*
|
|
19
|
+
* These tests are the ENFORCEMENT: the validator is the gate every writer
|
|
20
|
+
* passes through, and it must catch a decimal wherever it hides — a nested
|
|
21
|
+
* counter, a key-metric map, a flat `data` payload or an iteration snapshot.
|
|
22
|
+
*/
|
|
23
|
+
describe('findNonIntegerNumericLeaves', () => {
|
|
24
|
+
it('accepts a document of integers', () => {
|
|
25
|
+
expect((0, materialized_integer_contract_1.findNonIntegerNumericLeaves)({
|
|
26
|
+
_id: 'a',
|
|
27
|
+
tenant_id: 't',
|
|
28
|
+
amount: 13125,
|
|
29
|
+
nested: { count: 3, rows: [{ amount: 1, rate: 5000 }] },
|
|
30
|
+
createdAt: new Date('2026-09-15T00:00:00.000Z'),
|
|
31
|
+
flag: true,
|
|
32
|
+
empty: null,
|
|
33
|
+
})).toEqual([]);
|
|
34
|
+
});
|
|
35
|
+
it('rejects a decimal number and names its path', () => {
|
|
36
|
+
const violations = (0, materialized_integer_contract_1.findNonIntegerNumericLeaves)({
|
|
37
|
+
amount: 131.25,
|
|
38
|
+
nested: { rows: [{ rate: 0.2 }] },
|
|
39
|
+
});
|
|
40
|
+
expect(violations.map((violation) => violation.path)).toEqual(['amount', 'nested.rows[0].rate']);
|
|
41
|
+
expect(violations.every((violation) => violation.reason === 'decimal')).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
it('rejects NaN and Infinity', () => {
|
|
44
|
+
const violations = (0, materialized_integer_contract_1.findNonIntegerNumericLeaves)({
|
|
45
|
+
a: Number.NaN,
|
|
46
|
+
b: Number.POSITIVE_INFINITY,
|
|
47
|
+
c: Number.NEGATIVE_INFINITY,
|
|
48
|
+
});
|
|
49
|
+
expect(violations).toHaveLength(3);
|
|
50
|
+
expect(violations.every((violation) => violation.reason === 'non-finite')).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
it('rejects a decimal smuggled in as a string, but not an identifier', () => {
|
|
53
|
+
const violations = (0, materialized_integer_contract_1.findNonIntegerNumericLeaves)({
|
|
54
|
+
amount: '131.25',
|
|
55
|
+
count: '7',
|
|
56
|
+
// A scope field legitimately holds a numeric-looking string.
|
|
57
|
+
scope_field: '2026',
|
|
58
|
+
day_index: '2026-09-15',
|
|
59
|
+
order_id: '12345',
|
|
60
|
+
});
|
|
61
|
+
expect(violations.map((violation) => violation.path)).toEqual(['amount', 'count']);
|
|
62
|
+
expect(violations.every((violation) => violation.reason === 'numeric-string')).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
it('ignores dates, object ids, booleans and null', () => {
|
|
65
|
+
expect((0, materialized_integer_contract_1.findNonIntegerNumericLeaves)({
|
|
66
|
+
computed_at: new Date(),
|
|
67
|
+
_id: { _bsontype: 'ObjectId', value: 'abc' },
|
|
68
|
+
deleted: false,
|
|
69
|
+
missing: null,
|
|
70
|
+
})).toEqual([]);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
describe('assertIntegerOnlyDocument', () => {
|
|
74
|
+
it('throws with the offending path', () => {
|
|
75
|
+
expect(() => (0, materialized_integer_contract_1.assertIntegerOnlyDocument)({ amount: 1.5 }, { collection: 'demo' })).toThrow(/demo.*amount=1\.5 \(decimal\)/s);
|
|
76
|
+
});
|
|
77
|
+
it('passes an integer document', () => {
|
|
78
|
+
expect(() => (0, materialized_integer_contract_1.assertIntegerOnlyDocument)({ amount: 15, rate: 5000 })).not.toThrow();
|
|
79
|
+
});
|
|
80
|
+
it('checks every document of a batch', () => {
|
|
81
|
+
expect(() => (0, materialized_integer_contract_1.assertIntegerOnlyDocuments)([{ amount: 1 }, { amount: 2.5 }], { collection: 'demo' })).toThrow(/demo\[1\]/);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
describe('the gate catches a decimal wherever it hides', () => {
|
|
85
|
+
it('rejects a decimal in a TOP-LEVEL field', () => {
|
|
86
|
+
expect(() => (0, materialized_integer_contract_1.assertIntegerOnlyDocument)({ discount_amount: 131.25 })).toThrow(/discount_amount=131\.25/);
|
|
87
|
+
});
|
|
88
|
+
it('rejects a decimal in a NESTED Mixed map (discount_counters / sale_type_metrics)', () => {
|
|
89
|
+
expect(() => (0, materialized_integer_contract_1.assertIntegerOnlyDocument)({
|
|
90
|
+
discount_counters: {
|
|
91
|
+
'COMPLETED:POS_DINE-IN': { amount: 1085.77, count: 3 },
|
|
92
|
+
},
|
|
93
|
+
})).toThrow(/discount_counters\.COMPLETED:POS_DINE-IN\.amount=1085\.77/);
|
|
94
|
+
expect(() => (0, materialized_integer_contract_1.assertIntegerOnlyDocument)({
|
|
95
|
+
sale_type_metrics: {
|
|
96
|
+
'COMPLETED:POS_DINE-IN': { gross_amount: 220.6896 },
|
|
97
|
+
},
|
|
98
|
+
})).toThrow(/sale_type_metrics\.COMPLETED:POS_DINE-IN\.gross_amount=220\.6896/);
|
|
99
|
+
});
|
|
100
|
+
it('rejects a decimal in a FLAT-ROW `data` payload', () => {
|
|
101
|
+
expect(() => (0, materialized_integer_contract_1.assertIntegerOnlyDocument)({
|
|
102
|
+
tenant_id: 'tenant-a',
|
|
103
|
+
key: 'COMPLETED:POS_DINE-IN',
|
|
104
|
+
data: { amount: 131.25, rate: 5000, item_value: 420.5 },
|
|
105
|
+
})).toThrow(/data\.amount=131\.25/);
|
|
106
|
+
});
|
|
107
|
+
it('rejects a decimal in an ITERATION snapshot', () => {
|
|
108
|
+
expect(() => (0, materialized_integer_contract_1.assertIntegerOnlyDocument)({
|
|
109
|
+
report_id: 'r1',
|
|
110
|
+
iteration: 2,
|
|
111
|
+
schema_version: 1,
|
|
112
|
+
snapshot: { discount_counters: { 'COMPLETED:POS_DINE-IN': { amount: 1.5 } } },
|
|
113
|
+
})).toThrow(/snapshot\.discount_counters\.COMPLETED:POS_DINE-IN\.amount=1\.5/);
|
|
114
|
+
});
|
|
115
|
+
it('rejects a decimal in a source key_metrics map', () => {
|
|
116
|
+
expect(() => (0, materialized_integer_contract_1.assertIntegerOnlyDocument)({
|
|
117
|
+
source_key: 'manual:order',
|
|
118
|
+
key_metrics: {
|
|
119
|
+
'COMPLETED:POS_DINE-IN': { discount_amount: 87.175, order_count: 1 },
|
|
120
|
+
},
|
|
121
|
+
})).toThrow(/key_metrics\.COMPLETED:POS_DINE-IN\.discount_amount=87\.175/);
|
|
122
|
+
});
|
|
123
|
+
it('passes a document whose every nested map is integral', () => {
|
|
124
|
+
expect(() => (0, materialized_integer_contract_1.assertIntegerOnlyDocument)({
|
|
125
|
+
discount_counters: { 'COMPLETED:POS_DINE-IN': { amount: 108577, count: 3 } },
|
|
126
|
+
sale_type_metrics: { 'COMPLETED:POS_DINE-IN': { gross_amount: 22069 } },
|
|
127
|
+
key_metrics: { 'COMPLETED:POS_DINE-IN': { discount_amount: 8718 } },
|
|
128
|
+
data: { amount: 13125, rate: 5000 },
|
|
129
|
+
snapshot: { amount: 1 },
|
|
130
|
+
schema_version: 1,
|
|
131
|
+
iteration: 2,
|
|
132
|
+
})).not.toThrow();
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
describe('the discount documents are integer-only', () => {
|
|
136
|
+
const entry = (over = {}) => ({
|
|
137
|
+
kind: 'DISCOUNT',
|
|
138
|
+
slot: 'SPECIAL',
|
|
139
|
+
sub_kind: 'MANUAL',
|
|
140
|
+
level: 'ORDER',
|
|
141
|
+
entry_key: 'order:special',
|
|
142
|
+
order_id: 'o1',
|
|
143
|
+
amount: 13125,
|
|
144
|
+
rate: 5000,
|
|
145
|
+
is_percentage: true,
|
|
146
|
+
created_at: new Date('2026-09-15T10:00:00.000Z'),
|
|
147
|
+
local_hour: '2026-09-15 10',
|
|
148
|
+
...over,
|
|
149
|
+
});
|
|
150
|
+
it('the drill-down projection (a Mixed `data` payload) is integer-only', () => {
|
|
151
|
+
const built = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES, {
|
|
152
|
+
tenant_id: 'tenant-a',
|
|
153
|
+
brand_id: 'brand-a',
|
|
154
|
+
scope_context: 'localDate',
|
|
155
|
+
scope_field: '2026-09-15',
|
|
156
|
+
day_index: '2026-09-15',
|
|
157
|
+
computed_at: new Date('2026-09-15T23:00:00.000Z'),
|
|
158
|
+
discount_entries: {
|
|
159
|
+
'COMPLETED:POS_WALK-IN': { rows: [entry(), entry({ entry_key: 'order:special:2', rate: 2000 })] },
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
expect(built.rows.length).toBe(2);
|
|
163
|
+
// The `data` blob is a Mixed payload — the schema cannot cover it, so the
|
|
164
|
+
// validator is the only gate.
|
|
165
|
+
(0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(built.rows, { collection: 'materialized_report_drilldown_rows' });
|
|
166
|
+
const scope = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownScope)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES, { tenant_id: 'tenant-a', brand_id: 'brand-a', scope_context: 'localDate', scope_field: '2026-09-15' }, built.rows);
|
|
167
|
+
(0, materialized_integer_contract_1.assertIntegerOnlyDocument)(scope, { collection: 'materialized_report_drilldown_scopes' });
|
|
168
|
+
});
|
|
169
|
+
it('a rate is stored in BASIS POINTS, so a mixed source keeps "mixed"', () => {
|
|
170
|
+
// 5000 bps = 50%, 2000 bps = 20% -> the average is 3500 bps.
|
|
171
|
+
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED).toBe('MIXED');
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
describe('a FULL discount document set is integer-only', () => {
|
|
175
|
+
const TENANT = 'basha-istanbul-f7';
|
|
176
|
+
const BRAND = 'basha-istanbul-f7';
|
|
177
|
+
const AT = new Date('2026-09-30T23:00:00.000Z');
|
|
178
|
+
it('the reports, the entries, the sources and the drill-down rows hold no decimal', () => {
|
|
179
|
+
const orders = (0, fixtures_1.loadOrdersDataset)('basha').filter((order) => order.localDate === '2026-09-13');
|
|
180
|
+
expect(orders.length).toBeGreaterThan(0);
|
|
181
|
+
const compiled = (0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
|
|
182
|
+
tenantId: TENANT,
|
|
183
|
+
brandId: BRAND,
|
|
184
|
+
localDate: '2026-09-13',
|
|
185
|
+
orders,
|
|
186
|
+
gmtOffset: 5,
|
|
187
|
+
computedAt: AT,
|
|
188
|
+
});
|
|
189
|
+
// 1. the counters
|
|
190
|
+
(0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(compiled.reports, {
|
|
191
|
+
collection: 'materialized_tenant_discount_reports',
|
|
192
|
+
});
|
|
193
|
+
// 2. the drill-down rows
|
|
194
|
+
(0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(compiled.entries, {
|
|
195
|
+
collection: 'materialized_tenant_discount_entries',
|
|
196
|
+
});
|
|
197
|
+
// 3. the source projection, built from the compiled entries
|
|
198
|
+
const sources = (0, materialized_discount_source_helpers_1.buildDiscountSourcesForScope)({
|
|
199
|
+
tenantId: TENANT,
|
|
200
|
+
brandId: BRAND,
|
|
201
|
+
scopeContext: compiled.entries[0].scope_context,
|
|
202
|
+
scopeField: compiled.entries[0].scope_field,
|
|
203
|
+
dayIndex: compiled.entries[0].day_index ?? null,
|
|
204
|
+
sections: compiled.entries[0].discount_entries,
|
|
205
|
+
computedAt: AT,
|
|
206
|
+
sourceWatermark: compiled.entries[0].source_watermark ?? null,
|
|
207
|
+
});
|
|
208
|
+
(0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(sources, {
|
|
209
|
+
collection: 'materialized_tenant_discount_sources',
|
|
210
|
+
});
|
|
211
|
+
// 4. the flat drill-down projection (a Mixed `data` payload)
|
|
212
|
+
const rows = (0, materialized_report_drilldown_rows_1.buildMaterializedDrilldownRows)(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DISCOUNT_ENTRIES, compiled.entries[0]).rows;
|
|
213
|
+
(0, materialized_integer_contract_1.assertIntegerOnlyDocuments)(rows, { collection: 'materialized_report_drilldown_rows' });
|
|
214
|
+
// 5. NO stored average rate, and NO fractional average anywhere: the
|
|
215
|
+
// source keeps the integer SUM (basis points) and the integer COUNT.
|
|
216
|
+
for (const source of sources) {
|
|
217
|
+
expect(source).not.toHaveProperty('rate');
|
|
218
|
+
expect(source.percentage_rate_sum % 1).toBe(0);
|
|
219
|
+
expect(source.percentage_row_count % 1).toBe(0);
|
|
220
|
+
expect(source.fixed_row_count % 1).toBe(0);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
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 fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
9
|
+
/**
|
|
10
|
+
* THE integer-only storage contract, enforced at EVERY materialized write path.
|
|
11
|
+
*
|
|
12
|
+
* The runtime gate lives in the toolkit
|
|
13
|
+
* (`assertIntegerOnlyDocument` / `assertIntegerOnlyDocuments`); the workers
|
|
14
|
+
* service has the matching inventory suite for its own write paths.
|
|
15
|
+
*
|
|
16
|
+
* This suite is the INVENTORY for the SIMULATION / DEMO upload paths — the
|
|
17
|
+
* scripts that write materialized collections into a database. A write site
|
|
18
|
+
* that loses its gate would otherwise stay invisible until a decimal reached
|
|
19
|
+
* Mongo.
|
|
20
|
+
*
|
|
21
|
+
* A `deleteMany` carries no document, so it is not a gated write.
|
|
22
|
+
*/
|
|
23
|
+
const SIMULATIONS_DIR = path_1.default.resolve(__dirname, '../../../simulations');
|
|
24
|
+
/** The materialized write paths of the demo/simulation scripts. */
|
|
25
|
+
const WRITE_PATHS = [
|
|
26
|
+
'generate-demo-materialized-data.ts',
|
|
27
|
+
'upload-demo-materialized-data.ts',
|
|
28
|
+
'upload-drilldown-demo-only.ts',
|
|
29
|
+
];
|
|
30
|
+
/** Writes that carry documents into a collection (and therefore need a gate). */
|
|
31
|
+
const DOCUMENT_WRITES = ['insertMany(', 'insertOne(', 'bulkWrite(', 'replaceOne(', 'updateOne('];
|
|
32
|
+
/** The gate calls. */
|
|
33
|
+
const GATES = ['assertIntegerOnly(', 'assertIntegerOnlyDocuments(', 'assertIntegerOnlyDocument('];
|
|
34
|
+
const countOccurrences = (source, needles) => needles.reduce((total, needle) => {
|
|
35
|
+
let count = 0;
|
|
36
|
+
let index = source.indexOf(needle);
|
|
37
|
+
while (index !== -1) {
|
|
38
|
+
count += 1;
|
|
39
|
+
index = source.indexOf(needle, index + needle.length);
|
|
40
|
+
}
|
|
41
|
+
return total + count;
|
|
42
|
+
}, 0);
|
|
43
|
+
describe('every materialized write path is gated', () => {
|
|
44
|
+
for (const fileName of WRITE_PATHS) {
|
|
45
|
+
it(`${fileName} gates every document-carrying write`, () => {
|
|
46
|
+
const source = fs_1.default.readFileSync(path_1.default.join(SIMULATIONS_DIR, fileName), 'utf8');
|
|
47
|
+
const writes = countOccurrences(source, DOCUMENT_WRITES);
|
|
48
|
+
const gates = countOccurrences(source, GATES);
|
|
49
|
+
expect(writes).toBeGreaterThan(0);
|
|
50
|
+
expect(gates).toBeGreaterThanOrEqual(writes);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
it('the rate kind label is unchanged by the rate removal', () => {
|
|
54
|
+
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.MIXED).toBe('MIXED');
|
|
55
|
+
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.PERCENTAGE).toBe('PERCENTAGE');
|
|
56
|
+
expect(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM.FIXED).toBe('FIXED');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|