@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.
Files changed (96) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +254 -4
  2. package/esm/constants/TENANT_ENUMS.js +15 -0
  3. package/esm/constants/WORKER_ENDPOINTS.js +12 -0
  4. package/esm/helpers/materialized-reports/index.js +8 -0
  5. package/esm/helpers/materialized-reports/materialized-day-readiness.js +2 -2
  6. package/esm/helpers/materialized-reports/materialized-dinein-accumulator-helpers.js +557 -0
  7. package/esm/helpers/materialized-reports/materialized-dinein-classification-helpers.js +447 -0
  8. package/esm/helpers/materialized-reports/materialized-dinein-close-day-helpers.js +372 -0
  9. package/esm/helpers/materialized-reports/materialized-dinein-compile-helpers.js +500 -0
  10. package/esm/helpers/materialized-reports/materialized-dinein-entry-pages.js +218 -0
  11. package/esm/helpers/materialized-reports/materialized-dinein-key-helpers.js +87 -0
  12. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +7 -1
  13. package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +8 -0
  14. package/esm/helpers/materialized-reports/materialized-report-runs.js +143 -0
  15. package/esm/helpers/materialized-reports/report-platform/definition.js +93 -1
  16. package/esm/helpers/materialized-reports/tenant-dinein-formatter.js +240 -0
  17. package/esm/helpers/reporting-contract/report-contract.js +3 -0
  18. package/esm/schemas/models/index.js +3 -0
  19. package/esm/schemas/models/materialized_tenant_dinein_entries.js +57 -0
  20. package/esm/schemas/models/materialized_tenant_dinein_reports.js +73 -0
  21. package/esm/schemas/models/materialized_tenant_report_runs.js +105 -0
  22. package/esm/schemas/models/tenant_active_jobs.js +19 -1
  23. package/esm/schemas/types/index.js +3 -0
  24. package/esm/schemas/types/materialized_report_drilldown_rows.js +1 -0
  25. package/esm/schemas/types/materialized_tenant_dinein_entries.js +1 -0
  26. package/esm/schemas/types/materialized_tenant_dinein_reports.js +6 -0
  27. package/esm/schemas/types/materialized_tenant_report_runs.js +1 -0
  28. package/lib/constants/MATERIALIZED_REPORTS.d.ts +229 -4
  29. package/lib/constants/MATERIALIZED_REPORTS.js +255 -5
  30. package/lib/constants/TENANT_ENUMS.d.ts +14 -0
  31. package/lib/constants/TENANT_ENUMS.js +16 -1
  32. package/lib/constants/WORKER_ENDPOINTS.d.ts +12 -0
  33. package/lib/constants/WORKER_ENDPOINTS.js +12 -0
  34. package/lib/helpers/materialized-reports/index.d.ts +8 -0
  35. package/lib/helpers/materialized-reports/index.js +8 -0
  36. package/lib/helpers/materialized-reports/materialized-day-readiness.d.ts +1 -1
  37. package/lib/helpers/materialized-reports/materialized-day-readiness.js +2 -2
  38. package/lib/helpers/materialized-reports/materialized-dinein-accumulator-helpers.d.ts +67 -0
  39. package/lib/helpers/materialized-reports/materialized-dinein-accumulator-helpers.js +582 -0
  40. package/lib/helpers/materialized-reports/materialized-dinein-classification-helpers.d.ts +245 -0
  41. package/lib/helpers/materialized-reports/materialized-dinein-classification-helpers.js +469 -0
  42. package/lib/helpers/materialized-reports/materialized-dinein-close-day-helpers.d.ts +110 -0
  43. package/lib/helpers/materialized-reports/materialized-dinein-close-day-helpers.js +384 -0
  44. package/lib/helpers/materialized-reports/materialized-dinein-compile-helpers.d.ts +73 -0
  45. package/lib/helpers/materialized-reports/materialized-dinein-compile-helpers.js +504 -0
  46. package/lib/helpers/materialized-reports/materialized-dinein-entry-pages.d.ts +69 -0
  47. package/lib/helpers/materialized-reports/materialized-dinein-entry-pages.js +231 -0
  48. package/lib/helpers/materialized-reports/materialized-dinein-key-helpers.d.ts +44 -0
  49. package/lib/helpers/materialized-reports/materialized-dinein-key-helpers.js +96 -0
  50. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +6 -0
  51. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +8 -0
  52. package/lib/helpers/materialized-reports/materialized-report-runs.d.ts +105 -0
  53. package/lib/helpers/materialized-reports/materialized-report-runs.js +156 -0
  54. package/lib/helpers/materialized-reports/report-platform/definition.js +92 -0
  55. package/lib/helpers/materialized-reports/tenant-dinein-formatter.d.ts +225 -0
  56. package/lib/helpers/materialized-reports/tenant-dinein-formatter.js +243 -0
  57. package/lib/helpers/reporting-contract/report-contract.d.ts +1 -0
  58. package/lib/helpers/reporting-contract/report-contract.js +3 -0
  59. package/lib/helpers/types/workers.d.ts +65 -0
  60. package/lib/schemas/models/index.d.ts +3 -0
  61. package/lib/schemas/models/index.js +3 -0
  62. package/lib/schemas/models/materialized_tenant_day_readiness.d.ts +3 -3
  63. package/lib/schemas/models/materialized_tenant_dinein_entries.d.ts +100 -0
  64. package/lib/schemas/models/materialized_tenant_dinein_entries.js +60 -0
  65. package/lib/schemas/models/materialized_tenant_dinein_reports.d.ts +111 -0
  66. package/lib/schemas/models/materialized_tenant_dinein_reports.js +76 -0
  67. package/lib/schemas/models/materialized_tenant_report_runs.d.ts +97 -0
  68. package/lib/schemas/models/materialized_tenant_report_runs.js +108 -0
  69. package/lib/schemas/models/tenant_active_jobs.d.ts +12 -3
  70. package/lib/schemas/models/tenant_active_jobs.js +18 -0
  71. package/lib/schemas/types/index.d.ts +3 -0
  72. package/lib/schemas/types/index.js +3 -0
  73. package/lib/schemas/types/materialized_report_drilldown_rows.d.ts +1 -1
  74. package/lib/schemas/types/materialized_report_drilldown_rows.js +1 -0
  75. package/lib/schemas/types/materialized_tenant_dinein_entries.d.ts +127 -0
  76. package/lib/schemas/types/materialized_tenant_dinein_entries.js +2 -0
  77. package/lib/schemas/types/materialized_tenant_dinein_reports.d.ts +326 -0
  78. package/lib/schemas/types/materialized_tenant_dinein_reports.js +9 -0
  79. package/lib/schemas/types/materialized_tenant_report_runs.d.ts +55 -0
  80. package/lib/schemas/types/materialized_tenant_report_runs.js +2 -0
  81. package/lib/schemas/types/tenant_active_jobs.d.ts +15 -1
  82. package/lib/tests/materialized-reports/dinein-fixtures.d.ts +29 -0
  83. package/lib/tests/materialized-reports/dinein-fixtures.js +130 -0
  84. package/lib/tests/materialized-reports/materialized-day-readiness.test.js +3 -2
  85. package/lib/tests/materialized-reports/materialized-dinein-compile.test.d.ts +1 -0
  86. package/lib/tests/materialized-reports/materialized-dinein-compile.test.js +807 -0
  87. package/lib/tests/materialized-reports/materialized-dinein-plan.test.d.ts +1 -0
  88. package/lib/tests/materialized-reports/materialized-dinein-plan.test.js +419 -0
  89. package/lib/tests/materialized-reports/materialized-report-runs.test.d.ts +1 -0
  90. package/lib/tests/materialized-reports/materialized-report-runs.test.js +116 -0
  91. package/lib/tests/materialized-reports/materialized-tenant-dinein-schema.test.d.ts +1 -0
  92. package/lib/tests/materialized-reports/materialized-tenant-dinein-schema.test.js +185 -0
  93. package/lib/tests/materialized-reports/report-platform/registry-conformance.test.js +15 -2
  94. package/lib/tests/materialized-reports/tenant-dinein-formatter.test.d.ts +1 -0
  95. package/lib/tests/materialized-reports/tenant-dinein-formatter.test.js +458 -0
  96. package/package.json +1 -1
