@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,458 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
+ const materialized_dinein_close_day_helpers_1 = require("../../helpers/materialized-reports/materialized-dinein-close-day-helpers");
5
+ const materialized_integer_contract_1 = require("../../helpers/materialized-reports/materialized-integer-contract");
6
+ const tenant_dinein_formatter_1 = require("../../helpers/materialized-reports/tenant-dinein-formatter");
7
+ const dinein_fixtures_1 = require("./dinein-fixtures");
8
+ /**
9
+ * Dinein formatter — every rate/average is DERIVED, never stored.
10
+ *
11
+ * The formatter consumes integer-only documents and produces the page payload:
12
+ * summary + 24 hour rows + origin/table/floor/course/payment breakdowns +
13
+ * reservations. It is pure and never throws.
14
+ */
15
+ const documentsFor = (orders, extras = {}) => {
16
+ const compiledOrders = orders.map((overrides) => (0, dinein_fixtures_1.dineinOrder)(overrides));
17
+ const compile = (0, dinein_fixtures_1.compileDay)({
18
+ orders: compiledOrders,
19
+ fingerprints: compiledOrders.map((order) => (0, dinein_fixtures_1.dineinFingerprint)(String(order._id), {
20
+ num_of_guests: extras.guests?.[String(order._id)] ?? 4,
21
+ ...(extras.fingerprints?.[String(order._id)] ?? {}),
22
+ })),
23
+ reservations: (extras.reservations ?? []).map((overrides) => (0, dinein_fixtures_1.dineinReservation)(overrides)),
24
+ });
25
+ if (compile == null)
26
+ return [];
27
+ const plan = (0, materialized_dinein_close_day_helpers_1.buildTenantDineinCloseDayPlan)({
28
+ tenantId: dinein_fixtures_1.DINEIN_TENANT,
29
+ localDate: dinein_fixtures_1.DINEIN_DAY,
30
+ reports: compile.reports,
31
+ entries: compile.entries,
32
+ reason: 'NEW_DAY',
33
+ });
34
+ // The formatter consumes ONE scope level; the day documents are the level the
35
+ // page's current selection resolves to here.
36
+ return plan.writes.filter((write) => String(write.report.scope_context) === 'localDate').map((write) => write.report);
37
+ };
38
+ describe('dinein formatter', () => {
39
+ it('derives the headline rates from the stored integers', () => {
40
+ const documents = documentsFor([
41
+ {
42
+ _id: 'check-1',
43
+ amount_earned: 21400,
44
+ service_fee_amount: 1000,
45
+ is_service_fee_percentage: true,
46
+ is_service_fee_taxable: true,
47
+ rating: 5,
48
+ categories: [{ items: [{ _id: 'line-a', quantity: 3 }] }],
49
+ },
50
+ ], { guests: { 'check-1': 4 } });
51
+ const { summary } = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(documents);
52
+ expect(summary.orders).toBe(1);
53
+ expect(summary.sales).toBe(21400);
54
+ expect(summary.covers).toBe(4);
55
+ expect(summary.items).toBe(3);
56
+ // money averages stay in integer cents
57
+ expect(summary.average_ticket).toBe(21400);
58
+ expect(summary.average_covers_per_check).toBe(4);
59
+ expect(summary.service_charge_share).toBe(4.67);
60
+ expect(summary.service_charge_orders).toBe(1);
61
+ expect(summary.service_charge_percentage_orders).toBe(1);
62
+ expect(summary.service_charge_fixed_orders).toBe(0);
63
+ expect(summary.average_rating).toBe(5);
64
+ expect(summary.rated_orders).toBe(1);
65
+ expect(summary.cancelled_rate).toBe(0);
66
+ expect(summary.edit_rate).toBe(0);
67
+ expect(summary.average_edits_per_check).toBe(0);
68
+ });
69
+ it('derives the average seated occupancy from the measured sessions', () => {
70
+ const documents = documentsFor([
71
+ {
72
+ _id: 'seated',
73
+ createdAt: '2026-10-01T13:00:00.000Z',
74
+ dinein_details: {
75
+ table_id: 'table-t04',
76
+ table_name: 'T-04',
77
+ floor_id: 'floor-main',
78
+ start_time: '2026-10-01T13:00:00.000Z',
79
+ courses: null,
80
+ },
81
+ },
82
+ ], {
83
+ fingerprints: {
84
+ seated: { createdAt: '2026-10-01T13:40:00.000Z', num_of_guests: 2 },
85
+ },
86
+ });
87
+ const { summary } = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(documents);
88
+ expect(summary.occupancy_minutes_sum).toBe(40);
89
+ expect(summary.occupancy_sessions).toBe(1);
90
+ expect(summary.fallback_occupancy_sessions).toBe(0);
91
+ expect(summary.average_occupancy_minutes).toBe(40);
92
+ expect(summary.table_sessions).toBe(1);
93
+ expect(summary.tables_served).toBe(1);
94
+ expect(summary.tables_served_distinct).toBe(1);
95
+ expect(summary.tables_truncated).toBe(false);
96
+ });
97
+ it('exposes split-bill counters and their share of sales', () => {
98
+ const parent = (0, dinein_fixtures_1.dineinOrder)({
99
+ _id: 'parent',
100
+ is_deleted: true,
101
+ delete_reason: 'Auto Deleted on Split',
102
+ split_child_ids: ['child-1', 'child-2'],
103
+ split_type: 'EQUAL',
104
+ });
105
+ const compiled = (0, dinein_fixtures_1.compileDay)({
106
+ orders: [
107
+ (0, dinein_fixtures_1.dineinOrder)({ _id: 'child-1', split_parent_id: String(parent._id), amount_earned: 5000 }),
108
+ (0, dinein_fixtures_1.dineinOrder)({ _id: 'child-2', split_parent_id: String(parent._id), amount_earned: 5000 }),
109
+ ],
110
+ deletedSplitParents: [parent],
111
+ });
112
+ const plan = (0, materialized_dinein_close_day_helpers_1.buildTenantDineinCloseDayPlan)({
113
+ tenantId: dinein_fixtures_1.DINEIN_TENANT,
114
+ localDate: dinein_fixtures_1.DINEIN_DAY,
115
+ reports: compiled.reports,
116
+ entries: compiled.entries,
117
+ reason: 'NEW_DAY',
118
+ });
119
+ const { summary } = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(plan.writes.filter((write) => String(write.report.scope_context) === 'localDate').map((write) => write.report));
120
+ expect(summary.split_parents).toBe(1);
121
+ expect(summary.split_children).toBe(2);
122
+ expect(summary.split_checks).toBe(3);
123
+ expect(summary.split_sales).toBe(10000);
124
+ expect(summary.split_sales_share).toBe(100);
125
+ expect(summary.split_type_counts).toEqual({ EQUAL: 2 });
126
+ });
127
+ it('derives payment, offline and edit shares', () => {
128
+ const documents = documentsFor([
129
+ {
130
+ _id: 'mixed',
131
+ amount_earned: 10000,
132
+ synced_offline: true,
133
+ payments: [
134
+ { payment_type: 'CASH', amount_paid: 6000, amount_refunded: 0 },
135
+ { payment_type: 'CARD', amount_paid: 4000, amount_refunded: 0 },
136
+ ],
137
+ iterations: [
138
+ { categories: [{ items: [{ _id: 'line-a', quantity: 2 }] }] },
139
+ { categories: [{ items: [{ _id: 'line-a', quantity: 1 }] }] },
140
+ ],
141
+ categories: [{ items: [{ _id: 'line-a', quantity: 2 }] }],
142
+ },
143
+ ], { guests: { mixed: 2 } });
144
+ const { summary } = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(documents);
145
+ expect(summary.cash_amount).toBe(6000);
146
+ expect(summary.card_amount).toBe(4000);
147
+ expect(summary.cash_share).toBe(60);
148
+ expect(summary.card_share).toBe(40);
149
+ expect(summary.other_share).toBe(0);
150
+ expect(summary.cash_orders).toBe(1);
151
+ expect(summary.card_orders).toBe(1);
152
+ expect(summary.split_tender_orders).toBe(1);
153
+ expect(summary.offline_orders).toBe(1);
154
+ expect(summary.offline_sales_share).toBe(100);
155
+ expect(summary.edited_orders).toBe(1);
156
+ expect(summary.total_edits).toBe(2);
157
+ expect(summary.item_additions).toBe(1);
158
+ expect(summary.item_removals).toBe(0);
159
+ expect(summary.net_item_change).toBe(1);
160
+ expect(summary.edit_rate).toBe(100);
161
+ expect(summary.average_edits_per_check).toBe(2);
162
+ });
163
+ it('returns 24 hour rows with sales and covers, zero where nothing happened', () => {
164
+ const documents = documentsFor([{ _id: 'hour-check', localHour: 20, amount_earned: 9000 }], {
165
+ guests: { 'hour-check': 3 },
166
+ });
167
+ const formatted = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(documents);
168
+ expect(formatted.by_hour).toHaveLength(24);
169
+ expect(formatted.by_hour.map((row) => row.label)).toEqual(MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_HOUR_KEYS);
170
+ const hour20 = formatted.by_hour.find((row) => row.hour === 20);
171
+ expect(hour20).toMatchObject({ orders: 1, sales: 9000, covers: 3 });
172
+ expect(formatted.by_hour.find((row) => row.hour === 3)).toMatchObject({ orders: 0, sales: 0, covers: 0 });
173
+ });
174
+ it('exposes the origin / table / floor / course / payment breakdowns', () => {
175
+ const documents = documentsFor([
176
+ {
177
+ _id: 'breakdown',
178
+ amount_earned: 7000,
179
+ dinein_details: {
180
+ table_id: 'table-t11',
181
+ table_name: 'T-11',
182
+ floor_id: 'floor-terrace',
183
+ start_time: '2026-10-01T13:00:00.000Z',
184
+ courses: [
185
+ { course_id: 'c-starters', name: 'Starters', item_ids: ['i1'], called: true },
186
+ { course_id: 'c-mains', name: 'Mains', item_ids: ['i2', 'i3'], called: false },
187
+ ],
188
+ },
189
+ payments: [{ payment_type: 'CARD', amount_paid: 7000, amount_refunded: 0 }],
190
+ },
191
+ ], {
192
+ fingerprints: {
193
+ breakdown: {
194
+ table_id: 'table-t11',
195
+ name: 'T-11',
196
+ dinein_floor_id: 'floor-terrace',
197
+ createdAt: '2026-10-01T13:30:00.000Z',
198
+ },
199
+ },
200
+ });
201
+ const formatted = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(documents);
202
+ expect(formatted.by_origin).toEqual([{ origin: 'POS_DIRECT', orders: 1, sales: 7000, covers: 4 }]);
203
+ expect(formatted.by_table[0]).toMatchObject({
204
+ table_id: 'table-t11',
205
+ table_name: 'T-11',
206
+ floor_id: 'floor-terrace',
207
+ orders: 1,
208
+ sessions: 1,
209
+ covers: 4,
210
+ sales: 7000,
211
+ average_occupancy_minutes: 30,
212
+ });
213
+ expect(formatted.by_floor[0]).toMatchObject({ floor_id: 'floor-terrace', orders: 1, sessions: 1 });
214
+ // deterministic: `assigned DESC, key ASC`
215
+ expect(formatted.by_course.map((row) => row.course_id)).toEqual(['c-mains', 'c-starters']);
216
+ expect(formatted.by_course[0]).toMatchObject({ called: 0, on_hold: 1, items: 2, call_rate: 0 });
217
+ expect(formatted.by_course[1]).toMatchObject({ called: 1, items: 1, call_rate: 100 });
218
+ expect(formatted.by_payment_type).toEqual([{ payment_type: 'CARD', amount: 7000, orders: 1 }]);
219
+ expect(formatted.summary.course_call_rate).toBe(50);
220
+ });
221
+ it('derives reservation averages and exposes no conversion rate', () => {
222
+ const documents = documentsFor([], {
223
+ reservations: [
224
+ (0, dinein_fixtures_1.dineinReservation)({ _id: 'r1', num_of_guests: 4, reservation_duration: 90 }),
225
+ (0, dinein_fixtures_1.dineinReservation)({
226
+ _id: 'r2',
227
+ num_of_guests: 2,
228
+ reservation_duration: 60,
229
+ reservation_time: '2026-10-01T19:30:00.000Z',
230
+ createdAt: '2026-10-01T18:30:00.000Z',
231
+ }),
232
+ ],
233
+ });
234
+ const formatted = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(documents);
235
+ expect(formatted.reservations.reservations).toBe(2);
236
+ expect(formatted.reservations.reservation_covers).toBe(6);
237
+ expect(formatted.reservations.average_party_size).toBe(3);
238
+ expect(formatted.reservations.average_lead_minutes).toBe(330);
239
+ expect(formatted.reservations.average_duration_minutes).toBe(75);
240
+ expect(formatted.reservations).not.toHaveProperty('seated_rate');
241
+ expect(formatted.reservations).not.toHaveProperty('no_show_rate');
242
+ expect(formatted.reservations).not.toHaveProperty('conversion_rate');
243
+ });
244
+ it('applies the key filters to the counters but never to reservations', () => {
245
+ const documents = documentsFor([
246
+ { _id: 'pos', platform: 'POS_DINE-IN', amount_earned: 5000 },
247
+ { _id: 'web', platform: 'WEB_DINE-IN', amount_earned: 7000, consumer: { phone: '0300-1' } },
248
+ ], { reservations: [(0, dinein_fixtures_1.dineinReservation)()] });
249
+ const unfiltered = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(documents);
250
+ expect(unfiltered.summary.orders).toBe(2);
251
+ expect(unfiltered.summary.sales).toBe(12000);
252
+ expect(unfiltered.reservations.reservations).toBe(1);
253
+ const filtered = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(documents, { filters: { platforms: ['POS_DINE-IN'] } });
254
+ expect(filtered.summary.orders).toBe(1);
255
+ expect(filtered.summary.sales).toBe(5000);
256
+ // the reservation block has no order key, so a key filter cannot drop it
257
+ expect(filtered.reservations.reservations).toBe(1);
258
+ expect(filtered.by_origin.map((row) => row.origin)).toEqual(['POS_DIRECT']);
259
+ });
260
+ it('flags a truncated table map instead of reporting a false distinct count', () => {
261
+ const documents = documentsFor([{ amount_earned: 1000 }]);
262
+ const report = documents[0];
263
+ const key = Object.keys(report.dinein_counters)[0];
264
+ report.dinein_counters[key].by_table.__OTHER__ = {
265
+ table_id: '__OTHER__',
266
+ table_name: 'Other tables',
267
+ floor_id: 'UNKNOWN',
268
+ orders: 10,
269
+ sessions: 10,
270
+ covers: 10,
271
+ occupancy_minutes: 100,
272
+ sales: 1000,
273
+ };
274
+ const { summary } = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)([report]);
275
+ expect(summary.tables_truncated).toBe(true);
276
+ // a LOWER BOUND: the overflow row is not a table
277
+ expect(summary.tables_served_distinct).toBe(1);
278
+ });
279
+ it('never throws for an empty or missing document set', () => {
280
+ const empty = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(null);
281
+ expect(empty.summary.orders).toBe(0);
282
+ expect(empty.summary.average_ticket).toBeNull();
283
+ expect(empty.summary.average_rating).toBeNull();
284
+ expect(empty.summary.average_occupancy_minutes).toBeNull();
285
+ expect(empty.summary.tables_served_distinct).toBe(0);
286
+ expect(empty.by_hour).toHaveLength(24);
287
+ expect(empty.reservations.reservations).toBe(0);
288
+ expect(empty.reservations.average_party_size).toBeNull();
289
+ expect(empty.by_origin).toEqual([]);
290
+ expect(empty.scopes).toEqual([]);
291
+ expect((0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(undefined).summary.orders).toBe(0);
292
+ });
293
+ it('reports tenant ids and the scope level the documents covered', () => {
294
+ const documents = documentsFor([{}]);
295
+ const formatted = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(documents);
296
+ expect(formatted.tenant_ids).toEqual([dinein_fixtures_1.DINEIN_TENANT]);
297
+ // the day documents are formatted at their own scope level
298
+ expect(formatted.scopes).toEqual([{ scope_context: 'localDate', scope_field: dinein_fixtures_1.DINEIN_DAY }]);
299
+ });
300
+ /**
301
+ * The formatter's documented unit is 0–100 PERCENT for every derived rate /
302
+ * share. The admin adapter converts ONCE to a 0–1 ratio at its boundary; a
303
+ * test that pins the unit here prevents the two sides drifting again.
304
+ */
305
+ it('publishes derived rates in the documented 0–100 percent unit', () => {
306
+ const documents = documentsFor([
307
+ {
308
+ _id: 'percent-check',
309
+ amount_earned: 21400,
310
+ service_fee_amount: 1000,
311
+ is_service_fee_percentage: true,
312
+ is_service_fee_taxable: true,
313
+ categories: [{ items: [{ _id: 'line-a', quantity: 2 }] }],
314
+ iterations: [{ categories: [{ items: [{ _id: 'line-a', quantity: 1 }] }], platform: 'POS_DINE-IN' }],
315
+ dinein_details: {
316
+ table_id: 'table-t04',
317
+ table_name: 'T-04',
318
+ floor_id: 'floor-main',
319
+ start_time: '2026-10-01T13:00:00.000Z',
320
+ courses: [{ course_id: 'c-mains', name: 'Mains', item_ids: ['line-a'], called: true }],
321
+ },
322
+ payments: [
323
+ { payment_type: 'CARD', amount_paid: 20000, amount_refunded: 500 },
324
+ { payment_type: 'CASH', amount_paid: 1400, amount_refunded: 0 },
325
+ ],
326
+ },
327
+ ], { guests: { 'percent-check': 4 } });
328
+ const { summary, by_course } = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(documents);
329
+ // 1000 / 21400 = 4.67% — a 0–100 percent value, not a ratio.
330
+ expect(summary.service_charge_share).toBe(4.67);
331
+ // 1 edited check out of 1 = 100%.
332
+ expect(summary.edit_rate).toBe(100);
333
+ // 1 called course out of 1 = 100%.
334
+ expect(summary.course_call_rate).toBe(100);
335
+ // 19500 net card of 20900 collected = 93.3%.
336
+ expect(summary.card_share).toBe(93.3);
337
+ // A 0–100 percent value is > 1 whenever the share is above 1%.
338
+ expect(summary.service_charge_share).toBeGreaterThan(1);
339
+ expect(by_course[0].call_rate).toBe(100);
340
+ // No derived rate/share/average leaf lives in the STORED documents.
341
+ const derivedNames = new Set([
342
+ 'average_ticket',
343
+ 'average_covers_per_check',
344
+ 'cancelled_rate',
345
+ 'service_charge_share',
346
+ 'split_sales_share',
347
+ 'offline_sales_share',
348
+ 'edit_rate',
349
+ 'average_edits_per_check',
350
+ 'course_call_rate',
351
+ 'card_share',
352
+ 'cash_share',
353
+ 'other_share',
354
+ 'average_rating',
355
+ 'average_occupancy_minutes',
356
+ 'call_rate',
357
+ 'share',
358
+ 'rate',
359
+ 'average',
360
+ ]);
361
+ const numericPaths = [];
362
+ const walk = (node, path) => {
363
+ if (typeof node === 'number')
364
+ numericPaths.push(path);
365
+ if (Array.isArray(node))
366
+ node.forEach((item, index) => walk(item, `${path}[${index}]`));
367
+ else if (node != null && typeof node === 'object') {
368
+ for (const [key, value] of Object.entries(node)) {
369
+ walk(value, path === '' ? key : `${path}.${key}`);
370
+ }
371
+ }
372
+ };
373
+ for (const document of documents)
374
+ walk(document, '');
375
+ for (const path of numericPaths) {
376
+ const leaf = path.slice(path.lastIndexOf('.') + 1);
377
+ // `service_charge_rate_counts` is a COUNT map keyed by a string rate; its
378
+ // leaves are counts, not rates.
379
+ if (path.includes('service_charge_rate_counts'))
380
+ continue;
381
+ expect(derivedNames.has(leaf)).toBe(false);
382
+ }
383
+ for (const document of documents) {
384
+ expect((0, materialized_integer_contract_1.findNonIntegerNumericLeaves)(document)).toEqual([]);
385
+ }
386
+ });
387
+ /**
388
+ * The writer materializes a reservations-only day as a document with an EMPTY
389
+ * counter map. Formatting it must still publish the reservation block, the
390
+ * by-hour chart and the bounded snapshot — a zero-check day is not a zero
391
+ * reservation day.
392
+ */
393
+ it('formats a reservations-only document with an empty counter map', () => {
394
+ const compile = (0, dinein_fixtures_1.compileDay)({
395
+ orders: [],
396
+ reservations: [(0, dinein_fixtures_1.dineinReservation)({ num_of_guests: 4 })],
397
+ });
398
+ expect(compile).not.toBeNull();
399
+ const plan = (0, materialized_dinein_close_day_helpers_1.buildTenantDineinCloseDayPlan)({
400
+ tenantId: dinein_fixtures_1.DINEIN_TENANT,
401
+ localDate: dinein_fixtures_1.DINEIN_DAY,
402
+ reports: compile.reports,
403
+ entries: compile.entries,
404
+ reason: 'NEW_DAY',
405
+ });
406
+ const documents = plan.writes
407
+ .filter((write) => String(write.report.scope_context) === 'localDate')
408
+ .map((write) => write.report);
409
+ expect(documents).toHaveLength(1);
410
+ expect(documents[0].dinein_counters).toEqual({});
411
+ expect(documents[0].reservation_counters.reservations).toBe(1);
412
+ const formatted = (0, tenant_dinein_formatter_1.formatMaterializedDineinReport)(documents);
413
+ expect(formatted.summary.orders).toBe(0);
414
+ expect(formatted.summary.sales).toBe(0);
415
+ expect(formatted.summary.tables_served_distinct).toBe(0);
416
+ expect(formatted.reservations.reservations).toBe(1);
417
+ expect(formatted.reservations.reservation_covers).toBe(4);
418
+ expect(formatted.reservations.average_party_size).toBe(4);
419
+ expect(formatted.reservation_by_hour[18]).toEqual({
420
+ hour: 18,
421
+ label: '18',
422
+ reservations: 1,
423
+ reservation_covers: 4,
424
+ });
425
+ expect(formatted.upcoming_reservations).toHaveLength(1);
426
+ // No counter key was invented for a day with no check.
427
+ expect(formatted.by_status).toEqual([]);
428
+ expect(formatted.by_platform).toEqual([]);
429
+ });
430
+ it('keeps every compiled document and entry page integer-only', () => {
431
+ const documents = documentsFor([
432
+ {
433
+ _id: 'integer-check',
434
+ amount_earned: 21400,
435
+ service_fee_amount: 1000,
436
+ is_service_fee_percentage: true,
437
+ categories: [{ items: [{ _id: 'line-a', quantity: 2 }] }],
438
+ payments: [
439
+ { payment_type: 'CARD', amount_paid: 20000, amount_refunded: 500 },
440
+ { payment_type: 'CASH', amount_paid: 1400, amount_refunded: 0 },
441
+ ],
442
+ },
443
+ ], { guests: { 'integer-check': 4 }, reservations: [{ num_of_guests: 3 }] });
444
+ expect(documents.length).toBeGreaterThan(0);
445
+ for (const document of documents) {
446
+ expect((0, materialized_integer_contract_1.findNonIntegerNumericLeaves)(document)).toEqual([]);
447
+ }
448
+ const compile = (0, dinein_fixtures_1.compileDay)({
449
+ orders: [(0, dinein_fixtures_1.dineinOrder)({ _id: 'page-check' })],
450
+ fingerprints: [(0, dinein_fixtures_1.dineinFingerprint)('page-check', { num_of_guests: 2 })],
451
+ });
452
+ for (const page of compile.entries) {
453
+ expect((0, materialized_integer_contract_1.findNonIntegerNumericLeaves)(page)).toEqual([]);
454
+ }
455
+ // Every entry scope is page-partitioned by the source day and integer-only.
456
+ expect(compile.entries.map((page) => String(page.scope_context)).sort()).toEqual([...MATERIALIZED_REPORTS_1.MATERIALIZED_DINEIN_ENTRY_SCOPES].sort());
457
+ });
458
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.60",
3
+ "version": "0.4.62",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",