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