@managesome/eposmatic-toolkit 0.4.60 → 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.
Files changed (74) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +177 -4
  2. package/esm/helpers/materialized-reports/index.js +7 -0
  3. package/esm/helpers/materialized-reports/materialized-day-readiness.js +2 -2
  4. package/esm/helpers/materialized-reports/materialized-dinein-accumulator-helpers.js +557 -0
  5. package/esm/helpers/materialized-reports/materialized-dinein-classification-helpers.js +447 -0
  6. package/esm/helpers/materialized-reports/materialized-dinein-close-day-helpers.js +372 -0
  7. package/esm/helpers/materialized-reports/materialized-dinein-compile-helpers.js +500 -0
  8. package/esm/helpers/materialized-reports/materialized-dinein-entry-pages.js +218 -0
  9. package/esm/helpers/materialized-reports/materialized-dinein-key-helpers.js +87 -0
  10. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
  11. package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +8 -0
  12. package/esm/helpers/materialized-reports/report-platform/definition.js +93 -1
  13. package/esm/helpers/materialized-reports/tenant-dinein-formatter.js +240 -0
  14. package/esm/helpers/reporting-contract/report-contract.js +3 -0
  15. package/esm/schemas/models/index.js +2 -0
  16. package/esm/schemas/models/materialized_tenant_dinein_entries.js +57 -0
  17. package/esm/schemas/models/materialized_tenant_dinein_reports.js +73 -0
  18. package/esm/schemas/types/index.js +2 -0
  19. package/esm/schemas/types/materialized_report_drilldown_rows.js +1 -0
  20. package/esm/schemas/types/materialized_tenant_dinein_entries.js +1 -0
  21. package/esm/schemas/types/materialized_tenant_dinein_reports.js +6 -0
  22. package/lib/constants/MATERIALIZED_REPORTS.d.ts +160 -4
  23. package/lib/constants/MATERIALIZED_REPORTS.js +178 -5
  24. package/lib/helpers/materialized-reports/index.d.ts +7 -0
  25. package/lib/helpers/materialized-reports/index.js +7 -0
  26. package/lib/helpers/materialized-reports/materialized-day-readiness.d.ts +1 -1
  27. package/lib/helpers/materialized-reports/materialized-day-readiness.js +2 -2
  28. package/lib/helpers/materialized-reports/materialized-dinein-accumulator-helpers.d.ts +67 -0
  29. package/lib/helpers/materialized-reports/materialized-dinein-accumulator-helpers.js +582 -0
  30. package/lib/helpers/materialized-reports/materialized-dinein-classification-helpers.d.ts +245 -0
  31. package/lib/helpers/materialized-reports/materialized-dinein-classification-helpers.js +469 -0
  32. package/lib/helpers/materialized-reports/materialized-dinein-close-day-helpers.d.ts +110 -0
  33. package/lib/helpers/materialized-reports/materialized-dinein-close-day-helpers.js +384 -0
  34. package/lib/helpers/materialized-reports/materialized-dinein-compile-helpers.d.ts +73 -0
  35. package/lib/helpers/materialized-reports/materialized-dinein-compile-helpers.js +504 -0
  36. package/lib/helpers/materialized-reports/materialized-dinein-entry-pages.d.ts +69 -0
  37. package/lib/helpers/materialized-reports/materialized-dinein-entry-pages.js +231 -0
  38. package/lib/helpers/materialized-reports/materialized-dinein-key-helpers.d.ts +44 -0
  39. package/lib/helpers/materialized-reports/materialized-dinein-key-helpers.js +96 -0
  40. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +4 -0
  41. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +8 -0
  42. package/lib/helpers/materialized-reports/report-platform/definition.js +92 -0
  43. package/lib/helpers/materialized-reports/tenant-dinein-formatter.d.ts +225 -0
  44. package/lib/helpers/materialized-reports/tenant-dinein-formatter.js +243 -0
  45. package/lib/helpers/reporting-contract/report-contract.d.ts +1 -0
  46. package/lib/helpers/reporting-contract/report-contract.js +3 -0
  47. package/lib/schemas/models/index.d.ts +2 -0
  48. package/lib/schemas/models/index.js +2 -0
  49. package/lib/schemas/models/materialized_tenant_day_readiness.d.ts +3 -3
  50. package/lib/schemas/models/materialized_tenant_dinein_entries.d.ts +100 -0
  51. package/lib/schemas/models/materialized_tenant_dinein_entries.js +60 -0
  52. package/lib/schemas/models/materialized_tenant_dinein_reports.d.ts +111 -0
  53. package/lib/schemas/models/materialized_tenant_dinein_reports.js +76 -0
  54. package/lib/schemas/types/index.d.ts +2 -0
  55. package/lib/schemas/types/index.js +2 -0
  56. package/lib/schemas/types/materialized_report_drilldown_rows.d.ts +1 -1
  57. package/lib/schemas/types/materialized_report_drilldown_rows.js +1 -0
  58. package/lib/schemas/types/materialized_tenant_dinein_entries.d.ts +127 -0
  59. package/lib/schemas/types/materialized_tenant_dinein_entries.js +2 -0
  60. package/lib/schemas/types/materialized_tenant_dinein_reports.d.ts +326 -0
  61. package/lib/schemas/types/materialized_tenant_dinein_reports.js +9 -0
  62. package/lib/tests/materialized-reports/dinein-fixtures.d.ts +29 -0
  63. package/lib/tests/materialized-reports/dinein-fixtures.js +130 -0
  64. package/lib/tests/materialized-reports/materialized-day-readiness.test.js +3 -2
  65. package/lib/tests/materialized-reports/materialized-dinein-compile.test.d.ts +1 -0
  66. package/lib/tests/materialized-reports/materialized-dinein-compile.test.js +807 -0
  67. package/lib/tests/materialized-reports/materialized-dinein-plan.test.d.ts +1 -0
  68. package/lib/tests/materialized-reports/materialized-dinein-plan.test.js +419 -0
  69. package/lib/tests/materialized-reports/materialized-tenant-dinein-schema.test.d.ts +1 -0
  70. package/lib/tests/materialized-reports/materialized-tenant-dinein-schema.test.js +185 -0
  71. package/lib/tests/materialized-reports/report-platform/registry-conformance.test.js +15 -2
  72. package/lib/tests/materialized-reports/tenant-dinein-formatter.test.d.ts +1 -0
  73. package/lib/tests/materialized-reports/tenant-dinein-formatter.test.js +299 -0
  74. package/package.json +1 -1
