@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
@@ -1112,3 +1112,80 @@ export const MATERIALIZED_READINESS_REPORT_KEYS = [
1112
1112
  'DIALER',
1113
1113
  'DINEIN',
1114
1114
  ];
1115
+ /* ======================================================================== */
1116
+ /* Report run ledger (ONE row per report execution) */
1117
+ /* ======================================================================== */
1118
+ /**
1119
+ * DURABLE OPERATIONAL LEDGER of one report's execution, SEPARATE from
1120
+ * readiness.
1121
+ *
1122
+ * Readiness answers "may a historical read treat this tenant-day-report as
1123
+ * final?" (only COMMITTED / NEEDS_RECONCILIATION ever appear there).
1124
+ * This ledger answers "what happened to the execution?" and is the ONLY place
1125
+ * a failed attempt is recorded (PENDING / RUNNING / RETRYING / COMMITTED /
1126
+ * FAILED), so a FAILED report never - and can never - look materialized.
1127
+ *
1128
+ * One row per `(tenant_id, localDate, report_key, schema_version)`. The row is
1129
+ * operational state: it is written OUTSIDE the report transaction (a rollback
1130
+ * must not erase the fact that the attempt happened). Readiness is written
1131
+ * INSIDE the report transaction. The two therefore have different lifecycles
1132
+ * and are deliberately different collections.
1133
+ *
1134
+ * Phase 1 replaces the one all-report END_TENANT_DAY transaction with one
1135
+ * transaction per report: each report commits (or fails) on its own, and this
1136
+ * ledger is the operator-visible record of that per-report outcome.
1137
+ */
1138
+ export const MATERIALIZED_TENANT_REPORT_RUNS_COLLECTION_NAME = 'materialized_tenant_report_runs';
1139
+ /** Version of the report-run row shape. Pinned to 1 with the other collections. */
1140
+ export const MATERIALIZED_TENANT_REPORT_RUNS_SCHEMA_VERSION = 1;
1141
+ /** The execution state of one tenant-day-report run. */
1142
+ export var MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM;
1143
+ (function (MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM) {
1144
+ /** The row exists but no attempt has started yet. */
1145
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM["PENDING"] = "PENDING";
1146
+ /** An attempt is executing (its own transaction is open or about to open). */
1147
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM["RUNNING"] = "RUNNING";
1148
+ /** A transient failure happened; another attempt is starting. */
1149
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM["RETRYING"] = "RETRYING";
1150
+ /** The report transaction committed: documents + readiness are durable. */
1151
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM["COMMITTED"] = "COMMITTED";
1152
+ /** Every allowed attempt was exhausted; readiness was NOT written. */
1153
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM["FAILED"] = "FAILED";
1154
+ })(MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM || (MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM = {}));
1155
+ export const ALL_MATERIALIZED_TENANT_REPORT_RUN_STATUSES = Object.values(MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM);
1156
+ /** States in which an execution currently owns the report (retry is refused). */
1157
+ export const MATERIALIZED_TENANT_REPORT_RUN_IN_FLIGHT_STATUSES = [
1158
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.PENDING,
1159
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.RUNNING,
1160
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.RETRYING,
1161
+ ];
1162
+ /**
1163
+ * Bounded attempt budget of ONE report execution (transient errors only).
1164
+ * A deterministic code/compile error is NEVER retried inside the job.
1165
+ */
1166
+ export const MATERIALIZED_TENANT_REPORT_RUN_MAX_ATTEMPTS = 2;
1167
+ /** Bounded size of the stored `last_error` (single-line, PII-redacted). */
1168
+ export const MATERIALIZED_TENANT_REPORT_RUN_ERROR_MAX_LENGTH = 500;
1169
+ /**
1170
+ * Stale window of a report-run row (RUNNING / RETRYING).
1171
+ *
1172
+ * A run that has not reached a terminal state within this window MAY have been
1173
+ * interrupted (process crash). Recovery NEVER trusts the age alone: it also
1174
+ * requires the tenant's active-job heartbeat to be stale, so a live long-running
1175
+ * report is never taken over. The value mirrors the active-job lease window
1176
+ * (30 minutes) so both fences agree.
1177
+ */
1178
+ export const MATERIALIZED_TENANT_REPORT_RUN_STALE_MS = 30 * 60 * 1000;
1179
+ /**
1180
+ * Durable `last_error` of a run recovered after an interruption with NO
1181
+ * readiness row (the report transaction provably did not commit).
1182
+ */
1183
+ export const MATERIALIZED_REPORT_RUN_INTERRUPTED_ERROR = 'INTERRUPTED_RUN';
1184
+ /**
1185
+ * Durable `last_error` of a run whose transaction COMMIT outcome is UNKNOWN.
1186
+ *
1187
+ * Such a run is deliberately left NON-terminal (`RUNNING`): the transaction may
1188
+ * have committed. Stale-run recovery settles it from ownership-checked
1189
+ * readiness (`readiness.run_id === run.run_id`), never by guessing here.
1190
+ */
1191
+ export const MATERIALIZED_REPORT_RUN_COMMIT_OUTCOME_UNKNOWN_ERROR = 'COMMIT_OUTCOME_UNKNOWN';
@@ -88,3 +88,18 @@ export var TENANT_ACTIVE_JOBS_ENUM;
88
88
  TENANT_ACTIVE_JOBS_ENUM["Z_REPORT"] = "Z_REPORT";
89
89
  })(TENANT_ACTIVE_JOBS_ENUM || (TENANT_ACTIVE_JOBS_ENUM = {}));
90
90
  export const ALL_TENANT_ACTIVE_JOBS_NAMES = Object.values(TENANT_ACTIVE_JOBS_ENUM);
