@managesome/eposmatic-toolkit 0.4.3 → 0.4.5

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 (48) hide show
  1. package/esm/helpers/materialized-reports/index.js +1 -0
  2. package/esm/helpers/materialized-reports/materialized-reports-read-helpers.js +96 -22
  3. package/esm/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +281 -0
  4. package/esm/helpers/materialized-reports/tenant-sales-overview-formatter.js +126 -7
  5. package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +47 -1
  6. package/esm/helpers/order/generate-order.js +11 -0
  7. package/esm/helpers/store/common-order-actions.js +7 -3
  8. package/esm/helpers/types/materialized-reports-apis.js +1 -1
  9. package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +20 -26
  10. package/lib/helpers/materialized-reports/index.d.ts +1 -0
  11. package/lib/helpers/materialized-reports/index.js +1 -0
  12. package/lib/helpers/materialized-reports/materialized-reports-read-helpers.d.ts +50 -21
  13. package/lib/helpers/materialized-reports/materialized-reports-read-helpers.js +96 -21
  14. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.d.ts +58 -0
  15. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +286 -0
  16. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.d.ts +24 -0
  17. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.js +127 -7
  18. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +47 -1
  19. package/lib/helpers/order/generate-order.d.ts +2 -0
  20. package/lib/helpers/order/generate-order.js +13 -0
  21. package/lib/helpers/store/common-order-actions.js +7 -3
  22. package/lib/helpers/types/materialized-reports-apis.d.ts +115 -25
  23. package/lib/helpers/types/materialized-reports-apis.js +1 -0
  24. package/lib/schemas/models/internal_invoices.d.ts +3 -3
  25. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +26 -34
  26. package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +20 -26
  27. package/lib/schemas/models/orders.d.ts +692 -692
  28. package/lib/schemas/models/tenant_purchase_orders.d.ts +90 -90
  29. package/lib/schemas/models/tenants.d.ts +9 -9
  30. package/lib/schemas/models/warehouse_communications.d.ts +3 -3
  31. package/lib/schemas/models/warehouse_internal_movements.d.ts +48 -48
  32. package/lib/schemas/models/warehouse_purchase_order_rfqs.d.ts +63 -63
  33. package/lib/schemas/models/warehouse_purchase_orders.d.ts +96 -96
  34. package/lib/schemas/models/warehouse_sale_invoices.d.ts +3 -3
  35. package/lib/schemas/models/warehouse_sale_order_rfqs.d.ts +69 -69
  36. package/lib/schemas/models/warehouse_sale_orders.d.ts +45 -45
  37. package/lib/schemas/models/warehouses.d.ts +6 -6
  38. package/lib/tests/materialized-reports/materialized-reports-read-helpers.test.d.ts +1 -0
  39. package/lib/tests/materialized-reports/materialized-reports-read-helpers.test.js +100 -0
  40. package/lib/tests/materialized-reports/materialized-reports-scope-group-helpers.test.d.ts +1 -0
  41. package/lib/tests/materialized-reports/materialized-reports-scope-group-helpers.test.js +275 -0
  42. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +11 -0
  43. package/lib/tests/materialized-reports/tenant-sales-overview-formatter-aggregated.test.d.ts +1 -0
  44. package/lib/tests/materialized-reports/tenant-sales-overview-formatter-aggregated.test.js +143 -0
  45. package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +22 -0
  46. package/lib/tests/num-of-guests-limit.test.d.ts +1 -0
  47. package/lib/tests/num-of-guests-limit.test.js +76 -0
  48. package/package.json +1 -1
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
+ const materialized_reports_read_helpers_1 = require("../../helpers/materialized-reports/materialized-reports-read-helpers");
5
+ describe('buildMaterializedReportReadBody', () => {
6
+ test('builds the canonical body, normalizing tenants and merging same-context groups', () => {
7
+ const body = (0, materialized_reports_read_helpers_1.buildMaterializedReportReadBody)({
8
+ tenants: ['tenant-b', 'tenant-a', 'tenant-b'],
9
+ groups: [
10
+ {
11
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
12
+ scope_field: ['2026-09-02 18', '2026-09-01 21', '2026-09-02 18'],
13
+ },
14
+ {
15
+ // same context → merged into the first group (values unioned)
16
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
17
+ scope_field: ['2026-09-01 21', '2026-09-03 09'],
18
+ },
19
+ {
20
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
21
+ scope_field: ['2026-09-02', '2026-09-01'],
22
+ },
23
+ ],
24
+ });
25
+ expect(body).toEqual({
26
+ tenants: ['tenant-b', 'tenant-a'], // deduped, order kept
27
+ scope_groups: [
28
+ {
29
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
30
+ scope_field: ['2026-09-01 21', '2026-09-02 18', '2026-09-03 09'], // deduped + sorted
31
+ },
32
+ {
33
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
34
+ scope_field: ['2026-09-01', '2026-09-02'],
35
+ },
36
+ ],
37
+ });
38
+ });
39
+ test('resolves omitted scope_context to localDate and MERGES it with explicit localDate groups', () => {
40
+ const body = (0, materialized_reports_read_helpers_1.buildMaterializedReportReadBody)({
41
+ tenants: ['tenant-a'],
42
+ groups: [
43
+ { scope_field: ['2026-09-01'] },
44
+ { scope_field: ['2026-09-02', '2026-09-01'] },
45
+ {
46
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
47
+ scope_field: ['2026-09-03', '2026-09-01'],
48
+ },
49
+ ],
50
+ });
51
+ // ONE localDate group — an omitted context IS `localDate`; keeping it
52
+ // separate would make the backend double-count the shared documents.
53
+ expect(body).toEqual({
54
+ tenants: ['tenant-a'],
55
+ scope_groups: [
56
+ {
57
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
58
+ scope_field: ['2026-09-01', '2026-09-02', '2026-09-03'],
59
+ },
60
+ ],
61
+ });
62
+ });
63
+ test('trims tenants and scope values (whitespace-only is invalid)', () => {
64
+ const body = (0, materialized_reports_read_helpers_1.buildMaterializedReportReadBody)({
65
+ tenants: [' tenant-a ', 'tenant-a', 'tenant-a '],
66
+ groups: [{ scope_field: [' 2026-09-01 '] }],
67
+ });
68
+ expect(body).toEqual({
69
+ tenants: ['tenant-a'],
70
+ scope_groups: [
71
+ {
72
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
73
+ scope_field: ['2026-09-01'],
74
+ },
75
+ ],
76
+ });
77
+ });
78
+ test('returns null (never throws) for invalid input', () => {
79
+ const invalidInputs = [
80
+ null,
81
+ undefined,
82
+ {},
83
+ { tenants: [], groups: [{ scope_field: ['2026-09'] }] },
84
+ { tenants: [1], groups: [{ scope_field: ['2026-09'] }] },
85
+ { tenants: ['tenant-a'], groups: [] },
86
+ { tenants: ['tenant-a'], groups: [{ scope_field: [] }] },
87
+ { tenants: ['tenant-a'], groups: [{ scope_field: [''] }] },
88
+ { tenants: ['tenant-a'], groups: [{ scope_field: [' '] }] },
89
+ { tenants: [' '], groups: [{ scope_field: ['2026-09'] }] },
90
+ { tenants: ['tenant-a'], groups: [{ scope_field: [null] }] },
91
+ {
92
+ tenants: ['tenant-a'],
93
+ groups: [{ scope_context: 'nope', scope_field: ['2026-09'] }],
94
+ },
95
+ ];
96
+ invalidInputs.forEach((input) => {
97
+ expect((0, materialized_reports_read_helpers_1.buildMaterializedReportReadBody)(input)).toBeNull();
98
+ });
99
+ });
100
+ });
@@ -0,0 +1,275 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
+ const materialized_reports_scope_group_helpers_1 = require("../../helpers/materialized-reports/materialized-reports-scope-group-helpers");
5
+ /** Inclusive day-key range helper ('YYYY-MM-DD'). */
6
+ const range = (start, end) => {
7
+ const keys = [];
8
+ const [year, month, day] = start.split('-').map(Number);
9
+ const [endYear, endMonth, endDay] = end.split('-').map(Number);
10
+ const cursor = new Date(Date.UTC(year, month - 1, day));
11
+ const stop = new Date(Date.UTC(endYear, endMonth - 1, endDay));
12
+ while (cursor.getTime() <= stop.getTime()) {
13
+ keys.push(`${cursor.getUTCFullYear()}-${`${cursor.getUTCMonth() + 1}`.padStart(2, '0')}-${`${cursor.getUTCDate()}`.padStart(2, '0')}`);
14
+ cursor.setUTCDate(cursor.getUTCDate() + 1);
15
+ }
16
+ return keys;
17
+ };
18
+ const dayKeysOf = (groups) => (groups ?? [])
19
+ .filter((group) => group.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
20
+ .flatMap((group) => group.scope_field);
21
+ describe('compactMaterializedReportDayKeys', () => {
22
+ test('folds a full month into ONE localMonth value', () => {
23
+ expect((0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)(range('2026-09-01', '2026-09-30'))).toEqual([
24
+ { scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH, scope_field: ['2026-09'] },
25
+ ]);
26
+ });
27
+ test('folds a Sun–Sat week into ONE localWeek value', () => {
28
+ expect((0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)(range('2026-09-06', '2026-09-12'))).toEqual([
29
+ { scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK, scope_field: ['2026-09-06'] },
30
+ ]);
31
+ });
32
+ test('folds full quarters / semesters / years', () => {
33
+ expect((0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)(range('2026-07-01', '2026-09-30'))).toEqual([
34
+ { scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER, scope_field: ['2026-Q3'] },
35
+ ]);
36
+ expect((0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)(range('2026-01-01', '2026-06-30'))).toEqual([
37
+ { scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER, scope_field: ['2026-S1'] },
38
+ ]);
39
+ expect((0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)(range('2026-01-01', '2026-12-31'))).toEqual([
40
+ { scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR, scope_field: ['2026'] },
41
+ ]);
42
+ });
43
+ test('partial coverage keeps the leftovers as localDate values (weeks folded inside)', () => {
44
+ const groups = (0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)(range('2026-09-01', '2026-09-16'));
45
+ expect(groups).toEqual([
46
+ { scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK, scope_field: ['2026-09-06'] },
47
+ {
48
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
49
+ scope_field: [
50
+ '2026-09-01',
51
+ '2026-09-02',
52
+ '2026-09-03',
53
+ '2026-09-04',
54
+ '2026-09-05',
55
+ '2026-09-13',
56
+ '2026-09-14',
57
+ '2026-09-15',
58
+ '2026-09-16',
59
+ ],
60
+ },
61
+ ]);
62
+ });
63
+ test('two full months fold into two localMonth values', () => {
64
+ const groups = (0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)(range('2026-09-01', '2026-10-31'));
65
+ expect(groups).toEqual([
66
+ { scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH, scope_field: ['2026-09', '2026-10'] },
67
+ ]);
68
+ });
69
+ test('non-contiguous picks never compact', () => {
70
+ const groups = (0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)(['2026-09-01', '2026-09-09']);
71
+ expect(groups).toEqual([
72
+ { scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE, scope_field: ['2026-09-01', '2026-09-09'] },
73
+ ]);
74
+ });
75
+ test('returns null for malformed input', () => {
76
+ expect((0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)([])).toBeNull();
77
+ expect((0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)(['2026-13-01'])).toBeNull();
78
+ expect((0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)(['not-a-day'])).toBeNull();
79
+ expect((0, materialized_reports_scope_group_helpers_1.compactMaterializedReportDayKeys)(null)).toBeNull();
80
+ });
81
+ });
82
+ describe('buildMaterializedReportScopeGroups', () => {
83
+ test('no window → compaction only', () => {
84
+ expect((0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({ days: range('2026-09-01', '2026-09-30') })).toEqual([
85
+ { scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH, scope_field: ['2026-09'] },
86
+ ]);
87
+ });
88
+ test('RECURRING window → the same hours on every selected day (one localHour group)', () => {
89
+ const groups = (0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
90
+ days: ['2026-09-01', '2026-09-02'],
91
+ window: { fromHour: 18, toHour: 22, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.RECURRING },
92
+ });
93
+ expect(groups).toEqual([
94
+ {
95
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
96
+ scope_field: [
97
+ '2026-09-01 18',
98
+ '2026-09-01 19',
99
+ '2026-09-01 20',
100
+ '2026-09-01 21',
101
+ '2026-09-02 18',
102
+ '2026-09-02 19',
103
+ '2026-09-02 20',
104
+ '2026-09-02 21',
105
+ ],
106
+ },
107
+ ]);
108
+ });
109
+ test('SPAN window over a month → boundary hours + compacted middle days', () => {
110
+ const groups = (0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
111
+ days: range('2026-09-01', '2026-09-30'),
112
+ window: { fromHour: 18, toHour: 22, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.SPAN },
113
+ });
114
+ expect(groups).toEqual([
115
+ {
116
+ // full weeks inside Sep 2–29
117
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK,
118
+ scope_field: ['2026-09-06', '2026-09-13', '2026-09-20'],
119
+ },
120
+ {
121
+ // remaining full days
122
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
123
+ scope_field: ['2026-09-02', '2026-09-03', '2026-09-04', '2026-09-05', '2026-09-27', '2026-09-28', '2026-09-29'],
124
+ },
125
+ {
126
+ // start day 18:00 → business-day end (incl. post-midnight tail) +
127
+ // end day business-day start → 22:00
128
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
129
+ scope_field: [
130
+ '2026-09-01 00',
131
+ '2026-09-01 01',
132
+ '2026-09-01 02',
133
+ '2026-09-01 03',
134
+ '2026-09-01 04',
135
+ '2026-09-01 05',
136
+ '2026-09-01 18',
137
+ '2026-09-01 19',
138
+ '2026-09-01 20',
139
+ '2026-09-01 21',
140
+ '2026-09-01 22',
141
+ '2026-09-01 23',
142
+ '2026-09-30 06',
143
+ '2026-09-30 07',
144
+ '2026-09-30 08',
145
+ '2026-09-30 09',
146
+ '2026-09-30 10',
147
+ '2026-09-30 11',
148
+ '2026-09-30 12',
149
+ '2026-09-30 13',
150
+ '2026-09-30 14',
151
+ '2026-09-30 15',
152
+ '2026-09-30 16',
153
+ '2026-09-30 17',
154
+ '2026-09-30 18',
155
+ '2026-09-30 19',
156
+ '2026-09-30 20',
157
+ '2026-09-30 21',
158
+ ],
159
+ },
160
+ ]);
161
+ });
162
+ test('SPAN window over two adjacent days → boundary hours only', () => {
163
+ const groups = (0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
164
+ days: ['2026-09-10', '2026-09-11'],
165
+ window: { fromHour: 18, toHour: 22, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.SPAN },
166
+ });
167
+ const hours = groups?.find((group) => group.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR);
168
+ expect(hours?.scope_field).toHaveLength(28); // 12 start + 16 end
169
+ expect(hours?.scope_field).toContain('2026-09-10 18');
170
+ expect(hours?.scope_field).toContain('2026-09-10 00');
171
+ expect(hours?.scope_field).toContain('2026-09-11 21');
172
+ expect(groups).toHaveLength(1); // no middle days
173
+ });
174
+ test('SPAN with a NON-CONTIGUOUS day set → null (never read gap days)', () => {
175
+ expect((0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
176
+ days: ['2026-09-10', '2026-09-14'],
177
+ window: { fromHour: 18, toHour: 22, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.SPAN },
178
+ })).toBeNull();
179
+ // …sorted/gapped input is normalized first — still rejected
180
+ expect((0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
181
+ days: ['2026-09-14', '2026-09-10', '2026-09-10'],
182
+ window: { fromHour: 18, toHour: 22, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.SPAN },
183
+ })).toBeNull();
184
+ });
185
+ test('RECURRING tolerates gaps (same hours on every selected day)', () => {
186
+ const groups = (0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
187
+ days: ['2026-09-10', '2026-09-14'],
188
+ window: { fromHour: 18, toHour: 22, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.RECURRING },
189
+ });
190
+ expect(groups).toHaveLength(1);
191
+ expect(groups?.[0].scope_context).toBe(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR);
192
+ expect(groups?.[0].scope_field).toHaveLength(8); // 2 days × 4 hours
193
+ });
194
+ test('SPAN with a full-day boundary (x = 06:00) folds the start day into the day set', () => {
195
+ const groups = (0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
196
+ days: range('2026-09-01', '2026-09-30'),
197
+ window: { fromHour: 6, toHour: 22, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.SPAN },
198
+ });
199
+ // start day full → middle Sep 1–29 compacts; end day 06:00 → 22:00 stays hourly
200
+ const weeks = groups?.find((group) => group.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK);
201
+ expect(weeks?.scope_field).toEqual(['2026-09-06', '2026-09-13', '2026-09-20']);
202
+ const dates = groups?.find((group) => group.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
203
+ expect(dates?.scope_field).toContain('2026-09-01');
204
+ expect(dates?.scope_field).toContain('2026-09-29');
205
+ const hours = groups?.find((group) => group.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR);
206
+ expect(hours?.scope_field?.[0]).toBe('2026-09-30 06');
207
+ expect(hours?.scope_field).toHaveLength(16);
208
+ });
209
+ test('SPAN with a pre-06:00 start belongs to the previous business day tail', () => {
210
+ const groups = (0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
211
+ days: ['2026-09-10', '2026-09-11'],
212
+ window: { fromHour: 4, toHour: 22, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.SPAN },
213
+ });
214
+ const hours = groups?.find((group) => group.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR);
215
+ expect(hours?.scope_field).toContain('2026-09-09 04');
216
+ expect(hours?.scope_field).toContain('2026-09-09 05');
217
+ // Sep 10 becomes a FULL day in the middle
218
+ const dates = groups?.find((group) => group.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
219
+ expect(dates?.scope_field).toEqual(['2026-09-10']);
220
+ });
221
+ test('SPAN on a single day with an overnight window (18:00 → 2:00)', () => {
222
+ const groups = (0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
223
+ days: ['2026-09-10'],
224
+ window: { fromHour: 18, toHour: 2, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.SPAN },
225
+ });
226
+ expect(groups).toEqual([
227
+ {
228
+ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
229
+ // window order (chronological within the business day), not sorted
230
+ scope_field: [
231
+ '2026-09-10 18',
232
+ '2026-09-10 19',
233
+ '2026-09-10 20',
234
+ '2026-09-10 21',
235
+ '2026-09-10 22',
236
+ '2026-09-10 23',
237
+ '2026-09-10 00',
238
+ '2026-09-10 01',
239
+ ],
240
+ },
241
+ ]);
242
+ });
243
+ test('SPAN 06:00 → 06:00 on a single day is the full day', () => {
244
+ const groups = (0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
245
+ days: ['2026-09-10'],
246
+ window: { fromHour: 6, toHour: 6, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.SPAN },
247
+ });
248
+ expect(groups).toEqual([{ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE, scope_field: ['2026-09-10'] }]);
249
+ });
250
+ test('groups never overlap and everything requested is covered exactly once', () => {
251
+ const groups = (0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
252
+ days: range('2026-09-01', '2026-09-30'),
253
+ window: { fromHour: 18, toHour: 22, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.SPAN },
254
+ });
255
+ const all = (groups ?? []).flatMap((group) => group.scope_field);
256
+ // no duplicate values across groups
257
+ expect(new Set(all).size).toBe(all.length);
258
+ // the middles and the month itself are NOT both present (no double cover)
259
+ expect(all).not.toContain('2026-09');
260
+ });
261
+ test('returns null for malformed input / unservable windows', () => {
262
+ expect((0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)(null)).toBeNull();
263
+ expect((0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({ days: [] })).toBeNull();
264
+ expect((0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({ days: ['bad'] })).toBeNull();
265
+ expect((0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
266
+ days: ['2026-09-01'],
267
+ window: { fromHour: 24, toHour: 22, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.RECURRING },
268
+ })).toBeNull();
269
+ // recurring window crossing the 06:00 business-morning boundary is invalid
270
+ expect((0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
271
+ days: ['2026-09-01'],
272
+ window: { fromHour: 4, toHour: 8, kind: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.RECURRING },
273
+ })).toBeNull();
274
+ });
275
+ });
@@ -76,6 +76,17 @@ describe('materialized_tenant_sales_overview_reports schema', () => {
76
76
  it('exposes the agreed collection name', () => {
77
77
  expect(materialized_tenant_sales_overview_reports_1.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME).toBe('materialized_tenant_sales_overview_reports');
78
78
  });
79
+ it('declares one compound tenant index — brand reads pass explicit tenant lists ($in)', () => {
80
+ expect(materialized_tenant_sales_overview_reports_1.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_INDEXES).toEqual({
81
+ _id: { _id: 1 },
82
+ tenant_id__scope_context__scope_field__day_index: {
83
+ tenant_id: 1,
84
+ scope_context: 1,
85
+ scope_field: 1,
86
+ day_index: 1,
87
+ },
88
+ });
89
+ });
79
90
  it('validates a well-formed scope document and casts metrics to a Map', () => {
80
91
  const doc = new Model(validDocument());
81
92
  expect(doc.validateSync()).toBeUndefined();
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("../../constants");
4
+ const tenant_sales_overview_formatter_1 = require("../../helpers/materialized-reports/tenant-sales-overview-formatter");
5
+ /**
6
+ * Unit tests for the aggregated formatter — the frontend-facing half of the
7
+ * aggregated read. Records are hand-built (no database): the LIVE pipeline
8
+ * parity suite lives in operations-service
9
+ * (`src/tests/materialized-reports-aggregation.integration.test.ts`).
10
+ */
11
+ const orderMetric = (over = {}) => ({
12
+ count: 0,
13
+ gross_amount: 0,
14
+ discount_amount: 0,
15
+ discount_by_type: { special_discount: 0, loyalty_discount: 0, voucher_discount: 0 },
16
+ gst_amount: 0,
17
+ tax_amount: 0,
18
+ tip_amount: 0,
19
+ tax_invoice_provider_charges: 0,
20
+ delivery_charges: 0,
21
+ ...over,
22
+ });
23
+ const paymentMetric = (over = {}) => ({
24
+ payment_type: 'CASH',
25
+ count: 0,
26
+ gross_amount: 0,
27
+ amount_due: 0,
28
+ amount_paid: 0,
29
+ amount_refunded: 0,
30
+ card_charges: 0,
31
+ ...over,
32
+ });
33
+ const keyMetrics = (over = {}) => ({
34
+ TOTAL_SALES: { orders: orderMetric(), payments: [] },
35
+ AMOUNT_COLLECTED: { orders: orderMetric() },
36
+ CREDIT_BILLS: {
37
+ issued: { count: 0, amount: 0, settled_count: 0, settled_amount: 0 },
38
+ received: { count: 0, amount: 0 },
39
+ },
40
+ ...over,
41
+ });
42
+ const makeRecord = (over = {}) => ({
43
+ tenant_id: 'tenant-a',
44
+ brand_id: 'brand-1',
45
+ matched_documents: 3,
46
+ first_scope_field: '2026-09-01',
47
+ last_scope_field: '2026-09-03',
48
+ sale_type_key_filters: ['ORDER_STATUS', 'PLATFORM'],
49
+ sale_type_metrics: {
50
+ 'COMPLETED:APP_DINE-IN': keyMetrics({
51
+ TOTAL_SALES: {
52
+ orders: orderMetric({ count: 5, gross_amount: 250000 }),
53
+ payments: [paymentMetric({ count: 5, gross_amount: 250000, amount_paid: 250000 })],
54
+ },
55
+ AMOUNT_COLLECTED: { orders: orderMetric({ count: 4, gross_amount: 200000 }) },
56
+ CREDIT_BILLS: {
57
+ issued: { count: 1, amount: 100000, settled_count: 1, settled_amount: 100000 },
58
+ received: { count: 1, amount: 5000 },
59
+ },
60
+ }),
61
+ 'COMPLETED:POS_WALK-IN': keyMetrics({
62
+ TOTAL_SALES: {
63
+ orders: orderMetric({ count: 2, gross_amount: 80000 }),
64
+ payments: [paymentMetric({ count: 2, gross_amount: 80000, amount_paid: 80000 })],
65
+ },
66
+ }),
67
+ 'CANCELLED:APP_DINE-IN': keyMetrics({
68
+ TOTAL_SALES: {
69
+ orders: orderMetric({ count: 1, gross_amount: 5000 }),
70
+ payments: [paymentMetric({ count: 1, gross_amount: 5000 })],
71
+ },
72
+ }),
73
+ },
74
+ ...over,
75
+ });
76
+ describe('getFormattedTenantSalesOverviewAggregatedReporting', () => {
77
+ test('formats the page data from a single record (cancelled split, settlement line)', () => {
78
+ const out = (0, tenant_sales_overview_formatter_1.getFormattedTenantSalesOverviewAggregatedReporting)([makeRecord()]);
79
+ const page = out?.[0]?.[0];
80
+ expect(page?.tenant_ids).toEqual(['tenant-a']);
81
+ // headline = non-cancelled keys only
82
+ expect(page?.summary.count).toBe(7);
83
+ expect(page?.summary.gross_amount).toBe(3300);
84
+ expect(page?.amount_collected.count).toBe(4);
85
+ // cancelled facet split out
86
+ expect(page?.cancelled).toMatchObject({ count: 1, gross_amount: 50 });
87
+ // payments: CASH summed across non-cancelled keys + derived settlement line
88
+ expect(page?.payments.find((payment) => payment.payment_type === 'CASH')).toMatchObject({
89
+ count: 7,
90
+ gross_amount: 3300,
91
+ });
92
+ expect(page?.payments.find((payment) => payment.payment_type === 'CREDIT_SETTLEMENT')).toMatchObject({
93
+ count: 1,
94
+ gross_amount: 50,
95
+ derived: true,
96
+ });
97
+ // channels
98
+ expect(page?.channels.find((channel) => channel.platform === 'APP_DINE-IN')).toMatchObject({
99
+ count: 5,
100
+ gross_amount: 2500,
101
+ });
102
+ // credit-bill counters
103
+ expect(page?.credit_bills).toEqual({
104
+ issued: { count: 1, amount: 1000, settled_count: 1, settled_amount: 1000 },
105
+ received: { count: 1, amount: 50 },
106
+ outstanding_amount: 0,
107
+ });
108
+ // slice-derived sections are never available on aggregated records
109
+ expect(page?.weekday).toBeNull();
110
+ expect(page?.hourly).toBeNull();
111
+ });
112
+ test('platform / order-status filters prune client-side (zero refetch)', () => {
113
+ const out = (0, tenant_sales_overview_formatter_1.getFormattedTenantSalesOverviewAggregatedReporting)([makeRecord()], {
114
+ platforms: ['POS_WALK-IN'],
115
+ });
116
+ const page = out?.[0]?.[0];
117
+ expect(page?.summary.count).toBe(2);
118
+ expect(page?.channels).toHaveLength(1);
119
+ expect(page?.channels[0]).toMatchObject({ count: 2, gross_amount: 800 });
120
+ });
121
+ test('separate_view_per_tenant yields one page per tenant', () => {
122
+ const out = (0, tenant_sales_overview_formatter_1.getFormattedTenantSalesOverviewAggregatedReporting)([makeRecord(), makeRecord({ tenant_id: 'tenant-b' })], null, true);
123
+ expect(out?.[0]).toHaveLength(2);
124
+ expect(out?.[0]?.[0]?.tenant_ids).toEqual(['tenant-a']);
125
+ expect(out?.[0]?.[1]?.tenant_ids).toEqual(['tenant-b']);
126
+ });
127
+ test('mixed brands are rejected in the combined view but format per tenant', () => {
128
+ const records = [makeRecord(), makeRecord({ tenant_id: 'tenant-b', brand_id: 'brand-2' })];
129
+ expect((0, tenant_sales_overview_formatter_1.getFormattedTenantSalesOverviewAggregatedReporting)(records, null)).toBeNull();
130
+ const separate = (0, tenant_sales_overview_formatter_1.getFormattedTenantSalesOverviewAggregatedReporting)(records, null, true);
131
+ expect(separate?.[0]).toHaveLength(2);
132
+ });
133
+ test('an explicit day_indexes filter is rejected (must narrow the request scope)', () => {
134
+ expect((0, tenant_sales_overview_formatter_1.getFormattedTenantSalesOverviewAggregatedReporting)([makeRecord()], {
135
+ day_indexes: [constants_1.DAY_INDEX_ENUM.MON],
136
+ })).toBeNull();
137
+ });
138
+ test('empty input yields one empty page (never throws)', () => {
139
+ const out = (0, tenant_sales_overview_formatter_1.getFormattedTenantSalesOverviewAggregatedReporting)([]);
140
+ expect(out?.[0]).toHaveLength(1);
141
+ expect(out?.[0]?.[0]?.summary.count).toBe(0);
142
+ });
143
+ });
@@ -739,6 +739,28 @@ describe('subtract + versioned reconcile (iteration bump)', () => {
739
739
  expect(getKeyMetrics(previousIteration.snapshot, 'COMPLETED:POS_WALK-IN')?.[constants_1.REPORT_SALE_TYPE_ENUM.TOTAL_SALES].orders
740
740
  .count).toBe(5); // pre-write state
741
741
  });
742
+ it('prunes all-zero keys left behind by a full subtraction (reconcile artifacts)', () => {
743
+ // The month holds ONE key; subtracting that key's only day zeroes it out —
744
+ // the zombie must be pruned so period reads keep the same key set as day
745
+ // reads (which simply lose the key).
746
+ const month = report({
747
+ scopeContext: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
748
+ scopeField: '2026-09',
749
+ count: 2,
750
+ gross: 2000,
751
+ billReceived: 300,
752
+ });
753
+ const day = report({
754
+ scopeContext: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
755
+ scopeField: '2026-09-28',
756
+ count: 2,
757
+ gross: 2000,
758
+ billReceived: 300,
759
+ });
760
+ const result = expectNonNull((0, tenant_sales_overview_helpers_1.updateMaterializedTenantSalesOverviewReport)(month, { subtract: [day] }));
761
+ expect(getKeyMetrics(result, 'COMPLETED:POS_WALK-IN')).toBeUndefined();
762
+ expect(Object.keys(result.sale_type_metrics ?? {})).toHaveLength(0);
763
+ });
742
764
  it('replaces metrics wholesale (day/hour write) and bumps the iteration', () => {
743
765
  const existing = report({
744
766
  scopeContext: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const generate_order_1 = require("../helpers/order/generate-order");
4
+ const common_order_actions_1 = require("../helpers/store/common-order-actions");
5
+ // Corrupt value seen in production offline-order syncs:
6
+ // > BSON int32 max, so it serializes as a double and fails the orders collection validator.
7
+ const CORRUPT_NUM_OF_GUESTS = 3257330333;
8
+ function makeDineinDetails(num_of_guests) {
9
+ return {
10
+ floor_id: '68a58e189257360ef689d031',
11
+ table_id: '6890d1a9b0f67d68ffe71861',
12
+ table_name: 'F6',
13
+ num_of_guests,
14
+ start_time: new Date('2026-06-08T11:58:38.029Z'),
15
+ reservation_time: new Date('2026-06-08T11:58:38.029Z'),
16
+ courses: null,
17
+ };
18
+ }
19
+ describe('num_of_guests limiting', () => {
20
+ it('coerces and clamps corrupt guest counts into [1, MAX_NUM_OF_GUESTS]', () => {
21
+ expect((0, generate_order_1.getValidNumOfGuests)(CORRUPT_NUM_OF_GUESTS)).toBe(generate_order_1.MAX_NUM_OF_GUESTS);
22
+ expect((0, generate_order_1.getValidNumOfGuests)(Number.MAX_SAFE_INTEGER)).toBe(generate_order_1.MAX_NUM_OF_GUESTS);
23
+ expect((0, generate_order_1.getValidNumOfGuests)(generate_order_1.MAX_NUM_OF_GUESTS + 1)).toBe(generate_order_1.MAX_NUM_OF_GUESTS);
24
+ expect((0, generate_order_1.getValidNumOfGuests)(generate_order_1.MAX_NUM_OF_GUESTS)).toBe(generate_order_1.MAX_NUM_OF_GUESTS);
25
+ expect((0, generate_order_1.getValidNumOfGuests)(4)).toBe(4);
26
+ expect((0, generate_order_1.getValidNumOfGuests)(2.9)).toBe(2);
27
+ expect((0, generate_order_1.getValidNumOfGuests)(0)).toBe(1);
28
+ expect((0, generate_order_1.getValidNumOfGuests)(-5)).toBe(1);
29
+ expect((0, generate_order_1.getValidNumOfGuests)(NaN)).toBe(1);
30
+ expect((0, generate_order_1.getValidNumOfGuests)(undefined)).toBe(1);
31
+ });
32
+ it('sanitizes the order-level dinein_details', () => {
33
+ const details = (0, generate_order_1.generateOrderDineinDetails)(makeDineinDetails(CORRUPT_NUM_OF_GUESTS));
34
+ expect(details?.num_of_guests).toBe(generate_order_1.MAX_NUM_OF_GUESTS);
35
+ });
36
+ it('sanitizes top-level and iteration guests when generating an order', () => {
37
+ const order = {
38
+ _id: '6a26aebe66906f782bfb9c5c',
39
+ tenant_id: 'mixed-plate-mirpur',
40
+ platform: 'POS_DINE-IN',
41
+ order_number: 2575,
42
+ categories: [],
43
+ payments: [],
44
+ createdAt: new Date('2026-06-08T11:59:58.110Z'),
45
+ updatedAt: new Date('2026-06-08T11:59:58.110Z'),
46
+ dinein_details: makeDineinDetails(CORRUPT_NUM_OF_GUESTS),
47
+ iterations: [
48
+ {
49
+ categories: [],
50
+ consumer: { first_name: '', last_name: '', phone: '', email: '' },
51
+ platform: 'POS_DINE-IN',
52
+ order_status: 'PLACED',
53
+ updatedAt: new Date('2026-06-08T14:49:41.749Z'),
54
+ dinein_details: makeDineinDetails(CORRUPT_NUM_OF_GUESTS),
55
+ },
56
+ ],
57
+ };
58
+ const generated = (0, generate_order_1.generateOrderOnCreate)(order, true);
59
+ expect(generated.dinein_details.num_of_guests).toBe(generate_order_1.MAX_NUM_OF_GUESTS);
60
+ expect(generated.iterations[0].dinein_details.num_of_guests).toBe(generate_order_1.MAX_NUM_OF_GUESTS);
61
+ });
62
+ it('sanitizes guests copied into iteration snapshots', () => {
63
+ const iteration = (0, common_order_actions_1.generateOrderIteration)({
64
+ dinein_details: makeDineinDetails(CORRUPT_NUM_OF_GUESTS),
65
+ categories: [],
66
+ consumer: { first_name: '', last_name: '', phone: '', email: '' },
67
+ platform: 'POS_DINE-IN',
68
+ updatedAt: new Date('2026-06-08T14:49:41.749Z'),
69
+ });
70
+ expect(iteration.dinein_details.num_of_guests).toBe(generate_order_1.MAX_NUM_OF_GUESTS);
71
+ });
72
+ it('sanitizes guests in outgoing cent-converted payloads', () => {
73
+ const payload = (0, common_order_actions_1.getOrderWithPrice)({ dinein_details: makeDineinDetails(CORRUPT_NUM_OF_GUESTS) }, true);
74
+ expect(payload.dinein_details.num_of_guests).toBe(generate_order_1.MAX_NUM_OF_GUESTS);
75
+ });
76
+ });