@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,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';
@@ -47,6 +49,7 @@ export * from './materialized_tenant_payments_comparison_entries';
47
49
  export * from './materialized_tenant_payments_comparison_reports';
48
50
  export * from './materialized_tenant_prep_time_entries';
49
51
  export * from './materialized_tenant_prep_time_reports';
52
+ export * from './materialized_tenant_report_runs';
50
53
  export * from './materialized_tenant_reports_iterations';
51
54
  export * from './materialized_tenant_sales_comparison_entries';
52
55
  export * from './materialized_tenant_sales_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
+ ];
@@ -0,0 +1,105 @@
1
+ import { Schema } from 'mongoose';
2
+ import { ALL_MATERIALIZED_TENANT_REPORT_RUN_STATUSES, MATERIALIZED_READINESS_REPORT_KEYS, MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM, MATERIALIZED_TENANT_REPORT_RUNS_SCHEMA_VERSION, } from '../../constants/MATERIALIZED_REPORTS';
3
+ /**
4
+ * Mongoose schema for `materialized_tenant_report_runs` — the OPERATIONAL
5
+ * per-report execution ledger (PENDING / RUNNING / RETRYING / COMMITTED /
6
+ * FAILED).
7
+ *
8
+ * It is deliberately SEPARATE from `materialized_tenant_day_readiness`:
9
+ * readiness is the historical read gate and only ever carries COMMITTED /
10
+ * NEEDS_RECONCILIATION. A failed report must never look materialized, so its
11
+ * failure is recorded here, together with `attempts` and the redacted
12
+ * `last_error`.
13
+ *
14
+ * The row is written OUTSIDE the report transaction (a rollback must not erase
15
+ * the attempt), keyed by the unique compound
16
+ * `(tenant_id, localDate, report_key, schema_version)`.
17
+ */
18
+ export const MaterializedTenantReportRunSchema = new Schema({
19
+ tenant_id: { type: String, required: true },
20
+ localDate: { type: String, required: true },
21
+ report_key: {
22
+ type: String,
23
+ required: true,
24
+ enum: [...MATERIALIZED_READINESS_REPORT_KEYS],
25
+ },
26
+ schema_version: {
27
+ type: Number,
28
+ required: true,
29
+ immutable: true,
30
+ default: MATERIALIZED_TENANT_REPORT_RUNS_SCHEMA_VERSION,
31
+ },
32
+ status: {
33
+ type: String,
34
+ required: true,
35
+ enum: ALL_MATERIALIZED_TENANT_REPORT_RUN_STATUSES,
36
+ default: MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.PENDING,
37
+ },
38
+ attempts: {
39
+ type: Number,
40
+ required: true,
41
+ default: 0,
42
+ // Stored counters remain integers.
43
+ validate: {
44
+ validator: (value) => Number.isInteger(value) && value >= 0,
45
+ message: 'attempts must be a non-negative integer',
46
+ },
47
+ },
48
+ total_attempts: {
49
+ type: Number,
50
+ required: true,
51
+ default: 0,
52
+ // Stored counters remain integers.
53
+ validate: {
54
+ validator: (value) => Number.isInteger(value) && value >= 0,
55
+ message: 'total_attempts must be a non-negative integer',
56
+ },
57
+ },
58
+ last_error: { type: String, required: false, default: null },
59
+ run_id: { type: String, required: false, default: null },
60
+ started_at: { type: Schema.Types.Date, required: false, default: null },
61
+ finished_at: { type: Schema.Types.Date, required: false, default: null },
62
+ createdAt: {
63
+ type: Schema.Types.Date,
64
+ required: true,
65
+ default: () => new Date(),
66
+ immutable: true,
67
+ },
68
+ updatedAt: {
69
+ type: Schema.Types.Date,
70
+ required: true,
71
+ default: () => new Date(),
72
+ },
73
+ }, { timestamps: true, versionKey: false, minimize: false });
74
+ export const MATERIALIZED_TENANT_REPORT_RUNS_COLLECTION_INDEXES = {
75
+ _id: { _id: 1 },
76
+ tenant_id__localDate__report_key__schema_version: {
77
+ tenant_id: 1,
78
+ localDate: 1,
79
+ report_key: 1,
80
+ schema_version: 1,
81
+ },
82
+ };
83
+ /**
84
+ * Index OPTIONS the owner must CREATE (declared here, applied by a separate
85
+ * migration/deploy step — never by a read, a close or a retry at runtime).
86
+ */
87
+ export const MATERIALIZED_TENANT_REPORT_RUNS_INDEX_OPTIONS = [
88
+ {
89
+ key: { tenant_id: 1, localDate: 1, report_key: 1, schema_version: 1 },
90
+ name: 'tenant_id__localDate__report_key__schema_version',
91
+ unique: true,
92
+ },
93
+ // Admin materialization page: one tenant-day's reports, newest first.
94
+ {
95
+ key: { tenant_id: 1, localDate: 1, report_key: 1 },
96
+ name: 'tenant_id__localDate__report_key',
97
+ unique: false,
98
+ },
99
+ // Admin materialization page: the failed/running filters across tenants.
100
+ {
101
+ key: { status: 1, updatedAt: -1 },
102
+ name: 'status__updatedAt',
103
+ unique: false,
104
+ },
105
+ ];
@@ -1,5 +1,5 @@
1
1
  import { Schema } from 'mongoose';
2
- import { ALL_TENANT_ACTIVE_JOBS_NAMES } from '../../constants';
2
+ import { ALL_TENANT_ACTIVE_JOBS_NAMES, ALL_TENANT_DAY_JOB_OUTCOMES } from '../../constants';
3
3
  export const TenantActiveJobSchema = new Schema({
4
4
  _id: {
5
5
  type: Schema.Types.ObjectId,
@@ -44,6 +44,24 @@ export const TenantActiveJobSchema = new Schema({
44
44
  type: Number,
45
45
  default: 0,
46
46
  },
47
+ // Per-report progress of the current run (Phase 1 per-report model).
48
+ current_report_key: {
49
+ type: String,
50
+ required: false,
51
+ default: null,
52
+ },
53
+ current_report_progress: {
54
+ type: Number,
55
+ required: false,
56
+ default: 0,
57
+ },
58
+ // Terminal outcome of the last run: COMPLETED | PARTIAL | FAILED.
59
+ outcome: {
60
+ type: String,
61
+ enum: [...ALL_TENANT_DAY_JOB_OUTCOMES, null],
62
+ required: false,
63
+ default: null,
64
+ },
47
65
  started_at: {
48
66
  type: Schema.Types.Date,
49
67
  default: null,
@@ -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';
@@ -51,6 +53,7 @@ export * from './materialized_tenant_payments_comparison_entries';
51
53
  export * from './materialized_tenant_payments_comparison_reports';
52
54
  export * from './materialized_tenant_prep_time_entries';
53
55
  export * from './materialized_tenant_prep_time_reports';
56
+ export * from './materialized_tenant_report_runs';
54
57
  export * from './materialized_tenant_reports_iterations';
55
58
  export * from './materialized_tenant_sales_comparison_entries';
56
59
  export * from './materialized_tenant_sales_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
+ ];