@managesome/eposmatic-toolkit 0.4.5 → 0.4.8

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 (68) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +33 -0
  2. package/esm/constants/REPORT_CALENDAR.js +47 -0
  3. package/esm/constants/index.js +1 -0
  4. package/esm/helpers/materialized-reports/index.js +2 -0
  5. package/esm/helpers/materialized-reports/materialized-credit-bills-helpers.js +982 -0
  6. package/esm/helpers/materialized-reports/materialized-reports-read-helpers.js +1 -76
  7. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +716 -0
  8. package/esm/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +327 -41
  9. package/esm/helpers/materialized-reports/tenant-sales-overview-formatter.js +72 -11
  10. package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +118 -235
  11. package/esm/helpers/types/index.js +1 -0
  12. package/esm/schemas/models/index.js +1 -0
  13. package/esm/schemas/models/materialized_credit_bills.js +265 -0
  14. package/esm/schemas/models/materialized_tenant_reports_iterations.js +13 -0
  15. package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +39 -97
  16. package/esm/schemas/types/index.js +1 -0
  17. package/esm/schemas/types/materialized_credit_bills.js +1 -0
  18. package/lib/constants/MATERIALIZED_REPORTS.d.ts +28 -2
  19. package/lib/constants/MATERIALIZED_REPORTS.js +34 -1
  20. package/lib/constants/REPORT_CALENDAR.d.ts +31 -0
  21. package/lib/constants/REPORT_CALENDAR.js +50 -0
  22. package/lib/constants/index.d.ts +1 -0
  23. package/lib/constants/index.js +1 -0
  24. package/lib/helpers/materialized-reports/index.d.ts +2 -0
  25. package/lib/helpers/materialized-reports/index.js +2 -0
  26. package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.d.ts +307 -0
  27. package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.js +1003 -0
  28. package/lib/helpers/materialized-reports/materialized-reports-read-helpers.d.ts +0 -30
  29. package/lib/helpers/materialized-reports/materialized-reports-read-helpers.js +0 -76
  30. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +59 -0
  31. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +723 -0
  32. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.d.ts +66 -8
  33. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +326 -40
  34. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.d.ts +18 -2
  35. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.js +72 -11
  36. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.d.ts +46 -37
  37. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +119 -238
  38. package/lib/helpers/types/index.d.ts +1 -0
  39. package/lib/helpers/types/index.js +1 -0
  40. package/lib/helpers/types/materialized-reports-apis.d.ts +46 -17
  41. package/lib/helpers/types/report-calendar.d.ts +20 -0
  42. package/lib/helpers/types/report-calendar.js +2 -0
  43. package/lib/schemas/models/index.d.ts +1 -0
  44. package/lib/schemas/models/index.js +1 -0
  45. package/lib/schemas/models/materialized_credit_bills.d.ts +337 -0
  46. package/lib/schemas/models/materialized_credit_bills.js +268 -0
  47. package/lib/schemas/models/materialized_tenant_reports_iterations.d.ts +20 -6
  48. package/lib/schemas/models/materialized_tenant_reports_iterations.js +14 -1
  49. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +37 -165
  50. package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +40 -98
  51. package/lib/schemas/types/index.d.ts +1 -0
  52. package/lib/schemas/types/index.js +1 -0
  53. package/lib/schemas/types/materialized_credit_bills.d.ts +139 -0
  54. package/lib/schemas/types/materialized_credit_bills.js +2 -0
  55. package/lib/schemas/types/materialized_tenant_sales_overview_reports.d.ts +32 -77
  56. package/lib/tests/materialized-reports/materialized-credit-bills-helpers.test.d.ts +1 -0
  57. package/lib/tests/materialized-reports/materialized-credit-bills-helpers.test.js +715 -0
  58. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.d.ts +1 -0
  59. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.js +262 -0
  60. package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-schema.test.js +9 -0
  61. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +23 -24
  62. package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.js +327 -22
  63. package/lib/tests/materialized-reports/tenant-sales-overview-formatter-aggregated.test.js +27 -2
  64. package/lib/tests/materialized-reports/tenant-sales-overview-formatter.test.js +12 -27
  65. package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +79 -146
  66. package/package.json +1 -1
  67. package/lib/tests/materialized-reports/materialized-reports-read-helpers.test.js +0 -100
  68. /package/{lib/tests/materialized-reports/materialized-reports-read-helpers.test.d.ts → esm/helpers/types/report-calendar.js} +0 -0
