@managesome/eposmatic-toolkit 0.4.61 → 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 (38) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +77 -0
  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 +1 -0
  5. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +3 -1
  6. package/esm/helpers/materialized-reports/materialized-report-runs.js +143 -0
  7. package/esm/schemas/models/index.js +1 -0
  8. package/esm/schemas/models/materialized_tenant_report_runs.js +105 -0
  9. package/esm/schemas/models/tenant_active_jobs.js +19 -1
  10. package/esm/schemas/types/index.js +1 -0
  11. package/esm/schemas/types/materialized_tenant_report_runs.js +1 -0
  12. package/lib/constants/MATERIALIZED_REPORTS.d.ts +69 -0
  13. package/lib/constants/MATERIALIZED_REPORTS.js +78 -1
  14. package/lib/constants/TENANT_ENUMS.d.ts +14 -0
  15. package/lib/constants/TENANT_ENUMS.js +16 -1
  16. package/lib/constants/WORKER_ENDPOINTS.d.ts +12 -0
  17. package/lib/constants/WORKER_ENDPOINTS.js +12 -0
  18. package/lib/helpers/materialized-reports/index.d.ts +1 -0
  19. package/lib/helpers/materialized-reports/index.js +1 -0
  20. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +2 -0
  21. package/lib/helpers/materialized-reports/materialized-report-runs.d.ts +105 -0
  22. package/lib/helpers/materialized-reports/materialized-report-runs.js +156 -0
  23. package/lib/helpers/types/workers.d.ts +65 -0
  24. package/lib/schemas/models/index.d.ts +1 -0
  25. package/lib/schemas/models/index.js +1 -0
  26. package/lib/schemas/models/materialized_tenant_report_runs.d.ts +97 -0
  27. package/lib/schemas/models/materialized_tenant_report_runs.js +108 -0
  28. package/lib/schemas/models/tenant_active_jobs.d.ts +12 -3
  29. package/lib/schemas/models/tenant_active_jobs.js +18 -0
  30. package/lib/schemas/types/index.d.ts +1 -0
  31. package/lib/schemas/types/index.js +1 -0
  32. package/lib/schemas/types/materialized_tenant_report_runs.d.ts +55 -0
  33. package/lib/schemas/types/materialized_tenant_report_runs.js +2 -0
  34. package/lib/schemas/types/tenant_active_jobs.d.ts +15 -1
  35. package/lib/tests/materialized-reports/materialized-report-runs.test.d.ts +1 -0
  36. package/lib/tests/materialized-reports/materialized-report-runs.test.js +116 -0
  37. package/lib/tests/materialized-reports/tenant-dinein-formatter.test.js +159 -0
  38. package/package.json +1 -1
@@ -14,12 +14,15 @@ export declare const TenantActiveJobSchema: Schema<any, import("mongoose").Model
14
14
  tenant_id: string;
15
15
  progress: number;
16
16
  completed_at: NativeDate;
17
+ started_at: NativeDate;
17
18
  jobs_status: Map<string, boolean>;
18
19
  active_job: import("../../constants").TENANT_ACTIVE_JOBS_ENUM;
19
20
  failed_jobs: string[];
20
- started_at: NativeDate;
21
21
  last_heartbeat_at: NativeDate;
22
22
  lease_id?: string | null | undefined;
23
+ current_report_key?: string | null | undefined;
24
+ current_report_progress?: number | null | undefined;
25
+ outcome?: import("../../constants").TENANT_DAY_JOB_OUTCOME_ENUM | null | undefined;
23
26
  }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
24
27
  createdAt: NativeDate;
25
28
  updatedAt: NativeDate;
@@ -31,12 +34,15 @@ export declare const TenantActiveJobSchema: Schema<any, import("mongoose").Model
31
34
  tenant_id: string;
32
35
  progress: number;
33
36
  completed_at: NativeDate;
37
+ started_at: NativeDate;
34
38
  jobs_status: Map<string, boolean>;
35
39
  active_job: import("../../constants").TENANT_ACTIVE_JOBS_ENUM;
36
40
  failed_jobs: string[];
37
- started_at: NativeDate;
38
41
  last_heartbeat_at: NativeDate;
