@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.
- package/esm/constants/MATERIALIZED_REPORTS.js +33 -0
- package/esm/constants/REPORT_CALENDAR.js +47 -0
- package/esm/constants/index.js +1 -0
- package/esm/helpers/materialized-reports/index.js +2 -0
- package/esm/helpers/materialized-reports/materialized-credit-bills-helpers.js +982 -0
- package/esm/helpers/materialized-reports/materialized-reports-read-helpers.js +1 -76
- package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +716 -0
- package/esm/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +327 -41
- package/esm/helpers/materialized-reports/tenant-sales-overview-formatter.js +72 -11
- package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +118 -235
- package/esm/helpers/types/index.js +1 -0
- package/esm/schemas/models/index.js +1 -0
- package/esm/schemas/models/materialized_credit_bills.js +265 -0
- package/esm/schemas/models/materialized_tenant_reports_iterations.js +13 -0
- package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +39 -97
- package/esm/schemas/types/index.js +1 -0
- package/esm/schemas/types/materialized_credit_bills.js +1 -0
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +28 -2
- package/lib/constants/MATERIALIZED_REPORTS.js +34 -1
- package/lib/constants/REPORT_CALENDAR.d.ts +31 -0
- package/lib/constants/REPORT_CALENDAR.js +50 -0
- package/lib/constants/index.d.ts +1 -0
- package/lib/constants/index.js +1 -0
- package/lib/helpers/materialized-reports/index.d.ts +2 -0
- package/lib/helpers/materialized-reports/index.js +2 -0
- package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.d.ts +307 -0
- package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.js +1003 -0
- package/lib/helpers/materialized-reports/materialized-reports-read-helpers.d.ts +0 -30
- package/lib/helpers/materialized-reports/materialized-reports-read-helpers.js +0 -76
- package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +59 -0
- package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +723 -0
- package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.d.ts +66 -8
- package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +326 -40
- package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.d.ts +18 -2
- package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.js +72 -11
- package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.d.ts +46 -37
- package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +119 -238
- package/lib/helpers/types/index.d.ts +1 -0
- package/lib/helpers/types/index.js +1 -0
- package/lib/helpers/types/materialized-reports-apis.d.ts +46 -17
- package/lib/helpers/types/report-calendar.d.ts +20 -0
- package/lib/helpers/types/report-calendar.js +2 -0
- package/lib/schemas/models/index.d.ts +1 -0
- package/lib/schemas/models/index.js +1 -0
- package/lib/schemas/models/materialized_credit_bills.d.ts +337 -0
- package/lib/schemas/models/materialized_credit_bills.js +268 -0
- package/lib/schemas/models/materialized_tenant_reports_iterations.d.ts +20 -6
- package/lib/schemas/models/materialized_tenant_reports_iterations.js +14 -1
- package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +37 -165
- package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +40 -98
- package/lib/schemas/types/index.d.ts +1 -0
- package/lib/schemas/types/index.js +1 -0
- package/lib/schemas/types/materialized_credit_bills.d.ts +139 -0
- package/lib/schemas/types/materialized_credit_bills.js +2 -0
- package/lib/schemas/types/materialized_tenant_sales_overview_reports.d.ts +32 -77
- package/lib/tests/materialized-reports/materialized-credit-bills-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-credit-bills-helpers.test.js +715 -0
- package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.js +262 -0
- package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-schema.test.js +9 -0
- package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +23 -24
- package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.js +327 -22
- package/lib/tests/materialized-reports/tenant-sales-overview-formatter-aggregated.test.js +27 -2
- package/lib/tests/materialized-reports/tenant-sales-overview-formatter.test.js +12 -27
- package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +79 -146
- package/package.json +1 -1
- package/lib/tests/materialized-reports/materialized-reports-read-helpers.test.js +0 -100
- /package/{lib/tests/materialized-reports/materialized-reports-read-helpers.test.d.ts → esm/helpers/types/report-calendar.js} +0 -0
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildMaterializedReportScopeGroupsFromSelection = exports.validateMaterializedReportSelectionInput = exports.isValidBusinessHourWindow = exports.getDayKeysFromCalendarSelection = exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN = void 0;
|
|
4
|
+
const ENUMS_1 = require("../../constants/ENUMS");
|
|
5
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
6
|
+
const REPORT_CALENDAR_1 = require("../../constants/REPORT_CALENDAR");
|
|
7
|
+
const materialized_reports_scope_group_helpers_1 = require("./materialized-reports-scope-group-helpers");
|
|
8
|
+
const scope_helpers_1 = require("./scope-helpers");
|
|
9
|
+
/**
|
|
10
|
+
* Calendar-selection DECODER for the materialized report READ APIs — the
|
|
11
|
+
* backend-side twin of the calendar: it maps one `IReportCalendarSelection`
|
|
12
|
+
* to the exact `scope_groups` a read request needs, so clients ship the
|
|
13
|
+
* selection (a few hundred bytes) instead of enumerating tens of thousands
|
|
14
|
+
* of scope values.
|
|
15
|
+
*
|
|
16
|
+
* Semantics (identical to what the frontend compiler produced before this
|
|
17
|
+
* moved server-side):
|
|
18
|
+
* - LIVE reads ONLY the running business day — all of its exact `localHour`
|
|
19
|
+
* labels (the calendar commits the running day as `rangeStart`); summed by
|
|
20
|
+
* the backend it yields the running day's metrics so far, finalized at day
|
|
21
|
+
* close;
|
|
22
|
+
* - day-based selections compile through the SMART compiler
|
|
23
|
+
* (`buildMaterializedReportScopeGroups`): contiguous day runs fold into the
|
|
24
|
+
* LARGEST exact periods they fully cover (week → month → quarter → semester
|
|
25
|
+
* → year; anything left stays `localDate`);
|
|
26
|
+
* - a time-of-day window compiles to `localHour` labels: a CONTINUOUS SPAN on
|
|
27
|
+
* range-like views (toggle), the SAME hours on each selected day otherwise
|
|
28
|
+
* (dedicated window view / discrete selections);
|
|
29
|
+
* - the weekday refinement degenerates the read to standalone day documents
|
|
30
|
+
* (`localDate` / `localHour` docs carry `day_index`; period docs do not):
|
|
31
|
+
* it filters the effective day set BEFORE hour-window expansion;
|
|
32
|
+
* - period views read their exact period keys directly (week starts, ISO week
|
|
33
|
+
* labels, months, quarters, semesters, years).
|
|
34
|
+
*
|
|
35
|
+
* Validation: the raw selection is validated FIRST
|
|
36
|
+
* (`validateMaterializedReportSelectionInput`) — every date/period/time key
|
|
37
|
+
* must be well-formed (real calendar dates, exact period keys, whole-hour
|
|
38
|
+
* times) and the raw day span is bounded by
|
|
39
|
+
* `MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN`; malformed or oversized
|
|
40
|
+
* selections return `null` before any walk allocates anything.
|
|
41
|
+
*
|
|
42
|
+
* Returns `null` (never throws) when the selection cannot be served (missing
|
|
43
|
+
* range on a range view, malformed keys, an invalid refinement window, or an
|
|
44
|
+
* unservable empty result). Group values are exact; empty groups are dropped.
|
|
45
|
+
*/
|
|
46
|
+
/** Range-like views whose toggle window is a CONTINUOUS span. */
|
|
47
|
+
const SPAN_VIEWS = new Set([
|
|
48
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DATE_RANGE,
|
|
49
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.WEEK,
|
|
50
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.WEEK_OF_YEAR,
|
|
51
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.MONTH,
|
|
52
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.QUARTER,
|
|
53
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.SEMESTER,
|
|
54
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.YEAR,
|
|
55
|
+
]);
|
|
56
|
+
/** Views whose day set is one contiguous `rangeStart … rangeEnd` walk. */
|
|
57
|
+
const RANGE_LIKE_VIEWS = new Set([
|
|
58
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DATE_RANGE,
|
|
59
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DATE_RANGE_TIME_WINDOW,
|
|
60
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.WEEK,
|
|
61
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.WEEK_OF_YEAR,
|
|
62
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.QUARTER,
|
|
63
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.SEMESTER,
|
|
64
|
+
]);
|
|
65
|
+
/**
|
|
66
|
+
* Upper bound on the raw day span one selection may cover (~10 years).
|
|
67
|
+
* Bounds every date-walk expansion in the decoder; read services surface an
|
|
68
|
+
* over-bound selection as `selectionTooLarge` (400 "narrow the selection")
|
|
69
|
+
* BEFORE any decoding work happens.
|
|
70
|
+
*/
|
|
71
|
+
exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN = 3660;
|
|
72
|
+
/* ==================== Date-key primitives ==================== */
|
|
73
|
+
// Local 'YYYY-MM-DD' key for a Date (NOT UTC — the tenant's local day).
|
|
74
|
+
const formatLocalDateKey = (date) => {
|
|
75
|
+
const year = date.getFullYear();
|
|
76
|
+
const month = `${date.getMonth() + 1}`.padStart(2, '0');
|
|
77
|
+
const day = `${date.getDate()}`.padStart(2, '0');
|
|
78
|
+
return `${year}-${month}-${day}`;
|
|
79
|
+
};
|
|
80
|
+
const getDateFromKey = (key) => {
|
|
81
|
+
const [year, month, day] = key.split('-').map(Number);
|
|
82
|
+
return new Date(year, month - 1, day);
|
|
83
|
+
};
|
|
84
|
+
const addDaysToKey = (dateKey, days) => {
|
|
85
|
+
const date = getDateFromKey(dateKey);
|
|
86
|
+
date.setDate(date.getDate() + days);
|
|
87
|
+
return formatLocalDateKey(date);
|
|
88
|
+
};
|
|
89
|
+
// 'YYYY-Qn' quarter start ('YYYY-MM-01') — accepts a quarter key
|
|
90
|
+
const getQuarterStartKey = (quarterKey) => {
|
|
91
|
+
const [year, quarter] = quarterKey.split('-');
|
|
92
|
+
const month = (Number(quarter.slice(1)) - 1) * 3 + 1;
|
|
93
|
+
return `${year}-${`${month}`.padStart(2, '0')}-01`;
|
|
94
|
+
};
|
|
95
|
+
// 'YYYY-Qn' quarter end (last day of the quarter) — accepts a quarter key
|
|
96
|
+
const getQuarterEndKey = (quarterKey) => {
|
|
97
|
+
const [year, quarter] = quarterKey.split('-');
|
|
98
|
+
return formatLocalDateKey(new Date(Number(year), Number(quarter.slice(1)) * 3, 0));
|
|
99
|
+
};
|
|
100
|
+
// 'YYYY-Sn' semester start ('YYYY-MM-01') — accepts a semester key
|
|
101
|
+
const getSemesterStartKey = (semesterKey) => {
|
|
102
|
+
const [year, semester] = semesterKey.split('-');
|
|
103
|
+
return `${year}-${semester === 'S1' ? '01' : '07'}-01`;
|
|
104
|
+
};
|
|
105
|
+
// 'YYYY-Sn' semester end — accepts a semester key
|
|
106
|
+
const getSemesterEndKey = (semesterKey) => {
|
|
107
|
+
const [year, semester] = semesterKey.split('-');
|
|
108
|
+
return semester === 'S1' ? `${year}-06-30` : `${year}-12-31`;
|
|
109
|
+
};
|
|
110
|
+
// Monday start of the ISO week containing the given day key
|
|
111
|
+
const getIsoWeekStartKey = (dateKey) => {
|
|
112
|
+
const date = getDateFromKey(dateKey);
|
|
113
|
+
const offset = (date.getDay() + 6) % 7; // days since Monday
|
|
114
|
+
date.setDate(date.getDate() - offset);
|
|
115
|
+
return formatLocalDateKey(date);
|
|
116
|
+
};
|
|
117
|
+
// 'YYYY-Wnn' ISO label of a day key — `YYYY` is the ISO week-year, so
|
|
118
|
+
// boundary weeks (e.g. 2027-01-01 → '2026-W53') label correctly.
|
|
119
|
+
const getIsoWeekKey = (dateKey) => (0, scope_helpers_1.getMaterializedPeriodScopeFields)(dateKey)?.[MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK_OF_YEAR] ?? null;
|
|
120
|
+
// Monday day key of an ISO week label ('YYYY-Wnn'); null for malformed input
|
|
121
|
+
const getIsoWeekStartKeyFromLabel = (weekKey) => {
|
|
122
|
+
const match = /^(\d{4})-W(\d{2})$/.exec(weekKey);
|
|
123
|
+
if (!match)
|
|
124
|
+
return null;
|
|
125
|
+
const week = Number(match[2]);
|
|
126
|
+
if (week < 1 || week > 53)
|
|
127
|
+
return null;
|
|
128
|
+
// ISO week 1 is the week containing Jan 4 — its Monday anchors numbering
|
|
129
|
+
const weekOneMonday = getDateFromKey(getIsoWeekStartKey(`${match[1]}-01-04`));
|
|
130
|
+
weekOneMonday.setDate(weekOneMonday.getDate() + (week - 1) * 7);
|
|
131
|
+
return formatLocalDateKey(weekOneMonday);
|
|
132
|
+
};
|
|
133
|
+
/* ==================== Weekday refinement ==================== */
|
|
134
|
+
/**
|
|
135
|
+
* Active weekday refinement — `null` when unset (= every day). The decoder,
|
|
136
|
+
* the calendar's grid highlight and the selection summary all use the SAME
|
|
137
|
+
* effective-day semantics, so the UI can never claim days the report won't
|
|
138
|
+
* read.
|
|
139
|
+
*/
|
|
140
|
+
const getActiveWeekdayFilter = (weekdays) => {
|
|
141
|
+
if (!Array.isArray(weekdays) || weekdays.length === 0)
|
|
142
|
+
return null;
|
|
143
|
+
if (weekdays.length >= 7)
|
|
144
|
+
return null;
|
|
145
|
+
return weekdays;
|
|
146
|
+
};
|
|
147
|
+
/** Whether a day key is kept by the active weekday refinement. */
|
|
148
|
+
const isDayKeptByWeekdayFilter = (dateKey, weekdays) => {
|
|
149
|
+
if (weekdays == null)
|
|
150
|
+
return true;
|
|
151
|
+
const dayIndex = (0, scope_helpers_1.getDayIndexFromLocalDate)(dateKey);
|
|
152
|
+
return dayIndex != null && weekdays.includes(dayIndex);
|
|
153
|
+
};
|
|
154
|
+
/* ==================== Effective day set ==================== */
|
|
155
|
+
// All day keys covered by the current selection — the weekday refinement is
|
|
156
|
+
// applied INSIDE the walks so the cap counts the EFFECTIVE day set (a
|
|
157
|
+
// weekday-filtered multi-year selection stays servable while its raw day
|
|
158
|
+
// count exceeds the bound). Every walk is iteration-guarded; an over-bound
|
|
159
|
+
// expansion yields `null` (the decoder maps it to `[]` — unservable; the
|
|
160
|
+
// selection validator classifies it as `tooLarge`).
|
|
161
|
+
const getBaseSelectedDayKeys = (selection, weekdays) => {
|
|
162
|
+
const { view, rangeStart, rangeEnd, selectedDates, selectedPeriods } = selection;
|
|
163
|
+
const keys = [];
|
|
164
|
+
const appendWithinCap = (list) => {
|
|
165
|
+
for (const key of list) {
|
|
166
|
+
keys.push(key);
|
|
167
|
+
if (keys.length > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN)
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
return true;
|
|
171
|
+
};
|
|
172
|
+
const expandRange = (start, end) => {
|
|
173
|
+
const expanded = [];
|
|
174
|
+
let cursor = start;
|
|
175
|
+
while (cursor <= end) {
|
|
176
|
+
if (weekdays == null || isDayKeptByWeekdayFilter(cursor, weekdays)) {
|
|
177
|
+
expanded.push(cursor);
|
|
178
|
+
if (expanded.length > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN)
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
cursor = addDaysToKey(cursor, 1);
|
|
182
|
+
}
|
|
183
|
+
return expanded;
|
|
184
|
+
};
|
|
185
|
+
if (RANGE_LIKE_VIEWS.has(view)) {
|
|
186
|
+
if (!rangeStart)
|
|
187
|
+
return [];
|
|
188
|
+
return expandRange(rangeStart, rangeEnd ?? rangeStart);
|
|
189
|
+
}
|
|
190
|
+
if (view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.MULTI_DATE) {
|
|
191
|
+
if (selectedDates.length > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN)
|
|
192
|
+
return null;
|
|
193
|
+
return weekdays == null ? selectedDates : selectedDates.filter((key) => isDayKeptByWeekdayFilter(key, weekdays));
|
|
194
|
+
}
|
|
195
|
+
if (view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_WEEKS) {
|
|
196
|
+
for (const weekStart of selectedPeriods) {
|
|
197
|
+
const days = expandRange(weekStart, addDaysToKey(weekStart, 6));
|
|
198
|
+
if (days == null || !appendWithinCap(days))
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
return keys;
|
|
202
|
+
}
|
|
203
|
+
if (view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_WEEKS_OF_YEAR) {
|
|
204
|
+
for (const weekKey of selectedPeriods) {
|
|
205
|
+
const weekStart = getIsoWeekStartKeyFromLabel(weekKey);
|
|
206
|
+
if (weekStart == null)
|
|
207
|
+
continue;
|
|
208
|
+
const days = expandRange(weekStart, addDaysToKey(weekStart, 6));
|
|
209
|
+
if (days == null || !appendWithinCap(days))
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
return keys;
|
|
213
|
+
}
|
|
214
|
+
if (view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.MONTH || view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.YEAR) {
|
|
215
|
+
if (!rangeStart)
|
|
216
|
+
return [];
|
|
217
|
+
return expandRange(rangeStart, rangeEnd ?? rangeStart);
|
|
218
|
+
}
|
|
219
|
+
if (view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_MONTHS) {
|
|
220
|
+
for (const monthKey of selectedPeriods) {
|
|
221
|
+
const [year, month] = monthKey.split('-').map(Number);
|
|
222
|
+
const days = expandRange(`${monthKey}-01`, formatLocalDateKey(new Date(year, month, 0)));
|
|
223
|
+
if (days == null || !appendWithinCap(days))
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
return keys;
|
|
227
|
+
}
|
|
228
|
+
if (view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_QUARTERS) {
|
|
229
|
+
for (const quarterKey of selectedPeriods) {
|
|
230
|
+
const days = expandRange(getQuarterStartKey(quarterKey), getQuarterEndKey(quarterKey));
|
|
231
|
+
if (days == null || !appendWithinCap(days))
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
return keys;
|
|
235
|
+
}
|
|
236
|
+
if (view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_SEMESTERS) {
|
|
237
|
+
for (const semesterKey of selectedPeriods) {
|
|
238
|
+
const days = expandRange(getSemesterStartKey(semesterKey), getSemesterEndKey(semesterKey));
|
|
239
|
+
if (days == null || !appendWithinCap(days))
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
return keys;
|
|
243
|
+
}
|
|
244
|
+
if (view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_YEARS) {
|
|
245
|
+
for (const yearKey of selectedPeriods) {
|
|
246
|
+
const days = expandRange(`${yearKey}-01-01`, `${yearKey}-12-31`);
|
|
247
|
+
if (days == null || !appendWithinCap(days))
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
return keys;
|
|
251
|
+
}
|
|
252
|
+
return [];
|
|
253
|
+
};
|
|
254
|
+
/**
|
|
255
|
+
* All day keys covered by a selection — keeps only days that match the
|
|
256
|
+
* active weekday refinement, so non-matching days never contribute to
|
|
257
|
+
* summaries, DateContext bounds, or requests.
|
|
258
|
+
*/
|
|
259
|
+
const getDayKeysFromCalendarSelection = (selection) => {
|
|
260
|
+
try {
|
|
261
|
+
if (!(0, exports.validateMaterializedReportSelectionInput)(selection).ok)
|
|
262
|
+
return [];
|
|
263
|
+
// The base walk already applies the weekday refinement, so its cap counts
|
|
264
|
+
// the EFFECTIVE day set — `null` = over-bound → unservable.
|
|
265
|
+
return getBaseSelectedDayKeys(selection, getActiveWeekdayFilter(selection.weekdays)) ?? [];
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
return [];
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
exports.getDayKeysFromCalendarSelection = getDayKeysFromCalendarSelection;
|
|
272
|
+
/* ==================== Hour-window validity ==================== */
|
|
273
|
+
/**
|
|
274
|
+
* Whether an hour window `[fromHour, toHour)` fits one business day
|
|
275
|
+
* (06:00 → 06:00) — mirrors the toolkit's materialized window rule
|
|
276
|
+
* (`getMaterializedHourWindowScopeFields`): whole-hour buckets, end-exclusive
|
|
277
|
+
* (18 → 20 = hours 18, 19), midnight wrap allowed (22 → 2), but never a walk
|
|
278
|
+
* crossing the 06:00 business-morning boundary (4 → 8 is invalid). The
|
|
279
|
+
* calendar's time field uses this to disable unservable picker options, so an
|
|
280
|
+
* invalid window can never be composed for the read request.
|
|
281
|
+
*/
|
|
282
|
+
const isValidBusinessHourWindow = (fromHour, toHour) => {
|
|
283
|
+
if (!Number.isInteger(fromHour) || fromHour < 0 || fromHour > 23) {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
if (!Number.isInteger(toHour) || toHour < 0 || toHour > 23)
|
|
287
|
+
return false;
|
|
288
|
+
const hourCount = (((toHour - fromHour) % 24) + 24) % 24;
|
|
289
|
+
if (hourCount === 0)
|
|
290
|
+
return false; // ambiguous empty window
|
|
291
|
+
const startPosition = (fromHour + 18) % 24;
|
|
292
|
+
return startPosition + hourCount <= 24;
|
|
293
|
+
};
|
|
294
|
+
exports.isValidBusinessHourWindow = isValidBusinessHourWindow;
|
|
295
|
+
/* ==================== Decode internals ==================== */
|
|
296
|
+
/** `'HH:mm'` → hour number — whole `:00` hours only (minute keys are not servable). */
|
|
297
|
+
const hourOf = (time) => {
|
|
298
|
+
if (time == null)
|
|
299
|
+
return null;
|
|
300
|
+
const match = /^(\d{2}):(\d{2})$/.exec(time);
|
|
301
|
+
if (match == null || match[2] !== '00')
|
|
302
|
+
return null;
|
|
303
|
+
const hour = Number(match[1]);
|
|
304
|
+
return hour >= 0 && hour <= 23 ? hour : null;
|
|
305
|
+
};
|
|
306
|
+
/** Active time-of-day window (both ends set and servable) — else `null`. */
|
|
307
|
+
const resolveTimeOfDayWindow = (selection) => {
|
|
308
|
+
const fromHour = hourOf(selection.timeStart);
|
|
309
|
+
const toHour = hourOf(selection.timeEnd);
|
|
310
|
+
if (fromHour == null || toHour == null)
|
|
311
|
+
return null;
|
|
312
|
+
if (!(0, exports.isValidBusinessHourWindow)(fromHour, toHour))
|
|
313
|
+
return null;
|
|
314
|
+
return { fromHour, toHour };
|
|
315
|
+
};
|
|
316
|
+
const periodKeyOf = (dayKey, scope) => {
|
|
317
|
+
const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(dayKey);
|
|
318
|
+
if (fields == null)
|
|
319
|
+
return null;
|
|
320
|
+
switch (scope) {
|
|
321
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK:
|
|
322
|
+
return fields[MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK];
|
|
323
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH:
|
|
324
|
+
return fields[MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH];
|
|
325
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER:
|
|
326
|
+
return fields[MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER];
|
|
327
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER:
|
|
328
|
+
return fields[MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER];
|
|
329
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR:
|
|
330
|
+
return fields[MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR];
|
|
331
|
+
default:
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
/** Groups with at least one value — dropped groups leave `null`. */
|
|
336
|
+
const usableGroups = (groups) => {
|
|
337
|
+
const usable = (groups ?? []).filter((group) => group.scope_field.length > 0);
|
|
338
|
+
return usable.length > 0 ? usable : null;
|
|
339
|
+
};
|
|
340
|
+
/**
|
|
341
|
+
* Groups with the exact period values of one scope (period views) — the
|
|
342
|
+
* values are canonicalized (first-occurrence order) so duplicate picks never
|
|
343
|
+
* reach the read as duplicate `$in` entries.
|
|
344
|
+
*/
|
|
345
|
+
const scopedGroups = (scope, values) => usableGroups([{ scope_context: scope, scope_field: [...new Set(values)] }]);
|
|
346
|
+
/** Business-hour order of one day's hour buckets — 06…23 then 00…05. */
|
|
347
|
+
const BUSINESS_DAY_HOURS = [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5];
|
|
348
|
+
/**
|
|
349
|
+
* Every exact `localHour` label of one business day — the LIVE read. Summed
|
|
350
|
+
* by the backend it yields the running day's metrics so far (the day close
|
|
351
|
+
* finalizes them); a closed day's hours sum to its `localDate` totals.
|
|
352
|
+
*/
|
|
353
|
+
const runningDayHourLabels = (dayKey) => BUSINESS_DAY_HOURS.map((hour) => (0, scope_helpers_1.getMaterializedHourScopeField)(dayKey, hour)).filter((label) => label != null);
|
|
354
|
+
/** Week-start day keys from `startKey` to `endKey`, stepping 7 days. */
|
|
355
|
+
const enumerateWeekStarts = (startKey, endKey) => {
|
|
356
|
+
const keys = [];
|
|
357
|
+
let cursor = startKey;
|
|
358
|
+
while (cursor <= endKey) {
|
|
359
|
+
keys.push(cursor);
|
|
360
|
+
if (keys.length > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN)
|
|
361
|
+
return [];
|
|
362
|
+
cursor = addDaysToKey(cursor, 7);
|
|
363
|
+
}
|
|
364
|
+
return keys;
|
|
365
|
+
};
|
|
366
|
+
/** ISO week labels ('YYYY-Wnn') covering `startDayKey` … `endDayKey`. */
|
|
367
|
+
const enumerateIsoWeeks = (startDayKey, endDayKey) => {
|
|
368
|
+
const keys = [];
|
|
369
|
+
const end = getIsoWeekStartKey(endDayKey);
|
|
370
|
+
let cursor = getIsoWeekStartKey(startDayKey);
|
|
371
|
+
while (cursor <= end) {
|
|
372
|
+
const label = getIsoWeekKey(cursor);
|
|
373
|
+
if (label != null)
|
|
374
|
+
keys.push(label);
|
|
375
|
+
if (keys.length > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN)
|
|
376
|
+
return [];
|
|
377
|
+
cursor = addDaysToKey(cursor, 7);
|
|
378
|
+
}
|
|
379
|
+
return keys;
|
|
380
|
+
};
|
|
381
|
+
/** 'YYYY-MM' keys from `from` to `to` (inclusive). */
|
|
382
|
+
const enumerateMonths = (from, to) => {
|
|
383
|
+
const keys = [];
|
|
384
|
+
let [year, month] = from.split('-').map(Number);
|
|
385
|
+
const [endYear, endMonth] = to.split('-').map(Number);
|
|
386
|
+
while (year < endYear || (year === endYear && month <= endMonth)) {
|
|
387
|
+
keys.push(`${year}-${`${month}`.padStart(2, '0')}`);
|
|
388
|
+
if (keys.length > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN)
|
|
389
|
+
return [];
|
|
390
|
+
month += 1;
|
|
391
|
+
if (month > 12) {
|
|
392
|
+
month = 1;
|
|
393
|
+
year += 1;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return keys;
|
|
397
|
+
};
|
|
398
|
+
/** 'YYYY-Qn' keys from `from` to `to` (inclusive). */
|
|
399
|
+
const enumerateQuarters = (from, to) => {
|
|
400
|
+
const asIndex = (key) => {
|
|
401
|
+
const [year, quarter] = key.split('-');
|
|
402
|
+
return Number(year) * 4 + (Number(quarter.slice(1)) - 1);
|
|
403
|
+
};
|
|
404
|
+
const keys = [];
|
|
405
|
+
for (let index = asIndex(from); index <= asIndex(to); index += 1) {
|
|
406
|
+
keys.push(`${Math.floor(index / 4)}-Q${(index % 4) + 1}`);
|
|
407
|
+
if (keys.length > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN)
|
|
408
|
+
return [];
|
|
409
|
+
}
|
|
410
|
+
return keys;
|
|
411
|
+
};
|
|
412
|
+
/** 'YYYY-Sn' keys from `from` to `to` (inclusive). */
|
|
413
|
+
const enumerateSemesters = (from, to) => {
|
|
414
|
+
const asIndex = (key) => {
|
|
415
|
+
const [year, semester] = key.split('-');
|
|
416
|
+
return Number(year) * 2 + (Number(semester.slice(1)) - 1);
|
|
417
|
+
};
|
|
418
|
+
const keys = [];
|
|
419
|
+
for (let index = asIndex(from); index <= asIndex(to); index += 1) {
|
|
420
|
+
keys.push(`${Math.floor(index / 2)}-S${(index % 2) + 1}`);
|
|
421
|
+
if (keys.length > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN)
|
|
422
|
+
return [];
|
|
423
|
+
}
|
|
424
|
+
return keys;
|
|
425
|
+
};
|
|
426
|
+
/** 'YYYY' keys from `from` to `to` (inclusive). */
|
|
427
|
+
const enumerateYears = (from, to) => {
|
|
428
|
+
const count = Number(to) - Number(from) + 1;
|
|
429
|
+
if (!Number.isFinite(count) || count < 0 || count > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN)
|
|
430
|
+
return [];
|
|
431
|
+
return Array.from({ length: count }, (_, index) => `${Number(from) + index}`);
|
|
432
|
+
};
|
|
433
|
+
/** Exact period keys covered by a contiguous range of the given scope. */
|
|
434
|
+
const periodRangeFields = (rangeStart, rangeEnd, scope) => {
|
|
435
|
+
if (rangeStart == null)
|
|
436
|
+
return [];
|
|
437
|
+
const from = periodKeyOf(rangeStart, scope);
|
|
438
|
+
const to = periodKeyOf(rangeEnd ?? rangeStart, scope);
|
|
439
|
+
if (from == null || to == null)
|
|
440
|
+
return [];
|
|
441
|
+
switch (scope) {
|
|
442
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK:
|
|
443
|
+
return enumerateWeekStarts(from, to);
|
|
444
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH:
|
|
445
|
+
return enumerateMonths(from, to);
|
|
446
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER:
|
|
447
|
+
return enumerateQuarters(from, to);
|
|
448
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER:
|
|
449
|
+
return enumerateSemesters(from, to);
|
|
450
|
+
case MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR:
|
|
451
|
+
return enumerateYears(from, to);
|
|
452
|
+
default:
|
|
453
|
+
return [];
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
/** Whole-hour `'HH:00'` time key — minute-level keys are not servable. */
|
|
457
|
+
const WHOLE_HOUR_TIME_PATTERN = /^([01]\d|2[0-3]):00$/;
|
|
458
|
+
const isRealLocalDateKey = (key) => (0, scope_helpers_1.parseMaterializedLocalDate)(key) != null;
|
|
459
|
+
const epochDayOfLocalDateKey = (key) => {
|
|
460
|
+
const parts = (0, scope_helpers_1.parseMaterializedLocalDate)(key);
|
|
461
|
+
if (parts == null)
|
|
462
|
+
return null;
|
|
463
|
+
return Date.UTC(parts.year, parts.month - 1, parts.day) / 86400000;
|
|
464
|
+
};
|
|
465
|
+
/** ISO week count (52/53) of an ISO week-year — Dec 28 is always in its last ISO week. */
|
|
466
|
+
const getIsoWeeksInYear = (isoYear) => {
|
|
467
|
+
const label = getIsoWeekKey(`${isoYear}-12-28`);
|
|
468
|
+
const match = label == null ? null : /W(\d{2})$/.exec(label);
|
|
469
|
+
return match == null ? 52 : Number(match[1]);
|
|
470
|
+
};
|
|
471
|
+
/** Views whose day set is one contiguous `rangeStart … rangeEnd` walk (incl. MONTH/YEAR). */
|
|
472
|
+
const RANGE_EXPANDING_VIEWS = new Set([
|
|
473
|
+
...RANGE_LIKE_VIEWS,
|
|
474
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.MONTH,
|
|
475
|
+
REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.YEAR,
|
|
476
|
+
]);
|
|
477
|
+
/**
|
|
478
|
+
* Validates the RAW calendar selection BEFORE any decoding work happens:
|
|
479
|
+
* - time keys, when present, must be whole `'HH:00'` hours (minute-level keys
|
|
480
|
+
* are malformed wire values — they must never silently truncate);
|
|
481
|
+
* - the weekday refinement must carry real `DAY_INDEX_ENUM` entries;
|
|
482
|
+
* - every date key (`rangeStart` / `rangeEnd` / `selectedDates` / week-start
|
|
483
|
+
* picks) must be a REAL `'YYYY-MM-DD'` calendar date;
|
|
484
|
+
* - `selectedPeriods` picks must match their view's period grammar and
|
|
485
|
+
* semantics (months 01–12, ISO weeks within the year's week count,
|
|
486
|
+
* quarters Q1–Q4, semesters S1–S2, four-digit years);
|
|
487
|
+
* - contiguous range walks may not cover more than
|
|
488
|
+
* `MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN` days, and discrete picks may
|
|
489
|
+
* not exceed that count — both are `tooLarge` (narrow the selection).
|
|
490
|
+
*
|
|
491
|
+
* The decoder calls this first and maps BOTH failures to `null`; read
|
|
492
|
+
* services call it (via their read parser) to answer a clean 400 before the
|
|
493
|
+
* decoder is ever entered — a malformed request can never reach a date walk.
|
|
494
|
+
*/
|
|
495
|
+
const validateMaterializedReportSelectionInput = (selection) => {
|
|
496
|
+
const invalid = { ok: false, reason: 'invalid' };
|
|
497
|
+
const tooLarge = { ok: false, reason: 'tooLarge' };
|
|
498
|
+
if (selection == null || typeof selection !== 'object')
|
|
499
|
+
return invalid;
|
|
500
|
+
const { view, rangeStart, rangeEnd, selectedDates, selectedPeriods } = selection;
|
|
501
|
+
// Whole-hour time keys only — `'18:45'` must not silently read 18:00–18:44.
|
|
502
|
+
if (selection.timeStart != null && !WHOLE_HOUR_TIME_PATTERN.test(selection.timeStart))
|
|
503
|
+
return invalid;
|
|
504
|
+
if (selection.timeEnd != null && !WHOLE_HOUR_TIME_PATTERN.test(selection.timeEnd))
|
|
505
|
+
return invalid;
|
|
506
|
+
// Weekday refinement entries must be real day-index keys.
|
|
507
|
+
const weekdays = Array.isArray(selection.weekdays) ? selection.weekdays : [];
|
|
508
|
+
if (!weekdays.every((entry) => ENUMS_1.ALL_DAY_INDEX.includes(entry)))
|
|
509
|
+
return invalid;
|
|
510
|
+
// Every date key that could reach a date walk must be a REAL calendar date.
|
|
511
|
+
const dates = Array.isArray(selectedDates) ? selectedDates : [];
|
|
512
|
+
const dateKeys = [rangeStart, rangeEnd, ...dates];
|
|
513
|
+
if (dateKeys.some((key) => key != null && !isRealLocalDateKey(key)))
|
|
514
|
+
return invalid;
|
|
515
|
+
if (view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.MULTI_DATE && dates.length > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN) {
|
|
516
|
+
return tooLarge;
|
|
517
|
+
}
|
|
518
|
+
// Period picks — strict per-view grammar + semantics (a bad key would
|
|
519
|
+
// otherwise surface as a 200 "No Data" instead of a 400).
|
|
520
|
+
const periods = Array.isArray(selectedPeriods) ? selectedPeriods : [];
|
|
521
|
+
const overPicked = periods.length > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN ? tooLarge : null;
|
|
522
|
+
switch (view) {
|
|
523
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_WEEKS:
|
|
524
|
+
if (overPicked != null)
|
|
525
|
+
return overPicked;
|
|
526
|
+
if (!periods.every((key) => isRealLocalDateKey(key)))
|
|
527
|
+
return invalid;
|
|
528
|
+
break;
|
|
529
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_WEEKS_OF_YEAR:
|
|
530
|
+
if (overPicked != null)
|
|
531
|
+
return overPicked;
|
|
532
|
+
for (const key of periods) {
|
|
533
|
+
const match = /^(\d{4})-W(\d{2})$/.exec(key);
|
|
534
|
+
if (match == null)
|
|
535
|
+
return invalid;
|
|
536
|
+
const week = Number(match[2]);
|
|
537
|
+
if (week < 1 || week > getIsoWeeksInYear(Number(match[1])))
|
|
538
|
+
return invalid;
|
|
539
|
+
}
|
|
540
|
+
break;
|
|
541
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_MONTHS:
|
|
542
|
+
if (overPicked != null)
|
|
543
|
+
return overPicked;
|
|
544
|
+
if (!periods.every((key) => /^\d{4}-(0[1-9]|1[0-2])$/.test(key)))
|
|
545
|
+
return invalid;
|
|
546
|
+
break;
|
|
547
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_QUARTERS:
|
|
548
|
+
if (overPicked != null)
|
|
549
|
+
return overPicked;
|
|
550
|
+
if (!periods.every((key) => /^\d{4}-Q[1-4]$/.test(key)))
|
|
551
|
+
return invalid;
|
|
552
|
+
break;
|
|
553
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_SEMESTERS:
|
|
554
|
+
if (overPicked != null)
|
|
555
|
+
return overPicked;
|
|
556
|
+
if (!periods.every((key) => /^\d{4}-S[12]$/.test(key)))
|
|
557
|
+
return invalid;
|
|
558
|
+
break;
|
|
559
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_YEARS:
|
|
560
|
+
if (overPicked != null)
|
|
561
|
+
return overPicked;
|
|
562
|
+
if (!periods.every((key) => /^\d{4}$/.test(key)))
|
|
563
|
+
return invalid;
|
|
564
|
+
break;
|
|
565
|
+
default:
|
|
566
|
+
break;
|
|
567
|
+
}
|
|
568
|
+
// Contiguous range walks are bounded by the raw day span (~10 years).
|
|
569
|
+
if (RANGE_EXPANDING_VIEWS.has(view) && rangeStart != null && rangeEnd != null) {
|
|
570
|
+
const start = epochDayOfLocalDateKey(rangeStart);
|
|
571
|
+
const end = epochDayOfLocalDateKey(rangeEnd);
|
|
572
|
+
if (start != null && end != null && Math.abs(end - start) > exports.MATERIALIZED_REPORT_MAX_SELECTION_DAY_SPAN - 1) {
|
|
573
|
+
return tooLarge;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
// DIFFERENT_* picks expand to REAL days at decode time — with the weekday
|
|
577
|
+
// refinement applied. The decode caps the EFFECTIVE day set, so an
|
|
578
|
+
// oversize expansion must classify as `tooLarge` HERE instead of surfacing
|
|
579
|
+
// downstream as an unexplained "invalid" (the decoder maps it to `null`).
|
|
580
|
+
const expandsPicksToDays = view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_WEEKS ||
|
|
581
|
+
view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_WEEKS_OF_YEAR ||
|
|
582
|
+
view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_MONTHS ||
|
|
583
|
+
view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_QUARTERS ||
|
|
584
|
+
view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_SEMESTERS ||
|
|
585
|
+
view === REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_YEARS;
|
|
586
|
+
if (expandsPicksToDays && getBaseSelectedDayKeys(selection, getActiveWeekdayFilter(selection.weekdays)) == null) {
|
|
587
|
+
return tooLarge;
|
|
588
|
+
}
|
|
589
|
+
return { ok: true };
|
|
590
|
+
};
|
|
591
|
+
exports.validateMaterializedReportSelectionInput = validateMaterializedReportSelectionInput;
|
|
592
|
+
/* ==================== Decode ==================== */
|
|
593
|
+
/**
|
|
594
|
+
* Maps a committed calendar selection to the exact `scope_groups` of the
|
|
595
|
+
* overview read — the PUBLIC decoder: it validates the raw selection
|
|
596
|
+
* (`validateMaterializedReportSelectionInput`) first and NEVER throws.
|
|
597
|
+
* Returns `null` when the selection cannot be served (LIVE without a
|
|
598
|
+
* committed day, a malformed/oversized/empty selection, or unservable
|
|
599
|
+
* refinement input).
|
|
600
|
+
*/
|
|
601
|
+
const buildMaterializedReportScopeGroupsFromSelection = (selection) => {
|
|
602
|
+
try {
|
|
603
|
+
if (selection == null || typeof selection !== 'object')
|
|
604
|
+
return null;
|
|
605
|
+
if (!(0, exports.validateMaterializedReportSelectionInput)(selection).ok)
|
|
606
|
+
return null;
|
|
607
|
+
return decodeMaterializedReportScopeGroups(selection);
|
|
608
|
+
}
|
|
609
|
+
catch {
|
|
610
|
+
return null;
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
exports.buildMaterializedReportScopeGroupsFromSelection = buildMaterializedReportScopeGroupsFromSelection;
|
|
614
|
+
/** Internal decode — the public entry above validates + backstops first. */
|
|
615
|
+
const decodeMaterializedReportScopeGroups = (selection) => {
|
|
616
|
+
// LIVE reads ONLY the running business day — all of its exact hour labels
|
|
617
|
+
// (the calendar commits the running day as `rangeStart`). The backend sums
|
|
618
|
+
// them into one record; the day close finalizes the documents.
|
|
619
|
+
if (selection.mode === REPORT_CALENDAR_1.CALENDAR_MODE_ENUM.LIVE) {
|
|
620
|
+
const dayKey = selection.rangeStart ?? selection.rangeEnd;
|
|
621
|
+
if (dayKey == null)
|
|
622
|
+
return null;
|
|
623
|
+
return usableGroups([
|
|
624
|
+
{
|
|
625
|
+
scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
|
|
626
|
+
scope_field: runningDayHourLabels(dayKey),
|
|
627
|
+
},
|
|
628
|
+
]);
|
|
629
|
+
}
|
|
630
|
+
const { view, rangeStart, rangeEnd, selectedDates, selectedPeriods } = selection;
|
|
631
|
+
// =====================Refinements (every view)=====================
|
|
632
|
+
//
|
|
633
|
+
// Both refinements degenerate the read to standalone day/hour documents:
|
|
634
|
+
// - time window → `localHour`: the exact hour labels for the selected days;
|
|
635
|
+
// - weekday filter → `localDate`: the matching day keys (`localDate` /
|
|
636
|
+
// `localHour` docs carry `day_index`; period docs do not, hence the scope
|
|
637
|
+
// switch). The weekday refinement is applied inside the effective-day
|
|
638
|
+
// helper BEFORE hour-window expansion — the same helper feeds the
|
|
639
|
+
// calendar's grid highlight and summary aggregate, so UI and request can
|
|
640
|
+
// never disagree.
|
|
641
|
+
const window = resolveTimeOfDayWindow(selection);
|
|
642
|
+
const weekdayFilter = getActiveWeekdayFilter(selection.weekdays);
|
|
643
|
+
if (window != null || weekdayFilter != null) {
|
|
644
|
+
const days = (0, exports.getDayKeysFromCalendarSelection)(selection);
|
|
645
|
+
if (days.length === 0)
|
|
646
|
+
return null;
|
|
647
|
+
// Compile the effective days (+ window) into the minimal scope groups:
|
|
648
|
+
// day runs compact into the largest exact periods they fully cover, and
|
|
649
|
+
// a window becomes exact hour labels — a continuous SPAN on range-like
|
|
650
|
+
// views (toggle), the same hours on each selected day otherwise
|
|
651
|
+
// (dedicated window view / discrete selections).
|
|
652
|
+
const groups = (0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
|
|
653
|
+
days,
|
|
654
|
+
...(window != null
|
|
655
|
+
? {
|
|
656
|
+
window: {
|
|
657
|
+
fromHour: window.fromHour,
|
|
658
|
+
toHour: window.toHour,
|
|
659
|
+
// A weekday-refined day set is GAPPED — a continuous SPAN can
|
|
660
|
+
// never compile it. Weekday + window therefore always compiles
|
|
661
|
+
// RECURRING (the same hours on each matching weekday).
|
|
662
|
+
kind: SPAN_VIEWS.has(view) && weekdayFilter == null
|
|
663
|
+
? materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.SPAN
|
|
664
|
+
: materialized_reports_scope_group_helpers_1.MATERIALIZED_REPORT_WINDOW_KIND.RECURRING,
|
|
665
|
+
},
|
|
666
|
+
}
|
|
667
|
+
: {}),
|
|
668
|
+
});
|
|
669
|
+
if (groups != null) {
|
|
670
|
+
return usableGroups(groups);
|
|
671
|
+
}
|
|
672
|
+
// Uncompilable refinement selection (the picker mirrors the compiler
|
|
673
|
+
// rules, so this should be unreachable) — return `null` instead of
|
|
674
|
+
// silently dropping the window / weekday refinement and reading full
|
|
675
|
+
// days the UI never advertised.
|
|
676
|
+
return null;
|
|
677
|
+
}
|
|
678
|
+
// =====================Period views=====================
|
|
679
|
+
switch (view) {
|
|
680
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DATE_RANGE:
|
|
681
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DATE_RANGE_TIME_WINDOW:
|
|
682
|
+
// Every day of the range, compacted into the largest exact periods
|
|
683
|
+
// they fully cover; the dedicated window view without a complete
|
|
684
|
+
// window falls back here (same totals, no window).
|
|
685
|
+
return usableGroups((0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
|
|
686
|
+
days: (0, exports.getDayKeysFromCalendarSelection)(selection),
|
|
687
|
+
}));
|
|
688
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.MULTI_DATE:
|
|
689
|
+
// Discrete days still compact — adjacent runs that exactly cover a
|
|
690
|
+
// whole week / month / … fold into it, the rest stays `localDate`.
|
|
691
|
+
return usableGroups((0, materialized_reports_scope_group_helpers_1.buildMaterializedReportScopeGroups)({
|
|
692
|
+
days: Array.isArray(selectedDates) ? selectedDates : [],
|
|
693
|
+
}));
|
|
694
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.WEEK:
|
|
695
|
+
return scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK, periodRangeFields(rangeStart, rangeEnd, MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK));
|
|
696
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_WEEKS:
|
|
697
|
+
return scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK, Array.isArray(selectedPeriods) ? selectedPeriods : []);
|
|
698
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.WEEK_OF_YEAR:
|
|
699
|
+
return rangeStart == null
|
|
700
|
+
? null
|
|
701
|
+
: scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK_OF_YEAR, enumerateIsoWeeks(rangeStart, rangeEnd ?? rangeStart));
|
|
702
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_WEEKS_OF_YEAR:
|
|
703
|
+
return scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK_OF_YEAR, Array.isArray(selectedPeriods) ? selectedPeriods : []);
|
|
704
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.MONTH:
|
|
705
|
+
return scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH, periodRangeFields(rangeStart, rangeEnd, MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH));
|
|
706
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_MONTHS:
|
|
707
|
+
return scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH, Array.isArray(selectedPeriods) ? selectedPeriods : []);
|
|
708
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.QUARTER:
|
|
709
|
+
return scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER, periodRangeFields(rangeStart, rangeEnd, MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER));
|
|
710
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_QUARTERS:
|
|
711
|
+
return scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_QUARTER, Array.isArray(selectedPeriods) ? selectedPeriods : []);
|
|
712
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.SEMESTER:
|
|
713
|
+
return scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER, periodRangeFields(rangeStart, rangeEnd, MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER));
|
|
714
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_SEMESTERS:
|
|
715
|
+
return scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_SEMESTER, Array.isArray(selectedPeriods) ? selectedPeriods : []);
|
|
716
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.YEAR:
|
|
717
|
+
return scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR, periodRangeFields(rangeStart, rangeEnd, MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR));
|
|
718
|
+
case REPORT_CALENDAR_1.CALENDAR_VIEW_MODE_ENUM.DIFFERENT_YEARS:
|
|
719
|
+
return scopedGroups(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_YEAR, Array.isArray(selectedPeriods) ? selectedPeriods : []);
|
|
720
|
+
default:
|
|
721
|
+
return null;
|
|
722
|
+
}
|
|
723
|
+
};
|