@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,384 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isMaterializedDineinDayScope = exports.MATERIALIZED_DINEIN_PERIOD_SCOPES = void 0;
|
|
7
|
+
exports.getMaterializedDineinReportPeriodDayScopeFields = getMaterializedDineinReportPeriodDayScopeFields;
|
|
8
|
+
exports.needsMaterializedDineinReportWrite = needsMaterializedDineinReportWrite;
|
|
9
|
+
exports.needsMaterializedDineinEntriesWrite = needsMaterializedDineinEntriesWrite;
|
|
10
|
+
exports.foldMaterializedDineinPeriodCounters = foldMaterializedDineinPeriodCounters;
|
|
11
|
+
exports.buildTenantDineinCloseDayPlan = buildTenantDineinCloseDayPlan;
|
|
12
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
13
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
14
|
+
const materialized_tenant_dinein_reports_1 = require("../../schemas/models/materialized_tenant_dinein_reports");
|
|
15
|
+
const materialized_dinein_accumulator_helpers_1 = require("./materialized-dinein-accumulator-helpers");
|
|
16
|
+
const materialized_document_size_1 = require("./materialized-document-size");
|
|
17
|
+
const materialized_dinein_entry_pages_1 = require("./materialized-dinein-entry-pages");
|
|
18
|
+
const materialized_tenant_reports_iterations_helpers_1 = require("./materialized-tenant-reports-iterations-helpers");
|
|
19
|
+
const scope_helpers_1 = require("./scope-helpers");
|
|
20
|
+
/**
|
|
21
|
+
* Dinein close-day helpers.
|
|
22
|
+
*
|
|
23
|
+
* ## Period counters are RE-FOLDED, never patched
|
|
24
|
+
*
|
|
25
|
+
* Every touched period (week / ISO week / month / quarter / semester / year) is
|
|
26
|
+
* rebuilt from its whole set of DAY documents. Removing a day by decrementing
|
|
27
|
+
* its contribution is not usable here because the document also carries
|
|
28
|
+
* document-level BOUNDED maps (top tables / floors / courses / rates), which are
|
|
29
|
+
* re-ranked after the merge.
|
|
30
|
+
*
|
|
31
|
+
* ## The watermark comes from the FOLDED DAYS
|
|
32
|
+
*
|
|
33
|
+
* A folded document that inherited the STORED watermark would look unchanged to
|
|
34
|
+
* the write guard and be skipped forever. The watermark is the max over the DAY
|
|
35
|
+
* documents that belong to the period — never an unrelated day's.
|
|
36
|
+
*
|
|
37
|
+
* ## ENTRY pages are NOT folded
|
|
38
|
+
*
|
|
39
|
+
* `materialized_tenant_dinein_entries` is page-partitioned by source day. The
|
|
40
|
+
* plan never reads, merges or folds another day's page: the caller fetches
|
|
41
|
+
* exactly the closing day's pages, each is replaced by its page identity, and a
|
|
42
|
+
* fetched page the day no longer owns is deleted.
|
|
43
|
+
*/
|
|
44
|
+
/** Period COUNTER scopes this report materializes (everything but the day scope). */
|
|
45
|
+
exports.MATERIALIZED_DINEIN_PERIOD_SCOPES = MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_REPORT_SCOPES.filter((scope) => String(scope) !== MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
|
|
46
|
+
/**
|
|
47
|
+
* Every `localDate` label whose DAY counter document can contribute to a REPORT
|
|
48
|
+
* period the closing day rolls into - week through YEAR. The worker uses it to
|
|
49
|
+
* fetch exactly the sibling DAY counters the period re-fold needs.
|
|
50
|
+
*/
|
|
51
|
+
function getMaterializedDineinReportPeriodDayScopeFields(localDate) {
|
|
52
|
+
return (0, scope_helpers_1.enumerateMaterializedPeriodDays)(localDate, exports.MATERIALIZED_DINEIN_PERIOD_SCOPES);
|
|
53
|
+
}
|
|
54
|
+
/** True when the scope is the plain day scope. */
|
|
55
|
+
const isMaterializedDineinDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
|
|
56
|
+
exports.isMaterializedDineinDayScope = isMaterializedDineinDayScope;
|
|
57
|
+
const dineinScopeKey = (doc) => String(doc?.scope_context) + '|' + String(doc?.scope_field);
|
|
58
|
+
/** True when the stored report document does not already carry this content. */
|
|
59
|
+
function needsMaterializedDineinReportWrite(existing, next) {
|
|
60
|
+
if (existing == null)
|
|
61
|
+
return true;
|
|
62
|
+
const stored = new Date(existing.source_watermark ?? 0).getTime();
|
|
63
|
+
const incoming = new Date(next.source_watermark ?? 0).getTime();
|
|
64
|
+
if (Number.isNaN(stored) || Number.isNaN(incoming))
|
|
65
|
+
return true;
|
|
66
|
+
return incoming > stored;
|
|
67
|
+
}
|
|
68
|
+
/** True when the stored entry page does not already carry these rows. */
|
|
69
|
+
function needsMaterializedDineinEntriesWrite(existing, next) {
|
|
70
|
+
if (existing == null)
|
|
71
|
+
return true;
|
|
72
|
+
const stored = new Date(existing.source_watermark ?? 0).getTime();
|
|
73
|
+
const incoming = new Date(next.source_watermark ?? 0).getTime();
|
|
74
|
+
if (Number.isNaN(stored) || Number.isNaN(incoming))
|
|
75
|
+
return true;
|
|
76
|
+
return incoming > stored;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Folds every DAY document belonging to one period scope.
|
|
80
|
+
*
|
|
81
|
+
* Both the keyed counters and the document-level reservation block are additive;
|
|
82
|
+
* the bounded maps are re-bounded by the caller (`pruneDocumentDineinMaps`).
|
|
83
|
+
*/
|
|
84
|
+
function foldMaterializedDineinPeriodCounters(input) {
|
|
85
|
+
const { periodScope, periodScopeField, dayDocuments } = input;
|
|
86
|
+
const counters = {};
|
|
87
|
+
const reservations = (0, materialized_dinein_accumulator_helpers_1.freshMaterializedDineinReservationCounters)();
|
|
88
|
+
const upcomingLists = [];
|
|
89
|
+
const editedLists = [];
|
|
90
|
+
for (const dayDocument of dayDocuments ?? []) {
|
|
91
|
+
if (!(0, exports.isMaterializedDineinDayScope)(dayDocument?.scope_context))
|
|
92
|
+
continue;
|
|
93
|
+
const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(String(dayDocument.scope_field));
|
|
94
|
+
if (fields == null)
|
|
95
|
+
continue;
|
|
96
|
+
if (fields[periodScope] !== periodScopeField)
|
|
97
|
+
continue;
|
|
98
|
+
for (const [key, block] of Object.entries(dayDocument.dinein_counters ?? {})) {
|
|
99
|
+
counters[key] = (0, materialized_dinein_accumulator_helpers_1.mergeMaterializedDineinCounters)(counters[key] ?? (0, materialized_dinein_accumulator_helpers_1.freshMaterializedDineinCounters)(), block);
|
|
100
|
+
}
|
|
101
|
+
(0, materialized_dinein_accumulator_helpers_1.mergeMaterializedDineinReservationCounters)(reservations, dayDocument.reservation_counters ?? {});
|
|
102
|
+
upcomingLists.push(dayDocument.upcoming_reservations ?? null);
|
|
103
|
+
editedLists.push(dayDocument.top_edited_checks ?? null);
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
counters,
|
|
107
|
+
reservations,
|
|
108
|
+
// The DAY arrays are already bounded and masked; a period re-ranks their
|
|
109
|
+
// union and caps it again, so the document cannot grow with the period.
|
|
110
|
+
upcomingReservations: (0, materialized_dinein_accumulator_helpers_1.mergeMaterializedDineinUpcomingReservations)(upcomingLists, MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_UPCOMING_RESERVATION_LIMIT),
|
|
111
|
+
topEditedChecks: (0, materialized_dinein_accumulator_helpers_1.mergeMaterializedDineinTopEditedChecks)(editedLists, MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_TOP_EDITED_CHECK_LIMIT),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Builds the close-day plan for ONE tenant-day.
|
|
116
|
+
*
|
|
117
|
+
* The DAY scope comes from the compile. Every PERIOD scope the day touches is
|
|
118
|
+
* RE-FOLDED from its whole day set, and the touched set is the union of the
|
|
119
|
+
* closed day's periods AND every removed day's periods - emptying a day must
|
|
120
|
+
* clear it from the periods it used to belong to.
|
|
121
|
+
*/
|
|
122
|
+
function buildTenantDineinCloseDayPlan(input) {
|
|
123
|
+
if (input == null)
|
|
124
|
+
return null;
|
|
125
|
+
const { tenantId, reports, entries, reason } = input;
|
|
126
|
+
if (typeof tenantId !== 'string' || tenantId.length === 0)
|
|
127
|
+
return null;
|
|
128
|
+
if (typeof reason !== 'string' || reason.length === 0)
|
|
129
|
+
return null;
|
|
130
|
+
const computedAt = input.computedAt ?? new Date();
|
|
131
|
+
const force = input.force === true;
|
|
132
|
+
const compiledReports = (reports ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
133
|
+
const compiledEntries = (entries ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
134
|
+
const storedReports = (input.existing ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
135
|
+
const storedEntries = (input.existingEntries ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
136
|
+
const compiledDateDoc = compiledReports.find((doc) => (0, exports.isMaterializedDineinDayScope)(doc.scope_context));
|
|
137
|
+
const localDate = input.localDate ?? (compiledDateDoc == null ? null : String(compiledDateDoc.scope_field));
|
|
138
|
+
if (localDate == null || localDate === '')
|
|
139
|
+
return null;
|
|
140
|
+
const writes = [];
|
|
141
|
+
const entryWrites = [];
|
|
142
|
+
const staleDocuments = [];
|
|
143
|
+
const staleEntryDocuments = [];
|
|
144
|
+
const skippedDocuments = [];
|
|
145
|
+
const skippedEntryDocuments = [];
|
|
146
|
+
const storedReportsByScope = new Map();
|
|
147
|
+
for (const doc of storedReports)
|
|
148
|
+
storedReportsByScope.set(dineinScopeKey(doc), doc);
|
|
149
|
+
const storedEntriesByPage = new Map();
|
|
150
|
+
for (const doc of storedEntries) {
|
|
151
|
+
// Legacy documents without a page are inconsistent input - the caller must
|
|
152
|
+
// only fetch page-partitioned documents (the reader keeps no legacy fallback).
|
|
153
|
+
if (typeof doc.page !== 'string' || doc.page === '')
|
|
154
|
+
return null;
|
|
155
|
+
storedEntriesByPage.set((0, materialized_dinein_entry_pages_1.materializedDineinEntryPageKey)(doc), doc);
|
|
156
|
+
}
|
|
157
|
+
const addWrite = (report) => {
|
|
158
|
+
const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
|
|
159
|
+
const iteration = (0, materialized_tenant_reports_iterations_helpers_1.buildMaterializedTenantReportIteration)(persisted, {
|
|
160
|
+
reportType: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DINEIN_REPORTS,
|
|
161
|
+
reason,
|
|
162
|
+
computedAt,
|
|
163
|
+
});
|
|
164
|
+
if (iteration == null)
|
|
165
|
+
return false;
|
|
166
|
+
writes.push({ report: persisted, iteration: iteration });
|
|
167
|
+
return true;
|
|
168
|
+
};
|
|
169
|
+
const addEntryWrite = (report) => {
|
|
170
|
+
const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
|
|
171
|
+
const iteration = (0, materialized_tenant_reports_iterations_helpers_1.buildMaterializedTenantReportIteration)(persisted, {
|
|
172
|
+
reportType: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DINEIN_ENTRIES,
|
|
173
|
+
reason,
|
|
174
|
+
computedAt,
|
|
175
|
+
});
|
|
176
|
+
if (iteration == null)
|
|
177
|
+
return false;
|
|
178
|
+
entryWrites.push({ report: persisted, iteration: iteration });
|
|
179
|
+
return true;
|
|
180
|
+
};
|
|
181
|
+
// ---- the DAY scope, straight from the compile ----
|
|
182
|
+
if (compiledDateDoc != null) {
|
|
183
|
+
const existing = storedReportsByScope.get(dineinScopeKey(compiledDateDoc));
|
|
184
|
+
const next = {
|
|
185
|
+
...compiledDateDoc,
|
|
186
|
+
_id: existing?._id ?? new mongoose_1.default.Types.ObjectId().toHexString(),
|
|
187
|
+
iteration: Number(existing?.iteration ?? 0) + 1,
|
|
188
|
+
};
|
|
189
|
+
if (force || needsMaterializedDineinReportWrite(existing, next))
|
|
190
|
+
addWrite(next);
|
|
191
|
+
else
|
|
192
|
+
skippedDocuments.push(existing);
|
|
193
|
+
}
|
|
194
|
+
// ---- ENTRY PAGES: exactly the pages the closing day owns ----
|
|
195
|
+
const compiledEntriesByPage = new Map();
|
|
196
|
+
for (const doc of compiledEntries) {
|
|
197
|
+
const expectedPage = (0, materialized_dinein_entry_pages_1.dineinEntryPageOf)(doc.scope_context, localDate);
|
|
198
|
+
if (expectedPage == null || (doc.page !== expectedPage && !String(doc.page ?? '').startsWith(expectedPage + '#'))) {
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
const key = (0, materialized_dinein_entry_pages_1.materializedDineinEntryPageKey)(doc);
|
|
202
|
+
if (compiledEntriesByPage.has(key))
|
|
203
|
+
return null; // duplicate page - data issue
|
|
204
|
+
compiledEntriesByPage.set(key, doc);
|
|
205
|
+
}
|
|
206
|
+
// ---- DAY deletion: a stored day doc that no longer compiled ----
|
|
207
|
+
const compiledDateKeys = new Set(compiledReports.filter((doc) => (0, exports.isMaterializedDineinDayScope)(doc.scope_context)).map((doc) => dineinScopeKey(doc)));
|
|
208
|
+
const removedDates = [];
|
|
209
|
+
for (const doc of storedReports) {
|
|
210
|
+
if (!(0, exports.isMaterializedDineinDayScope)(doc.scope_context))
|
|
211
|
+
continue;
|
|
212
|
+
if (String(doc.scope_field) !== localDate)
|
|
213
|
+
continue;
|
|
214
|
+
if (compiledDateKeys.has(dineinScopeKey(doc)))
|
|
215
|
+
continue;
|
|
216
|
+
staleDocuments.push(doc);
|
|
217
|
+
removedDates.push(String(doc.scope_field));
|
|
218
|
+
}
|
|
219
|
+
// ---- PERIOD re-fold ----
|
|
220
|
+
const nextDayDocs = storedReports.filter((doc) => (0, exports.isMaterializedDineinDayScope)(doc.scope_context) && String(doc.scope_field) !== localDate);
|
|
221
|
+
if (compiledDateDoc != null) {
|
|
222
|
+
nextDayDocs.push(writes.find((write) => (0, exports.isMaterializedDineinDayScope)(write.report.scope_context))?.report ??
|
|
223
|
+
compiledDateDoc);
|
|
224
|
+
}
|
|
225
|
+
const touchedPeriods = new Map();
|
|
226
|
+
const noteDay = (day) => {
|
|
227
|
+
const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(day);
|
|
228
|
+
if (fields == null)
|
|
229
|
+
return;
|
|
230
|
+
for (const scope of exports.MATERIALIZED_DINEIN_PERIOD_SCOPES) {
|
|
231
|
+
const field = fields[String(scope)];
|
|
232
|
+
if (field == null || field === '')
|
|
233
|
+
continue;
|
|
234
|
+
touchedPeriods.set(String(scope) + '|' + field, { scope: String(scope), field });
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
noteDay(localDate);
|
|
238
|
+
for (const day of removedDates)
|
|
239
|
+
noteDay(day);
|
|
240
|
+
for (const { scope, field } of touchedPeriods.values()) {
|
|
241
|
+
const folded = foldMaterializedDineinPeriodCounters({
|
|
242
|
+
periodScope: scope,
|
|
243
|
+
periodScopeField: field,
|
|
244
|
+
dayDocuments: nextDayDocs,
|
|
245
|
+
});
|
|
246
|
+
// Each DAY is bounded to top-N tables/floors/courses/rates. The period fold
|
|
247
|
+
// unions 28-31 such bounds, so re-bound at the DOCUMENT level after the merge.
|
|
248
|
+
(0, materialized_dinein_accumulator_helpers_1.pruneDocumentDineinMaps)(folded.counters, {
|
|
249
|
+
tables: MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_TABLE_LIMIT,
|
|
250
|
+
floors: MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_FLOOR_LIMIT,
|
|
251
|
+
courses: MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_COURSE_LIMIT,
|
|
252
|
+
serviceChargeRates: MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_SERVICE_CHARGE_RATE_LIMIT,
|
|
253
|
+
});
|
|
254
|
+
const existing = storedReportsByScope.get(scope + '|' + field);
|
|
255
|
+
const keys = Object.keys(folded.counters).sort();
|
|
256
|
+
// `by_hour` is a nested MAP; summing the block's values would coerce it to
|
|
257
|
+
// NaN and keep an empty period alive forever. Check the scalar totals only.
|
|
258
|
+
const hasReservations = folded.reservations.reservations !== 0 ||
|
|
259
|
+
folded.reservations.reservation_covers !== 0 ||
|
|
260
|
+
folded.reservations.reservation_confirmed !== 0 ||
|
|
261
|
+
folded.reservations.reservation_pending !== 0 ||
|
|
262
|
+
folded.reservations.reservation_cancelled !== 0 ||
|
|
263
|
+
folded.reservations.reservation_lead_count !== 0 ||
|
|
264
|
+
folded.reservations.reservation_duration_count !== 0;
|
|
265
|
+
if (keys.length === 0 && !hasReservations) {
|
|
266
|
+
if (existing != null)
|
|
267
|
+
staleDocuments.push(existing);
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
const counters = {};
|
|
271
|
+
for (const key of keys)
|
|
272
|
+
counters[key] = folded.counters[key];
|
|
273
|
+
const report = {
|
|
274
|
+
_id: existing?._id ?? new mongoose_1.default.Types.ObjectId().toHexString(),
|
|
275
|
+
tenant_id: tenantId,
|
|
276
|
+
brand_id: existing?.brand_id ?? compiledDateDoc?.brand_id ?? '',
|
|
277
|
+
scope_context: scope,
|
|
278
|
+
scope_field: field,
|
|
279
|
+
day_index: null,
|
|
280
|
+
// Page-model documents carry the CURRENT key dimensions. Never inherit a
|
|
281
|
+
// pre-page filter array: the reader's `$all` gate would keep the period
|
|
282
|
+
// document invisible forever.
|
|
283
|
+
sale_type_key_filters: [...MATERIALIZED_REPORTS_1.DINEIN_KEY_FILTERS],
|
|
284
|
+
dinein_counters: counters,
|
|
285
|
+
reservation_counters: folded.reservations,
|
|
286
|
+
upcoming_reservations: folded.upcomingReservations,
|
|
287
|
+
top_edited_checks: folded.topEditedChecks,
|
|
288
|
+
iteration: existing?.iteration,
|
|
289
|
+
// A folded period is always a CURRENT-shape document.
|
|
290
|
+
schema_version: compiledDateDoc?.schema_version ?? materialized_tenant_dinein_reports_1.MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION,
|
|
291
|
+
// FROM THE DAYS THAT BELONG TO THIS PERIOD - never the stored watermark.
|
|
292
|
+
source_watermark: watermarkOfDays(nextDayDocs.filter((dayDoc) => {
|
|
293
|
+
const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(String(dayDoc?.scope_field));
|
|
294
|
+
return fields != null && fields[scope] === field;
|
|
295
|
+
}), computedAt),
|
|
296
|
+
computed_at: computedAt,
|
|
297
|
+
createdAt: existing?.createdAt ?? computedAt,
|
|
298
|
+
updatedAt: computedAt,
|
|
299
|
+
};
|
|
300
|
+
const next = { ...report, iteration: Number(existing?.iteration ?? 0) + 1 };
|
|
301
|
+
if (force || needsMaterializedDineinReportWrite(existing, next))
|
|
302
|
+
addWrite(next);
|
|
303
|
+
else
|
|
304
|
+
skippedDocuments.push(existing);
|
|
305
|
+
}
|
|
306
|
+
// ---- entry PAGE writes (replace by page identity) ----
|
|
307
|
+
for (const compiled of compiledEntriesByPage.values()) {
|
|
308
|
+
const key = (0, materialized_dinein_entry_pages_1.materializedDineinEntryPageKey)(compiled);
|
|
309
|
+
const existing = storedEntriesByPage.get(key) ?? null;
|
|
310
|
+
const next = {
|
|
311
|
+
...compiled,
|
|
312
|
+
_id: existing?._id ?? new mongoose_1.default.Types.ObjectId().toHexString(),
|
|
313
|
+
iteration: Number(existing?.iteration ?? 0) + 1,
|
|
314
|
+
};
|
|
315
|
+
if (existing == null || force || needsMaterializedDineinEntriesWrite(existing, next)) {
|
|
316
|
+
if (!addEntryWrite(next))
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
skippedEntryDocuments.push(existing);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
// Anything fetched but not recompiled is a page this day no longer owns.
|
|
324
|
+
for (const doc of storedEntriesByPage.values()) {
|
|
325
|
+
if (compiledEntriesByPage.has((0, materialized_dinein_entry_pages_1.materializedDineinEntryPageKey)(doc)))
|
|
326
|
+
continue;
|
|
327
|
+
staleEntryDocuments.push(doc);
|
|
328
|
+
}
|
|
329
|
+
// BSON BUDGETS: fail here, naming the collection + day + scope, instead of at
|
|
330
|
+
// bulkWrite inside the close.
|
|
331
|
+
for (const write of writes) {
|
|
332
|
+
(0, materialized_document_size_1.assertMaterializedDocumentSize)(write.report, {
|
|
333
|
+
label: 'materialized_tenant_dinein_reports:' +
|
|
334
|
+
tenantId +
|
|
335
|
+
':' +
|
|
336
|
+
localDate +
|
|
337
|
+
':' +
|
|
338
|
+
String(write.report.scope_context) +
|
|
339
|
+
':' +
|
|
340
|
+
write.report.scope_field,
|
|
341
|
+
maxBytes: MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_REPORT_MAX_BSON_BYTES,
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
for (const write of entryWrites) {
|
|
345
|
+
(0, materialized_document_size_1.assertMaterializedDocumentSize)(write.report, {
|
|
346
|
+
label: 'materialized_tenant_dinein_entries:' +
|
|
347
|
+
tenantId +
|
|
348
|
+
':' +
|
|
349
|
+
localDate +
|
|
350
|
+
':' +
|
|
351
|
+
String(write.report.scope_context) +
|
|
352
|
+
':' +
|
|
353
|
+
write.report.scope_field +
|
|
354
|
+
':page=' +
|
|
355
|
+
write.report.page,
|
|
356
|
+
maxBytes: MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_ENTRY_MAX_BSON_BYTES,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
return {
|
|
360
|
+
writes,
|
|
361
|
+
entryWrites,
|
|
362
|
+
staleDocuments,
|
|
363
|
+
staleEntryDocuments,
|
|
364
|
+
skippedDocuments,
|
|
365
|
+
skippedEntryDocuments,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Max watermark across the day documents that feed a fold.
|
|
370
|
+
*
|
|
371
|
+
* The value comes from the FOLDED DAYS only. Inheriting the stored document's
|
|
372
|
+
* watermark (or the close instant) as a floor would make a re-fold look
|
|
373
|
+
* unchanged to the write guard and freeze the period at its first closed day.
|
|
374
|
+
* The fallback is used only when no day document carries a usable watermark.
|
|
375
|
+
*/
|
|
376
|
+
function watermarkOfDays(dayDocs, fallback) {
|
|
377
|
+
let latest = 0;
|
|
378
|
+
for (const doc of dayDocs) {
|
|
379
|
+
const time = new Date(doc?.source_watermark ?? 0).getTime();
|
|
380
|
+
if (!Number.isNaN(time) && time > latest)
|
|
381
|
+
latest = time;
|
|
382
|
+
}
|
|
383
|
+
return latest === 0 ? fallback : new Date(latest);
|
|
384
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { IDineinSourceDayCounts, IMaterializedTenantDineinEntry } from '../../schemas/types/materialized_tenant_dinein_entries';
|
|
2
|
+
import { IMaterializedTenantDineinReport } from '../../schemas/types/materialized_tenant_dinein_reports';
|
|
3
|
+
import { IDineinFingerprintSource, IDineinOrderSource, IDineinReservationSource } from './materialized-dinein-classification-helpers';
|
|
4
|
+
/**
|
|
5
|
+
* Dinein — the day compile.
|
|
6
|
+
*
|
|
7
|
+
* Turns ONE business day's dine-in `orders` (+ the day's table fingerprints and
|
|
8
|
+
* reservations) into:
|
|
9
|
+
* - the `localDate` COUNTER document (period counters are folded by the close
|
|
10
|
+
* plan from the sibling DAY documents);
|
|
11
|
+
* - ONE ENTRY PAGE per entry scope (date / week / ISO week / month). The day
|
|
12
|
+
* page uses `page='all'`; each period page is partitioned by the source date,
|
|
13
|
+
* so a document is bounded by ONE day's checks.
|
|
14
|
+
*
|
|
15
|
+
* ## The source day is `order.localDate`, never the fingerprint day
|
|
16
|
+
*
|
|
17
|
+
* A table can close after midnight; the check still belongs to the day it was
|
|
18
|
+
* sold on. A fingerprint is joined by `order_id` and attributed to the JOINED
|
|
19
|
+
* order's day, so a late close never leaks into the next day's report.
|
|
20
|
+
*
|
|
21
|
+
* ## Nothing is invented
|
|
22
|
+
*
|
|
23
|
+
* - A check with no fingerprint has NO covers and NO session (coverage gap).
|
|
24
|
+
* - A fingerprint with no end, or a session that ends before it starts, is
|
|
25
|
+
* skipped (coverage gap), never stored as 0.
|
|
26
|
+
* - A reservation without a usable `reservation_time` is skipped
|
|
27
|
+
* (coverage gap), never dated by guesswork.
|
|
28
|
+
*
|
|
29
|
+
* Returns `null` for invalid arguments, or for a day with NO dine-in check and
|
|
30
|
+
* NO reservation: an emptied day writes nothing and the close plan deletes the
|
|
31
|
+
* documents/pages it used to own. Absence in a historical read means zero.
|
|
32
|
+
*/
|
|
33
|
+
export interface IDineinDayCompileInput {
|
|
34
|
+
tenantId: string;
|
|
35
|
+
brandId: string;
|
|
36
|
+
/** The closing tenant-local business day (`YYYY-MM-DD`). */
|
|
37
|
+
localDate: string;
|
|
38
|
+
/** The day's NON-DELETED dine-in orders (split children + non-split checks). */
|
|
39
|
+
orders?: IDineinOrderSource[];
|
|
40
|
+
/**
|
|
41
|
+
* The day's auto-deleted split PARENTS. They are excluded from sales/order
|
|
42
|
+
* counts and read only for rating/review discovery.
|
|
43
|
+
*/
|
|
44
|
+
deletedSplitParents?: IDineinOrderSource[];
|
|
45
|
+
/** The day's table-close fingerprints (already scoped to the day's orders). */
|
|
46
|
+
fingerprints?: IDineinFingerprintSource[];
|
|
47
|
+
/** The day's reservations (the caller resolved the tenant-local day). */
|
|
48
|
+
reservations?: IDineinReservationSource[];
|
|
49
|
+
/**
|
|
50
|
+
* Tenant's operational GMT offset in hours (may be fractional).
|
|
51
|
+
*
|
|
52
|
+
* `localDate` is already tenant-local, but a reservation stores an ABSOLUTE
|
|
53
|
+
* instant: the hour of the reservation chart is derived with this offset.
|
|
54
|
+
* Defaults to 0 (the caller already supplied tenant-local values).
|
|
55
|
+
*/
|
|
56
|
+
gmtOffset?: number;
|
|
57
|
+
computedAt?: Date;
|
|
58
|
+
}
|
|
59
|
+
export interface IDineinDayCompileResult {
|
|
60
|
+
reports: IMaterializedTenantDineinReport[];
|
|
61
|
+
entries: IMaterializedTenantDineinEntry[];
|
|
62
|
+
coverage: IDineinSourceDayCounts;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The server-side MASKED guest label for an upcoming reservation.
|
|
66
|
+
*
|
|
67
|
+
* Only initials are emitted (at most two, `A. K.`), never the full name, and
|
|
68
|
+
* `Guest` when the source carries nothing usable. The frontend never receives
|
|
69
|
+
* the name, phone or email.
|
|
70
|
+
*/
|
|
71
|
+
export declare function maskReservationGuestLabel(name: unknown): string;
|
|
72
|
+
/** Compiles one business day into a counter document plus its entry pages. */
|
|
73
|
+
export declare function compileTenantDineinReports(input: IDineinDayCompileInput): IDineinDayCompileResult | null;
|