39
42
  lease_id?: string | null | undefined;
43
+ current_report_key?: string | null | undefined;
44
+ current_report_progress?: number | null | undefined;
45
+ outcome?: import("../../constants").TENANT_DAY_JOB_OUTCOME_ENUM | null | undefined;
40
46
  }>> & import("mongoose").FlatRecord<{
41
47
  createdAt: NativeDate;
42
48
  updatedAt: NativeDate;
@@ -48,12 +54,15 @@ export declare const TenantActiveJobSchema: Schema<any, import("mongoose").Model
48
54
  tenant_id: string;
49
55
  progress: number;
50
56
  completed_at: NativeDate;
57
+ started_at: NativeDate;
51
58
  jobs_status: Map<string, boolean>;
52
59
  active_job: import("../../constants").TENANT_ACTIVE_JOBS_ENUM;
53
60
  failed_jobs: string[];
54
- started_at: NativeDate;
55
61
  last_heartbeat_at: NativeDate;
56
62
  lease_id?: string | null | undefined;
63
+ current_report_key?: string | null | undefined;
64
+ current_report_progress?: number | null | undefined;
65
+ outcome?: import("../../constants").TENANT_DAY_JOB_OUTCOME_ENUM | null | undefined;
57
66
  }> & Required<{
58
67
  _id: import("mongoose").Types.ObjectId;
59
68
  }> & {
@@ -47,6 +47,24 @@ exports.TenantActiveJobSchema = new mongoose_1.Schema({
47
47
  type: Number,
48
48
  default: 0,
49
49
  },
50
+ // Per-report progress of the current run (Phase 1 per-report model).
51
+ current_report_key: {
52
+ type: String,
53
+ required: false,
54
+ default: null,
55
+ },
56
+ current_report_progress: {
57
+ type: Number,
58
+ required: false,
59
+ default: 0,
60
+ },
61
+ // Terminal outcome of the last run: COMPLETED | PARTIAL | FAILED.
62
+ outcome: {
63
+ type: String,
64
+ enum: [...constants_1.ALL_TENANT_DAY_JOB_OUTCOMES, null],
65
+ required: false,
66
+ default: null,
67
+ },
50
68
  started_at: {
51
69
  type: mongoose_1.Schema.Types.Date,
52
70
  default: null,
@@ -53,6 +53,7 @@ export * from './materialized_tenant_payments_comparison_entries';
53
53
  export * from './materialized_tenant_payments_comparison_reports';
54
54
  export * from './materialized_tenant_prep_time_entries';
55
55
  export * from './materialized_tenant_prep_time_reports';
56
+ export * from './materialized_tenant_report_runs';
56
57
  export * from './materialized_tenant_reports_iterations';
57
58
  export * from './materialized_tenant_sales_comparison_entries';
58
59
  export * from './materialized_tenant_sales_comparison_reports';
@@ -69,6 +69,7 @@ __exportStar(require("./materialized_tenant_payments_comparison_entries"), expor
69
69
  __exportStar(require("./materialized_tenant_payments_comparison_reports"), exports);
70
70
  __exportStar(require("./materialized_tenant_prep_time_entries"), exports);
71
71
  __exportStar(require("./materialized_tenant_prep_time_reports"), exports);
72
+ __exportStar(require("./materialized_tenant_report_runs"), exports);
72
73
  __exportStar(require("./materialized_tenant_reports_iterations"), exports);
73
74
  __exportStar(require("./materialized_tenant_sales_comparison_entries"), exports);
74
75
  __exportStar(require("./materialized_tenant_sales_comparison_reports"), exports);
@@ -0,0 +1,55 @@
1
+ import { MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM, type TMaterializedReadinessReportKey } from '../../constants/MATERIALIZED_REPORTS';
2
+ /**
3
+ * One OPERATIONAL run row per
4
+ * `(tenant_id, localDate, report_key, schema_version)`.
5
+ *
6
+ * This is the durable execution ledger of the per-report materialization model
7
+ * (Phase 1 of END_TENANT_DAY): every report runs in ITS OWN transaction and
8
+ * commits or fails on its own. Readiness (`materialized_tenant_day_readiness`)
9
+ * stays the read gate and keeps only COMMITTED / NEEDS_RECONCILIATION; a failed
10
+ * report is visible HERE and nowhere else.
11
+ *
12
+ * Lifecycle:
13
+ * - `PENDING` row created before the first attempt (optional);
14
+ * - `RUNNING` an attempt is executing (`attempts` = attempts started);
15
+ * - `RETRYING` a TRANSIENT failure happened and another attempt is starting;
16
+ * - `COMMITTED` the report transaction committed (documents + readiness);
17
+ * - `FAILED` every allowed attempt was exhausted (`last_error` is set,
18
+ * readiness was NOT written).
19
+ *
20
+ * The row is operational state: it is written OUTSIDE the report transaction so
21
+ * a rollback cannot erase the failed attempt.
22
+ */
23
+ export interface IMaterializedTenantReportRun {
24
+ tenant_id: string;
25
+ /** Tenant-local business day, `YYYY-MM-DD`. */
26
+ localDate: string;
27
+ /** One of the SIX production report keys; never an out-of-scope report. */
28
+ report_key: TMaterializedReadinessReportKey | string;
29
+ /** Pinned run-ledger contract version (1). */
30
+ schema_version: number;
31
+ status: MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM;
32
+ /**
33
+ * Number of attempts STARTED by the LATEST execution (manual retry resets
34
+ * this to 1). The admin page shows it as "attempts this run".
35
+ */
36
+ attempts: number;
37
+ /**
38
+ * LIFETIME attempts across every manual retry of this report identity.
39
+ * Never reset by a retry, so failure history is never silently lost.
40
+ */
41
+ total_attempts: number;
42
+ /**
43
+ * PII-REDACTED, single-line, bounded explanation of the last failure.
44
+ * `null` while no attempt failed.
45
+ */
46
+ last_error: string | null;
47
+ /** Identity of the run that most recently owned this report (lease/run id). */
48
+ run_id: string | null;
49
+ /** When the latest execution started (first attempt). */
50
+ started_at: Date | null;
51
+ /** When the latest execution reached COMMITTED or FAILED. */
52
+ finished_at: Date | null;
53
+ createdAt: Date;
54
+ updatedAt: Date;
55
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +1,4 @@
1
- import { TENANT_ACTIVE_JOBS_ENUM } from '../../constants';
1
+ import { TENANT_ACTIVE_JOBS_ENUM, TENANT_DAY_JOB_OUTCOME_ENUM } from '../../constants';
2
2
  export interface ITenantActiveJob {
3
3
  _id: string;
4
4
  tenant_id: string;
@@ -15,6 +15,20 @@ export interface ITenantActiveJob {
15
15
  lease_id?: string | null;
16
16
  failed_jobs: TENANT_ACTIVE_JOBS_ENUM[];
17
17
  progress: number;
18
+ /**
19
+ * The report the current close/retry run is executing (PENDING -> COMMITTED
20
+ * in the report run ledger order). `null` when no report is in flight.
21
+ */
22
+ current_report_key?: string | null;
23
+ /** 0–100 progress INSIDE `current_report_key`. */
24
+ current_report_progress?: number;
25
+ /**
26
+ * Terminal outcome of the last run that owned the job: COMPLETED when every
27
+ * report committed, PARTIAL when at least one report FAILED (it stays
28
+ * retryable through the run ledger), FAILED when the run itself failed before
29
+ * any report work could be committed. `null` while a run is active.
30
+ */
31
+ outcome?: TENANT_DAY_JOB_OUTCOME_ENUM | null;
18
32
  started_at: Date | null;
19
33
  completed_at: Date | null;
20
34
  last_heartbeat_at: Date | null;
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
+ const materialized_report_runs_1 = require("../../helpers/materialized-reports/materialized-report-runs");
5
+ const materialized_tenant_report_runs_1 = require("../../schemas/models/materialized_tenant_report_runs");
6
+ const report_contract_1 = require("../../helpers/reporting-contract/report-contract");
7
+ /**
8
+ * Report run ledger contract (Phase 1 of END_TENANT_DAY).
9
+ *
10
+ * The ledger is the OPERATIONAL record of one report execution and is separate
11
+ * from readiness: FAILED never appears in readiness.
12
+ */
13
+ describe('materialized report run ledger contract', () => {
14
+ const identity = {
15
+ tenantId: 'tenant-a',
16
+ localDate: '2026-09-14',
17
+ reportKey: report_contract_1.REPORT_KEY_ENUM.OVERVIEW,
18
+ };
19
+ it('pins the operational collection + schema version', () => {
20
+ expect(MATERIALIZED_REPORTS_1.MATERIALIZED_TENANT_REPORT_RUNS_COLLECTION_NAME).toBe('materialized_tenant_report_runs');
21
+ expect(MATERIALIZED_REPORTS_1.MATERIALIZED_TENANT_REPORT_RUNS_SCHEMA_VERSION).toBe(1);
22
+ expect(MATERIALIZED_REPORTS_1.MATERIALIZED_TENANT_REPORT_RUN_MAX_ATTEMPTS).toBe(2);
23
+ });
24
+ it('has exactly the five operational statuses and no FAILED readiness leak', () => {
25
+ expect(Object.values(MATERIALIZED_REPORTS_1.MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM).sort()).toEqual(['COMMITTED', 'FAILED', 'PENDING', 'RETRYING', 'RUNNING'].sort());
26
+ // Readiness never gains FAILED: it is the read gate, not the ledger.
27
+ expect(Object.values(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_READINESS_STATUS_ENUM)).not.toContain('FAILED');
28
+ });
29
+ it('builds ONE stable identity key/filter per report', () => {
30
+ expect((0, materialized_report_runs_1.materializedReportRunKey)(identity)).toBe('tenant-a|2026-09-14|OVERVIEW|1');
31
+ expect((0, materialized_report_runs_1.buildMaterializedReportRunFilter)(identity)).toEqual({
32
+ tenant_id: 'tenant-a',
33
+ localDate: '2026-09-14',
34
+ report_key: 'OVERVIEW',
35
+ schema_version: 1,
36
+ });
37
+ });
38
+ it('allows retry only for FAILED or a MISSING run', () => {
39
+ expect((0, materialized_report_runs_1.canRetryMaterializedReportRun)(null)).toBe(true);
40
+ expect((0, materialized_report_runs_1.canRetryMaterializedReportRun)(undefined)).toBe(true);
41
+ expect((0, materialized_report_runs_1.canRetryMaterializedReportRun)('FAILED')).toBe(true);
42
+ for (const blocked of ['PENDING', 'RUNNING', 'RETRYING', 'COMMITTED']) {
43
+ expect((0, materialized_report_runs_1.canRetryMaterializedReportRun)(blocked)).toBe(false);
44
+ expect((0, materialized_report_runs_1.isMaterializedReportRunInFlight)(blocked)).toBe(blocked !== 'COMMITTED');
45
+ }
46
+ });
47
+ it('detects terminal states and only ages IN-FLIGHT runs as stale', () => {
48
+ expect((0, materialized_report_runs_1.isMaterializedReportRunTerminal)('COMMITTED')).toBe(true);
49
+ expect((0, materialized_report_runs_1.isMaterializedReportRunTerminal)('FAILED')).toBe(true);
50
+ for (const live of ['PENDING', 'RUNNING', 'RETRYING']) {
51
+ expect((0, materialized_report_runs_1.isMaterializedReportRunTerminal)(live)).toBe(false);
52
+ }
53
+ const now = new Date('2026-09-15T12:00:00.000Z');
54
+ const startedAt = new Date(now.getTime() - MATERIALIZED_REPORTS_1.MATERIALIZED_TENANT_REPORT_RUN_STALE_MS);
55
+ expect((0, materialized_report_runs_1.isMaterializedReportRunStale)({ run: { status: 'RUNNING', started_at: startedAt }, now })).toBe(true);
56
+ expect((0, materialized_report_runs_1.isMaterializedReportRunStale)({
57
+ run: { status: 'RUNNING', started_at: new Date(now.getTime() - 1000) },
58
+ now,
59
+ })).toBe(false);
60
+ // Terminal rows are never "stale": they are final.
61
+ expect((0, materialized_report_runs_1.isMaterializedReportRunStale)({ run: { status: 'COMMITTED', started_at: startedAt }, now })).toBe(false);
62
+ // A legacy row without started_at falls back to updatedAt.
63
+ expect((0, materialized_report_runs_1.isMaterializedReportRunStale)({
64
+ run: { status: 'RETRYING', updatedAt: startedAt },
65
+ now,
66
+ })).toBe(true);
67
+ });
68
+ it('proves readiness ownership by RUN id, never by status alone', () => {
69
+ // Same run -> the readiness row is proof.
70
+ expect((0, materialized_report_runs_1.isMaterializedReadinessOwnedByRun)({
71
+ readiness: { run_id: 'lease-B' },
72
+ run: { run_id: 'lease-B' },
73
+ })).toBe(true);
74
+ // An OLDER close's row proves nothing about the current run.
75
+ expect((0, materialized_report_runs_1.isMaterializedReadinessOwnedByRun)({
76
+ readiness: { run_id: 'lease-A' },
77
+ run: { run_id: 'lease-B' },
78
+ })).toBe(false);
79
+ expect((0, materialized_report_runs_1.isMaterializedReadinessOwnedByRun)({
80
+ readiness: null,
81
+ run: { run_id: 'lease-B' },
82
+ })).toBe(false);
83
+ // A run without an identity can never be proven by readiness.
84
+ expect((0, materialized_report_runs_1.isMaterializedReadinessOwnedByRun)({
85
+ readiness: { run_id: 'lease-B' },
86
+ run: { run_id: null },
87
+ })).toBe(false);
88
+ });
89
+ it('detects the UNKNOWN-COMMIT marker', () => {
90
+ expect((0, materialized_report_runs_1.isMaterializedReportRunCommitOutcomeUnknown)({
91
+ last_error: 'COMMIT_OUTCOME_UNKNOWN',
92
+ })).toBe(true);
93
+ expect((0, materialized_report_runs_1.isMaterializedReportRunCommitOutcomeUnknown)({
94
+ last_error: 'INTERRUPTED_RUN',
95
+ })).toBe(false);
96
+ expect((0, materialized_report_runs_1.isMaterializedReportRunCommitOutcomeUnknown)(null)).toBe(false);
97
+ });
98
+ it('declares (never applies) the unique per-report identity index', () => {
99
+ const unique = materialized_tenant_report_runs_1.MATERIALIZED_TENANT_REPORT_RUNS_INDEX_OPTIONS.filter((index) => index.unique === true);
100
+ expect(unique).toHaveLength(1);
101
+ expect(unique[0].key).toEqual({
102
+ tenant_id: 1,
103
+ localDate: 1,
104
+ report_key: 1,
105
+ schema_version: 1,
106
+ });
107
+ });
108
+ it('sanitizes durable errors: no PII, single line, bounded', () => {
109
+ const sanitized = (0, materialized_report_runs_1.sanitizeMaterializedReportRunError)(new Error('dialer failed for jane.doe@example.com phone +92 300 1234567\nsecond line ' + 'x'.repeat(2000)));
110
+ expect(sanitized).not.toContain('jane.doe@example.com');
111
+ expect(sanitized).not.toContain('1234567');
112
+ expect(sanitized).toContain('[redacted-email]');
113
+ expect(sanitized).not.toContain('\n');
114
+ expect(sanitized.length).toBeLessThanOrEqual(500);
115
+ });
116
+ });
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
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");
5
6
  const tenant_dinein_formatter_1 = require("../../helpers/materialized-reports/tenant-dinein-formatter");
6
7
  const dinein_fixtures_1 = require("./dinein-fixtures");
7
8
  /**
@@ -296,4 +297,162 @@ describe('dinein formatter', () => {
296
297
  // the day documents are formatted at their own scope level
297
298
  expect(formatted.scopes).toEqual([{ scope_context: 'localDate', scope_field: dinein_fixtures_1.DINEIN_DAY }]);
298
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
+ });
299
458
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.4.61",
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",