@@ -0,0 +1,262 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const ENUMS_1 = require("../../constants/ENUMS");
4
+ const REPORT_CALENDAR_1 = require("../../constants/REPORT_CALENDAR");
5
+ const materialized_reports_selection_helpers_1 = require("../../helpers/materialized-reports/materialized-reports-selection-helpers");
6
+ const selection = (over = {}) => ({
7
+ mode: REPORT_CALENDAR_1.CALENDAR_MODE_ENUM.HISTORICAL,
8
+ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DATE_RANGE,
9
+ rangeStart: null,
10
+ rangeEnd: null,
11
+ selectedDates: [],
12
+ timeStart: null,
13
+ timeEnd: null,
14
+ selectedPeriods: [],
15
+ weekdays: [],
16
+ ...over,
17
+ });
18
+ const hourLabels = (dayKey) => [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5].map((hour) => `${dayKey} ${`${hour}`.padStart(2, '0')}`);
19
+ describe('buildMaterializedReportScopeGroupsFromSelection', () => {
20
+ test('LIVE reads the running day as its 24 exact hour labels', () => {
21
+ const groups = (0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({
22
+ mode: REPORT_CALENDAR_1.CALENDAR_MODE_ENUM.LIVE,
23
+ rangeStart: '2026-09-30',
24
+ }));
25
+ expect(groups).toEqual([{ scope_context: 'localHour', scope_field: hourLabels('2026-09-30') }]);
26
+ });
27
+ test('LIVE without a committed day cannot be served', () => {
28
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ mode: REPORT_CALENDAR_1.CALENDAR_MODE_ENUM.LIVE }))).toBeNull();
29
+ });
30
+ test('single day → direct localDate value', () => {
31
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ rangeStart: '2026-10-14' }))).toEqual([
32
+ { scope_context: 'localDate', scope_field: ['2026-10-14'] },
33
+ ]);
34
+ });
35
+ test('a full month run compacts to its localMonth value', () => {
36
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ rangeStart: '2026-09-01', rangeEnd: '2026-09-30' }))).toEqual([{ scope_context: 'localMonth', scope_field: ['2026-09'] }]);
37
+ });
38
+ test('a full quarter run compacts to its localQuarter value', () => {
39
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ rangeStart: '2026-07-01', rangeEnd: '2026-09-30' }))).toEqual([{ scope_context: 'localQuarter', scope_field: ['2026-Q3'] }]);
40
+ });
41
+ test('MULTI_DATE: an exact full week folds to its week-start', () => {
42
+ // Sun 2026-09-27 … Sat 2026-10-03
43
+ const week = ['2026-09-27', '2026-09-28', '2026-09-29', '2026-09-30', '2026-10-01', '2026-10-02', '2026-10-03'];
44
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.MULTI_DATE, selectedDates: week }))).toEqual([{ scope_context: 'localWeek', scope_field: ['2026-09-27'] }]);
45
+ });
46
+ test('MONTH view enumerates the exact month keys of the range', () => {
47
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({
48
+ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.MONTH,
49
+ rangeStart: '2026-09-01',
50
+ rangeEnd: '2026-11-15',
51
+ }))).toEqual([{ scope_context: 'localMonth', scope_field: ['2026-09', '2026-10', '2026-11'] }]);
52
+ });
53
+ test('YEAR and DIFFERENT_MONTHS views read their exact period keys', () => {
54
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({
55
+ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.YEAR,
56
+ rangeStart: '2024-01-01',
57
+ rangeEnd: '2026-06-01',
58
+ }))).toEqual([{ scope_context: 'localYear', scope_field: ['2024', '2025', '2026'] }]);
59
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({
60
+ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_MONTHS,
61
+ selectedPeriods: ['2026-03', '2026-01'],
62
+ }))).toEqual([{ scope_context: 'localMonth', scope_field: ['2026-03', '2026-01'] }]);
63
+ });
64
+ test('weekday refinement narrows the day set before compiling (localDate fallback)', () => {
65
+ const groups = (0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({
66
+ rangeStart: '2026-10-01',
67
+ rangeEnd: '2026-10-31',
68
+ weekdays: [ENUMS_1.DAY_INDEX_ENUM.MON],
69
+ }));
70
+ // Oct 2026 Mondays — period docs carry no day_index, so the read stays on
71
+ // standalone day documents.
72
+ expect(groups).toEqual([
73
+ {
74
+ scope_context: 'localDate',
75
+ scope_field: ['2026-10-05', '2026-10-12', '2026-10-19', '2026-10-26'],
76
+ },
77
+ ]);
78
+ });
79
+ test('a 7-day weekday list is treated as “every day” (no refinement)', () => {
80
+ const groups = (0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({
81
+ rangeStart: '2026-09-01',
82
+ rangeEnd: '2026-09-30',
83
+ weekdays: [
84
+ ENUMS_1.DAY_INDEX_ENUM.MON,
85
+ ENUMS_1.DAY_INDEX_ENUM.TUE,
86
+ ENUMS_1.DAY_INDEX_ENUM.WED,
87
+ ENUMS_1.DAY_INDEX_ENUM.THU,
88
+ ENUMS_1.DAY_INDEX_ENUM.FRI,
89
+ ENUMS_1.DAY_INDEX_ENUM.SAT,
90
+ ENUMS_1.DAY_INDEX_ENUM.SUN,
91
+ ],
92
+ }));
93
+ expect(groups).toEqual([{ scope_context: 'localMonth', scope_field: ['2026-09'] }]);
94
+ });
95
+ test('dedicated window view repeats the SAME hours on every selected day', () => {
96
+ const groups = (0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({
97
+ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DATE_RANGE_TIME_WINDOW,
98
+ rangeStart: '2026-09-01',
99
+ rangeEnd: '2026-09-02',
100
+ timeStart: '16:00',
101
+ timeEnd: '18:00',
102
+ }));
103
+ expect(groups).toEqual([
104
+ {
105
+ scope_context: 'localHour',
106
+ scope_field: ['2026-09-01 16', '2026-09-01 17', '2026-09-02 16', '2026-09-02 17'],
107
+ },
108
+ ]);
109
+ });
110
+ test('range-like window view compiles a CONTINUOUS span (boundary hours + compacted middle)', () => {
111
+ const groups = (0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({
112
+ rangeStart: '2026-09-01',
113
+ rangeEnd: '2026-09-03',
114
+ timeStart: '18:00',
115
+ timeEnd: '20:00',
116
+ }));
117
+ expect(groups).not.toBeNull();
118
+ // The middle whole day folds into its localDate value…
119
+ expect(groups).toContainEqual({
120
+ scope_context: 'localDate',
121
+ scope_field: ['2026-09-02'],
122
+ });
123
+ // …while the boundary days carry their exact hour labels: the start day
124
+ // contributes its business-day tail (00–05) plus the hours from the window
125
+ // start through 23; the end day contributes 06 through the hour before the
126
+ // window end.
127
+ const hourGroup = groups?.find((group) => group.scope_context === 'localHour');
128
+ expect(hourGroup).toBeDefined();
129
+ const asLabels = (day, hours) => hours.map((hour) => `${day} ${hour}`);
130
+ expect(hourGroup?.scope_field).toEqual([
131
+ ...asLabels('2026-09-01', ['00', '01', '02', '03', '04', '05']),
132
+ ...asLabels('2026-09-01', ['18', '19', '20', '21', '22', '23']),
133
+ ...asLabels('2026-09-03', ['06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19']),
134
+ ]);
135
+ });
136
+ test('an incomplete dedicated window falls back to the plain range read', () => {
137
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({
138
+ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DATE_RANGE_TIME_WINDOW,
139
+ rangeStart: '2026-09-01',
140
+ rangeEnd: '2026-09-30',
141
+ timeStart: '04:00',
142
+ timeEnd: '08:00', // crosses the 06:00 business-morning boundary → unservable
143
+ }))).toEqual([{ scope_context: 'localMonth', scope_field: ['2026-09'] }]);
144
+ });
145
+ test('an empty selection cannot be served', () => {
146
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection())).toBeNull();
147
+ });
148
+ });
149
+ describe('getDayKeysFromCalendarSelection', () => {
150
+ test('expands a range, applies the weekday filter, and keeps order', () => {
151
+ expect((0, materialized_reports_selection_helpers_1.getDayKeysFromCalendarSelection)(selection({
152
+ rangeStart: '2026-10-03',
153
+ rangeEnd: '2026-10-06',
154
+ weekdays: [ENUMS_1.DAY_INDEX_ENUM.SAT, ENUMS_1.DAY_INDEX_ENUM.SUN],
155
+ }))).toEqual(['2026-10-03', '2026-10-04']);
156
+ });
157
+ });
158
+ describe('isValidBusinessHourWindow', () => {
159
+ test('accepts same-day and midnight-wrap windows, rejects boundary walks', () => {
160
+ expect((0, materialized_reports_selection_helpers_1.isValidBusinessHourWindow)(16, 18)).toBe(true);
161
+ expect((0, materialized_reports_selection_helpers_1.isValidBusinessHourWindow)(22, 2)).toBe(true);
162
+ expect((0, materialized_reports_selection_helpers_1.isValidBusinessHourWindow)(4, 8)).toBe(false);
163
+ expect((0, materialized_reports_selection_helpers_1.isValidBusinessHourWindow)(16, 16)).toBe(false);
164
+ expect((0, materialized_reports_selection_helpers_1.isValidBusinessHourWindow)(-1, 5)).toBe(false);
165
+ });
166
+ });
167
+ describe('selection input hardening (F3 / F10 / F11)', () => {
168
+ test('malformed date keys cannot be served — no string-walk loops', () => {
169
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ rangeStart: 'A', rangeEnd: 'Z' }))).toBeNull();
170
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.WEEK_OF_YEAR, rangeStart: 'A', rangeEnd: 'Z' }))).toBeNull();
171
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ rangeStart: '2026-02-31' }))).toBeNull();
172
+ // the shared day helper is bounded too — the same input can never hang
173
+ expect((0, materialized_reports_selection_helpers_1.getDayKeysFromCalendarSelection)(selection({ rangeStart: 'A', rangeEnd: 'Z' }))).toEqual([]);
174
+ });
175
+ test('oversized spans are rejected before any expansion', () => {
176
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ rangeStart: '2026-01-01', rangeEnd: '9999-12-31' }))).toBeNull();
177
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ rangeStart: '1000-01-01', rangeEnd: '9998-12-31' }))).toBeNull();
178
+ // ~10 years of days is still servable
179
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ rangeStart: '2026-01-01', rangeEnd: '2035-12-31' }))).not.toBeNull();
180
+ });
181
+ test('period keys are validated per view (bad keys → null, never a 200 no-data read)', () => {
182
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_MONTHS, selectedPeriods: ['2026-13'] }))).toBeNull();
183
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_QUARTERS, selectedPeriods: ['2026'] }))).toBeNull();
184
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_SEMESTERS, selectedPeriods: ['2026-S3'] }))).toBeNull();
185
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_WEEKS_OF_YEAR, selectedPeriods: ['2027-W53'] }))).toBeNull();
186
+ // …while semantically valid picks still decode.
187
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_WEEKS_OF_YEAR, selectedPeriods: ['2026-W53'] }))).toEqual([{ scope_context: 'localWeekOfYear', scope_field: ['2026-W53'] }]);
188
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_QUARTERS, selectedPeriods: ['2026-Q4'] }))).toEqual([{ scope_context: 'localQuarter', scope_field: ['2026-Q4'] }]);
189
+ });
190
+ test('minute-level time keys are rejected instead of silently truncating', () => {
191
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ rangeStart: '2026-09-10', timeStart: '18:45', timeEnd: '20:00' }))).toBeNull();
192
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ rangeStart: '2026-09-10', timeStart: '18:00', timeEnd: '20:00' }))).not.toBeNull();
193
+ });
194
+ test('malformed weekday entries are rejected', () => {
195
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({ rangeStart: '2026-09-10', weekdays: ['MONDAY'] }))).toBeNull();
196
+ });
197
+ test('weekday + window on range views compiles RECURRING — never a dead end (F9)', () => {
198
+ const groups = (0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({
199
+ rangeStart: '2026-09-01',
200
+ rangeEnd: '2026-09-30',
201
+ weekdays: [ENUMS_1.DAY_INDEX_ENUM.MON],
202
+ timeStart: '18:00',
203
+ timeEnd: '20:00',
204
+ }));
205
+ expect(groups).not.toBeNull();
206
+ const labels = (groups ?? []).flatMap((group) => group.scope_field);
207
+ // Sep 2026 Mondays (7/14/21/28) × hours 18–19 — one label per bucket.
208
+ expect(labels).toHaveLength(8);
209
+ expect(labels).toContain('2026-09-07 18');
210
+ expect(labels).toContain('2026-09-28 19');
211
+ });
212
+ });
213
+ describe('validateMaterializedReportSelectionInput', () => {
214
+ test('distinguishes tooLarge from invalid and approves servable selections', () => {
215
+ expect((0, materialized_reports_selection_helpers_1.validateMaterializedReportSelectionInput)(selection({ rangeStart: '2026-01-01', rangeEnd: '9999-12-31' }))).toEqual({ ok: false, reason: 'tooLarge' });
216
+ expect((0, materialized_reports_selection_helpers_1.validateMaterializedReportSelectionInput)(selection({ rangeStart: 'A' }))).toEqual({
217
+ ok: false,
218
+ reason: 'invalid',
219
+ });
220
+ expect((0, materialized_reports_selection_helpers_1.validateMaterializedReportSelectionInput)(selection({ rangeStart: '2026-09-10' }))).toEqual({ ok: true });
221
+ });
222
+ });
223
+ describe('N5 — canonical decode + reason parity', () => {
224
+ test('duplicate period picks decode once (first-occurrence order)', () => {
225
+ const groups = (0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(selection({
226
+ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_MONTHS,
227
+ selectedPeriods: ['2026-09', '2026-09', '2026-10'],
228
+ }));
229
+ expect(groups).toEqual([{ scope_context: 'localMonth', scope_field: ['2026-09', '2026-10'] }]);
230
+ });
231
+ test('weekday-filtered multi-year selections count EFFECTIVE days, not raw ones', () => {
232
+ const bigWeekdaySelection = selection({
233
+ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_YEARS,
234
+ selectedPeriods: Array.from({ length: 11 }, (_, index) => `${2015 + index}`),
235
+ weekdays: [ENUMS_1.DAY_INDEX_ENUM.MON],
236
+ });
237
+ // 11 years ≈ 4 018 raw days but only ~573 effective Mondays — servable.
238
+ expect((0, materialized_reports_selection_helpers_1.validateMaterializedReportSelectionInput)(bigWeekdaySelection)).toEqual({ ok: true });
239
+ const groups = (0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(bigWeekdaySelection);
240
+ expect(groups).not.toBeNull();
241
+ const labels = (groups ?? []).flatMap((group) => group.scope_field);
242
+ expect(labels.length).toBeGreaterThan(560);
243
+ expect(labels.length).toBeLessThan(590);
244
+ });
245
+ test('oversize DIFFERENT_* expansions classify as tooLarge (not "invalid")', () => {
246
+ const tooManyYears = selection({
247
+ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_YEARS,
248
+ selectedPeriods: Array.from({ length: 200 }, (_, index) => `${1900 + index}`),
249
+ });
250
+ // 200 raw years ≈ 73 000 days > the 3 660-day bound — tooLarge, and the
251
+ // decoder agrees (null), so ops never mislabels it as "Invalid request".
252
+ expect((0, materialized_reports_selection_helpers_1.validateMaterializedReportSelectionInput)(tooManyYears)).toEqual({ ok: false, reason: 'tooLarge' });
253
+ expect((0, materialized_reports_selection_helpers_1.buildMaterializedReportScopeGroupsFromSelection)(tooManyYears)).toBeNull();
254
+ // With the weekday refinement the EFFECTIVE set is still beyond the bound
255
+ // (200 × ~52 ≈ 10 400 Mondays).
256
+ expect((0, materialized_reports_selection_helpers_1.validateMaterializedReportSelectionInput)(selection({
257
+ view: REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_YEARS,
258
+ selectedPeriods: tooManyYears.selectedPeriods,
259
+ weekdays: [ENUMS_1.DAY_INDEX_ENUM.MON],
260
+ }))).toEqual({ ok: false, reason: 'tooLarge' });
261
+ });
262
+ });
@@ -70,4 +70,13 @@ describe('materialized_tenant_reports_iterations schema', () => {
70
70
  });
71
71
  expect(MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_REPORT_TYPES).toContain(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_OVERVIEW);
72
72
  });
