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