91
+ /**
92
+ * Terminal outcome of ONE close/retry run that owned the tenant's CALENDAR job.
93
+ *
94
+ * `PARTIAL` is the per-report model's outcome: at least one report FAILED while
95
+ * others committed. It is NEVER a readiness status - readiness keeps only
96
+ * COMMITTED / NEEDS_RECONCILIATION; the failed report lives in the operational
97
+ * run ledger (`materialized_tenant_report_runs`).
98
+ */
99
+ export var TENANT_DAY_JOB_OUTCOME_ENUM;
100
+ (function (TENANT_DAY_JOB_OUTCOME_ENUM) {
101
+ TENANT_DAY_JOB_OUTCOME_ENUM["COMPLETED"] = "COMPLETED";
102
+ TENANT_DAY_JOB_OUTCOME_ENUM["PARTIAL"] = "PARTIAL";
103
+ TENANT_DAY_JOB_OUTCOME_ENUM["FAILED"] = "FAILED";
104
+ })(TENANT_DAY_JOB_OUTCOME_ENUM || (TENANT_DAY_JOB_OUTCOME_ENUM = {}));
105
+ export const ALL_TENANT_DAY_JOB_OUTCOMES = Object.values(TENANT_DAY_JOB_OUTCOME_ENUM);
@@ -77,4 +77,16 @@ export const WORKER_ENDPOINTS = {
77
77
  // ========================>> Shift / Business Day / Cash Drawer (compile) <<======================= //
78
78
  END_INSTORE_SHIFT: '/end-instore-shift',
79
79
  END_TENANT_DAY: '/end-tenant-day',
80
+ /**
81
+ * INTERNAL, report-scoped retry of ONE failed END_TENANT_DAY report.
82
+ * Operations-service is the only caller (POST /materialization/reports/retry).
83
+ * Never a whole-day retry.
84
+ */
85
+ RETRY_MATERIALIZED_REPORT: '/internal/materialization/reports/retry',
86
+ /**
87
+ * INTERNAL, worker-owned recovery of INTERRUPTED report runs (RUNNING /
88
+ * RETRYING). Called before the retry API rejects a RUNNING report and before
89
+ * listing runs for the admin page. Never fabricates readiness.
90
+ */
91
+ RECONCILE_MATERIALIZED_REPORT_RUNS: '/internal/materialization/reports/reconcile',
80
92
  };
@@ -1,6 +1,7 @@
1
1
  export * from './key-helpers';
2
2
  export * from './materialized-credit-bills-helpers';
3
3
  export * from './materialized-day-readiness';
4
+ export * from './materialized-report-runs';
4
5
  export * from './materialized-index-bootstrap';
5
6
  export * from './materialized-integer-contract';
6
7
  export * from './materialized-discount-accumulator-helpers';
