@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,500 @@
|
|
|
1
|
+
import { MATERIALIZED_DINEIN_ENTRY_SCOPES, MATERIALIZED_DINEIN_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DINEIN_HOUR_KEYS, MATERIALIZED_DINEIN_REPORT_MAX_BSON_BYTES, MATERIALIZED_DINEIN_TOP_EDITED_CHECK_LIMIT, MATERIALIZED_DINEIN_UNKNOWN_TOKEN, MATERIALIZED_DINEIN_UPCOMING_RESERVATION_LIMIT, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { MATERIALIZED_TENANT_DINEIN_ENTRIES_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_dinein_entries';
|
|
3
|
+
import { MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_dinein_reports';
|
|
4
|
+
import { MATERIALIZED_DINEIN_REPORT_KEY_FILTERS, } from '../../schemas/types/materialized_tenant_dinein_reports';
|
|
5
|
+
import { freshMaterializedDineinCounters, freshMaterializedDineinReservationCounters, mergeMaterializedDineinTopEditedChecks, mergeMaterializedDineinUpcomingReservations, pruneDocumentDineinMaps, } from './materialized-dinein-accumulator-helpers';
|
|
6
|
+
import { addDineinOrderToCounters, classifyMaterializedDineinOrigin, freshDineinCoverage, isDeletedSplitParent, isDineinOrder, itemLineDelta, itemQuantityOf, splitRoleOf, toDineinDate as toDate, toDineinInt as num, validRatingOf, } from './materialized-dinein-classification-helpers';
|
|
7
|
+
import { dineinEntryPageOf, splitMaterializedDineinEntryPage } from './materialized-dinein-entry-pages';
|
|
8
|
+
import { buildMaterializedDineinKey } from './materialized-dinein-key-helpers';
|
|
9
|
+
import { assertMaterializedDocumentSize } from './materialized-document-size';
|
|
10
|
+
import { getDayIndexFromLocalDate, getMaterializedPeriodScopeFields, parseMaterializedLocalDate, } from './scope-helpers';
|
|
11
|
+
const text = (value) => (value == null ? '' : String(value).trim());
|
|
12
|
+
/**
|
|
13
|
+
* An OPTIONAL order number.
|
|
14
|
+
*
|
|
15
|
+
* `Number(null)` is 0, so a missing number must be rejected explicitly — a
|
|
16
|
+
* check without a number is `null` on the wire, never `#0`.
|
|
17
|
+
*/
|
|
18
|
+
const optionalOrderNumber = (value) => {
|
|
19
|
+
if (value == null || value === '')
|
|
20
|
+
return null;
|
|
21
|
+
const numeric = Number(value);
|
|
22
|
+
return Number.isInteger(numeric) ? numeric : null;
|
|
23
|
+
};
|
|
24
|
+
const byIdAsc = (a, b) => {
|
|
25
|
+
const left = text(a?._id);
|
|
26
|
+
const right = text(b?._id);
|
|
27
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
28
|
+
};
|
|
29
|
+
const laterOf = (current, candidate) => {
|
|
30
|
+
const time = candidate == null ? NaN : new Date(candidate).getTime();
|
|
31
|
+
if (Number.isNaN(time))
|
|
32
|
+
return current;
|
|
33
|
+
return time > current.getTime() ? new Date(time) : current;
|
|
34
|
+
};
|
|
35
|
+
/** The tenant-local hour (`0`..`23`) of an instant, or null when unusable. */
|
|
36
|
+
function tenantLocalHourOf(value, gmtOffset) {
|
|
37
|
+
const date = toDate(value);
|
|
38
|
+
if (date == null || !Number.isFinite(gmtOffset))
|
|
39
|
+
return null;
|
|
40
|
+
const shifted = new Date(date.getTime() + Math.round(gmtOffset * 60 * 60 * 1000));
|
|
41
|
+
return shifted.getUTCHours();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The server-side MASKED guest label for an upcoming reservation.
|
|
45
|
+
*
|
|
46
|
+
* Only initials are emitted (at most two, `A. K.`), never the full name, and
|
|
47
|
+
* `Guest` when the source carries nothing usable. The frontend never receives
|
|
48
|
+
* the name, phone or email.
|
|
49
|
+
*/
|
|
50
|
+
export function maskReservationGuestLabel(name) {
|
|
51
|
+
const words = text(name)
|
|
52
|
+
.split(/\s+/)
|
|
53
|
+
// Only NAME words contribute: a digit / symbol token is not an initial.
|
|
54
|
+
.filter((word) => /^[A-Za-z]/.test(word));
|
|
55
|
+
const initials = words
|
|
56
|
+
.slice(0, 2)
|
|
57
|
+
.map((word) => word[0].toUpperCase() + '.')
|
|
58
|
+
.join(' ');
|
|
59
|
+
return initials === '' ? 'Guest' : initials;
|
|
60
|
+
}
|
|
61
|
+
/** Compiles one business day into a counter document plus its entry pages. */
|
|
62
|
+
export function compileTenantDineinReports(input) {
|
|
63
|
+
var _a;
|
|
64
|
+
if (input == null)
|
|
65
|
+
return null;
|
|
66
|
+
const { tenantId, brandId, localDate } = input;
|
|
67
|
+
if (typeof tenantId !== 'string' ||
|
|
68
|
+
tenantId === '' ||
|
|
69
|
+
typeof brandId !== 'string' ||
|
|
70
|
+
typeof localDate !== 'string' ||
|
|
71
|
+
parseMaterializedLocalDate(localDate) == null) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const computedAt = input.computedAt ?? new Date();
|
|
75
|
+
const gmtOffset = Number.isFinite(Number(input.gmtOffset)) ? Number(input.gmtOffset) : 0;
|
|
76
|
+
const coverage = freshDineinCoverage();
|
|
77
|
+
let watermark = computedAt;
|
|
78
|
+
/* -------------------- split parents (rating discovery only) -------------------- */
|
|
79
|
+
const parentById = new Map();
|
|
80
|
+
for (const parent of input.deletedSplitParents ?? []) {
|
|
81
|
+
if (parent == null)
|
|
82
|
+
continue;
|
|
83
|
+
if (String(parent.tenant_id ?? tenantId) !== tenantId)
|
|
84
|
+
continue;
|
|
85
|
+
if (!isDineinOrder(parent))
|
|
86
|
+
continue;
|
|
87
|
+
if (!isDeletedSplitParent(parent))
|
|
88
|
+
continue;
|
|
89
|
+
const id = text(parent._id);
|
|
90
|
+
if (id === '')
|
|
91
|
+
continue;
|
|
92
|
+
parentById.set(id, parent);
|
|
93
|
+
}
|
|
94
|
+
coverage.deleted_split_parents_excluded = parentById.size;
|
|
95
|
+
for (const parent of parentById.values())
|
|
96
|
+
watermark = laterOf(watermark, parent.updatedAt ?? parent.createdAt);
|
|
97
|
+
/* -------------------- the count population -------------------- */
|
|
98
|
+
const counted = [];
|
|
99
|
+
const countedById = new Map();
|
|
100
|
+
for (const order of input.orders ?? []) {
|
|
101
|
+
if (order == null)
|
|
102
|
+
continue;
|
|
103
|
+
if (String(order.tenant_id ?? tenantId) !== tenantId)
|
|
104
|
+
continue;
|
|
105
|
+
if (!isDineinOrder(order))
|
|
106
|
+
continue;
|
|
107
|
+
// The deleted split parent is NEVER a collectable check, even when the
|
|
108
|
+
// caller passes it in the regular orders array.
|
|
109
|
+
if (order.is_deleted === true)
|
|
110
|
+
continue;
|
|
111
|
+
const id = text(order._id);
|
|
112
|
+
if (id === '' || countedById.has(id))
|
|
113
|
+
continue;
|
|
114
|
+
const day = text(order.localDate);
|
|
115
|
+
if (day !== '' && day !== localDate) {
|
|
116
|
+
coverage.orders_out_of_day += 1;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (toDate(order.createdAt) == null && toDate(order.updatedAt) == null)
|
|
120
|
+
continue;
|
|
121
|
+
countedById.set(id, order);
|
|
122
|
+
counted.push(order);
|
|
123
|
+
}
|
|
124
|
+
counted.sort(byIdAsc);
|
|
125
|
+
coverage.orders_counted = counted.length;
|
|
126
|
+
/* -------------------- fingerprints → table sessions -------------------- */
|
|
127
|
+
const fingerprintById = new Map();
|
|
128
|
+
for (const fingerprint of input.fingerprints ?? []) {
|
|
129
|
+
if (fingerprint == null)
|
|
130
|
+
continue;
|
|
131
|
+
if (String(fingerprint.tenant_id ?? tenantId) !== tenantId)
|
|
132
|
+
continue;
|
|
133
|
+
const id = text(fingerprint._id);
|
|
134
|
+
if (id === '')
|
|
135
|
+
continue;
|
|
136
|
+
if (fingerprintById.has(id)) {
|
|
137
|
+
coverage.fingerprints_duplicate += 1;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
fingerprintById.set(id, fingerprint);
|
|
141
|
+
}
|
|
142
|
+
coverage.fingerprints_total = fingerprintById.size;
|
|
143
|
+
const fingerprintsByOrder = new Map();
|
|
144
|
+
for (const fingerprint of fingerprintById.values()) {
|
|
145
|
+
watermark = laterOf(watermark, fingerprint.updatedAt ?? fingerprint.createdAt);
|
|
146
|
+
const orderId = text(fingerprint.order_id);
|
|
147
|
+
const order = orderId === '' ? null : countedById.get(orderId);
|
|
148
|
+
if (order == null) {
|
|
149
|
+
coverage.fingerprints_unattached += 1;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const bucket = fingerprintsByOrder.get(orderId);
|
|
153
|
+
if (bucket == null)
|
|
154
|
+
fingerprintsByOrder.set(orderId, [fingerprint]);
|
|
155
|
+
else
|
|
156
|
+
bucket.push(fingerprint);
|
|
157
|
+
}
|
|
158
|
+
const sessionsByOrder = new Map();
|
|
159
|
+
const coversByOrder = new Map();
|
|
160
|
+
for (const order of counted) {
|
|
161
|
+
const orderId = text(order._id);
|
|
162
|
+
const key = buildMaterializedDineinKey(order.order_status, order.platform);
|
|
163
|
+
const fingerprints = (fingerprintsByOrder.get(orderId) ?? [])
|
|
164
|
+
.slice()
|
|
165
|
+
.sort((a, b) => (toDate(a.createdAt)?.getTime() ?? 0) - (toDate(b.createdAt)?.getTime() ?? 0) || byIdAsc(a, b));
|
|
166
|
+
if (fingerprints.length === 0) {
|
|
167
|
+
coverage.orders_without_fingerprint += 1;
|
|
168
|
+
sessionsByOrder.set(orderId, []);
|
|
169
|
+
coversByOrder.set(orderId, 0);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
const sessions = [];
|
|
173
|
+
for (const fingerprint of fingerprints) {
|
|
174
|
+
const end = toDate(fingerprint.createdAt);
|
|
175
|
+
if (end == null) {
|
|
176
|
+
coverage.fingerprints_missing_end += 1;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const realStart = toDate(order.dinein_details?.start_time);
|
|
180
|
+
const fallbackStart = toDate(order.createdAt);
|
|
181
|
+
const start = realStart ?? fallbackStart;
|
|
182
|
+
if (start == null) {
|
|
183
|
+
coverage.fingerprints_missing_start += 1;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const durationMinutes = Math.round((end.getTime() - start.getTime()) / 60000);
|
|
187
|
+
if (durationMinutes < 0) {
|
|
188
|
+
coverage.fingerprints_negative_duration += 1;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const fallback = realStart == null;
|
|
192
|
+
if (fallback)
|
|
193
|
+
coverage.fingerprints_fallback_start += 1;
|
|
194
|
+
coverage.fingerprints_joined += 1;
|
|
195
|
+
const tableId = text(fingerprint.table_id) || text(order.dinein_details?.table_id) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
|
|
196
|
+
const floorId = text(fingerprint.dinein_floor_id) || text(order.dinein_details?.floor_id) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
|
|
197
|
+
sessions.push({
|
|
198
|
+
fingerprint_id: text(fingerprint._id),
|
|
199
|
+
order_id: orderId,
|
|
200
|
+
table_id: tableId,
|
|
201
|
+
table_name: text(fingerprint.name) || text(order.dinein_details?.table_name),
|
|
202
|
+
floor_id: floorId,
|
|
203
|
+
guests: Math.max(0, num(fingerprint.num_of_guests)),
|
|
204
|
+
start,
|
|
205
|
+
end,
|
|
206
|
+
duration_minutes: durationMinutes,
|
|
207
|
+
fallback_start: fallback,
|
|
208
|
+
key,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
sessionsByOrder.set(orderId, sessions);
|
|
212
|
+
// COVERS: the check's SETTLING session (the last valid table close), so a
|
|
213
|
+
// party that moved tables is still counted once. Never `num_of_guests`.
|
|
214
|
+
const settling = sessions.length === 0 ? null : sessions[sessions.length - 1];
|
|
215
|
+
coversByOrder.set(orderId, settling == null ? 0 : settling.guests);
|
|
216
|
+
}
|
|
217
|
+
/* -------------------- ratings (own, or inherited from a split parent) -------------------- */
|
|
218
|
+
const childrenByParent = new Map();
|
|
219
|
+
for (const order of counted) {
|
|
220
|
+
const parentId = text(order.split_parent_id);
|
|
221
|
+
if (parentId === '')
|
|
222
|
+
continue;
|
|
223
|
+
const bucket = childrenByParent.get(parentId);
|
|
224
|
+
if (bucket == null)
|
|
225
|
+
childrenByParent.set(parentId, [order]);
|
|
226
|
+
else
|
|
227
|
+
bucket.push(order);
|
|
228
|
+
}
|
|
229
|
+
const inheritedRating = new Map();
|
|
230
|
+
for (const [parentId, children] of childrenByParent) {
|
|
231
|
+
const parentRating = validRatingOf(parentById.get(parentId)?.rating);
|
|
232
|
+
if (parentRating == null)
|
|
233
|
+
continue;
|
|
234
|
+
// ONE child inherits the parent rating (the smallest order id), so a rating
|
|
235
|
+
// written once on the parent is never counted once per child.
|
|
236
|
+
const chosen = [...children].sort(byIdAsc)[0];
|
|
237
|
+
if (chosen == null)
|
|
238
|
+
continue;
|
|
239
|
+
if (validRatingOf(chosen.rating) != null)
|
|
240
|
+
continue;
|
|
241
|
+
inheritedRating.set(text(chosen._id), parentRating);
|
|
242
|
+
coverage.ratings_from_deleted_parents += 1;
|
|
243
|
+
}
|
|
244
|
+
/* -------------------- accumulate counters + rows -------------------- */
|
|
245
|
+
const countersByKey = new Map();
|
|
246
|
+
const rowsByKey = new Map();
|
|
247
|
+
for (const order of counted) {
|
|
248
|
+
const orderId = text(order._id);
|
|
249
|
+
const key = buildMaterializedDineinKey(order.order_status, order.platform);
|
|
250
|
+
const counters = countersByKey.get(key) ?? freshMaterializedDineinCounters();
|
|
251
|
+
countersByKey.set(key, counters);
|
|
252
|
+
const parentId = text(order.split_parent_id);
|
|
253
|
+
const parent = parentId === '' ? null : (parentById.get(parentId) ?? null);
|
|
254
|
+
const splitType = parentId === '' ? null : text(parent?.split_type) || text(order.split_type) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN;
|
|
255
|
+
const rating = validRatingOf(order.rating) ?? inheritedRating.get(orderId) ?? null;
|
|
256
|
+
const covers = coversByOrder.get(orderId) ?? 0;
|
|
257
|
+
addDineinOrderToCounters({
|
|
258
|
+
counters,
|
|
259
|
+
order,
|
|
260
|
+
covers,
|
|
261
|
+
sessions: sessionsByOrder.get(orderId) ?? [],
|
|
262
|
+
rating,
|
|
263
|
+
splitType,
|
|
264
|
+
});
|
|
265
|
+
watermark = laterOf(watermark, order.updatedAt ?? order.createdAt);
|
|
266
|
+
const time = toDate(order.createdAt) ?? toDate(order.updatedAt);
|
|
267
|
+
if (time == null)
|
|
268
|
+
continue;
|
|
269
|
+
const row = {
|
|
270
|
+
order_id: orderId,
|
|
271
|
+
order_number: optionalOrderNumber(order.order_number),
|
|
272
|
+
localDate: text(order.localDate) || localDate,
|
|
273
|
+
time: time.toISOString(),
|
|
274
|
+
table_id: text(order.dinein_details?.table_id) || null,
|
|
275
|
+
table_name: text(order.dinein_details?.table_name) || null,
|
|
276
|
+
floor_id: text(order.dinein_details?.floor_id) || null,
|
|
277
|
+
covers,
|
|
278
|
+
status: text(order.order_status) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN,
|
|
279
|
+
platform: text(order.platform) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN,
|
|
280
|
+
origin: classifyMaterializedDineinOrigin(order),
|
|
281
|
+
split_type: splitType,
|
|
282
|
+
split_role: splitRoleOf(order),
|
|
283
|
+
offline: order.synced_offline === true,
|
|
284
|
+
rating,
|
|
285
|
+
sales: num(order.amount_earned),
|
|
286
|
+
service_charge: num(order.service_fee_amount),
|
|
287
|
+
items: itemQuantityOf(order),
|
|
288
|
+
edits: Array.isArray(order.iterations) ? order.iterations.length : 0,
|
|
289
|
+
};
|
|
290
|
+
const bucket = rowsByKey.get(key);
|
|
291
|
+
if (bucket == null)
|
|
292
|
+
rowsByKey.set(key, [row]);
|
|
293
|
+
else
|
|
294
|
+
bucket.push(row);
|
|
295
|
+
}
|
|
296
|
+
/* -------------------- most-edited checks (bounded snapshot) -------------------- */
|
|
297
|
+
const topEditedChecks = [];
|
|
298
|
+
for (const order of counted) {
|
|
299
|
+
const edits = Array.isArray(order.iterations) ? order.iterations.length : 0;
|
|
300
|
+
if (edits === 0)
|
|
301
|
+
continue;
|
|
302
|
+
// The SAME line-delta helper the counters use, so the snapshot can never
|
|
303
|
+
// disagree with `item_additions` / `item_removals`.
|
|
304
|
+
let additions = 0;
|
|
305
|
+
let removals = 0;
|
|
306
|
+
const versions = [
|
|
307
|
+
...(Array.isArray(order.iterations) ? [...order.iterations].reverse() : []),
|
|
308
|
+
order,
|
|
309
|
+
];
|
|
310
|
+
for (let index = 1; index < versions.length; index += 1) {
|
|
311
|
+
const delta = itemLineDelta(versions[index - 1], versions[index]);
|
|
312
|
+
additions += delta.additions;
|
|
313
|
+
removals += delta.removals;
|
|
314
|
+
}
|
|
315
|
+
topEditedChecks.push({
|
|
316
|
+
order_id: text(order._id),
|
|
317
|
+
order_number: optionalOrderNumber(order.order_number),
|
|
318
|
+
table_id: text(order.dinein_details?.table_id) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN,
|
|
319
|
+
table_name: text(order.dinein_details?.table_name),
|
|
320
|
+
floor_id: text(order.dinein_details?.floor_id) || MATERIALIZED_DINEIN_UNKNOWN_TOKEN,
|
|
321
|
+
edits,
|
|
322
|
+
item_additions: additions,
|
|
323
|
+
item_removals: removals,
|
|
324
|
+
covers: coversByOrder.get(text(order._id)) ?? 0,
|
|
325
|
+
sales: num(order.amount_earned),
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
/* -------------------- split parents per key + split_checks -------------------- */
|
|
329
|
+
// A parent is attributed to the key of its DETERMINISTIC FIRST counted child,
|
|
330
|
+
// so a group whose children land in several keys never counts its parent twice.
|
|
331
|
+
for (const children of childrenByParent.values()) {
|
|
332
|
+
const chosen = [...children].sort(byIdAsc)[0];
|
|
333
|
+
if (chosen == null)
|
|
334
|
+
continue;
|
|
335
|
+
const key = buildMaterializedDineinKey(chosen.order_status, chosen.platform);
|
|
336
|
+
const counters = countersByKey.get(key);
|
|
337
|
+
if (counters == null)
|
|
338
|
+
continue;
|
|
339
|
+
counters.split_parents += 1;
|
|
340
|
+
}
|
|
341
|
+
for (const counters of countersByKey.values()) {
|
|
342
|
+
counters.split_checks = counters.split_parents + counters.split_children;
|
|
343
|
+
}
|
|
344
|
+
/* -------------------- reservations -------------------- */
|
|
345
|
+
const reservationCounters = freshMaterializedDineinReservationCounters();
|
|
346
|
+
const upcomingReservations = [];
|
|
347
|
+
for (const reservation of input.reservations ?? []) {
|
|
348
|
+
if (reservation == null)
|
|
349
|
+
continue;
|
|
350
|
+
if (String(reservation.tenant_id ?? tenantId) !== tenantId)
|
|
351
|
+
continue;
|
|
352
|
+
const sourceDay = text(reservation.source_local_date);
|
|
353
|
+
if (sourceDay !== '' && sourceDay !== localDate) {
|
|
354
|
+
coverage.reservations_out_of_day += 1;
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
const reservationTime = toDate(reservation.reservation_time);
|
|
358
|
+
if (reservationTime == null) {
|
|
359
|
+
coverage.reservations_missing_time += 1;
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
coverage.reservations_counted += 1;
|
|
363
|
+
watermark = laterOf(watermark, reservation.updatedAt ?? reservation.createdAt);
|
|
364
|
+
reservationCounters.reservations += 1;
|
|
365
|
+
reservationCounters.reservation_covers += Math.max(0, num(reservation.num_of_guests));
|
|
366
|
+
const status = text(reservation.reservation_status).toUpperCase();
|
|
367
|
+
if (status === 'CONFIRMED')
|
|
368
|
+
reservationCounters.reservation_confirmed += 1;
|
|
369
|
+
else if (status === 'PENDING')
|
|
370
|
+
reservationCounters.reservation_pending += 1;
|
|
371
|
+
else if (status === 'CANCELLED')
|
|
372
|
+
reservationCounters.reservation_cancelled += 1;
|
|
373
|
+
const createdAt = toDate(reservation.createdAt);
|
|
374
|
+
if (createdAt != null) {
|
|
375
|
+
const leadMinutes = Math.round((reservationTime.getTime() - createdAt.getTime()) / 60000);
|
|
376
|
+
// A booking created AFTER its slot has no lead time; it is not clamped to 0.
|
|
377
|
+
if (leadMinutes >= 0) {
|
|
378
|
+
reservationCounters.reservation_lead_minutes_sum += leadMinutes;
|
|
379
|
+
reservationCounters.reservation_lead_count += 1;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
const duration = num(reservation.reservation_duration);
|
|
383
|
+
if (duration >= 0) {
|
|
384
|
+
reservationCounters.reservation_duration_minutes_sum += duration;
|
|
385
|
+
reservationCounters.reservation_duration_count += 1;
|
|
386
|
+
}
|
|
387
|
+
// The reservation chart is folded from THIS map, never from order hours.
|
|
388
|
+
const localHour = tenantLocalHourOf(reservationTime, gmtOffset);
|
|
389
|
+
if (localHour != null && MATERIALIZED_DINEIN_HOUR_KEYS.includes(String(localHour))) {
|
|
390
|
+
const hourKey = String(localHour);
|
|
391
|
+
const hourRow = ((_a = reservationCounters.by_hour)[hourKey] ?? (_a[hourKey] = {
|
|
392
|
+
reservations: 0,
|
|
393
|
+
reservation_covers: 0,
|
|
394
|
+
}));
|
|
395
|
+
hourRow.reservations += 1;
|
|
396
|
+
hourRow.reservation_covers += Math.max(0, num(reservation.num_of_guests));
|
|
397
|
+
}
|
|
398
|
+
// Bounded, MASKED snapshot row (no name / phone / email).
|
|
399
|
+
const leadMinutes = createdAt == null ? null : Math.round((reservationTime.getTime() - createdAt.getTime()) / 60000);
|
|
400
|
+
upcomingReservations.push({
|
|
401
|
+
reservation_id: text(reservation._id),
|
|
402
|
+
guest_label: maskReservationGuestLabel(reservation.customer_name),
|
|
403
|
+
reservation_time: reservationTime.toISOString(),
|
|
404
|
+
local_hour: localHour == null ? 0 : localHour,
|
|
405
|
+
party_size: Math.max(0, num(reservation.num_of_guests)),
|
|
406
|
+
status,
|
|
407
|
+
source: text(reservation.order_from) || 'UNKNOWN',
|
|
408
|
+
lead_minutes: leadMinutes != null && leadMinutes >= 0 ? leadMinutes : null,
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
if (counted.length === 0 && reservationCounters.reservations === 0)
|
|
412
|
+
return null;
|
|
413
|
+
/* -------------------- bound the document maps -------------------- */
|
|
414
|
+
pruneDocumentDineinMaps(countersByKey);
|
|
415
|
+
const sortedKeys = [...countersByKey.keys()].sort();
|
|
416
|
+
const dineinCounters = {};
|
|
417
|
+
for (const key of sortedKeys)
|
|
418
|
+
dineinCounters[key] = countersByKey.get(key);
|
|
419
|
+
const dayIndex = getDayIndexFromLocalDate(localDate);
|
|
420
|
+
const reports = [
|
|
421
|
+
{
|
|
422
|
+
_id: undefined,
|
|
423
|
+
tenant_id: tenantId,
|
|
424
|
+
brand_id: brandId,
|
|
425
|
+
scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
426
|
+
scope_field: localDate,
|
|
427
|
+
day_index: dayIndex,
|
|
428
|
+
sale_type_key_filters: [...MATERIALIZED_DINEIN_REPORT_KEY_FILTERS],
|
|
429
|
+
dinein_counters: dineinCounters,
|
|
430
|
+
reservation_counters: reservationCounters,
|
|
431
|
+
upcoming_reservations: mergeMaterializedDineinUpcomingReservations([upcomingReservations], MATERIALIZED_DINEIN_UPCOMING_RESERVATION_LIMIT),
|
|
432
|
+
top_edited_checks: mergeMaterializedDineinTopEditedChecks([topEditedChecks], MATERIALIZED_DINEIN_TOP_EDITED_CHECK_LIMIT),
|
|
433
|
+
schema_version: MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION,
|
|
434
|
+
source_watermark: watermark,
|
|
435
|
+
computed_at: computedAt,
|
|
436
|
+
createdAt: computedAt,
|
|
437
|
+
updatedAt: computedAt,
|
|
438
|
+
},
|
|
439
|
+
];
|
|
440
|
+
/* -------------------- entry pages -------------------- */
|
|
441
|
+
const sections = {};
|
|
442
|
+
for (const key of [...rowsByKey.keys()].sort()) {
|
|
443
|
+
const rows = rowsByKey.get(key);
|
|
444
|
+
// NEWEST FIRST with a total tie-break: the same order the keyset reader
|
|
445
|
+
// uses, so the stored page and the API agree.
|
|
446
|
+
rows.sort((a, b) => new Date(b.time).getTime() - new Date(a.time).getTime() ||
|
|
447
|
+
(a.order_id < b.order_id ? 1 : a.order_id > b.order_id ? -1 : 0));
|
|
448
|
+
sections[key] = { rows };
|
|
449
|
+
}
|
|
450
|
+
const entries = [];
|
|
451
|
+
if (Object.keys(sections).length > 0) {
|
|
452
|
+
const periodFields = getMaterializedPeriodScopeFields(localDate);
|
|
453
|
+
if (periodFields == null)
|
|
454
|
+
return null;
|
|
455
|
+
for (const scope of MATERIALIZED_DINEIN_ENTRY_SCOPES) {
|
|
456
|
+
const scopeField = periodFields[scope];
|
|
457
|
+
const page = dineinEntryPageOf(scope, localDate);
|
|
458
|
+
if (typeof scopeField !== 'string' || scopeField === '' || page == null)
|
|
459
|
+
return null;
|
|
460
|
+
const pageDoc = {
|
|
461
|
+
_id: undefined,
|
|
462
|
+
tenant_id: tenantId,
|
|
463
|
+
brand_id: brandId,
|
|
464
|
+
scope_context: scope,
|
|
465
|
+
scope_field: scopeField,
|
|
466
|
+
page,
|
|
467
|
+
day_index: scope === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE ? dayIndex : null,
|
|
468
|
+
dinein_entries: sections,
|
|
469
|
+
schema_version: MATERIALIZED_TENANT_DINEIN_ENTRIES_SCHEMA_VERSION,
|
|
470
|
+
source_watermark: watermark,
|
|
471
|
+
computed_at: computedAt,
|
|
472
|
+
createdAt: computedAt,
|
|
473
|
+
updatedAt: computedAt,
|
|
474
|
+
};
|
|
475
|
+
entries.push(...splitMaterializedDineinEntryPage(pageDoc));
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
// BSON budgets: fail at COMPILE time, naming the collection + day + scope,
|
|
479
|
+
// rather than at insert time inside the close transaction.
|
|
480
|
+
assertMaterializedDocumentSize(reports[0], {
|
|
481
|
+
label: `materialized_tenant_dinein_reports:${tenantId}:${localDate}:localDate:${localDate}`,
|
|
482
|
+
maxBytes: MATERIALIZED_DINEIN_REPORT_MAX_BSON_BYTES,
|
|
483
|
+
});
|
|
484
|
+
for (const page of entries) {
|
|
485
|
+
assertMaterializedDocumentSize(page, {
|
|
486
|
+
label: 'materialized_tenant_dinein_entries:' +
|
|
487
|
+
tenantId +
|
|
488
|
+
':' +
|
|
489
|
+
localDate +
|
|
490
|
+
':' +
|
|
491
|
+
String(page.scope_context) +
|
|
492
|
+
':' +
|
|
493
|
+
page.scope_field +
|
|
494
|
+
':page=' +
|
|
495
|
+
page.page,
|
|
496
|
+
maxBytes: MATERIALIZED_DINEIN_ENTRY_MAX_BSON_BYTES,
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
return { reports, entries, coverage };
|
|
500
|
+
}
|