73
+ it('exports UNIQUE index options for the materializer to CREATE (F12)', () => {
74
+ expect(materialized_tenant_reports_iterations_1.MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS).toEqual([
75
+ {
76
+ key: { report_id: 1, iteration: -1 },
77
+ name: 'report_id__iteration',
78
+ unique: true,
79
+ },
80
+ ]);
81
+ });
73
82
  });
@@ -34,22 +34,6 @@ const saleTypeMetric = () => ({
34
34
  orders: orderMetric(),
35
35
  payments: [paymentMetric('CASH')],
36
36
  });
37
- const creditBill = () => ({
38
- order_id: new mongoose_1.default.Types.ObjectId().toString(),
39
- payment_id: new mongoose_1.default.Types.ObjectId().toString(),
40
- amount: 1200,
41
- received_amount: 600, // partially collected
42
- issued_at: new Date('2026-09-29T22:10:00.000Z'),
43
- settled_at: null,
44
- });
45
- const receivedCreditBill = () => ({
46
- order_id: new mongoose_1.default.Types.ObjectId().toString(),
47
- payment_id: new mongoose_1.default.Types.ObjectId().toString(),
48
- amount: 1200,
49
- received_amount: 1200,
50
- credit_note: 'CN-2026-09-30',
51
- issued_local_date: '2026-09-29',
52
- });
53
37
  const validDocument = () => ({
54
38
  _id: new mongoose_1.default.Types.ObjectId().toString(),
55
39
  tenant_id: 'z2-isb',
@@ -63,8 +47,8 @@ const validDocument = () => ({
63
47
  [constants_1.REPORT_SALE_TYPE_ENUM.TOTAL_SALES]: saleTypeMetric(),
64
48
  [constants_1.REPORT_SALE_TYPE_ENUM.AMOUNT_COLLECTED]: saleTypeMetric(),
65
49
  [constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS]: {
66
- issued: { count: 1, amount: 1200, settled_count: 0, settled_amount: 600, bills: [creditBill()] },
67
- received: { count: 1, amount: 1200, bills: [receivedCreditBill()] },
50
+ issued: { count: 1, amount: 1200, settled_count: 0, settled_amount: 0 },
51
+ received: { count: 1, amount: 600 },
68
52
  },
69
53
  },
70
54
  },
@@ -87,6 +71,15 @@ describe('materialized_tenant_sales_overview_reports schema', () => {
87
71
  },
88
72
  });
89
73
  });