@@ -0,0 +1,240 @@
1
+ import { MATERIALIZED_DINEIN_HOUR_KEYS, MATERIALIZED_DINEIN_OTHER_TOKEN } from '../../constants/MATERIALIZED_REPORTS';
2
+ import { freshMaterializedDineinCounters, freshMaterializedDineinReservationCounters, mergeMaterializedDineinCounters, mergeMaterializedDineinReservationCounters, mergeMaterializedDineinTopEditedChecks, mergeMaterializedDineinUpcomingReservations, } from './materialized-dinein-accumulator-helpers';
3
+ import { dineinKeyPassesFilters } from './materialized-dinein-key-helpers';
4
+ const num = (value) => {
5
+ const number = typeof value === 'number' ? value : Number(value);
6
+ return Number.isFinite(number) ? number : 0;
7
+ };
8
+ const percent = (numerator, denominator) => {
9
+ if (!Number.isFinite(denominator) || denominator <= 0)
10
+ return null;
11
+ return Math.round((numerator / denominator) * 10000) / 100;
12
+ };
13
+ const ratio = (numerator, denominator, decimals = 2) => {
14
+ if (!Number.isFinite(denominator) || denominator <= 0)
15
+ return null;
16
+ const factor = Math.pow(10, decimals);
17
+ return Math.round((numerator / denominator) * factor) / factor;
18
+ };
19
+ const minutesRatio = (totalMinutes, count) => ratio(totalMinutes, count, 1);
20
+ const summaryOf = (counters) => {
21
+ const paymentTotal = counters.card_amount + counters.cash_amount + counters.other_amount;
22
+ const measuredSessions = counters.occupancy_sessions + counters.fallback_occupancy_sessions;
23
+ return {
24
+ orders: counters.orders,
25
+ completed_orders: counters.completed_orders,
26
+ cancelled_orders: counters.cancelled_orders,
27
+ covers: counters.covers,
28
+ items: counters.items,
29
+ average_ticket: ratio(counters.sales, counters.orders),
30
+ average_covers_per_check: ratio(counters.covers, counters.orders),
31
+ cancelled_rate: percent(counters.cancelled_orders, counters.orders),
32
+ sales: counters.sales,
33
+ discounts: counters.discounts,
34
+ tax: counters.tax,
35
+ tips: counters.tips,
36
+ service_charge_amount: counters.service_charge_amount,
37
+ service_charge_taxable_amount: counters.service_charge_taxable_amount,
38
+ surcharge_amount: counters.surcharge_amount,
39
+ refunds: counters.refunds,
40
+ service_charge_share: percent(counters.service_charge_amount, counters.sales),
41
+ tables_served: counters.tables_served,
42
+ tables_served_distinct: 0,
43
+ tables_truncated: false,
44
+ table_sessions: counters.table_sessions,
45
+ occupancy_minutes_sum: counters.occupancy_minutes_sum,
46
+ occupancy_sessions: counters.occupancy_sessions,
47
+ fallback_occupancy_sessions: counters.fallback_occupancy_sessions,
48
+ average_occupancy_minutes: minutesRatio(counters.occupancy_minutes_sum, measuredSessions),
49
+ split_parents: counters.split_parents,
50
+ split_children: counters.split_children,
51
+ split_checks: counters.split_checks,
52
+ split_sales: counters.split_sales,
53
+ split_type_counts: { ...(counters.split_type_counts ?? {}) },
54
+ split_sales_share: percent(counters.split_sales, counters.sales),
55
+ offline_orders: counters.offline_orders,
56
+ offline_sales: counters.offline_sales,
57
+ offline_sales_share: percent(counters.offline_sales, counters.sales),
58
+ origin_orders: counters.origin_orders,
59
+ origin_sales: counters.origin_sales,
60
+ origin_covers: counters.origin_covers,
61
+ service_charge_orders: counters.service_charge_orders,
62
+ service_charge_percentage_orders: counters.service_charge_percentage_orders,
63
+ service_charge_fixed_orders: counters.service_charge_fixed_orders,
64
+ service_charge_original_price_orders: counters.service_charge_original_price_orders,
65
+ service_charge_rate_counts: { ...(counters.service_charge_rate_counts ?? {}) },
66
+ edited_orders: counters.edited_orders,
67
+ total_edits: counters.total_edits,
68
+ item_additions: counters.item_additions,
69
+ item_removals: counters.item_removals,
70
+ net_item_change: counters.net_item_change,
71
+ edit_rate: percent(counters.edited_orders, counters.orders),
72
+ average_edits_per_check: ratio(counters.total_edits, counters.orders),
73
+ courses_assigned: counters.courses_assigned,
74
+ courses_called: counters.courses_called,
75
+ courses_on_hold: counters.courses_on_hold,
76
+ course_items: counters.course_items,
77
+ course_call_rate: percent(counters.courses_called, counters.courses_assigned),
78
+ card_amount: counters.card_amount,
79
+ cash_amount: counters.cash_amount,
80
+ other_amount: counters.other_amount,
81
+ card_orders: counters.card_orders,
82
+ cash_orders: counters.cash_orders,
83
+ split_tender_orders: counters.split_tender_orders,
84
+ card_share: percent(counters.card_amount, paymentTotal),
85
+ cash_share: percent(counters.cash_amount, paymentTotal),
86
+ other_share: percent(counters.other_amount, paymentTotal),
87
+ rated_orders: counters.rated_orders,
88
+ rating_sum: counters.rating_sum,
89
+ rating_1: counters.rating_1,
90
+ rating_2: counters.rating_2,
91
+ rating_3: counters.rating_3,
92
+ rating_4: counters.rating_4,
93
+ rating_5: counters.rating_5,
94
+ review_count: counters.review_count,
95
+ average_rating: ratio(counters.rating_sum, counters.rated_orders),
96
+ };
97
+ };
98
+ const rankDesc = (a, b, keyA, keyB) => b - a || (keyA < keyB ? -1 : keyA > keyB ? 1 : 0);
99
+ /**
100
+ * Formats the report documents of one scope level into the page payload.
101
+ *
102
+ * Accepts the array returned by a live/historical read or a single stored
103
+ * document; it never throws and never mutates its input.
104
+ */
105
+ export function formatMaterializedDineinReport(documents, options = {}) {
106
+ const docs = (documents ?? []).filter((doc) => doc != null);
107
+ const filters = options.filters ?? null;
108
+ const total = freshMaterializedDineinCounters();
109
+ const reservations = freshMaterializedDineinReservationCounters();
110
+ const tenantIds = new Set();
111
+ const scopes = new Set();
112
+ const upcomingLists = [];
113
+ const editedLists = [];
114
+ for (const doc of docs) {
115
+ if (doc.tenant_id != null)
116
+ tenantIds.add(String(doc.tenant_id));
117
+ scopes.add(`${String(doc.scope_context)}|${String(doc.scope_field)}`);
118
+ for (const [key, block] of Object.entries(doc.dinein_counters ?? {})) {
119
+ if (!dineinKeyPassesFilters(key, filters))
120
+ continue;
121
+ mergeMaterializedDineinCounters(total, block);
122
+ }
123
+ // Reservation counters are document-level: never key-filtered.
124
+ mergeMaterializedDineinReservationCounters(reservations, doc.reservation_counters ?? freshMaterializedDineinReservationCounters());
125
+ upcomingLists.push(doc.upcoming_reservations ?? null);
126
+ editedLists.push(doc.top_edited_checks ?? null);
127
+ }
128
+ const tables = total.by_table ?? {};
129
+ const namedTables = Object.keys(tables).filter((key) => key !== MATERIALIZED_DINEIN_OTHER_TOKEN);
130
+ const tablesTruncated = tables[MATERIALIZED_DINEIN_OTHER_TOKEN] != null;
131
+ const summary = summaryOf(total);
132
+ summary.tables_served_distinct = namedTables.length;
133
+ summary.tables_truncated = tablesTruncated;
134
+ const byHour = MATERIALIZED_DINEIN_HOUR_KEYS.map((label) => {
135
+ const row = total.by_hour?.[label] ?? { orders: 0, sales: 0, covers: 0 };
136
+ return {
137
+ hour: Number(label),
138
+ label,
139
+ orders: num(row.orders),
140
+ sales: num(row.sales),
141
+ covers: num(row.covers),
142
+ };
143
+ });
144
+ const byOrigin = Object.entries(total.by_origin ?? {})
145
+ .map(([origin, row]) => ({
146
+ origin,
147
+ orders: num(row?.orders),
148
+ sales: num(row?.sales),
149
+ covers: num(row?.covers),
150
+ }))
151
+ .sort((a, b) => rankDesc(a.orders, b.orders, a.origin, b.origin));
152
+ const byTable = Object.entries(tables)
153
+ .map(([key, row]) => ({
154
+ key,
155
+ table_id: String(row?.table_id ?? key),
156
+ table_name: String(row?.table_name ?? ''),
157
+ floor_id: String(row?.floor_id ?? ''),
158
+ orders: num(row?.orders),
159
+ sessions: num(row?.sessions),
160
+ covers: num(row?.covers),
161
+ occupancy_minutes: num(row?.occupancy_minutes),
162
+ sales: num(row?.sales),
163
+ average_occupancy_minutes: minutesRatio(num(row?.occupancy_minutes), num(row?.sessions)),
164
+ }))
165
+ .sort((a, b) => rankDesc(a.sessions + a.orders, b.sessions + b.orders, a.key, b.key));
166
+ const byFloor = Object.entries(total.by_floor ?? {})
167
+ .map(([key, row]) => ({
168
+ key,
169
+ floor_id: String(row?.floor_id ?? key),
170
+ orders: num(row?.orders),
171
+ sessions: num(row?.sessions),
172
+ covers: num(row?.covers),
173
+ occupancy_minutes: num(row?.occupancy_minutes),
174
+ sales: num(row?.sales),
175
+ }))
176
+ .sort((a, b) => rankDesc(a.sessions + a.orders, b.sessions + b.orders, a.key, b.key));
177
+ const byCourse = Object.entries(total.by_course ?? {})
178
+ .map(([key, row]) => ({
179
+ key,
180
+ course_id: String(row?.course_id ?? key),
181
+ name: String(row?.name ?? key),
182
+ assigned: num(row?.assigned),
183
+ called: num(row?.called),
184
+ on_hold: num(row?.on_hold),
185
+ items: num(row?.items),
186
+ call_rate: percent(num(row?.called), num(row?.assigned)),
187
+ }))
188
+ .sort((a, b) => rankDesc(a.assigned, b.assigned, a.key, b.key));
189
+ const breakdownRows = (map) => Object.entries(map ?? {})
190
+ .map(([key, row]) => ({
191
+ key,
192
+ orders: num(row?.orders),
193
+ covers: num(row?.covers),
194
+ sales: num(row?.sales),
195
+ }))
196
+ .sort((a, b) => rankDesc(a.orders, b.orders, a.key, b.key));
197
+ const byStatus = breakdownRows(total.by_status);
198
+ const byPlatform = breakdownRows(total.by_platform);
199
+ const reservationByHour = MATERIALIZED_DINEIN_HOUR_KEYS.map((label) => {
200
+ const row = reservations.by_hour?.[label] ?? { reservations: 0, reservation_covers: 0 };
201
+ return {
202
+ hour: Number(label),
203
+ label,
204
+ reservations: num(row.reservations),
205
+ reservation_covers: num(row.reservation_covers),
206
+ };
207
+ });
208
+ const byPaymentType = Object.entries(total.by_payment_type ?? {})
209
+ .map(([paymentType, row]) => ({
210
+ payment_type: paymentType,
211
+ amount: num(row?.amount),
212
+ orders: num(row?.orders),
213
+ }))
214
+ .sort((a, b) => rankDesc(a.amount, b.amount, a.payment_type, b.payment_type));
215
+ return {
216
+ tenant_ids: [...tenantIds].sort(),
217
+ summary,
218
+ by_hour: byHour,
219
+ by_origin: byOrigin,
220
+ by_table: byTable,
221
+ by_floor: byFloor,
222
+ by_course: byCourse,
223
+ by_payment_type: byPaymentType,
224
+ by_status: byStatus,
225
+ by_platform: byPlatform,
226
+ reservation_by_hour: reservationByHour,
227
+ upcoming_reservations: mergeMaterializedDineinUpcomingReservations(upcomingLists),
228
+ top_edited_checks: mergeMaterializedDineinTopEditedChecks(editedLists),
229
+ reservations: {
230
+ ...reservations,
231
+ average_party_size: ratio(reservations.reservation_covers, reservations.reservations),
232
+ average_lead_minutes: ratio(reservations.reservation_lead_minutes_sum, reservations.reservation_lead_count, 1),
233
+ average_duration_minutes: ratio(reservations.reservation_duration_minutes_sum, reservations.reservation_duration_count, 1),
234
+ },
235
+ scopes: [...scopes].sort().map((identity) => {
236
+ const [scope_context, scope_field] = identity.split('|');
237
+ return { scope_context, scope_field };
238
+ }),
239
+ };
240
+ }
@@ -31,6 +31,7 @@ export var REPORT_KEY_ENUM;
31
31
  REPORT_KEY_ENUM["CASH_IN_OUT"] = "CASH_IN_OUT";
