@managesome/eposmatic-toolkit 0.4.60 → 0.4.62
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 +254 -4
- package/esm/constants/TENANT_ENUMS.js +15 -0
- package/esm/constants/WORKER_ENDPOINTS.js +12 -0
- package/esm/helpers/materialized-reports/index.js +8 -0
- package/esm/helpers/materialized-reports/materialized-day-readiness.js +2 -2
- package/esm/helpers/materialized-reports/materialized-dinein-accumulator-helpers.js +557 -0
- package/esm/helpers/materialized-reports/materialized-dinein-classification-helpers.js +447 -0
- package/esm/helpers/materialized-reports/materialized-dinein-close-day-helpers.js +372 -0
- package/esm/helpers/materialized-reports/materialized-dinein-compile-helpers.js +500 -0
- package/esm/helpers/materialized-reports/materialized-dinein-entry-pages.js +218 -0
- package/esm/helpers/materialized-reports/materialized-dinein-key-helpers.js +87 -0
- package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +7 -1
- package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +8 -0
- package/esm/helpers/materialized-reports/materialized-report-runs.js +143 -0
- package/esm/helpers/materialized-reports/report-platform/definition.js +93 -1
- package/esm/helpers/materialized-reports/tenant-dinein-formatter.js +240 -0
- package/esm/helpers/reporting-contract/report-contract.js +3 -0
- package/esm/schemas/models/index.js +3 -0
- package/esm/schemas/models/materialized_tenant_dinein_entries.js +57 -0
- package/esm/schemas/models/materialized_tenant_dinein_reports.js +73 -0
- package/esm/schemas/models/materialized_tenant_report_runs.js +105 -0
- package/esm/schemas/models/tenant_active_jobs.js +19 -1
- package/esm/schemas/types/index.js +3 -0
- package/esm/schemas/types/materialized_report_drilldown_rows.js +1 -0
- package/esm/schemas/types/materialized_tenant_dinein_entries.js +1 -0
- package/esm/schemas/types/materialized_tenant_dinein_reports.js +6 -0
- package/esm/schemas/types/materialized_tenant_report_runs.js +1 -0
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +229 -4
- package/lib/constants/MATERIALIZED_REPORTS.js +255 -5
- package/lib/constants/TENANT_ENUMS.d.ts +14 -0
- package/lib/constants/TENANT_ENUMS.js +16 -1
- package/lib/constants/WORKER_ENDPOINTS.d.ts +12 -0
- package/lib/constants/WORKER_ENDPOINTS.js +12 -0
- package/lib/helpers/materialized-reports/index.d.ts +8 -0
- package/lib/helpers/materialized-reports/index.js +8 -0
- package/lib/helpers/materialized-reports/materialized-day-readiness.d.ts +1 -1
- package/lib/helpers/materialized-reports/materialized-day-readiness.js +2 -2
- package/lib/helpers/materialized-reports/materialized-dinein-accumulator-helpers.d.ts +67 -0
- package/lib/helpers/materialized-reports/materialized-dinein-accumulator-helpers.js +582 -0
- package/lib/helpers/materialized-reports/materialized-dinein-classification-helpers.d.ts +245 -0
- package/lib/helpers/materialized-reports/materialized-dinein-classification-helpers.js +469 -0
- package/lib/helpers/materialized-reports/materialized-dinein-close-day-helpers.d.ts +110 -0
- package/lib/helpers/materialized-reports/materialized-dinein-close-day-helpers.js +384 -0
- package/lib/helpers/materialized-reports/materialized-dinein-compile-helpers.d.ts +73 -0
- package/lib/helpers/materialized-reports/materialized-dinein-compile-helpers.js +504 -0
- package/lib/helpers/materialized-reports/materialized-dinein-entry-pages.d.ts +69 -0
- package/lib/helpers/materialized-reports/materialized-dinein-entry-pages.js +231 -0
- package/lib/helpers/materialized-reports/materialized-dinein-key-helpers.d.ts +44 -0
- package/lib/helpers/materialized-reports/materialized-dinein-key-helpers.js +96 -0
- package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +6 -0
- package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +8 -0
- package/lib/helpers/materialized-reports/materialized-report-runs.d.ts +105 -0
- package/lib/helpers/materialized-reports/materialized-report-runs.js +156 -0
- package/lib/helpers/materialized-reports/report-platform/definition.js +92 -0
- package/lib/helpers/materialized-reports/tenant-dinein-formatter.d.ts +225 -0
- package/lib/helpers/materialized-reports/tenant-dinein-formatter.js +243 -0
- package/lib/helpers/reporting-contract/report-contract.d.ts +1 -0
- package/lib/helpers/reporting-contract/report-contract.js +3 -0
- package/lib/helpers/types/workers.d.ts +65 -0
- package/lib/schemas/models/index.d.ts +3 -0
- package/lib/schemas/models/index.js +3 -0
- package/lib/schemas/models/materialized_tenant_day_readiness.d.ts +3 -3
- package/lib/schemas/models/materialized_tenant_dinein_entries.d.ts +100 -0
- package/lib/schemas/models/materialized_tenant_dinein_entries.js +60 -0
- package/lib/schemas/models/materialized_tenant_dinein_reports.d.ts +111 -0
- package/lib/schemas/models/materialized_tenant_dinein_reports.js +76 -0
- package/lib/schemas/models/materialized_tenant_report_runs.d.ts +97 -0
- package/lib/schemas/models/materialized_tenant_report_runs.js +108 -0
- package/lib/schemas/models/tenant_active_jobs.d.ts +12 -3
- package/lib/schemas/models/tenant_active_jobs.js +18 -0
- package/lib/schemas/types/index.d.ts +3 -0
- package/lib/schemas/types/index.js +3 -0
- package/lib/schemas/types/materialized_report_drilldown_rows.d.ts +1 -1
- package/lib/schemas/types/materialized_report_drilldown_rows.js +1 -0
- package/lib/schemas/types/materialized_tenant_dinein_entries.d.ts +127 -0
- package/lib/schemas/types/materialized_tenant_dinein_entries.js +2 -0
- package/lib/schemas/types/materialized_tenant_dinein_reports.d.ts +326 -0
- package/lib/schemas/types/materialized_tenant_dinein_reports.js +9 -0
- package/lib/schemas/types/materialized_tenant_report_runs.d.ts +55 -0
- package/lib/schemas/types/materialized_tenant_report_runs.js +2 -0
- package/lib/schemas/types/tenant_active_jobs.d.ts +15 -1
- package/lib/tests/materialized-reports/dinein-fixtures.d.ts +29 -0
- package/lib/tests/materialized-reports/dinein-fixtures.js +130 -0
- package/lib/tests/materialized-reports/materialized-day-readiness.test.js +3 -2
- package/lib/tests/materialized-reports/materialized-dinein-compile.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-dinein-compile.test.js +807 -0
- package/lib/tests/materialized-reports/materialized-dinein-plan.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-dinein-plan.test.js +419 -0
- package/lib/tests/materialized-reports/materialized-report-runs.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-report-runs.test.js +116 -0
- package/lib/tests/materialized-reports/materialized-tenant-dinein-schema.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-tenant-dinein-schema.test.js +185 -0
- package/lib/tests/materialized-reports/report-platform/registry-conformance.test.js +15 -2
- package/lib/tests/materialized-reports/tenant-dinein-formatter.test.d.ts +1 -0
- package/lib/tests/materialized-reports/tenant-dinein-formatter.test.js +458 -0
- package/package.json +1 -1
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isMaterializedDineinEntryScope = isMaterializedDineinEntryScope;
|
|
4
|
+
exports.dineinEntryPageOf = dineinEntryPageOf;
|
|
5
|
+
exports.dineinEntryPageBase = dineinEntryPageBase;
|
|
6
|
+
exports.isDineinEntryPageForSourceDay = isDineinEntryPageForSourceDay;
|
|
7
|
+
exports.materializedDineinEntrySubPage = materializedDineinEntrySubPage;
|
|
8
|
+
exports.materializedDineinEntryPageKey = materializedDineinEntryPageKey;
|
|
9
|
+
exports.splitMaterializedDineinEntryPage = splitMaterializedDineinEntryPage;
|
|
10
|
+
exports.mergeMaterializedDineinEntryPages = mergeMaterializedDineinEntryPages;
|
|
11
|
+
exports.buildMaterializedDineinEntryPageRefs = buildMaterializedDineinEntryPageRefs;
|
|
12
|
+
exports.dineinMonthScopeFieldsOfDays = dineinMonthScopeFieldsOfDays;
|
|
13
|
+
exports.buildMaterializedDineinDetailsScopeGroups = buildMaterializedDineinDetailsScopeGroups;
|
|
14
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
15
|
+
const materialized_document_size_1 = require("./materialized-document-size");
|
|
16
|
+
const scope_helpers_1 = require("./scope-helpers");
|
|
17
|
+
/**
|
|
18
|
+
* Dinein ENTRY PAGES — the page identity of
|
|
19
|
+
* `materialized_tenant_dinein_entries`.
|
|
20
|
+
*
|
|
21
|
+
* ## One page per source day
|
|
22
|
+
*
|
|
23
|
+
* | scope | page |
|
|
24
|
+
* | --- | --- |
|
|
25
|
+
* | `localDate` | `'all'` (the scope is already one day) |
|
|
26
|
+
* | `localWeek` | `<source localDate>` |
|
|
27
|
+
* | `localWeekOfYear` | `<source localDate>` |
|
|
28
|
+
* | `localMonth` | `<source localDate>` |
|
|
29
|
+
*
|
|
30
|
+
* There is NO quarter / semester / year entry document. A month is covered by
|
|
31
|
+
* its 28-31 pages, and a broad details selection is served from the month pages
|
|
32
|
+
* it touches (`buildMaterializedDineinDetailsScopeGroups`).
|
|
33
|
+
*
|
|
34
|
+
* ## Deterministic `#N` sub-pages, only when needed
|
|
35
|
+
*
|
|
36
|
+
* An oversized page splits into `page#0`, `page#1`, … . A page that fits keeps
|
|
37
|
+
* its ORIGINAL identity (no suffix), so existing pages are untouched until they
|
|
38
|
+
* actually grow. The sub-pages are disjoint — every check appears exactly once —
|
|
39
|
+
* and the reader unions them.
|
|
40
|
+
*/
|
|
41
|
+
const SUB_PAGE_PATTERN = /^(.*)#(\d+)$/;
|
|
42
|
+
/** True when a scope context carries entry pages (day + the three period scopes). */
|
|
43
|
+
function isMaterializedDineinEntryScope(scope) {
|
|
44
|
+
return MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_ENTRY_SCOPES.includes(String(scope));
|
|
45
|
+
}
|
|
46
|
+
/** The `page` value a compiled entry document of `scopeContext` must carry. */
|
|
47
|
+
function dineinEntryPageOf(scopeContext, localDate) {
|
|
48
|
+
const scope = String(scopeContext);
|
|
49
|
+
if (!isMaterializedDineinEntryScope(scope))
|
|
50
|
+
return null;
|
|
51
|
+
if (scope === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE) {
|
|
52
|
+
return MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_ENTRY_PAGE_ALL;
|
|
53
|
+
}
|
|
54
|
+
if (typeof localDate !== 'string' || localDate === '')
|
|
55
|
+
return null;
|
|
56
|
+
return localDate;
|
|
57
|
+
}
|
|
58
|
+
/** The unsuffixed source-day page (`all` / `YYYY-MM-DD`). */
|
|
59
|
+
function dineinEntryPageBase(page) {
|
|
60
|
+
const text = String(page ?? '');
|
|
61
|
+
const match = SUB_PAGE_PATTERN.exec(text);
|
|
62
|
+
return match == null ? text : match[1];
|
|
63
|
+
}
|
|
64
|
+
/** True when a stored page belongs to the source day (`all` / `YYYY-MM-DD` + `#N`). */
|
|
65
|
+
function isDineinEntryPageForSourceDay(scopeContext, page, localDate) {
|
|
66
|
+
const base = dineinEntryPageOf(scopeContext, localDate);
|
|
67
|
+
if (base == null)
|
|
68
|
+
return false;
|
|
69
|
+
return dineinEntryPageBase(page) === base;
|
|
70
|
+
}
|
|
71
|
+
/** The deterministic identity of one chunk. */
|
|
72
|
+
function materializedDineinEntrySubPage(basePage, index) {
|
|
73
|
+
return `${basePage}#${Math.max(0, Math.floor(index))}`;
|
|
74
|
+
}
|
|
75
|
+
/** Stable identity string of one entry page (tests / diagnostics / map keys). */
|
|
76
|
+
function materializedDineinEntryPageKey(ref) {
|
|
77
|
+
return `${String(ref?.scope_context)}|${String(ref?.scope_field)}|${String(ref?.page)}`;
|
|
78
|
+
}
|
|
79
|
+
const countRows = (sections) => {
|
|
80
|
+
let rows = 0;
|
|
81
|
+
for (const section of Object.values(sections ?? {}))
|
|
82
|
+
rows += section?.rows?.length ?? 0;
|
|
83
|
+
return rows;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Splits ONE source-day entry page when it exceeds the budget.
|
|
87
|
+
*
|
|
88
|
+
* Rows are walked in their stored (deterministic) key/row order and packed until
|
|
89
|
+
* the next row would cross the budget. A page that fits keeps its original
|
|
90
|
+
* identity. Throws when a single row cannot fit, because silently dropping a
|
|
91
|
+
* check would understate the statement.
|
|
92
|
+
*/
|
|
93
|
+
function splitMaterializedDineinEntryPage(doc, maxBytes = MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_ENTRY_MAX_BSON_BYTES) {
|
|
94
|
+
if (doc == null)
|
|
95
|
+
return [];
|
|
96
|
+
if ((0, materialized_document_size_1.materializedDocumentBsonBytes)(doc) <= maxBytes)
|
|
97
|
+
return [doc];
|
|
98
|
+
const sections = doc.dinein_entries ?? null;
|
|
99
|
+
if (sections == null || countRows(sections) === 0)
|
|
100
|
+
return [doc];
|
|
101
|
+
const basePage = dineinEntryPageBase(doc.page);
|
|
102
|
+
const envelope = { ...doc, page: basePage, dinein_entries: {} };
|
|
103
|
+
const envelopeBytes = (0, materialized_document_size_1.materializedDocumentBsonBytes)(envelope);
|
|
104
|
+
const chunks = [];
|
|
105
|
+
let current = {};
|
|
106
|
+
let currentBytes = envelopeBytes;
|
|
107
|
+
let currentRows = 0;
|
|
108
|
+
const flush = () => {
|
|
109
|
+
if (currentRows > 0)
|
|
110
|
+
chunks.push(current);
|
|
111
|
+
current = {};
|
|
112
|
+
currentBytes = envelopeBytes;
|
|
113
|
+
currentRows = 0;
|
|
114
|
+
};
|
|
115
|
+
for (const key of Object.keys(sections)) {
|
|
116
|
+
for (const row of sections[key]?.rows ?? []) {
|
|
117
|
+
const rowBytes = (0, materialized_document_size_1.materializedDocumentBsonBytes)(row) + key.length + 24;
|
|
118
|
+
if (currentRows > 0 && currentBytes + rowBytes > maxBytes - 512)
|
|
119
|
+
flush();
|
|
120
|
+
if (current[key] == null)
|
|
121
|
+
current[key] = { rows: [] };
|
|
122
|
+
current[key].rows.push(row);
|
|
123
|
+
currentBytes += rowBytes;
|
|
124
|
+
currentRows += 1;
|
|
125
|
+
if (currentBytes > maxBytes) {
|
|
126
|
+
throw new Error(`Dinein entry page ${String(doc.scope_context)}:${String(doc.scope_field)}:${basePage} ` +
|
|
127
|
+
'cannot split below the budget (one row is too large)');
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
flush();
|
|
132
|
+
return chunks.map((chunk, index) => {
|
|
133
|
+
const page = {
|
|
134
|
+
...doc,
|
|
135
|
+
page: materializedDineinEntrySubPage(basePage, index),
|
|
136
|
+
dinein_entries: chunk,
|
|
137
|
+
};
|
|
138
|
+
if ((0, materialized_document_size_1.materializedDocumentBsonBytes)(page) > maxBytes) {
|
|
139
|
+
throw new Error(`Dinein entry sub-page ${String(doc.scope_context)}:${String(doc.scope_field)}:${page.page} ` +
|
|
140
|
+
'exceeds the entry-page budget');
|
|
141
|
+
}
|
|
142
|
+
return page;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Unions all pages of ONE source day (the base page or its `#N` sub-pages) into
|
|
147
|
+
* one logical document. Rows are disjoint by construction; the `order_id`
|
|
148
|
+
* dedupe makes a duplicated page harmless instead of doubling a check.
|
|
149
|
+
*/
|
|
150
|
+
function mergeMaterializedDineinEntryPages(pages, identity) {
|
|
151
|
+
const list = (pages ?? []).filter((page) => page != null);
|
|
152
|
+
if (list.length === 0)
|
|
153
|
+
return null;
|
|
154
|
+
const sections = {};
|
|
155
|
+
const seen = new Set();
|
|
156
|
+
for (const page of list) {
|
|
157
|
+
for (const [key, section] of Object.entries(page.dinein_entries ?? {})) {
|
|
158
|
+
for (const row of section?.rows ?? []) {
|
|
159
|
+
const rowIdentity = String(row.order_id ?? '');
|
|
160
|
+
if (rowIdentity !== '') {
|
|
161
|
+
if (seen.has(rowIdentity))
|
|
162
|
+
continue;
|
|
163
|
+
seen.add(rowIdentity);
|
|
164
|
+
}
|
|
165
|
+
if (sections[key] == null)
|
|
166
|
+
sections[key] = { rows: [] };
|
|
167
|
+
sections[key].rows.push(row);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const first = list[0];
|
|
172
|
+
return {
|
|
173
|
+
...first,
|
|
174
|
+
...(identity ?? {}),
|
|
175
|
+
dinein_entries: Object.keys(sections).length === 0 ? null : sections,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* The BASE page refs belonging to ONE source day. The close reader expands each
|
|
180
|
+
* ref to the base plus any deterministic `#N` sub-pages; no other day's page is
|
|
181
|
+
* ever fetched.
|
|
182
|
+
*
|
|
183
|
+
* Returns `null` for a malformed date.
|
|
184
|
+
*/
|
|
185
|
+
function buildMaterializedDineinEntryPageRefs(localDate) {
|
|
186
|
+
const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(localDate);
|
|
187
|
+
if (fields == null)
|
|
188
|
+
return null;
|
|
189
|
+
const refs = [];
|
|
190
|
+
for (const scope of MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_ENTRY_SCOPES) {
|
|
191
|
+
const scopeField = fields[String(scope)];
|
|
192
|
+
const page = dineinEntryPageOf(scope, localDate);
|
|
193
|
+
if (typeof scopeField !== 'string' || scopeField === '' || page == null)
|
|
194
|
+
return null;
|
|
195
|
+
refs.push({ scope_context: scope, scope_field: scopeField, page });
|
|
196
|
+
}
|
|
197
|
+
return refs;
|
|
198
|
+
}
|
|
199
|
+
/** The distinct `YYYY-MM` scope values of a day set, ascending. */
|
|
200
|
+
function dineinMonthScopeFieldsOfDays(days) {
|
|
201
|
+
const months = new Set();
|
|
202
|
+
for (const day of days ?? []) {
|
|
203
|
+
const match = /^(\d{4})-(\d{2})-\d{2}$/.exec(String(day));
|
|
204
|
+
if (match == null)
|
|
205
|
+
continue;
|
|
206
|
+
months.add(`${match[1]}-${match[2]}`);
|
|
207
|
+
}
|
|
208
|
+
return [...months].sort();
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Resolves the EXACT selected days into the scope groups a DINEIN DETAILS read
|
|
212
|
+
* uses:
|
|
213
|
+
*
|
|
214
|
+
* - one day → the `localDate` page (`page='all'`) — the direct path;
|
|
215
|
+
* - many days → the `localMonth` groups that contain them. The reader scans the
|
|
216
|
+
* pages of those months and filters rows to the EXACT days via
|
|
217
|
+
* `source_local_date`, so a quarter / semester / year / custom multi-month
|
|
218
|
+
* range is served, never refused.
|
|
219
|
+
*/
|
|
220
|
+
function buildMaterializedDineinDetailsScopeGroups(days) {
|
|
221
|
+
const unique = [...new Set((days ?? []).map((day) => String(day)))].sort();
|
|
222
|
+
if (unique.length === 0)
|
|
223
|
+
return null;
|
|
224
|
+
if (unique.length === 1) {
|
|
225
|
+
return [{ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE, scope_field: [unique[0]] }];
|
|
226
|
+
}
|
|
227
|
+
const months = dineinMonthScopeFieldsOfDays(unique);
|
|
228
|
+
if (months.length === 0)
|
|
229
|
+
return null;
|
|
230
|
+
return [{ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH, scope_field: months }];
|
|
231
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Key helpers for the dinein report.
|
|
3
|
+
*
|
|
4
|
+
* The key grammar is `<ORDER_STATUS>:<PLATFORM>` — the two dimensions every
|
|
5
|
+
* order-shaped materialized counter document declares (the same grammar as the
|
|
6
|
+
* sales overview and the discounts report). A component the source does not
|
|
7
|
+
* carry is encoded as `UNKNOWN`, never omitted, so a check without a platform
|
|
8
|
+
* still shows up in the totals.
|
|
9
|
+
*/
|
|
10
|
+
/** The key-component filters a dinein read may carry. */
|
|
11
|
+
export interface IDineinKeyFilters {
|
|
12
|
+
/** Selected order statuses (key components). */
|
|
13
|
+
order_statuses?: string[] | null;
|
|
14
|
+
/** Selected platforms (key components). */
|
|
15
|
+
platforms?: string[] | null;
|
|
16
|
+
}
|
|
17
|
+
/** Normalizes one key component: blank/missing -> `UNKNOWN`, `:` -> `-`. */
|
|
18
|
+
export declare function normalizeMaterializedDineinDimension(value: unknown): string;
|
|
19
|
+
/** Builds `<ORDER_STATUS>:<PLATFORM>`. */
|
|
20
|
+
export declare function buildMaterializedDineinKey(orderStatus: unknown, platform: unknown): string;
|
|
21
|
+
/** Splits a key back into its components, or null when it is malformed. */
|
|
22
|
+
export declare function parseMaterializedDineinKey(key: unknown): {
|
|
23
|
+
order_status: string;
|
|
24
|
+
platform: string;
|
|
25
|
+
} | null;
|
|
26
|
+
/**
|
|
27
|
+
* Whether one compiled KEY passes the request filters.
|
|
28
|
+
*
|
|
29
|
+
* Only whole key components are compared, so this is exactly the predicate the
|
|
30
|
+
* historical aggregation applies. `UNKNOWN` means "the source carried no
|
|
31
|
+
* value", never "everything the filter excluded".
|
|
32
|
+
*/
|
|
33
|
+
export declare function dineinKeyPassesFilters(key: unknown, filters?: IDineinKeyFilters | null): boolean;
|
|
34
|
+
/** True when the filter object selects no key component. */
|
|
35
|
+
export declare function isDineinKeyFilterEmpty(filters?: IDineinKeyFilters | null): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* The Mongo key regex for the `<ORDER_STATUS>:<PLATFORM>` grammar.
|
|
38
|
+
*
|
|
39
|
+
* Used by the historical aggregation so a filtered read never sums excluded
|
|
40
|
+
* keys. Returns null when the filters select nothing (no regex constraint).
|
|
41
|
+
*/
|
|
42
|
+
export declare function buildDineinKeyFilterRegex(filters?: IDineinKeyFilters | null): RegExp | null;
|
|
43
|
+
/** Restricts a counter map to the keys the filters accept. */
|
|
44
|
+
export declare function filterMaterializedDineinCounters<T>(counters: Record<string, T> | null | undefined, filters?: IDineinKeyFilters | null): Record<string, T>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeMaterializedDineinDimension = normalizeMaterializedDineinDimension;
|
|
4
|
+
exports.buildMaterializedDineinKey = buildMaterializedDineinKey;
|
|
5
|
+
exports.parseMaterializedDineinKey = parseMaterializedDineinKey;
|
|
6
|
+
exports.dineinKeyPassesFilters = dineinKeyPassesFilters;
|
|
7
|
+
exports.isDineinKeyFilterEmpty = isDineinKeyFilterEmpty;
|
|
8
|
+
exports.buildDineinKeyFilterRegex = buildDineinKeyFilterRegex;
|
|
9
|
+
exports.filterMaterializedDineinCounters = filterMaterializedDineinCounters;
|
|
10
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
11
|
+
/** Normalizes one key component: blank/missing -> `UNKNOWN`, `:` -> `-`. */
|
|
12
|
+
function normalizeMaterializedDineinDimension(value) {
|
|
13
|
+
if (value == null)
|
|
14
|
+
return MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
|
|
15
|
+
const text = String(value).trim();
|
|
16
|
+
if (text === '')
|
|
17
|
+
return MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
|
|
18
|
+
// The separator must never appear inside a component, or the key is ambiguous.
|
|
19
|
+
return text.split(MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_KEY_SEPARATOR).join('-');
|
|
20
|
+
}
|
|
21
|
+
/** Builds `<ORDER_STATUS>:<PLATFORM>`. */
|
|
22
|
+
function buildMaterializedDineinKey(orderStatus, platform) {
|
|
23
|
+
return (normalizeMaterializedDineinDimension(orderStatus) +
|
|
24
|
+
MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_KEY_SEPARATOR +
|
|
25
|
+
normalizeMaterializedDineinDimension(platform));
|
|
26
|
+
}
|
|
27
|
+
/** Splits a key back into its components, or null when it is malformed. */
|
|
28
|
+
function parseMaterializedDineinKey(key) {
|
|
29
|
+
if (key == null)
|
|
30
|
+
return null;
|
|
31
|
+
const parts = String(key).split(MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_KEY_SEPARATOR);
|
|
32
|
+
if (parts.length !== 2)
|
|
33
|
+
return null;
|
|
34
|
+
const [order_status, platform] = parts;
|
|
35
|
+
if (order_status === '' || platform === '')
|
|
36
|
+
return null;
|
|
37
|
+
return { order_status, platform };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Whether one compiled KEY passes the request filters.
|
|
41
|
+
*
|
|
42
|
+
* Only whole key components are compared, so this is exactly the predicate the
|
|
43
|
+
* historical aggregation applies. `UNKNOWN` means "the source carried no
|
|
44
|
+
* value", never "everything the filter excluded".
|
|
45
|
+
*/
|
|
46
|
+
function dineinKeyPassesFilters(key, filters) {
|
|
47
|
+
if (filters == null)
|
|
48
|
+
return true;
|
|
49
|
+
const parsed = parseMaterializedDineinKey(key);
|
|
50
|
+
if (parsed == null)
|
|
51
|
+
return false;
|
|
52
|
+
const orderStatuses = (filters.order_statuses ?? []).map((value) => String(value));
|
|
53
|
+
if (orderStatuses.length > 0 && !orderStatuses.includes(parsed.order_status))
|
|
54
|
+
return false;
|
|
55
|
+
const platforms = (filters.platforms ?? []).map((value) => String(value));
|
|
56
|
+
if (platforms.length > 0 && !platforms.includes(parsed.platform))
|
|
57
|
+
return false;
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
61
|
+
/** True when the filter object selects no key component. */
|
|
62
|
+
function isDineinKeyFilterEmpty(filters) {
|
|
63
|
+
if (filters == null)
|
|
64
|
+
return true;
|
|
65
|
+
const orderStatuses = filters.order_statuses ?? [];
|
|
66
|
+
const platforms = filters.platforms ?? [];
|
|
67
|
+
return orderStatuses.length === 0 && platforms.length === 0;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The Mongo key regex for the `<ORDER_STATUS>:<PLATFORM>` grammar.
|
|
71
|
+
*
|
|
72
|
+
* Used by the historical aggregation so a filtered read never sums excluded
|
|
73
|
+
* keys. Returns null when the filters select nothing (no regex constraint).
|
|
74
|
+
*/
|
|
75
|
+
function buildDineinKeyFilterRegex(filters) {
|
|
76
|
+
if (isDineinKeyFilterEmpty(filters))
|
|
77
|
+
return null;
|
|
78
|
+
const component = (values) => {
|
|
79
|
+
if (values == null || values.length === 0)
|
|
80
|
+
return '[^:]+';
|
|
81
|
+
return '(?:' + values.map((value) => escapeRegExp(String(value))).join('|') + ')';
|
|
82
|
+
};
|
|
83
|
+
return new RegExp('^' + component(filters?.order_statuses) + ':' + component(filters?.platforms) + '$');
|
|
84
|
+
}
|
|
85
|
+
/** Restricts a counter map to the keys the filters accept. */
|
|
86
|
+
function filterMaterializedDineinCounters(counters, filters) {
|
|
87
|
+
const source = counters ?? {};
|
|
88
|
+
if (filters == null)
|
|
89
|
+
return source;
|
|
90
|
+
const filtered = {};
|
|
91
|
+
for (const [key, block] of Object.entries(source)) {
|
|
92
|
+
if (dineinKeyPassesFilters(key, filters))
|
|
93
|
+
filtered[key] = block;
|
|
94
|
+
}
|
|
95
|
+
return filtered;
|
|
96
|
+
}
|
|
@@ -13,11 +13,14 @@ const MATERIALIZED_REPORTS_2 = require("../../constants/MATERIALIZED_REPORTS");
|
|
|
13
13
|
const materialized_tenant_day_readiness_1 = require("../../schemas/models/materialized_tenant_day_readiness");
|
|
14
14
|
const materialized_tenant_dialer_entries_1 = require("../../schemas/models/materialized_tenant_dialer_entries");
|
|
15
15
|
const materialized_tenant_dialer_reports_1 = require("../../schemas/models/materialized_tenant_dialer_reports");
|
|
16
|
+
const materialized_tenant_dinein_entries_1 = require("../../schemas/models/materialized_tenant_dinein_entries");
|
|
17
|
+
const materialized_tenant_dinein_reports_1 = require("../../schemas/models/materialized_tenant_dinein_reports");
|
|
16
18
|
const materialized_tenant_discount_entries_1 = require("../../schemas/models/materialized_tenant_discount_entries");
|
|
17
19
|
const materialized_tenant_discount_reports_1 = require("../../schemas/models/materialized_tenant_discount_reports");
|
|
18
20
|
const materialized_tenant_discount_sources_1 = require("../../schemas/models/materialized_tenant_discount_sources");
|
|
19
21
|
const materialized_tenant_prep_time_entries_1 = require("../../schemas/models/materialized_tenant_prep_time_entries");
|
|
20
22
|
const materialized_tenant_prep_time_reports_1 = require("../../schemas/models/materialized_tenant_prep_time_reports");
|
|
23
|
+
const materialized_tenant_report_runs_1 = require("../../schemas/models/materialized_tenant_report_runs");
|
|
21
24
|
const materialized_tenant_reports_iterations_1 = require("../../schemas/models/materialized_tenant_reports_iterations");
|
|
22
25
|
const materialized_tenant_sales_overview_reports_1 = require("../../schemas/models/materialized_tenant_sales_overview_reports");
|
|
23
26
|
const tenant_active_jobs_1 = require("../../schemas/models/tenant_active_jobs");
|
|
@@ -45,6 +48,7 @@ const INDEXES = {
|
|
|
45
48
|
[materialized_tenant_discount_sources_1.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME]: materialized_tenant_discount_sources_1.MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS,
|
|
46
49
|
[materialized_tenant_prep_time_reports_1.MATERIALIZED_TENANT_PREP_TIME_REPORTS_COLLECTION_NAME]: materialized_tenant_prep_time_reports_1.MATERIALIZED_TENANT_PREP_TIME_REPORTS_INDEX_OPTIONS,
|
|
47
50
|
[materialized_tenant_prep_time_entries_1.MATERIALIZED_TENANT_PREP_TIME_ENTRIES_COLLECTION_NAME]: materialized_tenant_prep_time_entries_1.MATERIALIZED_TENANT_PREP_TIME_ENTRIES_INDEX_OPTIONS,
|
|
51
|
+
[MATERIALIZED_REPORTS_2.MATERIALIZED_TENANT_REPORT_RUNS_COLLECTION_NAME]: materialized_tenant_report_runs_1.MATERIALIZED_TENANT_REPORT_RUNS_INDEX_OPTIONS,
|
|
48
52
|
[materialized_tenant_reports_iterations_1.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME]: materialized_tenant_reports_iterations_1.MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS,
|
|
49
53
|
[materialized_tenant_delivery_geo_cells_1.MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_COLLECTION_NAME]: materialized_tenant_delivery_geo_cells_1.MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_INDEX_OPTIONS,
|
|
50
54
|
[MATERIALIZED_REPORTS_1.MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_COLLECTION_NAME]: materialized_tenant_delivery_geo_scopes_1.MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_INDEX_OPTIONS,
|
|
@@ -53,6 +57,8 @@ const INDEXES = {
|
|
|
53
57
|
[MATERIALIZED_REPORTS_2.MATERIALIZED_TENANT_DAY_READINESS_COLLECTION_NAME]: materialized_tenant_day_readiness_1.MATERIALIZED_TENANT_DAY_READINESS_INDEX_OPTIONS,
|
|
54
58
|
[materialized_tenant_dialer_reports_1.MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_NAME]: materialized_tenant_dialer_reports_1.MATERIALIZED_TENANT_DIALER_REPORTS_INDEX_OPTIONS,
|
|
55
59
|
[materialized_tenant_dialer_entries_1.MATERIALIZED_TENANT_DIALER_ENTRIES_COLLECTION_NAME]: materialized_tenant_dialer_entries_1.MATERIALIZED_TENANT_DIALER_ENTRIES_INDEX_OPTIONS,
|
|
60
|
+
[materialized_tenant_dinein_reports_1.MATERIALIZED_TENANT_DINEIN_REPORTS_COLLECTION_NAME]: materialized_tenant_dinein_reports_1.MATERIALIZED_TENANT_DINEIN_REPORTS_INDEX_OPTIONS,
|
|
61
|
+
[materialized_tenant_dinein_entries_1.MATERIALIZED_TENANT_DINEIN_ENTRIES_COLLECTION_NAME]: materialized_tenant_dinein_entries_1.MATERIALIZED_TENANT_DINEIN_ENTRIES_INDEX_OPTIONS,
|
|
56
62
|
[materialized_report_drilldown_rows_1.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME]: materialized_report_drilldown_rows_1.MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS,
|
|
57
63
|
[materialized_report_drilldown_rows_1.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME]: materialized_report_drilldown_rows_1.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS,
|
|
58
64
|
[tenant_calendar_1.TENANT_CALENDAR_COLLECTION_NAME]: tenant_calendar_1.TENANT_CALENDAR_INDEX_OPTIONS,
|
|
@@ -74,6 +74,14 @@ const ROW_SPECS = {
|
|
|
74
74
|
orderId: () => '',
|
|
75
75
|
cursorId: (row) => row.event_id,
|
|
76
76
|
},
|
|
77
|
+
[MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DINEIN_ENTRIES]: {
|
|
78
|
+
rowsKey: 'dinein_entries',
|
|
79
|
+
// `time` is an ISO string on the check row; `toDate` accepts both.
|
|
80
|
+
time: (row) => row.time,
|
|
81
|
+
orderId: (row) => row.order_id,
|
|
82
|
+
// One row per collectable check, so the order itself is the row identity.
|
|
83
|
+
cursorId: (row) => row.order_id,
|
|
84
|
+
},
|
|
77
85
|
};
|
|
78
86
|
/** The nested map key an entry collection uses for its row sections. */
|
|
79
87
|
const getDrilldownRowsKey = (reportType) => ROW_SPECS[reportType].rowsKey;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM, type TMaterializedReadinessReportKey } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
/**
|
|
3
|
+
* PURE report-run-ledger helpers shared by the writer (workers-service) and the
|
|
4
|
+
* admin/retry API (operations-service).
|
|
5
|
+
*
|
|
6
|
+
* The ledger row identity is `(tenant_id, localDate, report_key,
|
|
7
|
+
* schema_version)` — the SAME identity as the readiness row, but a DIFFERENT
|
|
8
|
+
* collection with a DIFFERENT lifecycle:
|
|
9
|
+
*
|
|
10
|
+
* - readiness (COMMITTED / NEEDS_RECONCILIATION) is written INSIDE the report
|
|
11
|
+
* transaction, so it can never exist for a failed report;
|
|
12
|
+
* - the run row (PENDING / RUNNING / RETRYING / COMMITTED / FAILED) is written
|
|
13
|
+
* OUTSIDE the transaction, so a rollback cannot hide the attempt.
|
|
14
|
+
*/
|
|
15
|
+
export interface IMaterializedReportRunIdentity {
|
|
16
|
+
tenantId: string;
|
|
17
|
+
localDate: string;
|
|
18
|
+
reportKey: TMaterializedReadinessReportKey | string;
|
|
19
|
+
schemaVersion?: number;
|
|
20
|
+
}
|
|
21
|
+
/** Stable string key of one run row (Map keys / log correlation). */
|
|
22
|
+
export declare function materializedReportRunKey(parts: IMaterializedReportRunIdentity): string;
|
|
23
|
+
/** The Mongo filter of the ONE run row of a report. */
|
|
24
|
+
export declare function buildMaterializedReportRunFilter(parts: IMaterializedReportRunIdentity): {
|
|
25
|
+
tenant_id: string;
|
|
26
|
+
localDate: string;
|
|
27
|
+
report_key: string;
|
|
28
|
+
schema_version: number;
|
|
29
|
+
};
|
|
30
|
+
export declare function isMaterializedReportRunStatus(value: unknown): value is MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM;
|
|
31
|
+
/**
|
|
32
|
+
* Is the report CURRENTLY being executed?
|
|
33
|
+
*
|
|
34
|
+
* Only a run that owns the report may be in this state; the retry API refuses
|
|
35
|
+
* it (409) instead of racing the executor.
|
|
36
|
+
*/
|
|
37
|
+
export declare function isMaterializedReportRunInFlight(status: unknown): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Does this run carry the UNKNOWN-COMMIT marker?
|
|
40
|
+
*
|
|
41
|
+
* Such a run MUST stay non-terminal until recovery settles it from
|
|
42
|
+
* ownership-checked readiness (a ledger error alone must never decide it).
|
|
43
|
+
*/
|
|
44
|
+
export declare function isMaterializedReportRunCommitOutcomeUnknown(run: {
|
|
45
|
+
last_error?: string | null;
|
|
46
|
+
} | null | undefined): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Proves that a readiness row belongs to the run that owns this report.
|
|
49
|
+
*
|
|
50
|
+
* The row identity is the tenant/day/report/schema; ownership is the RUN
|
|
51
|
+
* identity. A row written by an OLDER close/re-close carries a DIFFERENT
|
|
52
|
+
* `run_id` and therefore proves nothing about the current run: it must keep the
|
|
53
|
+
* old report readable while the current run is recorded as FAILED
|
|
54
|
+
* (`INTERRUPTED_RUN`) and stays retryable. Never delete that older row.
|
|
55
|
+
*/
|
|
56
|
+
export declare function isMaterializedReadinessOwnedByRun(args: {
|
|
57
|
+
readiness: {
|
|
58
|
+
run_id?: string | null;
|
|
59
|
+
} | null | undefined;
|
|
60
|
+
run: {
|
|
61
|
+
run_id?: string | null;
|
|
62
|
+
} | null | undefined;
|
|
63
|
+
}): boolean;
|
|
64
|
+
/** COMMITTED / FAILED: the execution reached a terminal state. */
|
|
65
|
+
export declare function isMaterializedReportRunTerminal(status: unknown): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* The instant a run started (its `started_at`, or `updatedAt` when a legacy /
|
|
68
|
+
* partial row has none).
|
|
69
|
+
*/
|
|
70
|
+
export declare function materializedReportRunStartedAt(run: {
|
|
71
|
+
started_at?: Date | string | null;
|
|
72
|
+
updatedAt?: Date | string | null;
|
|
73
|
+
}): Date | null;
|
|
74
|
+
/**
|
|
75
|
+
* Is an IN-FLIGHT run old enough that an interruption is plausible?
|
|
76
|
+
*
|
|
77
|
+
* AGE ALONE IS NEVER ENOUGH: recovery must also observe a STALE
|
|
78
|
+
* `tenant_active_jobs` heartbeat, otherwise a healthy long-running report would
|
|
79
|
+
* be taken over. This helper only answers the age question.
|
|
80
|
+
*/
|
|
81
|
+
export declare function isMaterializedReportRunStale(args: {
|
|
82
|
+
run: {
|
|
83
|
+
status?: unknown;
|
|
84
|
+
started_at?: Date | string | null;
|
|
85
|
+
updatedAt?: Date | string | null;
|
|
86
|
+
};
|
|
87
|
+
now?: Date;
|
|
88
|
+
staleAfterMs?: number;
|
|
89
|
+
}): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* May this report be retried?
|
|
92
|
+
*
|
|
93
|
+
* A MISSING row is retryable (the report never ran: a failed day, a crash
|
|
94
|
+
* before the first attempt, or a pre-ledger day). A FAILED row is retryable.
|
|
95
|
+
* A COMMITTED row is NOT (the retry would be a no-op), and an in-flight row is
|
|
96
|
+
* NOT (the executor owns it).
|
|
97
|
+
*/
|
|
98
|
+
export declare function canRetryMaterializedReportRun(status: unknown): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Sanitizes an execution error for durable storage / API output.
|
|
101
|
+
*
|
|
102
|
+
* Deliberately conservative: the message is single-lined, PII-redacted and
|
|
103
|
+
* bounded. The ERROR STACK and raw driver payload are never stored.
|
|
104
|
+
*/
|
|
105
|
+
export declare function sanitizeMaterializedReportRunError(error: unknown): string | null;
|