@@ -0,0 +1,582 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.emptyMaterializedDineinHourRow = emptyMaterializedDineinHourRow;
4
+ exports.mergeMaterializedDineinHourRow = mergeMaterializedDineinHourRow;
5
+ exports.emptyMaterializedDineinTableRow = emptyMaterializedDineinTableRow;
6
+ exports.emptyMaterializedDineinFloorRow = emptyMaterializedDineinFloorRow;
7
+ exports.emptyMaterializedDineinCourseRow = emptyMaterializedDineinCourseRow;
8
+ exports.emptyMaterializedDineinOriginRow = emptyMaterializedDineinOriginRow;
9
+ exports.emptyMaterializedDineinBreakdownRow = emptyMaterializedDineinBreakdownRow;
10
+ exports.mergeMaterializedDineinBreakdownRow = mergeMaterializedDineinBreakdownRow;
11
+ exports.emptyMaterializedDineinReservationHourRow = emptyMaterializedDineinReservationHourRow;
12
+ exports.mergeMaterializedDineinReservationHourRow = mergeMaterializedDineinReservationHourRow;
13
+ exports.emptyMaterializedDineinPaymentRow = emptyMaterializedDineinPaymentRow;
14
+ exports.freshMaterializedDineinReservationCounters = freshMaterializedDineinReservationCounters;
15
+ exports.freshMaterializedDineinCounters = freshMaterializedDineinCounters;
16
+ exports.mergeMaterializedDineinTableRow = mergeMaterializedDineinTableRow;
17
+ exports.mergeMaterializedDineinFloorRow = mergeMaterializedDineinFloorRow;
18
+ exports.mergeMaterializedDineinCourseRow = mergeMaterializedDineinCourseRow;
19
+ exports.mergeMaterializedDineinOriginRow = mergeMaterializedDineinOriginRow;
20
+ exports.mergeMaterializedDineinPaymentRow = mergeMaterializedDineinPaymentRow;
21
+ exports.mergeMaterializedDineinCounters = mergeMaterializedDineinCounters;
22
+ exports.mergeMaterializedDineinReservationCounters = mergeMaterializedDineinReservationCounters;
23
+ exports.pruneDocumentDineinMaps = pruneDocumentDineinMaps;
24
+ exports.mergeMaterializedDineinUpcomingReservations = mergeMaterializedDineinUpcomingReservations;
25
+ exports.mergeMaterializedDineinTopEditedChecks = mergeMaterializedDineinTopEditedChecks;
26
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
27
+ /**
28
+ * Folding the dinein counters across DAY documents into a PERIOD document.
29
+ *
30
+ * ## Everything stored is ADDITIVE
31
+ *
32
+ * The document stores integer counts, integer cents, integer minutes and sums
33
+ * whose denominators (`rating_sum` / `rated_orders`,
34
+ * `reservation_lead_minutes_sum` / `reservation_lead_count`, occupancy) are
35
+ * stored beside them, so the fold is a plain element-wise sum and the formatter
36
+ * can derive every average exactly. No average is ever stored, so no average is
37
+ * ever summed.
38
+ *
39
+ * ## Bounded maps are merged row-by-row
40
+ *
41
+ * `by_table` / `by_floor` / `by_course` / `service_charge_rate_counts` are
42
+ * keyed maps, so a period fold unions their keys and adds their counters. The
43
+ * union can exceed a DAY's cap (28-31 days × top-N); the close plan therefore
44
+ * re-bounds the merged document afterwards (`pruneDocumentDineinMaps`), so the
45
+ * stored period document keeps its own top-N plus an `__OTHER__` overflow row.
46
+ *
47
+ * ## `tables_served` is ADDED
48
+ *
49
+ * It is a distinct-table count at DAY grain, so the period fold is a sum of
50
+ * daily distinct counts ("table-days"). The formatter exposes the true distinct
51
+ * count for the period from `by_table` keys — see the formatter docs.
52
+ */
53
+ const num = (value) => {
54
+ const number = typeof value === 'number' ? value : Number(value);
55
+ return Number.isFinite(number) ? Math.round(number) : 0;
56
+ };
57
+ const hoursZeroed = () => {
58
+ const byHour = {};
59
+ for (const hour of MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_HOUR_KEYS)
60
+ byHour[hour] = { orders: 0, sales: 0, covers: 0 };
61
+ return byHour;
62
+ };
63
+ /** A zeroed hour row. */
64
+ function emptyMaterializedDineinHourRow() {
65
+ return { orders: 0, sales: 0, covers: 0 };
66
+ }
67
+ /** Merges one hour row into another. */
68
+ function mergeMaterializedDineinHourRow(target, source) {
69
+ target.orders += num(source?.orders);
70
+ target.sales += num(source?.sales);
71
+ target.covers += num(source?.covers);
72
+ }
73
+ /** A zeroed table row. */
74
+ function emptyMaterializedDineinTableRow(tableId = MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_UNKNOWN_TOKEN, tableName = '', floorId = MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_UNKNOWN_TOKEN) {
75
+ return {
76
+ table_id: tableId,
77
+ table_name: tableName,
78
+ floor_id: floorId,
79
+ orders: 0,
80
+ sessions: 0,
81
+ covers: 0,
82
+ occupancy_minutes: 0,
83
+ sales: 0,
84
+ };
85
+ }
86
+ /** A zeroed floor row. */
87
+ function emptyMaterializedDineinFloorRow(floorId = MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_UNKNOWN_TOKEN) {
88
+ return {
89
+ floor_id: floorId,
90
+ orders: 0,
91
+ sessions: 0,
92
+ covers: 0,
93
+ occupancy_minutes: 0,
94
+ sales: 0,
95
+ };
96
+ }
97
+ /** A zeroed course row. */
98
+ function emptyMaterializedDineinCourseRow(courseId, name) {
99
+ return { course_id: courseId, name, assigned: 0, called: 0, on_hold: 0, items: 0 };
100
+ }
101
+ /** A zeroed origin row. */
102
+ function emptyMaterializedDineinOriginRow() {
103
+ return { orders: 0, sales: 0, covers: 0 };
104
+ }
105
+ /** A zeroed `by_status` / `by_platform` row. */
106
+ function emptyMaterializedDineinBreakdownRow() {
107
+ return { orders: 0, covers: 0, sales: 0 };
108
+ }
109
+ /** Merges one rich breakdown row into another. */
110
+ function mergeMaterializedDineinBreakdownRow(target, source) {
111
+ target.orders += num(source?.orders);
112
+ target.covers += num(source?.covers);
113
+ target.sales += num(source?.sales);
114
+ }
115
+ /** A zeroed reservation-hour row. */
116
+ function emptyMaterializedDineinReservationHourRow() {
117
+ return { reservations: 0, reservation_covers: 0 };
118
+ }
119
+ /** Merges one reservation-hour row into another. */
120
+ function mergeMaterializedDineinReservationHourRow(target, source) {
121
+ target.reservations += num(source?.reservations);
122
+ target.reservation_covers += num(source?.reservation_covers);
123
+ }
124
+ /** All 24 reservation-hour keys, initialized. */
125
+ const reservationHoursZeroed = () => {
126
+ const byHour = {};
127
+ for (const hour of MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_HOUR_KEYS) {
128
+ byHour[hour] = emptyMaterializedDineinReservationHourRow();
129
+ }
130
+ return byHour;
131
+ };
132
+ /** A zeroed payment-type row. */
133
+ function emptyMaterializedDineinPaymentRow() {
134
+ return { amount: 0, orders: 0 };
135
+ }
136
+ /** A zeroed reservation block. */
137
+ function freshMaterializedDineinReservationCounters() {
138
+ return {
139
+ reservations: 0,
140
+ reservation_covers: 0,
141
+ reservation_confirmed: 0,
142
+ reservation_pending: 0,
143
+ reservation_cancelled: 0,
144
+ reservation_lead_minutes_sum: 0,
145
+ reservation_lead_count: 0,
146
+ reservation_duration_minutes_sum: 0,
147
+ reservation_duration_count: 0,
148
+ by_hour: reservationHoursZeroed(),
149
+ };
150
+ }
151
+ /** A zeroed counter block, with every bounded map present. */
152
+ function freshMaterializedDineinCounters() {
153
+ return {
154
+ orders: 0,
155
+ completed_orders: 0,
156
+ cancelled_orders: 0,
157
+ covers: 0,
158
+ items: 0,
159
+ sales: 0,
160
+ discounts: 0,
161
+ tax: 0,
162
+ tips: 0,
163
+ service_charge_amount: 0,
164
+ service_charge_taxable_amount: 0,
165
+ surcharge_amount: 0,
166
+ refunds: 0,
167
+ tables_served: 0,
168
+ table_sessions: 0,
169
+ occupancy_minutes_sum: 0,
170
+ occupancy_sessions: 0,
171
+ fallback_occupancy_sessions: 0,
172
+ split_parents: 0,
173
+ split_children: 0,
174
+ split_checks: 0,
175
+ split_sales: 0,
176
+ split_type_counts: {},
177
+ offline_orders: 0,
178
+ offline_sales: 0,
179
+ origin_orders: 0,
180
+ origin_sales: 0,
181
+ origin_covers: 0,
182
+ service_charge_orders: 0,
183
+ service_charge_percentage_orders: 0,
184
+ service_charge_fixed_orders: 0,
185
+ service_charge_original_price_orders: 0,
186
+ service_charge_rate_counts: {},
187
+ edited_orders: 0,
188
+ total_edits: 0,
189
+ item_additions: 0,
190
+ item_removals: 0,
191
+ net_item_change: 0,
192
+ courses_assigned: 0,
193
+ courses_called: 0,
194
+ courses_on_hold: 0,
195
+ course_items: 0,
196
+ by_course: {},
197
+ card_amount: 0,
198
+ cash_amount: 0,
199
+ other_amount: 0,
200
+ card_orders: 0,
201
+ cash_orders: 0,
202
+ split_tender_orders: 0,
203
+ by_payment_type: {},
204
+ rated_orders: 0,
205
+ rating_sum: 0,
206
+ rating_1: 0,
207
+ rating_2: 0,
208
+ rating_3: 0,
209
+ rating_4: 0,
210
+ rating_5: 0,
211
+ review_count: 0,
212
+ by_hour: hoursZeroed(),
213
+ by_status: {},
214
+ by_platform: {},
215
+ by_origin: {},
216
+ by_table: {},
217
+ by_floor: {},
218
+ };
219
+ }
220
+ /** Merges one table row into another (all counters additive). */
221
+ function mergeMaterializedDineinTableRow(target, source) {
222
+ target.orders += num(source?.orders);
223
+ target.sessions += num(source?.sessions);
224
+ target.covers += num(source?.covers);
225
+ target.occupancy_minutes += num(source?.occupancy_minutes);
226
+ target.sales += num(source?.sales);
227
+ if (!target.table_name && source?.table_name)
228
+ target.table_name = source.table_name;
229
+ if (target.floor_id === MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_UNKNOWN_TOKEN && source?.floor_id) {
230
+ target.floor_id = source.floor_id;
231
+ }
232
+ }
233
+ /** Merges one floor row into another. */
234
+ function mergeMaterializedDineinFloorRow(target, source) {
235
+ target.orders += num(source?.orders);
236
+ target.sessions += num(source?.sessions);
237
+ target.covers += num(source?.covers);
238
+ target.occupancy_minutes += num(source?.occupancy_minutes);
239
+ target.sales += num(source?.sales);
240
+ }
241
+ /** Merges one course row into another. */
242
+ function mergeMaterializedDineinCourseRow(target, source) {
243
+ target.assigned += num(source?.assigned);
244
+ target.called += num(source?.called);
245
+ target.on_hold += num(source?.on_hold);
246
+ target.items += num(source?.items);
247
+ if (!target.name && source?.name)
248
+ target.name = source.name;
249
+ }
250
+ /** Merges one origin row into another. */
251
+ function mergeMaterializedDineinOriginRow(target, source) {
252
+ target.orders += num(source?.orders);
253
+ target.sales += num(source?.sales);
254
+ target.covers += num(source?.covers);
255
+ }
256
+ /** Merges one payment-type row into another. */
257
+ function mergeMaterializedDineinPaymentRow(target, source) {
258
+ target.amount += num(source?.amount);
259
+ target.orders += num(source?.orders);
260
+ }
261
+ const addCountMap = (target, source) => {
262
+ for (const [key, value] of Object.entries(source ?? {})) {
263
+ target[key] = num(target[key]) + num(value);
264
+ }
265
+ };
266
+ /**
267
+ * Merges `source` into `target`, IN PLACE. Null-safe: a missing sub-map is
268
+ * treated as empty rather than throwing, so one malformed historical document
269
+ * cannot break a whole period fold.
270
+ */
271
+ function mergeMaterializedDineinCounters(target, source) {
272
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
273
+ if (target == null || source == null)
274
+ return target;
275
+ target.orders += num(source.orders);
276
+ target.completed_orders += num(source.completed_orders);
277
+ target.cancelled_orders += num(source.cancelled_orders);
278
+ target.covers += num(source.covers);
279
+ target.items += num(source.items);
280
+ target.sales += num(source.sales);
281
+ target.discounts += num(source.discounts);
282
+ target.tax += num(source.tax);
283
+ target.tips += num(source.tips);
284
+ target.service_charge_amount += num(source.service_charge_amount);
285
+ target.service_charge_taxable_amount += num(source.service_charge_taxable_amount);
286
+ target.surcharge_amount += num(source.surcharge_amount);
287
+ target.refunds += num(source.refunds);
288
+ target.tables_served += num(source.tables_served);
289
+ target.table_sessions += num(source.table_sessions);
290
+ target.occupancy_minutes_sum += num(source.occupancy_minutes_sum);
291
+ target.occupancy_sessions += num(source.occupancy_sessions);
292
+ target.fallback_occupancy_sessions += num(source.fallback_occupancy_sessions);
293
+ target.split_parents += num(source.split_parents);
294
+ target.split_children += num(source.split_children);
295
+ target.split_checks += num(source.split_checks);
296
+ target.split_sales += num(source.split_sales);
297
+ target.split_type_counts ?? (target.split_type_counts = {});
298
+ addCountMap(target.split_type_counts, source.split_type_counts);
299
+ target.offline_orders += num(source.offline_orders);
300
+ target.offline_sales += num(source.offline_sales);
301
+ target.origin_orders += num(source.origin_orders);
302
+ target.origin_sales += num(source.origin_sales);
303
+ target.origin_covers += num(source.origin_covers);
304
+ target.service_charge_orders += num(source.service_charge_orders);
305
+ target.service_charge_percentage_orders += num(source.service_charge_percentage_orders);
306
+ target.service_charge_fixed_orders += num(source.service_charge_fixed_orders);
307
+ target.service_charge_original_price_orders += num(source.service_charge_original_price_orders);
308
+ target.service_charge_rate_counts ?? (target.service_charge_rate_counts = {});
309
+ addCountMap(target.service_charge_rate_counts, source.service_charge_rate_counts);
310
+ target.edited_orders += num(source.edited_orders);
311
+ target.total_edits += num(source.total_edits);
312
+ target.item_additions += num(source.item_additions);
313
+ target.item_removals += num(source.item_removals);
314
+ target.net_item_change += num(source.net_item_change);
315
+ target.courses_assigned += num(source.courses_assigned);
316
+ target.courses_called += num(source.courses_called);
317
+ target.courses_on_hold += num(source.courses_on_hold);
318
+ target.course_items += num(source.course_items);
319
+ target.by_course ?? (target.by_course = {});
320
+ for (const [courseId, row] of Object.entries(source.by_course ?? {})) {
321
+ (_a = target.by_course)[courseId] ?? (_a[courseId] = emptyMaterializedDineinCourseRow(courseId, row?.name ?? ''));
322
+ mergeMaterializedDineinCourseRow(target.by_course[courseId], row);
323
+ }
324
+ target.card_amount += num(source.card_amount);
325
+ target.cash_amount += num(source.cash_amount);
326
+ target.other_amount += num(source.other_amount);
327
+ target.card_orders += num(source.card_orders);
328
+ target.cash_orders += num(source.cash_orders);
329
+ target.split_tender_orders += num(source.split_tender_orders);
330
+ target.by_payment_type ?? (target.by_payment_type = {});
331
+ for (const [paymentType, row] of Object.entries(source.by_payment_type ?? {})) {
332
+ (_b = target.by_payment_type)[paymentType] ?? (_b[paymentType] = emptyMaterializedDineinPaymentRow());
333
+ mergeMaterializedDineinPaymentRow(target.by_payment_type[paymentType], row);
334
+ }
335
+ target.rated_orders += num(source.rated_orders);
336
+ target.rating_sum += num(source.rating_sum);
337
+ target.rating_1 += num(source.rating_1);
338
+ target.rating_2 += num(source.rating_2);
339
+ target.rating_3 += num(source.rating_3);
340
+ target.rating_4 += num(source.rating_4);
341
+ target.rating_5 += num(source.rating_5);
342
+ target.review_count += num(source.review_count);
343
+ target.by_hour ?? (target.by_hour = hoursZeroed());
344
+ for (const hour of MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_HOUR_KEYS) {
345
+ (_c = target.by_hour)[hour] ?? (_c[hour] = emptyMaterializedDineinHourRow());
346
+ mergeMaterializedDineinHourRow(target.by_hour[hour], source.by_hour?.[hour] ?? emptyMaterializedDineinHourRow());
347
+ }
348
+ for (const [hour, row] of Object.entries(source.by_hour ?? {})) {
349
+ if (MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_HOUR_KEYS.includes(hour))
350
+ continue;
351
+ (_d = target.by_hour)[hour] ?? (_d[hour] = emptyMaterializedDineinHourRow());
352
+ mergeMaterializedDineinHourRow(target.by_hour[hour], row);
353
+ }
354
+ target.by_status ?? (target.by_status = {});
355
+ for (const [status, row] of Object.entries(source.by_status ?? {})) {
356
+ (_e = target.by_status)[status] ?? (_e[status] = emptyMaterializedDineinBreakdownRow());
357
+ mergeMaterializedDineinBreakdownRow(target.by_status[status], row);
358
+ }
359
+ target.by_platform ?? (target.by_platform = {});
360
+ for (const [platform, row] of Object.entries(source.by_platform ?? {})) {
361
+ (_f = target.by_platform)[platform] ?? (_f[platform] = emptyMaterializedDineinBreakdownRow());
362
+ mergeMaterializedDineinBreakdownRow(target.by_platform[platform], row);
363
+ }
364
+ target.by_origin ?? (target.by_origin = {});
365
+ for (const [origin, row] of Object.entries(source.by_origin ?? {})) {
366
+ (_g = target.by_origin)[origin] ?? (_g[origin] = emptyMaterializedDineinOriginRow());
367
+ mergeMaterializedDineinOriginRow(target.by_origin[origin], row);
368
+ }
369
+ target.by_table ?? (target.by_table = {});
370
+ for (const [tableId, row] of Object.entries(source.by_table ?? {})) {
371
+ (_h = target.by_table)[tableId] ?? (_h[tableId] = emptyMaterializedDineinTableRow(row?.table_id ?? tableId, row?.table_name ?? '', row?.floor_id ?? MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_UNKNOWN_TOKEN));
372
+ mergeMaterializedDineinTableRow(target.by_table[tableId], row);
373
+ }
374
+ target.by_floor ?? (target.by_floor = {});
375
+ for (const [floorId, row] of Object.entries(source.by_floor ?? {})) {
376
+ (_j = target.by_floor)[floorId] ?? (_j[floorId] = emptyMaterializedDineinFloorRow(row?.floor_id ?? floorId));
377
+ mergeMaterializedDineinFloorRow(target.by_floor[floorId], row);
378
+ }
379
+ return target;
380
+ }
381
+ /** Merges one reservation block into another (all counters additive). */
382
+ function mergeMaterializedDineinReservationCounters(target, source) {
383
+ var _a, _b;
384
+ if (target == null || source == null)
385
+ return target;
386
+ target.reservations += num(source.reservations);
387
+ target.reservation_covers += num(source.reservation_covers);
388
+ target.reservation_confirmed += num(source.reservation_confirmed);
389
+ target.reservation_pending += num(source.reservation_pending);
390
+ target.reservation_cancelled += num(source.reservation_cancelled);
391
+ target.reservation_lead_minutes_sum += num(source.reservation_lead_minutes_sum);
392
+ target.reservation_lead_count += num(source.reservation_lead_count);
393
+ target.reservation_duration_minutes_sum += num(source.reservation_duration_minutes_sum);
394
+ target.reservation_duration_count += num(source.reservation_duration_count);
395
+ target.by_hour ?? (target.by_hour = reservationHoursZeroed());
396
+ for (const hour of MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_HOUR_KEYS) {
397
+ (_a = target.by_hour)[hour] ?? (_a[hour] = emptyMaterializedDineinReservationHourRow());
398
+ mergeMaterializedDineinReservationHourRow(target.by_hour[hour], source.by_hour?.[hour] ?? emptyMaterializedDineinReservationHourRow());
399
+ }
400
+ for (const [hour, row] of Object.entries(source.by_hour ?? {})) {
401
+ if (MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_HOUR_KEYS.includes(hour))
402
+ continue;
403
+ (_b = target.by_hour)[hour] ?? (_b[hour] = emptyMaterializedDineinReservationHourRow());
404
+ mergeMaterializedDineinReservationHourRow(target.by_hour[hour], row);
405
+ }
406
+ return target;
407
+ }
408
+ /**
409
+ * Re-bounds ONE keyed map of every counter block at the DOCUMENT level.
410
+ *
411
+ * A day document bounds its own map; a period folds 28-31 such bounds, so the
412
+ * union can exceed the cap. Ranking is `metric DESC, key ASC` (a deterministic
413
+ * tie-break: two closes of the same day must produce the same document) and
414
+ * everything below the top-N is merged into the reserved `__OTHER__` row of the
415
+ * SAME key, so the document still reconciles against its totals.
416
+ */
417
+ function pruneDocumentMap(countersValues, field, limit, metric, merge, empty, decorateOther) {
418
+ if (!Number.isFinite(limit) || limit < 0)
419
+ return;
420
+ const totals = new Map();
421
+ for (const counters of countersValues) {
422
+ const map = (counters?.[field] ?? {});
423
+ for (const [key, row] of Object.entries(map)) {
424
+ if (key === MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN)
425
+ continue;
426
+ totals.set(key, (totals.get(key) ?? 0) + metric(row));
427
+ }
428
+ }
429
+ if (totals.size <= limit)
430
+ return;
431
+ const ranked = [...totals.entries()].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
432
+ const kept = new Set(ranked.slice(0, Math.max(0, Math.floor(limit))).map(([key]) => key));
433
+ for (const counters of countersValues) {
434
+ const map = (counters?.[field] ?? {});
435
+ const dropped = Object.entries(map).filter(([key]) => key !== MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN && !kept.has(key));
436
+ if (dropped.length === 0)
437
+ continue;
438
+ const other = map[MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN] ?? empty(dropped[0][1]);
439
+ for (const [key, row] of dropped) {
440
+ merge(other, row);
441
+ delete map[key];
442
+ }
443
+ decorateOther(other);
444
+ map[MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN] = other;
445
+ counters[field] = map;
446
+ }
447
+ }
448
+ const rankTableRow = (row) => num(row?.sessions) + num(row?.orders);
449
+ const rankFloorRow = (row) => num(row?.sessions) + num(row?.orders);
450
+ const rankCourseRow = (row) => num(row?.assigned);
451
+ /** Applies every document-level bound to one counter map (in place). */
452
+ function pruneDocumentDineinMaps(countersByKey, limits = {}) {
453
+ const countersValues = countersByKey instanceof Map ? [...countersByKey.values()] : Object.values(countersByKey ?? {});
454
+ if (countersValues.length === 0)
455
+ return;
456
+ pruneDocumentMap(countersValues, 'by_table', limits.tables ?? 200, rankTableRow, mergeMaterializedDineinTableRow, () => emptyMaterializedDineinTableRow(MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN, '', MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_UNKNOWN_TOKEN), (row) => {
457
+ row.table_id = MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN;
458
+ row.table_name = 'Other tables';
459
+ });
460
+ pruneDocumentMap(countersValues, 'by_floor', limits.floors ?? 50, rankFloorRow, mergeMaterializedDineinFloorRow, () => emptyMaterializedDineinFloorRow(MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN), (row) => {
461
+ row.floor_id = MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN;
462
+ });
463
+ pruneDocumentMap(countersValues, 'by_course', limits.courses ?? 50, rankCourseRow, mergeMaterializedDineinCourseRow, () => emptyMaterializedDineinCourseRow(MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN, ''), (row) => {
464
+ row.course_id = MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN;
465
+ row.name = 'Other courses';
466
+ });
467
+ // The service-charge rate map is a plain count map, not a row map: bound it
468
+ // with the same ranking rule and fold the tail into the reserved key.
469
+ const rateLimit = limits.serviceChargeRates ?? 50;
470
+ if (rateLimit >= 0) {
471
+ const rates = new Set();
472
+ for (const counters of countersValues) {
473
+ for (const rate of Object.keys(counters?.service_charge_rate_counts ?? {})) {
474
+ if (rate !== MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN)
475
+ rates.add(rate);
476
+ }
477
+ }
478
+ if (rates.size > rateLimit) {
479
+ const totals = new Map();
480
+ for (const counters of countersValues) {
481
+ for (const [rate, value] of Object.entries(counters?.service_charge_rate_counts ?? {})) {
482
+ if (rate === MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN)
483
+ continue;
484
+ totals.set(rate, (totals.get(rate) ?? 0) + num(value));
485
+ }
486
+ }
487
+ const ranked = [...totals.entries()].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
488
+ const kept = new Set(ranked.slice(0, Math.max(0, Math.floor(rateLimit))).map(([rate]) => rate));
489
+ for (const counters of countersValues) {
490
+ const map = counters?.service_charge_rate_counts;
491
+ if (map == null)
492
+ continue;
493
+ const dropped = Object.entries(map).filter(([rate]) => rate !== MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN && !kept.has(rate));
494
+ if (dropped.length === 0)
495
+ continue;
496
+ let overflow = num(map[MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN]);
497
+ for (const [rate, value] of dropped) {
498
+ overflow += num(value);
499
+ delete map[rate];
500
+ }
501
+ map[MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_OTHER_TOKEN] = overflow;
502
+ counters.service_charge_rate_counts = map;
503
+ }
504
+ }
505
+ }
506
+ }
507
+ /* ------------------------------------------------------------------------ */
508
+ /* Bounded summary snapshots */
509
+ /* ------------------------------------------------------------------------ */
510
+ /**
511
+ * Merges the UPCOMING RESERVATION snapshots of several day documents.
512
+ *
513
+ * Dedupe by `reservation_id` (the same booking can be re-closed), then keep the
514
+ * SOONEST `limit` rows. Ordering is `reservation_time ASC, reservation_id ASC`
515
+ * so two folds of the same days produce the same array.
516
+ */
517
+ function mergeMaterializedDineinUpcomingReservations(lists, limit = MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_UPCOMING_RESERVATION_LIMIT) {
518
+ const byId = new Map();
519
+ for (const list of lists ?? []) {
520
+ for (const row of list ?? []) {
521
+ if (row == null)
522
+ continue;
523
+ const id = String(row.reservation_id ?? '');
524
+ if (id === '')
525
+ continue;
526
+ byId.set(id, {
527
+ reservation_id: id,
528
+ // The label is already masked by the writer; a re-close may improve it.
529
+ guest_label: String(row.guest_label ?? ''),
530
+ reservation_time: String(row.reservation_time ?? ''),
531
+ local_hour: num(row.local_hour),
532
+ party_size: num(row.party_size),
533
+ status: String(row.status ?? ''),
534
+ source: String(row.source ?? ''),
535
+ lead_minutes: row.lead_minutes == null ? null : num(row.lead_minutes),
536
+ });
537
+ }
538
+ }
539
+ const sorted = [...byId.values()].sort((a, b) => {
540
+ const at = new Date(a.reservation_time).getTime();
541
+ const bt = new Date(b.reservation_time).getTime();
542
+ if (Number.isNaN(at) || Number.isNaN(bt)) {
543
+ return a.reservation_id < b.reservation_id ? -1 : a.reservation_id > b.reservation_id ? 1 : 0;
544
+ }
545
+ return at - bt || (a.reservation_id < b.reservation_id ? -1 : a.reservation_id > b.reservation_id ? 1 : 0);
546
+ });
547
+ return sorted.slice(0, Math.max(0, Math.floor(limit)));
548
+ }
549
+ /**
550
+ * Merges the MOST-EDITED-CHECK snapshots of several day documents.
551
+ *
552
+ * Dedupe by `order_id`, rank `edits DESC, item_additions DESC, order_id ASC`
553
+ * and keep `limit` rows. Deterministic, so a period re-fold is stable.
554
+ */
555
+ function mergeMaterializedDineinTopEditedChecks(lists, limit = MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_TOP_EDITED_CHECK_LIMIT) {
556
+ const byId = new Map();
557
+ for (const list of lists ?? []) {
558
+ for (const row of list ?? []) {
559
+ if (row == null)
560
+ continue;
561
+ const id = String(row.order_id ?? '');
562
+ if (id === '')
563
+ continue;
564
+ byId.set(id, {
565
+ order_id: id,
566
+ order_number: row.order_number == null ? null : num(row.order_number),
567
+ table_id: String(row.table_id ?? ''),
568
+ table_name: String(row.table_name ?? ''),
569
+ floor_id: String(row.floor_id ?? ''),
570
+ edits: num(row.edits),
571
+ item_additions: num(row.item_additions),
572
+ item_removals: num(row.item_removals),
573
+ covers: num(row.covers),
574
+ sales: num(row.sales),
575
+ });
576
+ }
577
+ }
578
+ const sorted = [...byId.values()].sort((a, b) => b.edits - a.edits ||
579
+ b.item_additions - a.item_additions ||
580
+ (a.order_id < b.order_id ? -1 : a.order_id > b.order_id ? 1 : 0));
581
+ return sorted.slice(0, Math.max(0, Math.floor(limit)));
582
+ }