32
32
  REPORT_KEY_ENUM["DELIVERY"] = "DELIVERY";
33
33
  REPORT_KEY_ENUM["DIALER"] = "DIALER";
34
+ REPORT_KEY_ENUM["DINEIN"] = "DINEIN";
34
35
  REPORT_KEY_ENUM["SALES_COMPARISON"] = "SALES_COMPARISON";
35
36
  REPORT_KEY_ENUM["HOURLY_COMPARISON"] = "HOURLY_COMPARISON";
36
37
  REPORT_KEY_ENUM["PAYMENTS_COMPARISON"] = "PAYMENTS_COMPARISON";
@@ -53,6 +54,7 @@ export const REPORT_ROUTE_SEGMENTS = {
53
54
  [REPORT_KEY_ENUM.CASH_IN_OUT]: 'cash-in-out',
54
55
  [REPORT_KEY_ENUM.DELIVERY]: 'delivery',
55
56
  [REPORT_KEY_ENUM.DIALER]: 'dialer',
57
+ [REPORT_KEY_ENUM.DINEIN]: 'dinein',
56
58
  [REPORT_KEY_ENUM.SALES_COMPARISON]: 'sales-comparison',
57
59
  [REPORT_KEY_ENUM.HOURLY_COMPARISON]: 'hourly-comparison',
58
60
  [REPORT_KEY_ENUM.PAYMENTS_COMPARISON]: 'payments-comparison',
@@ -84,6 +86,7 @@ export const REPORT_CAPABILITIES = {
84
86
  [REPORT_KEY_ENUM.CASH_IN_OUT]: { live: true, historical: true, details: true },
85
87
  [REPORT_KEY_ENUM.DELIVERY]: { live: true, historical: true, details: true },
86
88
  [REPORT_KEY_ENUM.DIALER]: { live: true, historical: true, details: true },
89
+ [REPORT_KEY_ENUM.DINEIN]: { live: true, historical: true, details: true },
87
90
  [REPORT_KEY_ENUM.HOURLY_COMPARISON]: { live: true, historical: true, details: true },
88
91
  [REPORT_KEY_ENUM.PAYMENTS_COMPARISON]: { live: true, historical: true, details: true },
89
92
  [REPORT_KEY_ENUM.CREDIT_BILLS]: { live: false, historical: true, details: true },
@@ -40,6 +40,8 @@ export * from './materialized_tenant_discount_entries';
40
40
  export * from './materialized_tenant_discount_reports';
41
41
  export * from './materialized_tenant_dialer_entries';
42
42
  export * from './materialized_tenant_dialer_reports';
43
+ export * from './materialized_tenant_dinein_entries';
44
+ export * from './materialized_tenant_dinein_reports';
43
45
  export * from './materialized_tenant_discount_sources';
44
46
  export * from './materialized_tenant_hourly_comparison_entries';
45
47
  export * from './materialized_tenant_hourly_comparison_reports';
@@ -0,0 +1,57 @@
1
+ import { Schema } from 'mongoose';
2
+ import { ALL_DAY_INDEX } from '../../constants/ENUMS';
3
+ import { MATERIALIZED_DINEIN_ENTRY_SCOPES } from '../../constants/MATERIALIZED_REPORTS';
4
+ /**
5
+ * Mongoose schema for `materialized_tenant_dinein_entries`.
6
+ *
7
+ * `dinein_entries` is `Mixed`: it is a key-addressed map of row sections
8
+ * (`<ORDER_STATUS>:<PLATFORM>` → rows), and the keys ARE the data.
9
+ *
10
+ * The UNIQUE identity includes `page`, exactly like the discount / delivery /
11
+ * dialer page collections: a page-per-source-day design cannot coexist with a
12
+ * unique index that omits `page`, because every period page of a month shares
13
+ * `day_index: null`.
14
+ */
15
+ /** Document shape version. Bump on a row-shape change + backfill. */
16
+ export const MATERIALIZED_TENANT_DINEIN_ENTRIES_SCHEMA_VERSION = 1;
17
+ export const MaterializedTenantDineinEntriesSchema = new Schema({
18
+ tenant_id: { type: String, required: true },
19
+ brand_id: { type: String, required: true },
20
+ scope_context: { type: String, required: true, enum: MATERIALIZED_DINEIN_ENTRY_SCOPES },
21
+ scope_field: { type: String, required: true },
22
+ page: { type: String, required: true },
23
+ day_index: { type: String, default: null, enum: [...ALL_DAY_INDEX, null] },
24
+ dinein_entries: { type: Schema.Types.Mixed, required: true, default: null },
25
+ iteration: { type: Number, required: true, min: 1, default: 1 },
26
+ schema_version: {
27
+ type: Number,
28
+ required: true,
29
+ immutable: true,
30
+ default: MATERIALIZED_TENANT_DINEIN_ENTRIES_SCHEMA_VERSION,
31
+ },
32
+ source_watermark: { type: Schema.Types.Date, required: true },
33
+ computed_at: { type: Schema.Types.Date, required: true },
34
+ createdAt: { type: Schema.Types.Date, required: true, default: () => new Date(), immutable: true },
35
+ updatedAt: { type: Schema.Types.Date, required: true, default: () => new Date() },
36
+ }, { timestamps: true, versionKey: false, minimize: false });
37
+ export const MATERIALIZED_TENANT_DINEIN_ENTRIES_COLLECTION_NAME = 'materialized_tenant_dinein_entries';
38
+ /**
39
+ * The single UNIQUE compound identity index (tenant + scope + value + page).
40
+ * Declared ONLY — never applied by this package or the services.
41
+ */
42
+ export const MATERIALIZED_TENANT_DINEIN_ENTRIES_COLLECTION_INDEXES = {
43
+ _id: { _id: 1 },
44
+ tenant_id__scope_context__scope_field__page: {
45
+ tenant_id: 1,
46
+ scope_context: 1,
47
+ scope_field: 1,
48
+ page: 1,
49
+ },
50
+ };
51
+ export const MATERIALIZED_TENANT_DINEIN_ENTRIES_INDEX_OPTIONS = [
52
+ {
53
+ key: { tenant_id: 1, scope_context: 1, scope_field: 1, page: 1 },
54
+ name: 'tenant_id__scope_context__scope_field__page',
55
+ unique: true,
56
+ },
57
+ ];
@@ -0,0 +1,73 @@
1
+ import { Schema } from 'mongoose';
2
+ import { ALL_DAY_INDEX } from '../../constants/ENUMS';
3
+ import { ALL_MATERIALIZED_REPORT_KEY_FILTERS, DINEIN_KEY_FILTERS, MATERIALIZED_DINEIN_REPORT_SCOPES, } from '../../constants/MATERIALIZED_REPORTS';
4
+ /**
5
+ * Mongoose schema for `materialized_tenant_dinein_reports`.
6
+ *
7
+ * `dinein_counters` is `Mixed` on purpose: it is a key-addressed map of bounded
8
+ * counter blocks (`<ORDER_STATUS>:<PLATFORM>` → counters + bounded dimension
9
+ * maps), and the keys ARE the data — Mongoose cannot type them without a fixed
10
+ * key set. `reservation_counters` is a DOCUMENT-level block because a
11
+ * reservation has no order and must survive any order-status/platform filter.
12
+ *
13
+ * The collection holds COUNTERS only. Itemized check rows live in the sibling
14
+ * `materialized_tenant_dinein_entries` collection (one page per source day).
15
+ */
16
+ /** Document shape version. Bump on a key-grammar/metric change + backfill. */
17
+ export const MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION = 1;
18
+ export const MaterializedTenantDineinReportSchema = new Schema({
19
+ tenant_id: { type: String, required: true },
20
+ brand_id: { type: String, required: true },
21
+ scope_context: { type: String, required: true, enum: MATERIALIZED_DINEIN_REPORT_SCOPES },
22
+ scope_field: { type: String, required: true },
23
+ day_index: { type: String, default: null, enum: [...ALL_DAY_INDEX, null] },
24
+ sale_type_key_filters: {
25
+ type: [String],
26
+ required: true,
27
+ default: () => [...DINEIN_KEY_FILTERS],
28
+ validate: {
29
+ validator: (value) => value.length > 0 &&
30
+ value.every((entry) => ALL_MATERIALIZED_REPORT_KEY_FILTERS.includes(entry)),
31
+ message: 'sale_type_key_filters must be a non-empty subset of the known key filters',
32
+ },
33
+ },
34
+ dinein_counters: { type: Schema.Types.Mixed, required: true, default: null },
35
+ reservation_counters: { type: Schema.Types.Mixed, required: true, default: null },
36
+ // Bounded, masked display snapshots. `Mixed` for the same reason as the
37
+ // counters: the arrays are written whole and never queried element-wise.
38
+ upcoming_reservations: { type: Schema.Types.Mixed, default: null },
39
+ top_edited_checks: { type: Schema.Types.Mixed, default: null },
40
+ iteration: { type: Number, required: true, min: 1, default: 1 },
41
+ schema_version: {
42
+ type: Number,
43
+ required: true,
44
+ immutable: true,
45
+ default: MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION,
46
+ },
47
+ source_watermark: { type: Schema.Types.Date, required: true },
48
+ computed_at: { type: Schema.Types.Date, required: true },
49
+ createdAt: { type: Schema.Types.Date, required: true, default: () => new Date(), immutable: true },
50
+ updatedAt: { type: Schema.Types.Date, required: true, default: () => new Date() },
51
+ }, { timestamps: true, versionKey: false, minimize: false });
52
+ export const MATERIALIZED_TENANT_DINEIN_REPORTS_COLLECTION_NAME = 'materialized_tenant_dinein_reports';
53
+ /**
54
+ * The single UNIQUE compound identity index, like every materialized report
55
+ * collection. Declared ONLY — no service calls `index()` or `ensureIndexes`, and
56
+ * the services run with `autoIndex: false`. Applying it is an owner deploy step.
57
+ */
58
+ export const MATERIALIZED_TENANT_DINEIN_REPORTS_COLLECTION_INDEXES = {
59
+ _id: { _id: 1 },
60
+ tenant_id__scope_context__scope_field__day_index: {
61
+ tenant_id: 1,
62
+ scope_context: 1,
63
+ scope_field: 1,
64
+ day_index: 1,
65
+ },
66
+ };
67
+ export const MATERIALIZED_TENANT_DINEIN_REPORTS_INDEX_OPTIONS = [
68
+ {
69
+ key: { tenant_id: 1, scope_context: 1, scope_field: 1, day_index: 1 },
70
+ name: 'tenant_id__scope_context__scope_field__day_index',
71
+ unique: true,
72
+ },
73
+ ];
@@ -44,6 +44,8 @@ export * from './materialized_tenant_discount_entries';
44
44
  export * from './materialized_tenant_discount_reports';
45
45
  export * from './materialized_tenant_dialer_entries';
46
46
  export * from './materialized_tenant_dialer_reports';
47
+ export * from './materialized_tenant_dinein_entries';
48
+ export * from './materialized_tenant_dinein_reports';
47
49
  export * from './materialized_tenant_discount_sources';
48
50
  export * from './materialized_tenant_hourly_comparison_entries';
49
51
  export * from './materialized_tenant_hourly_comparison_reports';
@@ -8,4 +8,5 @@ export const ALL_DRILLDOWN_REPORT_TYPES = [
8
8
  MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES,
9
9
  MATERIALIZED_REPORT_TYPE_ENUM.TENANT_PAYMENTS_COMPARISON_ENTRIES,
10
10
  MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DIALER_ENTRIES,
11
+ MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DINEIN_ENTRIES,
11
12
  ];
@@ -0,0 +1,6 @@
1
+ import { DINEIN_KEY_FILTERS, } from '../../constants/MATERIALIZED_REPORTS';
2
+ /** The default key-filter declaration of a dinein document. */
3
+ export const MATERIALIZED_DINEIN_REPORT_KEY_FILTERS = [
4
+ DINEIN_KEY_FILTERS[0],
5
+ DINEIN_KEY_FILTERS[1],
6
+ ];
@@ -565,7 +565,9 @@ export declare enum MATERIALIZED_REPORT_TYPE_ENUM {
565
565
  TENANT_PAYMENTS_COMPARISON_REPORTS = "TENANT_PAYMENTS_COMPARISON_REPORTS",
566
566
  TENANT_PAYMENTS_COMPARISON_ENTRIES = "TENANT_PAYMENTS_COMPARISON_ENTRIES",
567
567
  TENANT_DIALER_REPORTS = "TENANT_DIALER_REPORTS",
568
- TENANT_DIALER_ENTRIES = "TENANT_DIALER_ENTRIES"
568
+ TENANT_DIALER_ENTRIES = "TENANT_DIALER_ENTRIES",
569
+ TENANT_DINEIN_REPORTS = "TENANT_DINEIN_REPORTS",
570
+ TENANT_DINEIN_ENTRIES = "TENANT_DINEIN_ENTRIES"
569
571
  }
570
572
  export declare const ALL_MATERIALIZED_REPORT_TYPES: MATERIALIZED_REPORT_TYPE_ENUM[];
571
573
  /**
@@ -734,8 +736,162 @@ export declare const MATERIALIZED_DIALER_HOUR_KEYS: string[];
734
736
  export declare const MATERIALIZED_DIALER_REPORT_MAX_BSON_BYTES: number;
735
737
  export declare const MATERIALIZED_DIALER_ENTRY_MAX_BSON_BYTES: number;
736
738
  /**
737
- * COMMITTED-READINESS ledger for the five reports written by END_TENANT_DAY and
738
- * the range sync: Overview, Discounts, Prep Time, Delivery and Dialer.
739
+ * The Dinein report: covers, checks, seated occupancy, courses, split bills,
740
+ * reservations and guest experience for the dine-in population.
741
+ *
742
+ * ## Population
743
+ *
744
+ * `orders` with `dinein_details != null` for the order's OWN `localDate`. The
745
+ * deleted split PARENT is excluded from sales/order counts (the children are the
746
+ * collectable checks) and is read only for rating/review discovery.
747
+ *
748
+ * ## Two collections, one report
749
+ *
750
+ * `materialized_tenant_dinein_reports` is COUNTERS ONLY, keyed by
751
+ * `<ORDER_STATUS>:<PLATFORM>` (`sale_type_key_filters`); the reservation block is
752
+ * day-level because a reservation has no order and must never be dropped by an
753
+ * order-status/platform filter. `materialized_tenant_dinein_entries` holds ONE
754
+ * PAGE per source day (the discounts/delivery/dialer page model).
755
+ */
756
+ /**
757
+ * The two collections are named/versioned by their model modules
758
+ * (`materialized_tenant_dinein_reports` / `..._entries`), exactly like the other
759
+ * materialized report collections.
760
+ */
761
+ /**
762
+ * Scopes materialized by `materialized_tenant_dinein_reports`.
763
+ *
764
+ * NO `localHour`: the hour dimension is the `by_hour` map inside the document
765
+ * (the sales/covers-by-hour chart), exactly like Delivery. `localDate` is the day
766
+ * the ORDER was sold on (`order.localDate`), never the fingerprint's creation
767
+ * day: a table can close after midnight and still belong to the day it was sold.
768
+ */
769
+ export declare const MATERIALIZED_DINEIN_REPORT_SCOPES: MATERIALIZED_REPORT_SCOPE_ENUM[];
770
+ /**
771
+ * Entry scopes. A PERIOD entry document is PAGE-PARTITIONED by source day
772
+ * (`page = <source localDate>`) so a document is bounded by ONE day's checks; a
773
+ * month is covered by its 28-31 pages. There is no quarter/semester/year entry
774
+ * document, and a broad details selection is served from the month pages it
775
+ * touches (never refused).
776
+ */
777
+ export declare const MATERIALIZED_DINEIN_ENTRY_SCOPES: MATERIALIZED_REPORT_SCOPE_ENUM[];
778
+ /** Page key of the DAY-scoped entry document (the day scope is already one day). */
779
+ export declare const MATERIALIZED_DINEIN_ENTRY_PAGE_ALL = "all";
780
+ /**
781
+ * Ordered key filters of the dinein report (`COMPLETED:POS_DINE-IN`) — the SAME
782
+ * two dimensions every order-shaped materialized counter document declares. They
783
+ * are the only filters the SUMMARY can apply exactly, because they are the key
784
+ * grammar; origin / table / floor / split / offline are ROW filters and are
785
+ * declared `appliesTo: DETAILS` (a summary request that sends one is REJECTED,
786
+ * never silently answered unfiltered).
787
+ */
788
+ export declare const DINEIN_KEY_FILTERS: MATERIALIZED_REPORT_KEY_FILTER_ENUM[];
789
+ /** Token for a missing key/row dimension. A missing dimension is never dropped. */
790
+ export declare const MATERIALIZED_DINEIN_UNKNOWN_TOKEN = "UNKNOWN";
791
+ /** Key-component separator (`<ORDER_STATUS>:<PLATFORM>`). */
792
+ export declare const MATERIALIZED_DINEIN_KEY_SEPARATOR = ":";
793
+ /**
794
+ * Token of the ONE overflow row every BOUNDED map keeps after its top-N
795
+ * (tables / floors / courses / service-charge rates), so a capped map still
796
+ * reconciles against the document totals instead of silently dropping rows.
797
+ */
798
+ export declare const MATERIALIZED_DINEIN_OTHER_TOKEN = "__OTHER__";
799
+ /** Per-document caps of the bounded maps (ranked `key ASC` after the metric). */
800
+ export declare const MATERIALIZED_DINEIN_TABLE_LIMIT = 200;
801
+ export declare const MATERIALIZED_DINEIN_FLOOR_LIMIT = 50;
802
+ export declare const MATERIALIZED_DINEIN_COURSE_LIMIT = 50;
803
+ export declare const MATERIALIZED_DINEIN_SERVICE_CHARGE_RATE_LIMIT = 50;
804
+ /**
805
+ * Caps of the two BOUNDED SUMMARY SNAPSHOTS.
806
+ *
807
+ * `upcoming_reservations` and `top_edited_checks` are display-only tables, not a
808
+ * second pagination source (the Orders tab always uses the cursor read). The DAY
809
+ * document keeps its own top-N; a PERIOD document re-ranks the union of its day
810
+ * documents' snapshots and keeps top-N again, so a month cannot grow the arrays.
811
+ */
812
+ export declare const MATERIALIZED_DINEIN_UPCOMING_RESERVATION_LIMIT = 10;
813
+ export declare const MATERIALIZED_DINEIN_TOP_EDITED_CHECK_LIMIT = 10;
814
+ /** The 24 hour keys of `by_hour`, initialized to 0 so absence is unambiguously zero. */
815
+ export declare const MATERIALIZED_DINEIN_HOUR_KEYS: string[];
816
+ /**
817
+ * `delete_reason` of a split parent that was auto-deleted by the split flow.
818
+ *
819
+ * The parent still carries the rating/review of the visit but is NOT a
820
+ * collectable check: it never contributes sales or an order count.
821
+ */
822
+ export declare const MATERIALIZED_DINEIN_SPLIT_PARENT_DELETE_REASON = "Auto Deleted on Split";
823
+ /**
824
+ * Where the first version of a check was taken. Derived from the OLDEST
825
+ * iteration (or the current order when it has none) — see
826
+ * `classifyMaterializedDineinOrigin`.
827
+ */
828
+ export declare enum MATERIALIZED_DINEIN_ORIGIN_ENUM {
829
+ /** `POS_DINE-IN` — a terminal the guest ordered at. */
830
+ POS_DIRECT = "POS_DIRECT",
831
+ /** `APP_DINE-IN` / `WEB_DINE-IN` WITH a staff or order-taker — a staff tablet. */
832
+ STAFF_TABLET = "STAFF_TABLET",
833
+ /** `APP_DINE-IN` / `WEB_DINE-IN`, no staff, but a consumer phone — QR self-order. */
834
+ QR_SELF_ORDER = "QR_SELF_ORDER",
835
+ /** Anything else. Never invented into one of the three real origins. */
836
+ UNKNOWN_ORIGIN = "UNKNOWN_ORIGIN"
837
+ }
838
+ export declare const ALL_MATERIALIZED_DINEIN_ORIGINS: MATERIALIZED_DINEIN_ORIGIN_ENUM[];
839
+ /** The role a counted check plays in a split bill. The parent never appears as a row. */
840
+ export declare enum MATERIALIZED_DINEIN_SPLIT_ROLE_ENUM {
841
+ NONE = "NONE",
842
+ CHILD = "CHILD"
843
+ }
844
+ export declare const ALL_MATERIALIZED_DINEIN_SPLIT_ROLES: MATERIALIZED_DINEIN_SPLIT_ROLE_ENUM[];
845
+ /** Payment classification used by `card_amount` / `cash_amount` / `other_amount`. */
846
+ export declare enum MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM {
847
+ CASH = "CASH",
848
+ CARD = "CARD",
849
+ OTHER = "OTHER"
850
+ }
851
+ export declare const ALL_MATERIALIZED_DINEIN_PAYMENT_CLASSES: MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM[];
852
+ /**
853
+ * Per-collection BSON budgets (bytes). MongoDB's hard limit is 16 MiB per
854
+ * document; 15 MiB is the owner-approved practical ceiling for the dinein
855
+ * report and each entry page (same as Delivery/Dialer). A writer that exceeds
856
+ * its budget fails at COMPILE/PLAN time with the collection + day + scope in the
857
+ * message — never midway through the close transaction with a server-side
858
+ * "object too large" error.
859
+ */
860
+ export declare const MATERIALIZED_DINEIN_REPORT_MAX_BSON_BYTES: number;
861
+ export declare const MATERIALIZED_DINEIN_ENTRY_MAX_BSON_BYTES: number;
862
+ /**
863
+ * SOURCE-collection indexes the Dinein writer needs, DECLARED ONLY.
864
+ *
865
+ * The services run with `autoIndex: false`; nothing here is applied at runtime.
866
+ * `dinein_tables_fingerprints` is queried by the day's order ids (session join)
867
+ * and by `createdAt` (the table-close event scan); `reservations` is queried by
868
+ * the tenant-local `reservation_time` day. Creating these indexes is an OWNER
869
+ * deploy step (see the operations index plan).
870
+ */
871
+ export declare const MATERIALIZED_DINEIN_FINGERPRINTS_ORDER_INDEX_OPTIONS: {
872
+ key: {
873
+ tenant_id: number;
874
+ order_id: number;
875
+ };
876
+ name: string;
877
+ }[];
878
+ export declare const MATERIALIZED_DINEIN_FINGERPRINTS_CREATED_AT_INDEX_OPTIONS: {
879
+ key: {
880
+ tenant_id: number;
881
+ createdAt: number;
882
+ };
883
+ name: string;
884
+ }[];
885
+ export declare const MATERIALIZED_DINEIN_RESERVATIONS_TIME_INDEX_OPTIONS: {
886
+ key: {
887
+ tenant_id: number;
888
+ reservation_time: number;
889
+ };
890
+ name: string;
891
+ }[];
892
+ /**
893
+ * COMMITTED-READINESS ledger for the SIX reports written by END_TENANT_DAY and
894
+ * the range sync: Overview, Discounts, Prep Time, Delivery, Dialer and Dinein.
739
895
  *
740
896
  * One row per (tenant_id, localDate, report_key, schema_version). The row is
741
897
  * written in the SAME transaction/session as the report documents, so it can
@@ -769,5 +925,5 @@ export declare const ALL_MATERIALIZED_REPORT_READINESS_STATUSES: MATERIALIZED_RE
769
925
  * The ONLY reports that own a readiness row. Adding another report here means
770
926
  * giving it a writer that participates in the same day transaction.
771
927
  */
772
- export declare const MATERIALIZED_READINESS_REPORT_KEYS: readonly ["OVERVIEW", "DISCOUNTS", "PREP_TIME", "DELIVERY", "DIALER"];
928
+ export declare const MATERIALIZED_READINESS_REPORT_KEYS: readonly ["OVERVIEW", "DISCOUNTS", "PREP_TIME", "DELIVERY", "DIALER", "DINEIN"];
773
929
  export type TMaterializedReadinessReportKey = (typeof MATERIALIZED_READINESS_REPORT_KEYS)[number];