@@ -5,7 +5,7 @@ import { MATERIALIZED_TENANT_DELIVERY_ENTRIES_COLLECTION_NAME, MATERIALIZED_TENA
5
5
  import { MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_COLLECTION_NAME, MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_delivery_geo_cells';
6
6
  import { MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_INDEX_OPTIONS } from '../../schemas/models/materialized_tenant_delivery_geo_scopes';
7
7
  import { MATERIALIZED_TENANT_DELIVERY_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_DELIVERY_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_delivery_reports';
8
- import { MATERIALIZED_TENANT_DAY_READINESS_COLLECTION_NAME } from '../../constants/MATERIALIZED_REPORTS';
8
+ import { MATERIALIZED_TENANT_DAY_READINESS_COLLECTION_NAME, MATERIALIZED_TENANT_REPORT_RUNS_COLLECTION_NAME, } from '../../constants/MATERIALIZED_REPORTS';
9
9
  import { MATERIALIZED_TENANT_DAY_READINESS_INDEX_OPTIONS } from '../../schemas/models/materialized_tenant_day_readiness';
10
10
  import { MATERIALIZED_TENANT_DIALER_ENTRIES_COLLECTION_NAME, MATERIALIZED_TENANT_DIALER_ENTRIES_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_dialer_entries';
11
11
  import { MATERIALIZED_TENANT_DIALER_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_DIALER_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_dialer_reports';
@@ -16,6 +16,7 @@ import { MATERIALIZED_TENANT_DISCOUNT_REPORTS_COLLECTION_NAME, MATERIALIZED_TENA
16
16
  import { MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME, MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_discount_sources';
17
17
  import { MATERIALIZED_TENANT_PREP_TIME_ENTRIES_COLLECTION_NAME, MATERIALIZED_TENANT_PREP_TIME_ENTRIES_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_prep_time_entries';
18
18
  import { MATERIALIZED_TENANT_PREP_TIME_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_PREP_TIME_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_prep_time_reports';
19
+ import { MATERIALIZED_TENANT_REPORT_RUNS_INDEX_OPTIONS } from '../../schemas/models/materialized_tenant_report_runs';
19
20
  import { MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME, MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_reports_iterations';
20
21
  import { MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_sales_overview_reports';
21
22
  import { TENANT_ACTIVE_JOBS_COLLECTION_NAME, TENANT_ACTIVE_JOBS_INDEX_OPTIONS, } from '../../schemas/models/tenant_active_jobs';
@@ -43,6 +44,7 @@ const INDEXES = {
43
44
  [MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME]: MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS,
44
45
  [MATERIALIZED_TENANT_PREP_TIME_REPORTS_COLLECTION_NAME]: MATERIALIZED_TENANT_PREP_TIME_REPORTS_INDEX_OPTIONS,
45
46
  [MATERIALIZED_TENANT_PREP_TIME_ENTRIES_COLLECTION_NAME]: MATERIALIZED_TENANT_PREP_TIME_ENTRIES_INDEX_OPTIONS,
47
+ [MATERIALIZED_TENANT_REPORT_RUNS_COLLECTION_NAME]: MATERIALIZED_TENANT_REPORT_RUNS_INDEX_OPTIONS,
46
48
  [MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME]: MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS,
47
49
  [MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_COLLECTION_NAME]: MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_INDEX_OPTIONS,
48
50
  [MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_COLLECTION_NAME]: MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_INDEX_OPTIONS,
@@ -0,0 +1,143 @@
1
+ import { MATERIALIZED_REPORT_RUN_COMMIT_OUTCOME_UNKNOWN_ERROR, MATERIALIZED_TENANT_REPORT_RUN_ERROR_MAX_LENGTH, MATERIALIZED_TENANT_REPORT_RUN_STALE_MS, MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM, MATERIALIZED_TENANT_REPORT_RUNS_SCHEMA_VERSION, } from '../../constants/MATERIALIZED_REPORTS';
2
+ /** Stable string key of one run row (Map keys / log correlation). */
3
+ export function materializedReportRunKey(parts) {
4
+ return [
5
+ String(parts.tenantId),
6
+ String(parts.localDate),
7
+ String(parts.reportKey),
8
+ String(parts.schemaVersion ?? MATERIALIZED_TENANT_REPORT_RUNS_SCHEMA_VERSION),
9
+ ].join('|');
10
+ }
11
+ /** The Mongo filter of the ONE run row of a report. */
12
+ export function buildMaterializedReportRunFilter(parts) {
13
+ return {
14
+ tenant_id: String(parts.tenantId),
15
+ localDate: String(parts.localDate),
16
+ report_key: String(parts.reportKey),
17
+ schema_version: parts.schemaVersion ?? MATERIALIZED_TENANT_REPORT_RUNS_SCHEMA_VERSION,
18
+ };
19
+ }
20
+ export function isMaterializedReportRunStatus(value) {
21
+ return Object.values(MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM).includes(value);
22
+ }
23
+ /**
24
+ * Is the report CURRENTLY being executed?
25
+ *
26
+ * Only a run that owns the report may be in this state; the retry API refuses
27
+ * it (409) instead of racing the executor.
28
+ */
29
+ export function isMaterializedReportRunInFlight(status) {
30
+ return (status === MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.PENDING ||
31
+ status === MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.RUNNING ||
32
+ status === MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.RETRYING);
33
+ }
34
+ /**
35
+ * Does this run carry the UNKNOWN-COMMIT marker?
36
+ *
37
+ * Such a run MUST stay non-terminal until recovery settles it from
38
+ * ownership-checked readiness (a ledger error alone must never decide it).
39
+ */
40
+ export function isMaterializedReportRunCommitOutcomeUnknown(run) {
41
+ return String(run?.last_error ?? '') === MATERIALIZED_REPORT_RUN_COMMIT_OUTCOME_UNKNOWN_ERROR;
42
+ }
43
+ /**
44
+ * Proves that a readiness row belongs to the run that owns this report.
45
+ *
46
+ * The row identity is the tenant/day/report/schema; ownership is the RUN
47
+ * identity. A row written by an OLDER close/re-close carries a DIFFERENT
48
+ * `run_id` and therefore proves nothing about the current run: it must keep the
49
+ * old report readable while the current run is recorded as FAILED
50
+ * (`INTERRUPTED_RUN`) and stays retryable. Never delete that older row.
51
+ */
52
+ export function isMaterializedReadinessOwnedByRun(args) {
53
+ const readinessRun = args.readiness?.run_id;
54
+ const currentRun = args.run?.run_id;
55
+ return readinessRun != null && currentRun != null && String(readinessRun) === String(currentRun);
56
+ }
57
+ /** COMMITTED / FAILED: the execution reached a terminal state. */
58
+ export function isMaterializedReportRunTerminal(status) {
59
+ return (status === MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.COMMITTED ||
60
+ status === MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.FAILED);
61
+ }
62
+ /**
63
+ * The instant a run started (its `started_at`, or `updatedAt` when a legacy /
64
+ * partial row has none).
65
+ */
66
+ export function materializedReportRunStartedAt(run) {
67
+ const candidate = run.started_at ?? run.updatedAt ?? null;
68
+ if (candidate == null)
69
+ return null;
70
+ const date = candidate instanceof Date ? candidate : new Date(candidate);
71
+ return Number.isNaN(date.getTime()) ? null : date;
72
+ }
73
+ /**
74
+ * Is an IN-FLIGHT run old enough that an interruption is plausible?
75
+ *
76
+ * AGE ALONE IS NEVER ENOUGH: recovery must also observe a STALE
77
+ * `tenant_active_jobs` heartbeat, otherwise a healthy long-running report would
78
+ * be taken over. This helper only answers the age question.
79
+ */
80
+ export function isMaterializedReportRunStale(args) {
81
+ if (!isMaterializedReportRunInFlight(args.run?.status))
82
+ return false;
83
+ const startedAt = materializedReportRunStartedAt(args.run);
84
+ if (startedAt == null)
85
+ return false;
86
+ const now = args.now ?? new Date();
87
+ const staleAfterMs = Number.isFinite(args.staleAfterMs) && args.staleAfterMs > 0
88
+ ? args.staleAfterMs
89
+ : MATERIALIZED_TENANT_REPORT_RUN_STALE_MS;
90
+ return now.getTime() - startedAt.getTime() >= staleAfterMs;
91
+ }
92
+ /**
93
+ * May this report be retried?
94
+ *
95
+ * A MISSING row is retryable (the report never ran: a failed day, a crash
96
+ * before the first attempt, or a pre-ledger day). A FAILED row is retryable.
97
+ * A COMMITTED row is NOT (the retry would be a no-op), and an in-flight row is
98
+ * NOT (the executor owns it).
99
+ */
100
+ export function canRetryMaterializedReportRun(status) {
101
+ if (status == null)
102
+ return true;
103
+ return status === MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.FAILED;
104
+ }
105
+ /* ------------------------------ PII safety ------------------------------ */
106
+ /**
107
+ * Patterns that must NEVER reach `last_error` (operator-visible, may be logged
108
+ * or exported): e-mail addresses, long digit runs (phone numbers / card-like
109
+ * values) and bearer/basic credentials.
110
+ */
111
+ const PII_PATTERNS = [
112
+ [/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g, '[redacted-email]'],
113
+ // 7+ digits with common separators (phone numbers, card fragments).
114
+ [/(?:\+?\d[\s().-]?){7,}\d/g, '[redacted-number]'],
115
+ [/\b(?:bearer|basic)\s+[A-Za-z0-9._~+/-]+=*/gi, '[redacted-credential]'],
116
+ [/\b(?:password|secret|token|api[_-]?key)\s*[:=]\s*\S+/gi, '[redacted-credential]'],
117
+ ];
118
+ /**
119
+ * Sanitizes an execution error for durable storage / API output.
120
+ *
121
+ * Deliberately conservative: the message is single-lined, PII-redacted and
122
+ * bounded. The ERROR STACK and raw driver payload are never stored.
123
+ */
124
+ export function sanitizeMaterializedReportRunError(error) {
125
+ if (error == null)
126
+ return null;
127
+ const raw = typeof error === 'string'
128
+ ? error
129
+ : typeof error.message === 'string'
130
+ ? String(error.message)
131
+ : '';
132
+ let message = raw.replace(/\s+/g, ' ').trim();
133
+ if (message === '') {
134
+ message =
135
+ typeof error.name === 'string'
136
+ ? String(error.name)
137
+ : 'UNKNOWN_ERROR';
138
+ }
139
+ for (const [pattern, replacement] of PII_PATTERNS) {
140
+ message = message.replace(pattern, replacement);
141
+ }
142
+ return message.slice(0, MATERIALIZED_TENANT_REPORT_RUN_ERROR_MAX_LENGTH);
143
+ }
@@ -49,6 +49,7 @@ export * from './materialized_tenant_payments_comparison_entries';
49
49
  export * from './materialized_tenant_payments_comparison_reports';
50
50
  export * from './materialized_tenant_prep_time_entries';
51
51
  export * from './materialized_tenant_prep_time_reports';
52
+ export * from './materialized_tenant_report_runs';
52
53
  export * from './materialized_tenant_reports_iterations';
53
54
  export * from './materialized_tenant_sales_comparison_entries';
54
55
  export * from './materialized_tenant_sales_comparison_reports';
@@ -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,
@@ -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';
@@ -927,3 +927,72 @@ export declare const ALL_MATERIALIZED_REPORT_READINESS_STATUSES: MATERIALIZED_RE
927
927
  */
928
928
  export declare const MATERIALIZED_READINESS_REPORT_KEYS: readonly ["OVERVIEW", "DISCOUNTS", "PREP_TIME", "DELIVERY", "DIALER", "DINEIN"];
929
929
  export type TMaterializedReadinessReportKey = (typeof MATERIALIZED_READINESS_REPORT_KEYS)[number];
930
+ /**
931
+ * DURABLE OPERATIONAL LEDGER of one report's execution, SEPARATE from
932
+ * readiness.
933
+ *
934
+ * Readiness answers "may a historical read treat this tenant-day-report as
935
+ * final?" (only COMMITTED / NEEDS_RECONCILIATION ever appear there).
936
+ * This ledger answers "what happened to the execution?" and is the ONLY place
937
+ * a failed attempt is recorded (PENDING / RUNNING / RETRYING / COMMITTED /
938
+ * FAILED), so a FAILED report never - and can never - look materialized.
939
+ *
940
+ * One row per `(tenant_id, localDate, report_key, schema_version)`. The row is
941
+ * operational state: it is written OUTSIDE the report transaction (a rollback
942
+ * must not erase the fact that the attempt happened). Readiness is written
943
+ * INSIDE the report transaction. The two therefore have different lifecycles
944
+ * and are deliberately different collections.
945
+ *
946
+ * Phase 1 replaces the one all-report END_TENANT_DAY transaction with one
947
+ * transaction per report: each report commits (or fails) on its own, and this
948
+ * ledger is the operator-visible record of that per-report outcome.
949
+ */
950
+ export declare const MATERIALIZED_TENANT_REPORT_RUNS_COLLECTION_NAME = "materialized_tenant_report_runs";
951
+ /** Version of the report-run row shape. Pinned to 1 with the other collections. */
952
+ export declare const MATERIALIZED_TENANT_REPORT_RUNS_SCHEMA_VERSION = 1;
953
+ /** The execution state of one tenant-day-report run. */
954
+ export declare enum MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM {
955
+ /** The row exists but no attempt has started yet. */
956
+ PENDING = "PENDING",
957
+ /** An attempt is executing (its own transaction is open or about to open). */
958
+ RUNNING = "RUNNING",
959
+ /** A transient failure happened; another attempt is starting. */
960
+ RETRYING = "RETRYING",
961
+ /** The report transaction committed: documents + readiness are durable. */
962
+ COMMITTED = "COMMITTED",
963
+ /** Every allowed attempt was exhausted; readiness was NOT written. */
964
+ FAILED = "FAILED"
965
+ }
966
+ export declare const ALL_MATERIALIZED_TENANT_REPORT_RUN_STATUSES: MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM[];
967
+ /** States in which an execution currently owns the report (retry is refused). */
968
+ export declare const MATERIALIZED_TENANT_REPORT_RUN_IN_FLIGHT_STATUSES: readonly [MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.PENDING, MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.RUNNING, MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.RETRYING];
969
+ /**
970
+ * Bounded attempt budget of ONE report execution (transient errors only).
971
+ * A deterministic code/compile error is NEVER retried inside the job.
972
+ */
973
+ export declare const MATERIALIZED_TENANT_REPORT_RUN_MAX_ATTEMPTS = 2;
974
+ /** Bounded size of the stored `last_error` (single-line, PII-redacted). */
975
+ export declare const MATERIALIZED_TENANT_REPORT_RUN_ERROR_MAX_LENGTH = 500;
976
+ /**
977
+ * Stale window of a report-run row (RUNNING / RETRYING).
978
+ *
979
+ * A run that has not reached a terminal state within this window MAY have been
980
+ * interrupted (process crash). Recovery NEVER trusts the age alone: it also
981
+ * requires the tenant's active-job heartbeat to be stale, so a live long-running
982
+ * report is never taken over. The value mirrors the active-job lease window
983
+ * (30 minutes) so both fences agree.
984
+ */
985
+ export declare const MATERIALIZED_TENANT_REPORT_RUN_STALE_MS: number;
986
+ /**
987
+ * Durable `last_error` of a run recovered after an interruption with NO
988
+ * readiness row (the report transaction provably did not commit).
989
+ */
990
+ export declare const MATERIALIZED_REPORT_RUN_INTERRUPTED_ERROR = "INTERRUPTED_RUN";
991
+ /**
992
+ * Durable `last_error` of a run whose transaction COMMIT outcome is UNKNOWN.
993
+ *
994
+ * Such a run is deliberately left NON-terminal (`RUNNING`): the transaction may
995
+ * have committed. Stale-run recovery settles it from ownership-checked
996
+ * readiness (`readiness.run_id === run.run_id`), never by guessing here.
997
+ */
998
+ export declare const MATERIALIZED_REPORT_RUN_COMMIT_OUTCOME_UNKNOWN_ERROR = "COMMIT_OUTCOME_UNKNOWN";
@@ -12,7 +12,7 @@
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.MATERIALIZED_DELIVERY_GEO_SCOPES = exports.MATERIALIZED_DELIVERY_GEO_RESOLUTION = exports.MATERIALIZED_DELIVERY_GEO_MAX_KEYS = exports.MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_SCHEMA_VERSION = exports.MATERIALIZED_TENANT_DELIVERY_GEO_SCOPES_COLLECTION_NAME = exports.MATERIALIZED_TENANT_DELIVERY_GEO_CELLS_SCHEMA_VERSION = exports.MATERIALIZED_DELIVERY_OTHER_TOKEN = exports.MATERIALIZED_DELIVERY_AREA_LIMIT = exports.MATERIALIZED_DELIVERY_RIDER_LIMIT = exports.MATERIALIZED_DELIVERY_DISTANCE_BUCKETS = exports.DELIVERY_KEY_FILTERS = exports.MATERIALIZED_DELIVERY_ENTRY_PAGE_ALL = exports.MATERIALIZED_DELIVERY_ENTRY_SCOPES = exports.MATERIALIZED_DELIVERY_REPORT_SCOPES = exports.MATERIALIZED_CASH_SOURCE_LIMIT = exports.CASH_KEY_FILTERS = exports.MATERIALIZED_CASH_REPORT_SCOPES = exports.ALL_MATERIALIZED_REPORT_CREDIT_BILL_EVENT_TYPES = exports.MATERIALIZED_REPORT_CREDIT_BILL_EVENT_TYPE_ENUM = exports.MATERIALIZED_CREDIT_SETTLEMENT_PAYMENT_TYPE = exports.MATERIALIZED_DISCOUNT_SOURCE_KEY_PREFIX = exports.MATERIALIZED_DISCOUNT_SOURCE_KEY = exports.ALL_MATERIALIZED_DISCOUNT_RATE_KINDS = exports.MATERIALIZED_DISCOUNT_RATE_KIND_ENUM = exports.ALL_MATERIALIZED_DISCOUNT_SOURCE_CLASSES = exports.MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM = exports.MATERIALIZED_DISCOUNT_PRICE_OVERRIDE_PREFIX = exports.MATERIALIZED_DISCOUNT_MANUAL_PREFIX = exports.MATERIALIZED_DISCOUNT_SPECIAL_DISCOUNT_ID = exports.MATERIALIZED_DISCOUNT_SOURCE_LIMIT = exports.ALL_MATERIALIZED_DISCOUNT_LEVELS = exports.MATERIALIZED_DISCOUNT_LEVEL_ENUM = exports.ALL_MATERIALIZED_DISCOUNT_ENTRY_KINDS = exports.MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM = exports.ALL_MATERIALIZED_DISCOUNT_SUB_KINDS = exports.MATERIALIZED_DISCOUNT_SUB_KIND_ENUM = exports.ALL_MATERIALIZED_DISCOUNT_SLOTS = exports.MATERIALIZED_DISCOUNT_SLOT_ENUM = exports.DISCOUNTS_KEY_FILTERS = exports.MATERIALIZED_DISCOUNT_REPORT_SCOPES = exports.MATERIALIZED_DISCOUNT_ENTRY_PAGE_ALL = exports.SALES_OVERVIEW_KEY_FILTERS = exports.ALL_MATERIALIZED_REPORT_KEY_FILTERS = exports.MATERIALIZED_REPORT_KEY_FILTER_ENUM = exports.MATERIALIZED_CREDIT_BILLS_SCOPES = exports.MATERIALIZED_REPORT_SCOPES_WITH_DAY_INDEX = exports.ALL_MATERIALIZED_REPORT_WINDOW_KINDS = exports.MATERIALIZED_REPORT_WINDOW_KIND_ENUM = exports.ALL_MATERIALIZED_REPORT_SCOPES = exports.MATERIALIZED_REPORT_SCOPE_ENUM = void 0;
14
14
  exports.MATERIALIZED_DIALER_ENTRY_PAGE_ALL = exports.MATERIALIZED_DIALER_ENTRY_SCOPES = exports.MATERIALIZED_DIALER_REPORT_SCOPES = exports.ALL_PAYMENTS_COMPARISON_METRICS = exports.PAYMENTS_COMPARISON_METRIC_ENUM = exports.MATERIALIZED_TENANT_PAYMENTS_COMPARISON_ENTRIES_SCHEMA_VERSION = exports.MATERIALIZED_TENANT_PAYMENTS_COMPARISON_REPORTS_SCHEMA_VERSION = exports.PAYMENTS_COMPARISON_KEY_FILTERS = exports.MATERIALIZED_PAYMENTS_COMPARISON_REPORT_SCOPES = exports.MATERIALIZED_TENANT_PAYMENTS_COMPARISON_ENTRIES_COLLECTION_NAME = exports.MATERIALIZED_TENANT_PAYMENTS_COMPARISON_REPORTS_COLLECTION_NAME = exports.MATERIALIZED_TENANT_HOURLY_COMPARISON_ENTRIES_SCHEMA_VERSION = exports.MATERIALIZED_TENANT_HOURLY_COMPARISON_REPORTS_SCHEMA_VERSION = exports.HOURLY_COMPARISON_KEY_FILTERS = exports.MATERIALIZED_HOURLY_COMPARISON_REPORT_SCOPES = exports.MATERIALIZED_TENANT_HOURLY_COMPARISON_ENTRIES_COLLECTION_NAME = exports.MATERIALIZED_TENANT_HOURLY_COMPARISON_REPORTS_COLLECTION_NAME = exports.ALL_SALES_COMPARISON_METRICS = exports.SALES_COMPARISON_METRIC_ENUM = exports.ALL_SALES_COMPARISON_BASELINES = exports.SALES_COMPARISON_BASELINE_ENUM = exports.MATERIALIZED_TENANT_SALES_COMPARISON_ENTRIES_SCHEMA_VERSION = exports.MATERIALIZED_TENANT_SALES_COMPARISON_REPORTS_SCHEMA_VERSION = exports.SALES_COMPARISON_KEY_FILTERS = exports.MATERIALIZED_SALES_COMPARISON_REPORT_SCOPES = exports.MATERIALIZED_TENANT_SALES_COMPARISON_ENTRIES_COLLECTION_NAME = exports.MATERIALIZED_TENANT_SALES_COMPARISON_REPORTS_COLLECTION_NAME = exports.ALL_MATERIALIZED_REPORT_TYPES = exports.MATERIALIZED_REPORT_TYPE_ENUM = exports.ALL_MATERIALIZED_PREP_TIME_TARGETS = exports.MATERIALIZED_PREP_TIME_TARGET_ENUM = exports.MATERIALIZED_PREP_TIME_ON_TIME_MAX_DELTA = exports.MATERIALIZED_PREP_TIME_TOP_BUCKET = exports.MATERIALIZED_PREP_TIME_MAX_MINUTES = exports.ALL_MATERIALIZED_PREP_TIME_BUCKET_LABELS = exports.MATERIALIZED_PREP_TIME_BUCKETS = exports.ALL_MATERIALIZED_PREP_TIME_ANCHORS = exports.MATERIALIZED_PREP_TIME_ANCHOR_ENUM = exports.PREP_TIME_KEY_FILTERS = exports.MATERIALIZED_PREP_TIME_ITEM_MAX_READ_DAYS = exports.MATERIALIZED_PREP_TIME_ITEM_SCOPES = exports.MATERIALIZED_PREP_TIME_REPORT_SCOPES = exports.MATERIALIZED_DELIVERY_GEO_CELL_MAX_BSON_BYTES = exports.MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES = exports.MATERIALIZED_DELIVERY_REPORT_MAX_BSON_BYTES = exports.MATERIALIZED_DELIVERY_GEO_UNKNOWN_CELL = exports.MATERIALIZED_DELIVERY_GEO_MONTH_RETENTION_MONTHS = exports.MATERIALIZED_DELIVERY_GEO_DAY_RETENTION_DAYS = exports.MATERIALIZED_DELIVERY_GEO_MAX_CELLS = exports.MATERIALIZED_DELIVERY_GEO_MIN_ORDERS = void 0;
15
- exports.MATERIALIZED_READINESS_REPORT_KEYS = exports.ALL_MATERIALIZED_REPORT_READINESS_STATUSES = exports.MATERIALIZED_REPORT_READINESS_STATUS_ENUM = exports.MATERIALIZED_TENANT_DAY_READINESS_SCHEMA_VERSION = exports.MATERIALIZED_TENANT_DAY_READINESS_COLLECTION_NAME = exports.MATERIALIZED_DINEIN_RESERVATIONS_TIME_INDEX_OPTIONS = exports.MATERIALIZED_DINEIN_FINGERPRINTS_CREATED_AT_INDEX_OPTIONS = exports.MATERIALIZED_DINEIN_FINGERPRINTS_ORDER_INDEX_OPTIONS = exports.MATERIALIZED_DINEIN_ENTRY_MAX_BSON_BYTES = exports.MATERIALIZED_DINEIN_REPORT_MAX_BSON_BYTES = exports.ALL_MATERIALIZED_DINEIN_PAYMENT_CLASSES = exports.MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM = exports.ALL_MATERIALIZED_DINEIN_SPLIT_ROLES = exports.MATERIALIZED_DINEIN_SPLIT_ROLE_ENUM = exports.ALL_MATERIALIZED_DINEIN_ORIGINS = exports.MATERIALIZED_DINEIN_ORIGIN_ENUM = exports.MATERIALIZED_DINEIN_SPLIT_PARENT_DELETE_REASON = exports.MATERIALIZED_DINEIN_HOUR_KEYS = exports.MATERIALIZED_DINEIN_TOP_EDITED_CHECK_LIMIT = exports.MATERIALIZED_DINEIN_UPCOMING_RESERVATION_LIMIT = exports.MATERIALIZED_DINEIN_SERVICE_CHARGE_RATE_LIMIT = exports.MATERIALIZED_DINEIN_COURSE_LIMIT = exports.MATERIALIZED_DINEIN_FLOOR_LIMIT = exports.MATERIALIZED_DINEIN_TABLE_LIMIT = exports.MATERIALIZED_DINEIN_OTHER_TOKEN = exports.MATERIALIZED_DINEIN_KEY_SEPARATOR = exports.MATERIALIZED_DINEIN_UNKNOWN_TOKEN = exports.DINEIN_KEY_FILTERS = exports.MATERIALIZED_DINEIN_ENTRY_PAGE_ALL = exports.MATERIALIZED_DINEIN_ENTRY_SCOPES = exports.MATERIALIZED_DINEIN_REPORT_SCOPES = exports.MATERIALIZED_DIALER_ENTRY_MAX_BSON_BYTES = exports.MATERIALIZED_DIALER_REPORT_MAX_BSON_BYTES = exports.MATERIALIZED_DIALER_HOUR_KEYS = exports.MATERIALIZED_DIALER_KEY_SEPARATOR = exports.MATERIALIZED_DIALER_UNKNOWN_TOKEN = exports.DIALER_KEY_FILTERS = void 0;
15
+ exports.MATERIALIZED_REPORT_RUN_COMMIT_OUTCOME_UNKNOWN_ERROR = exports.MATERIALIZED_REPORT_RUN_INTERRUPTED_ERROR = exports.MATERIALIZED_TENANT_REPORT_RUN_STALE_MS = exports.MATERIALIZED_TENANT_REPORT_RUN_ERROR_MAX_LENGTH = exports.MATERIALIZED_TENANT_REPORT_RUN_MAX_ATTEMPTS = exports.MATERIALIZED_TENANT_REPORT_RUN_IN_FLIGHT_STATUSES = exports.ALL_MATERIALIZED_TENANT_REPORT_RUN_STATUSES = exports.MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM = exports.MATERIALIZED_TENANT_REPORT_RUNS_SCHEMA_VERSION = exports.MATERIALIZED_TENANT_REPORT_RUNS_COLLECTION_NAME = exports.MATERIALIZED_READINESS_REPORT_KEYS = exports.ALL_MATERIALIZED_REPORT_READINESS_STATUSES = exports.MATERIALIZED_REPORT_READINESS_STATUS_ENUM = exports.MATERIALIZED_TENANT_DAY_READINESS_SCHEMA_VERSION = exports.MATERIALIZED_TENANT_DAY_READINESS_COLLECTION_NAME = exports.MATERIALIZED_DINEIN_RESERVATIONS_TIME_INDEX_OPTIONS = exports.MATERIALIZED_DINEIN_FINGERPRINTS_CREATED_AT_INDEX_OPTIONS = exports.MATERIALIZED_DINEIN_FINGERPRINTS_ORDER_INDEX_OPTIONS = exports.MATERIALIZED_DINEIN_ENTRY_MAX_BSON_BYTES = exports.MATERIALIZED_DINEIN_REPORT_MAX_BSON_BYTES = exports.ALL_MATERIALIZED_DINEIN_PAYMENT_CLASSES = exports.MATERIALIZED_DINEIN_PAYMENT_CLASS_ENUM = exports.ALL_MATERIALIZED_DINEIN_SPLIT_ROLES = exports.MATERIALIZED_DINEIN_SPLIT_ROLE_ENUM = exports.ALL_MATERIALIZED_DINEIN_ORIGINS = exports.MATERIALIZED_DINEIN_ORIGIN_ENUM = exports.MATERIALIZED_DINEIN_SPLIT_PARENT_DELETE_REASON = exports.MATERIALIZED_DINEIN_HOUR_KEYS = exports.MATERIALIZED_DINEIN_TOP_EDITED_CHECK_LIMIT = exports.MATERIALIZED_DINEIN_UPCOMING_RESERVATION_LIMIT = exports.MATERIALIZED_DINEIN_SERVICE_CHARGE_RATE_LIMIT = exports.MATERIALIZED_DINEIN_COURSE_LIMIT = exports.MATERIALIZED_DINEIN_FLOOR_LIMIT = exports.MATERIALIZED_DINEIN_TABLE_LIMIT = exports.MATERIALIZED_DINEIN_OTHER_TOKEN = exports.MATERIALIZED_DINEIN_KEY_SEPARATOR = exports.MATERIALIZED_DINEIN_UNKNOWN_TOKEN = exports.DINEIN_KEY_FILTERS = exports.MATERIALIZED_DINEIN_ENTRY_PAGE_ALL = exports.MATERIALIZED_DINEIN_ENTRY_SCOPES = exports.MATERIALIZED_DINEIN_REPORT_SCOPES = exports.MATERIALIZED_DIALER_ENTRY_MAX_BSON_BYTES = exports.MATERIALIZED_DIALER_REPORT_MAX_BSON_BYTES = exports.MATERIALIZED_DIALER_HOUR_KEYS = exports.MATERIALIZED_DIALER_KEY_SEPARATOR = exports.MATERIALIZED_DIALER_UNKNOWN_TOKEN = exports.DIALER_KEY_FILTERS = void 0;
16
16
  /**
17
17
  * Time-scope of a materialized report document. Every document covers exactly
18
18
  * one tenant × one scope context × one scope value (`scope_field`), and the
@@ -1117,3 +1117,80 @@ exports.MATERIALIZED_READINESS_REPORT_KEYS = [
1117
1117
  'DIALER',
1118
1118
  'DINEIN',
1119
1119
  ];
1120
+ /* ======================================================================== */
1121
+ /* Report run ledger (ONE row per report execution) */
1122
+ /* ======================================================================== */
1123
+ /**
1124
+ * DURABLE OPERATIONAL LEDGER of one report's execution, SEPARATE from
1125
+ * readiness.
1126
+ *
1127
+ * Readiness answers "may a historical read treat this tenant-day-report as
1128
+ * final?" (only COMMITTED / NEEDS_RECONCILIATION ever appear there).
1129
+ * This ledger answers "what happened to the execution?" and is the ONLY place
1130
+ * a failed attempt is recorded (PENDING / RUNNING / RETRYING / COMMITTED /
1131
+ * FAILED), so a FAILED report never - and can never - look materialized.
1132
+ *
1133
+ * One row per `(tenant_id, localDate, report_key, schema_version)`. The row is
1134
+ * operational state: it is written OUTSIDE the report transaction (a rollback
1135
+ * must not erase the fact that the attempt happened). Readiness is written
1136
+ * INSIDE the report transaction. The two therefore have different lifecycles
1137
+ * and are deliberately different collections.
1138
+ *
1139
+ * Phase 1 replaces the one all-report END_TENANT_DAY transaction with one
1140
+ * transaction per report: each report commits (or fails) on its own, and this
1141
+ * ledger is the operator-visible record of that per-report outcome.
1142
+ */
1143
+ exports.MATERIALIZED_TENANT_REPORT_RUNS_COLLECTION_NAME = 'materialized_tenant_report_runs';
1144
+ /** Version of the report-run row shape. Pinned to 1 with the other collections. */
1145
+ exports.MATERIALIZED_TENANT_REPORT_RUNS_SCHEMA_VERSION = 1;
1146
+ /** The execution state of one tenant-day-report run. */
1147
+ var MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM;
1148
+ (function (MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM) {
1149
+ /** The row exists but no attempt has started yet. */
1150
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM["PENDING"] = "PENDING";
1151
+ /** An attempt is executing (its own transaction is open or about to open). */
1152
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM["RUNNING"] = "RUNNING";
1153
+ /** A transient failure happened; another attempt is starting. */
1154
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM["RETRYING"] = "RETRYING";
1155
+ /** The report transaction committed: documents + readiness are durable. */
1156
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM["COMMITTED"] = "COMMITTED";
1157
+ /** Every allowed attempt was exhausted; readiness was NOT written. */
1158
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM["FAILED"] = "FAILED";
1159
+ })(MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM || (exports.MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM = MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM = {}));
1160
+ exports.ALL_MATERIALIZED_TENANT_REPORT_RUN_STATUSES = Object.values(MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM);
1161
+ /** States in which an execution currently owns the report (retry is refused). */
1162
+ exports.MATERIALIZED_TENANT_REPORT_RUN_IN_FLIGHT_STATUSES = [
1163
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.PENDING,
1164
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.RUNNING,
1165
+ MATERIALIZED_TENANT_REPORT_RUN_STATUS_ENUM.RETRYING,
1166
+ ];
1167
+ /**
1168
+ * Bounded attempt budget of ONE report execution (transient errors only).
1169
+ * A deterministic code/compile error is NEVER retried inside the job.
1170
+ */
1171
+ exports.MATERIALIZED_TENANT_REPORT_RUN_MAX_ATTEMPTS = 2;
1172
+ /** Bounded size of the stored `last_error` (single-line, PII-redacted). */
1173
+ exports.MATERIALIZED_TENANT_REPORT_RUN_ERROR_MAX_LENGTH = 500;
1174
+ /**
1175
+ * Stale window of a report-run row (RUNNING / RETRYING).
1176
+ *
1177
+ * A run that has not reached a terminal state within this window MAY have been
1178
+ * interrupted (process crash). Recovery NEVER trusts the age alone: it also
1179
+ * requires the tenant's active-job heartbeat to be stale, so a live long-running
1180
+ * report is never taken over. The value mirrors the active-job lease window
1181
+ * (30 minutes) so both fences agree.
1182
+ */
1183
+ exports.MATERIALIZED_TENANT_REPORT_RUN_STALE_MS = 30 * 60 * 1000;
1184
+ /**
1185
+ * Durable `last_error` of a run recovered after an interruption with NO
1186
+ * readiness row (the report transaction provably did not commit).
1187
+ */
1188
+ exports.MATERIALIZED_REPORT_RUN_INTERRUPTED_ERROR = 'INTERRUPTED_RUN';
1189
+ /**
1190
+ * Durable `last_error` of a run whose transaction COMMIT outcome is UNKNOWN.
1191
+ *
1192
+ * Such a run is deliberately left NON-terminal (`RUNNING`): the transaction may
1193
+ * have committed. Stale-run recovery settles it from ownership-checked
1194
+ * readiness (`readiness.run_id === run.run_id`), never by guessing here.
1195
+ */
1196
+ exports.MATERIALIZED_REPORT_RUN_COMMIT_OUTCOME_UNKNOWN_ERROR = 'COMMIT_OUTCOME_UNKNOWN';
@@ -78,3 +78,17 @@ export declare enum TENANT_ACTIVE_JOBS_ENUM {
78
78
  Z_REPORT = "Z_REPORT"
79
79
  }
80
80
  export declare const ALL_TENANT_ACTIVE_JOBS_NAMES: TENANT_ACTIVE_JOBS_ENUM[];
81
+ /**
82
+ * Terminal outcome of ONE close/retry run that owned the tenant's CALENDAR job.
83
+ *
84
+ * `PARTIAL` is the per-report model's outcome: at least one report FAILED while
85
+ * others committed. It is NEVER a readiness status - readiness keeps only
86
+ * COMMITTED / NEEDS_RECONCILIATION; the failed report lives in the operational
87
+ * run ledger (`materialized_tenant_report_runs`).
88
+ */
89
+ export declare enum TENANT_DAY_JOB_OUTCOME_ENUM {
90
+ COMPLETED = "COMPLETED",
91
+ PARTIAL = "PARTIAL",
92
+ FAILED = "FAILED"
93
+ }
94
+ export declare const ALL_TENANT_DAY_JOB_OUTCOMES: TENANT_DAY_JOB_OUTCOME_ENUM[];