@managesome/eposmatic-toolkit 0.4.4 → 0.4.7

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 (60) hide show
  1. package/esm/constants/REPORT_CALENDAR.js +47 -0
  2. package/esm/constants/index.js +1 -0
  3. package/esm/helpers/materialized-reports/index.js +2 -0
  4. package/esm/helpers/materialized-reports/materialized-reports-read-helpers.js +20 -21
  5. package/esm/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +281 -0
  6. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +473 -0
  7. package/esm/helpers/materialized-reports/tenant-sales-overview-formatter.js +126 -7
  8. package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +47 -1
  9. package/esm/helpers/order/generate-order.js +11 -0
  10. package/esm/helpers/store/common-order-actions.js +7 -3
  11. package/esm/helpers/types/index.js +1 -0
  12. package/esm/helpers/types/materialized-reports-apis.js +1 -1
  13. package/esm/helpers/types/report-calendar.js +1 -0
  14. package/lib/constants/REPORT_CALENDAR.d.ts +31 -0
  15. package/lib/constants/REPORT_CALENDAR.js +50 -0
  16. package/lib/constants/index.d.ts +1 -0
  17. package/lib/constants/index.js +1 -0
  18. package/lib/helpers/materialized-reports/index.d.ts +2 -0
  19. package/lib/helpers/materialized-reports/index.js +2 -0
  20. package/lib/helpers/materialized-reports/materialized-reports-read-helpers.d.ts +20 -21
  21. package/lib/helpers/materialized-reports/materialized-reports-read-helpers.js +20 -21
  22. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.d.ts +58 -0
  23. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +286 -0
  24. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +25 -0
  25. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +479 -0
  26. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.d.ts +24 -0
  27. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.js +127 -7
  28. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +47 -1
  29. package/lib/helpers/order/generate-order.d.ts +2 -0
  30. package/lib/helpers/order/generate-order.js +13 -0
  31. package/lib/helpers/store/common-order-actions.js +7 -3
  32. package/lib/helpers/types/index.d.ts +1 -0
  33. package/lib/helpers/types/index.js +1 -0
  34. package/lib/helpers/types/materialized-reports-apis.d.ts +117 -25
  35. package/lib/helpers/types/materialized-reports-apis.js +1 -0
  36. package/lib/helpers/types/report-calendar.d.ts +20 -0
  37. package/lib/helpers/types/report-calendar.js +2 -0
  38. package/lib/schemas/models/internal_invoices.d.ts +3 -3
  39. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +6 -6
  40. package/lib/schemas/models/orders.d.ts +692 -692
  41. package/lib/schemas/models/tenant_purchase_orders.d.ts +90 -90
  42. package/lib/schemas/models/tenants.d.ts +9 -9
  43. package/lib/schemas/models/warehouse_communications.d.ts +3 -3
  44. package/lib/schemas/models/warehouse_internal_movements.d.ts +48 -48
  45. package/lib/schemas/models/warehouse_purchase_order_rfqs.d.ts +63 -63
  46. package/lib/schemas/models/warehouse_purchase_orders.d.ts +96 -96
  47. package/lib/schemas/models/warehouse_sale_invoices.d.ts +3 -3
  48. package/lib/schemas/models/warehouse_sale_order_rfqs.d.ts +69 -69
  49. package/lib/schemas/models/warehouse_sale_orders.d.ts +45 -45
  50. package/lib/schemas/models/warehouses.d.ts +6 -6
  51. package/lib/tests/materialized-reports/materialized-reports-scope-group-helpers.test.d.ts +1 -0
  52. package/lib/tests/materialized-reports/materialized-reports-scope-group-helpers.test.js +275 -0
  53. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.d.ts +1 -0
  54. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.js +166 -0
  55. package/lib/tests/materialized-reports/tenant-sales-overview-formatter-aggregated.test.d.ts +1 -0
  56. package/lib/tests/materialized-reports/tenant-sales-overview-formatter-aggregated.test.js +143 -0
  57. package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +22 -0
  58. package/lib/tests/num-of-guests-limit.test.d.ts +1 -0
  59. package/lib/tests/num-of-guests-limit.test.js +76 -0
  60. package/package.json +1 -1
@@ -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
+ });
@@ -0,0 +1,166 @@
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
+ });
@@ -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 {};