74
+ it('exports UNIQUE index options for the materializer to CREATE (F12)', () => {
75
+ expect(materialized_tenant_sales_overview_reports_1.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_INDEX_OPTIONS).toEqual([
76
+ {
77
+ key: { tenant_id: 1, scope_context: 1, scope_field: 1, day_index: 1 },
78
+ name: 'tenant_id__scope_context__scope_field__day_index',
79
+ unique: true,
80
+ },
81
+ ]);
82
+ });
90
83
  it('validates a well-formed scope document and casts metrics to a Map', () => {
91
84
  const doc = new Model(validDocument());
92
85
  expect(doc.validateSync()).toBeUndefined();
@@ -134,16 +127,22 @@ describe('materialized_tenant_sales_overview_reports schema', () => {
134
127
  ];
135
128
  expect(new Model(document).validateSync()).toBeUndefined();
136
129
  });
137
- it('validates the per-key credit-bill ledger and applies zero defaults when omitted', () => {
130
+ it('validates the per-key credit-bill COUNTERS and applies zero defaults when omitted', () => {
138
131
  const doc = new Model(validDocument());
139
132
  expect(doc.validateSync()).toBeUndefined();
140
133
  const metrics = doc.get('sale_type_metrics');
141
134
  const entry = metrics.get('COMPLETED:POS_TAKE-AWAY');
142
135
  expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.count).toBe(1);
143
- expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.bills[0].received_amount).toBe(600);
144
- expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.bills[0].settled_at).toBeNull();
145
- expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].received.bills[0].received_amount).toBe(1200);
146
- expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].received.bills[0].credit_note).toBe('CN-2026-09-30');
136
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.amount).toBe(1200);
137
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.settled_count).toBe(0);
138
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.settled_amount).toBe(0);
139
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].received.count).toBe(1);
140
+ expect(entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].received.amount).toBe(600);
141
+ // The itemized ledger lives in `materialized_tenant_credit_bills` — report
142
+ // documents must carry numbers only (unknown paths are strict-mode
143
+ // stripped, so a stray `bills` array can never round-trip).
144
+ expect('bills' in (entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued ?? {})).toBe(false);
145
+ expect('bills' in (entry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].received ?? {})).toBe(false);
147
146
  const minimal = { ...validDocument() };
148
147
  const minimalMetrics = minimal.sale_type_metrics;
149
148
  delete minimalMetrics['COMPLETED:POS_TAKE-AWAY'][constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS];
@@ -155,8 +154,8 @@ describe('materialized_tenant_sales_overview_reports schema', () => {
155
154
  expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.amount).toBe(0);
156
155
  expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.settled_count).toBe(0);
157
156
  expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.settled_amount).toBe(0);
158
- expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].issued.bills).toEqual([]);
159
157
  expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].received.count).toBe(0);
158
+ expect(defaultEntry?.[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS].received.amount).toBe(0);
160
159
  });
161
160
  it('accepts an empty metrics map (day with no orders) and applies key-filter defaults', () => {
162
161
  const document = { ...validDocument(), sale_type_metrics: {} };