@managesome/eposmatic-toolkit 0.4.61 → 0.4.63

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 (122) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +77 -0
  2. package/esm/constants/MATERIALIZED_SCHEMA_VERSIONS.js +17 -0
  3. package/esm/constants/TENANT_ENUMS.js +15 -0
  4. package/esm/constants/WORKER_ENDPOINTS.js +14 -2
  5. package/esm/helpers/materialized-reports/index.js +4 -0
  6. package/esm/helpers/materialized-reports/materialized-credit-bills-helpers.js +3 -3
  7. package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +5 -5
  8. package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +2 -2
  9. package/esm/helpers/materialized-reports/materialized-dialer-close-day-helpers.js +5 -5
  10. package/esm/helpers/materialized-reports/materialized-dialer-compile-helpers.js +2 -2
  11. package/esm/helpers/materialized-reports/materialized-dinein-close-day-helpers.js +5 -5
  12. package/esm/helpers/materialized-reports/materialized-dinein-compile-helpers.js +2 -2
  13. package/esm/helpers/materialized-reports/materialized-discount-compile-helpers.js +3 -3
  14. package/esm/helpers/materialized-reports/materialized-document-size.js +2 -2
  15. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +3 -1
  16. package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +3 -3
  17. package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +3 -3
  18. package/esm/helpers/materialized-reports/materialized-report-runs.js +143 -0
  19. package/esm/helpers/materialized-reports/report-compile.js +38 -0
  20. package/esm/helpers/materialized-reports/tenant-sales-overview-bson.js +27 -0
  21. package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +3 -22
  22. package/esm/schemas/models/index.js +1 -0
  23. package/esm/schemas/models/materialized_credit_bills.js +2 -1
  24. package/esm/schemas/models/materialized_tenant_delivery_entries.js +2 -1
  25. package/esm/schemas/models/materialized_tenant_delivery_reports.js +2 -1
  26. package/esm/schemas/models/materialized_tenant_dialer_entries.js +2 -1
  27. package/esm/schemas/models/materialized_tenant_dialer_reports.js +2 -1
  28. package/esm/schemas/models/materialized_tenant_dinein_entries.js +2 -1
  29. package/esm/schemas/models/materialized_tenant_dinein_reports.js +2 -1
  30. package/esm/schemas/models/materialized_tenant_report_runs.js +105 -0
  31. package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +2 -1
  32. package/esm/schemas/models/tenant_active_jobs.js +19 -1
  33. package/esm/schemas/types/index.js +1 -0
  34. package/lib/constants/MATERIALIZED_REPORTS.d.ts +69 -0
  35. package/lib/constants/MATERIALIZED_REPORTS.js +78 -1
  36. package/lib/constants/MATERIALIZED_SCHEMA_VERSIONS.d.ts +17 -0
  37. package/lib/constants/MATERIALIZED_SCHEMA_VERSIONS.js +20 -0
  38. package/lib/constants/TENANT_ENUMS.d.ts +14 -0
  39. package/lib/constants/TENANT_ENUMS.js +16 -1
  40. package/lib/constants/WORKER_ENDPOINTS.d.ts +12 -2
  41. package/lib/constants/WORKER_ENDPOINTS.js +14 -2
  42. package/lib/helpers/materialized-reports/index.d.ts +3 -0
  43. package/lib/helpers/materialized-reports/index.js +27 -0
  44. package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.js +4 -4
  45. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +6 -6
  46. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +4 -4
  47. package/lib/helpers/materialized-reports/materialized-dialer-close-day-helpers.js +6 -6
  48. package/lib/helpers/materialized-reports/materialized-dialer-compile-helpers.js +6 -6
  49. package/lib/helpers/materialized-reports/materialized-dinein-close-day-helpers.js +6 -6
  50. package/lib/helpers/materialized-reports/materialized-dinein-compile-helpers.js +6 -6
  51. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +3 -3
  52. package/lib/helpers/materialized-reports/materialized-document-size.js +2 -5
  53. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +2 -0
  54. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +3 -3
  55. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +3 -3
  56. package/lib/helpers/materialized-reports/materialized-report-runs.d.ts +105 -0
  57. package/lib/helpers/materialized-reports/materialized-report-runs.js +156 -0
  58. package/lib/helpers/materialized-reports/report-compile.d.ts +38 -0
  59. package/lib/helpers/materialized-reports/report-compile.js +87 -0
  60. package/lib/helpers/materialized-reports/tenant-sales-overview-bson.d.ts +12 -0
  61. package/lib/helpers/materialized-reports/tenant-sales-overview-bson.js +33 -0
  62. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.d.ts +1 -12
  63. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +4 -24
  64. package/lib/helpers/types/workers.d.ts +65 -0
  65. package/lib/schemas/models/index.d.ts +1 -0
  66. package/lib/schemas/models/index.js +1 -0
  67. package/lib/schemas/models/materialized_credit_bills.d.ts +29 -46
  68. package/lib/schemas/models/materialized_credit_bills.js +3 -2
  69. package/lib/schemas/models/materialized_tenant_cash_entries.d.ts +9 -9
  70. package/lib/schemas/models/materialized_tenant_cash_reports.d.ts +9 -9
  71. package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +11 -10
  72. package/lib/schemas/models/materialized_tenant_delivery_entries.js +3 -2
  73. package/lib/schemas/models/materialized_tenant_delivery_geo_cells.d.ts +9 -9
  74. package/lib/schemas/models/materialized_tenant_delivery_geo_scopes.d.ts +6 -6
  75. package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +11 -10
  76. package/lib/schemas/models/materialized_tenant_delivery_reports.js +3 -2
  77. package/lib/schemas/models/materialized_tenant_dialer_entries.d.ts +8 -7
  78. package/lib/schemas/models/materialized_tenant_dialer_entries.js +3 -2
  79. package/lib/schemas/models/materialized_tenant_dialer_reports.d.ts +11 -10
  80. package/lib/schemas/models/materialized_tenant_dialer_reports.js +3 -2
  81. package/lib/schemas/models/materialized_tenant_dinein_entries.d.ts +8 -7
  82. package/lib/schemas/models/materialized_tenant_dinein_entries.js +3 -2
  83. package/lib/schemas/models/materialized_tenant_dinein_reports.d.ts +11 -10
  84. package/lib/schemas/models/materialized_tenant_dinein_reports.js +3 -2
  85. package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +9 -9
  86. package/lib/schemas/models/materialized_tenant_discount_reports.d.ts +9 -9
  87. package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +9 -9
  88. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.d.ts +9 -9
  89. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.d.ts +9 -9
  90. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.d.ts +9 -9
  91. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.d.ts +9 -9
  92. package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +9 -9
  93. package/lib/schemas/models/materialized_tenant_prep_time_reports.d.ts +9 -9
  94. package/lib/schemas/models/materialized_tenant_report_runs.d.ts +97 -0
  95. package/lib/schemas/models/materialized_tenant_report_runs.js +108 -0
  96. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.d.ts +9 -9
  97. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.d.ts +9 -9
  98. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +11 -35
  99. package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +3 -2
  100. package/lib/schemas/models/tenant_active_jobs.d.ts +12 -3
  101. package/lib/schemas/models/tenant_active_jobs.js +18 -0
  102. package/lib/schemas/types/index.d.ts +1 -0
  103. package/lib/schemas/types/index.js +1 -0
  104. package/lib/schemas/types/materialized_tenant_report_runs.d.ts +55 -0
  105. package/lib/schemas/types/tenant_active_jobs.d.ts +15 -1
  106. package/lib/tests/materialized-reports/materialized-report-runs.test.js +116 -0
  107. package/lib/tests/materialized-reports/tenant-dinein-formatter.test.js +159 -0
  108. package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.js +2 -1
  109. package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +5 -4
  110. package/lib/tests/reporting-contract/no-live-transport.test.js +106 -0
  111. package/package.json +2 -1
  112. package/lib/helpers/order/payment-collection.d.ts +0 -25
  113. package/lib/helpers/order/payment-collection.js +0 -51
  114. package/lib/schemas/models/applied_order_discounts.d.ts +0 -80
  115. package/lib/schemas/models/applied_order_discounts.js +0 -106
  116. package/lib/schemas/types/applied_order_discounts.d.ts +0 -22
  117. package/lib/tests/payment-collection-timestamps.test.d.ts +0 -1
  118. package/lib/tests/payment-collection-timestamps.test.js +0 -71
  119. /package/{lib/tests/materialized-reports/analytics-growth-helpers.test.d.ts → esm/schemas/types/materialized_tenant_report_runs.js} +0 -0
  120. /package/lib/schemas/types/{applied_order_discounts.js → materialized_tenant_report_runs.js} +0 -0
  121. /package/lib/tests/materialized-reports/{analytics-overview-helpers.test.d.ts → materialized-report-runs.test.d.ts} +0 -0
  122. /package/lib/tests/{materialized-reports/analytics-performance-helpers.test.d.ts → reporting-contract/no-live-transport.test.d.ts} +0 -0
@@ -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';
@@ -0,0 +1,17 @@
1
+ /**
2
+ * PURE materialized-report schema versions.
3
+ *
4
+ * The close-day plans stamp these versions on the documents they build, so the
5
+ * COMPILE graph must be able to read them without importing a mongoose schema
6
+ * model (the schema files re-export these constants for their existing API).
7
+ *
8
+ * Keep this module dependency-free: it is imported by the worker compile path.
9
+ */
10
+ export const MATERIALIZED_CREDIT_BILLS_SCHEMA_VERSION = 1;
11
+ export const MATERIALIZED_TENANT_DELIVERY_ENTRIES_SCHEMA_VERSION = 1;
12
+ export const MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION = 1;
13
+ export const MATERIALIZED_TENANT_DIALER_ENTRIES_SCHEMA_VERSION = 1;
14
+ export const MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION = 1;
15
+ export const MATERIALIZED_TENANT_DINEIN_ENTRIES_SCHEMA_VERSION = 1;
16
+ export const MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION = 1;
17
+ export const MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_SCHEMA_VERSION = 1;
@@ -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);
@@ -31,8 +31,8 @@ export const WORKER_ENDPOINTS = {
31
31
  REMOVE_DINEIN_TABLE: '/remove-dinein-table',
32
32
  // ========================>> Modifiers / Catalog <<======================= //
33
33
  UPDATE_MODIFIER_NAME_IN_ITEMS: '/update-modifier-name-in-items',
34
- ITEMS_USING_MODIFIER: '/items-using-modifier',
35
- AUDIT_ITEM_MODIFIERS: '/audit-item-modifiers',
34
+ // The two catalog reads that used to live here (items-using-modifier and
35
+ // audit-item-modifiers) are OWNED by items-service now and run locally.
36
36
  // ========================>> Payment (SwitchPay) <<======================= //
37
37
  PROCESS_SWITCHPAY_PAYMENT_FOR_ORDER: '/process-switchpay-payment-for-order',
38
38
  COMMIT_SWITCHPAY_PAYMENT_FOR_ORDER: '/commit-switchpay-payment-for-order',
@@ -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';
@@ -68,4 +69,7 @@ export * from './tenant-dinein-formatter';
68
69
  export * from './tenant-prep-time-formatter';
69
70
  export * from './tenant-sales-overview-formatter';
70
71
  export * from './tenant-sales-overview-helpers';
72
+ export * from './tenant-sales-overview-bson';
71
73
  export * from './report-platform';
74
+ // Worker-only PURE compile surface (also importable via the deep lib path).
75
+ export * as reportCompile from './report-compile';
@@ -1,7 +1,7 @@
1
- import mongoose from 'mongoose';
1
+ import ObjectID from 'bson-objectid';
2
2
  import { PAYMENT_TYPE_ENUM, REPORT_SALE_TYPE_ENUM } from '../../constants';
3
3
  import { ALL_MATERIALIZED_REPORT_SCOPES, MATERIALIZED_REPORT_CREDIT_BILL_EVENT_TYPE_ENUM, MATERIALIZED_REPORT_SCOPE_ENUM, SALES_OVERVIEW_KEY_FILTERS, } from '../../constants/MATERIALIZED_REPORTS';
4
- import { MATERIALIZED_CREDIT_BILLS_SCHEMA_VERSION } from '../../schemas/models/materialized_credit_bills';
4
+ import { MATERIALIZED_CREDIT_BILLS_SCHEMA_VERSION } from '../../constants/MATERIALIZED_SCHEMA_VERSIONS';
5
5
  import { getLocalDocumentFields, getLocalFieldsFromLocalDate } from '../utils/time-helpers';
6
6
  import { buildMaterializedReportKey } from './key-helpers';
7
7
  import { materializedDayInputSignatureChanged } from './materialized-day-input-signature';
@@ -521,7 +521,7 @@ export function buildMaterializedCreditBillDayReports(input) {
521
521
  return [];
522
522
  const sourceWatermark = watermark ?? computedAt;
523
523
  const makeDoc = (scopeContext, scopeField, sections) => ({
524
- _id: new mongoose.Types.ObjectId().toHexString(),
524
+ _id: new ObjectID().toHexString(),
525
525
  tenant_id: tenantId,
526
526
  brand_id: brandId,
527
527
  scope_context: scopeContext,
@@ -1,6 +1,6 @@
1
- import mongoose from 'mongoose';
1
+ import ObjectID from 'bson-objectid';
2
2
  import { DELIVERY_KEY_FILTERS, MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DELIVERY_REPORT_MAX_BSON_BYTES, MATERIALIZED_DELIVERY_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
3
- import { MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_delivery_reports';
3
+ import { MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION } from '../../constants/MATERIALIZED_SCHEMA_VERSIONS';
4
4
  import { mergeMaterializedDeliveryCounters } from './materialized-delivery-accumulator-helpers';
5
5
  import { freshDeliveryCounters } from './materialized-delivery-classification-helpers';
6
6
  import { pruneDocumentDeliveryAreas, pruneDocumentDeliveryRiders } from './materialized-delivery-compile-helpers';
@@ -154,7 +154,7 @@ export function buildTenantDeliveryCloseDayPlan(input) {
154
154
  const existing = storedReportsByScope.get(deliveryScopeKey(compiledDateDoc));
155
155
  const next = {
156
156
  ...compiledDateDoc,
157
- _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
157
+ _id: existing?._id ?? new ObjectID().toHexString(),
158
158
  iteration: Number(existing?.iteration ?? 0) + 1,
159
159
  };
160
160
  if (force || needsMaterializedDeliveryReportWrite(existing, next))
@@ -236,7 +236,7 @@ export function buildTenantDeliveryCloseDayPlan(input) {
236
236
  counterRows.push({ key, counters: folded[key] });
237
237
  }
238
238
  const report = {
239
- _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
239
+ _id: existing?._id ?? new ObjectID().toHexString(),
240
240
  tenant_id: tenantId,
241
241
  brand_id: existing?.brand_id ?? compiledDateDoc?.brand_id ?? '',
242
242
  scope_context: scope,
@@ -275,7 +275,7 @@ export function buildTenantDeliveryCloseDayPlan(input) {
275
275
  const existing = storedEntriesByPage.get(key) ?? null;
276
276
  const next = {
277
277
  ...compiled,
278
- _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
278
+ _id: existing?._id ?? new ObjectID().toHexString(),
279
279
  iteration: Number(existing?.iteration ?? 0) + 1,
280
280
  };
281
281
  if (existing == null || force || needsMaterializedDeliveryEntriesWrite(existing, next)) {
@@ -1,8 +1,8 @@
1
1
  import { ALL_DAY_INDEX } from '../../constants/ENUMS';
2
2
  import { DELIVERY_KEY_FILTERS, MATERIALIZED_DELIVERY_AREA_LIMIT, MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DELIVERY_ENTRY_SCOPES, MATERIALIZED_DELIVERY_OTHER_TOKEN, MATERIALIZED_DELIVERY_REPORT_MAX_BSON_BYTES, MATERIALIZED_DELIVERY_RIDER_LIMIT, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
3
3
  import { DELIVERY_STATUS_ENUM, RIDER_EARNINGS_STATUS_ENUM } from '../../constants/enums/delivery';
4
- import { MATERIALIZED_TENANT_DELIVERY_ENTRIES_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_delivery_entries';
5
- import { MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_delivery_reports';
4
+ import { MATERIALIZED_TENANT_DELIVERY_ENTRIES_SCHEMA_VERSION } from '../../constants/MATERIALIZED_SCHEMA_VERSIONS';
5
+ import { MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION } from '../../constants/MATERIALIZED_SCHEMA_VERSIONS';
6
6
  import { mergeMaterializedDeliveryCounters } from './materialized-delivery-accumulator-helpers';
7
7
  import { addDelivery, addDeliveryOrder, buildMaterializedDeliveryKey, DELIVERY_UNKNOWN_TOKEN, finalizeDeliveryAccumulator, freshDeliveryAccumulator, freshDeliveryCounters, isDeliveryOrder, MATERIALIZED_DELIVERY_KEY_SEPARATOR, parseMaterializedDeliveryKey, } from './materialized-delivery-classification-helpers';
8
8
  import { deliveryEntryPageOf, splitMaterializedDeliveryEntryPage } from './materialized-delivery-entry-pages';
@@ -1,6 +1,6 @@
1
- import mongoose from 'mongoose';
1
+ import ObjectID from 'bson-objectid';
2
2
  import { DIALER_KEY_FILTERS, MATERIALIZED_DIALER_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DIALER_REPORT_MAX_BSON_BYTES, MATERIALIZED_DIALER_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
3
- import { MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_dialer_reports';
3
+ import { MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION } from '../../constants/MATERIALIZED_SCHEMA_VERSIONS';
4
4
  import { freshDialerCounters, mergeDialerCounters } from './materialized-dialer-accumulator-helpers';
5
5
  import { dialerEntryPageOf, materializedDialerEntryPageKey } from './materialized-dialer-entry-pages';
6
6
  import { assertMaterializedDocumentSize } from './materialized-document-size';
@@ -154,7 +154,7 @@ export function buildTenantDialerCloseDayPlan(input) {
154
154
  const existing = storedReportsByScope.get(dialerScopeKey(compiledDateDoc));
155
155
  const next = {
156
156
  ...compiledDateDoc,
157
- _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
157
+ _id: existing?._id ?? new ObjectID().toHexString(),
158
158
  iteration: Number(existing?.iteration ?? 0) + 1,
159
159
  };
160
160
  if (force || needsMaterializedDialerReportWrite(existing, next))
@@ -227,7 +227,7 @@ export function buildTenantDialerCloseDayPlan(input) {
227
227
  for (const key of keys)
228
228
  counters[key] = folded[key];
229
229
  const report = {
230
- _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
230
+ _id: existing?._id ?? new ObjectID().toHexString(),
231
231
  tenant_id: tenantId,
232
232
  brand_id: existing?.brand_id ?? compiledDateDoc?.brand_id ?? '',
233
233
  scope_context: scope,
@@ -259,7 +259,7 @@ export function buildTenantDialerCloseDayPlan(input) {
259
259
  const existing = storedEntriesByPage.get(key) ?? null;
260
260
  const next = {
261
261
  ...compiled,
262
- _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
262
+ _id: existing?._id ?? new ObjectID().toHexString(),
263
263
  iteration: Number(existing?.iteration ?? 0) + 1,
264
264
  };
265
265
  if (existing == null || force || needsMaterializedDialerEntriesWrite(existing, next)) {
@@ -1,6 +1,6 @@
1
1
  import { MATERIALIZED_DIALER_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DIALER_ENTRY_SCOPES, MATERIALIZED_DIALER_REPORT_MAX_BSON_BYTES, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
2
- import { MATERIALIZED_TENANT_DIALER_ENTRIES_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_dialer_entries';
3
- import { MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION as REPORT_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_dialer_reports';
2
+ import { MATERIALIZED_TENANT_DIALER_ENTRIES_SCHEMA_VERSION } from '../../constants/MATERIALIZED_SCHEMA_VERSIONS';
3
+ import { MATERIALIZED_TENANT_DIALER_REPORTS_SCHEMA_VERSION as REPORT_SCHEMA_VERSION } from '../../constants/MATERIALIZED_SCHEMA_VERSIONS';
4
4
  import { MATERIALIZED_DIALER_REPORT_KEY_FILTERS, } from '../../schemas/types/materialized_tenant_dialer_reports';
5
5
  import { addDialerEvent, dialerHourKey, freshDialerCounters } from './materialized-dialer-accumulator-helpers';
6
6
  import { dialerEntryPageOf, splitMaterializedDialerEntryPage } from './materialized-dialer-entry-pages';
@@ -1,6 +1,6 @@
1
- import mongoose from 'mongoose';
1
+ import ObjectID from 'bson-objectid';
2
2
  import { DINEIN_KEY_FILTERS, MATERIALIZED_DINEIN_COURSE_LIMIT, MATERIALIZED_DINEIN_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DINEIN_FLOOR_LIMIT, MATERIALIZED_DINEIN_REPORT_MAX_BSON_BYTES, MATERIALIZED_DINEIN_REPORT_SCOPES, MATERIALIZED_DINEIN_SERVICE_CHARGE_RATE_LIMIT, MATERIALIZED_DINEIN_TABLE_LIMIT, MATERIALIZED_DINEIN_TOP_EDITED_CHECK_LIMIT, MATERIALIZED_DINEIN_UPCOMING_RESERVATION_LIMIT, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
3
- import { MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_dinein_reports';
3
+ import { MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION } from '../../constants/MATERIALIZED_SCHEMA_VERSIONS';
4
4
  import { freshMaterializedDineinCounters, freshMaterializedDineinReservationCounters, mergeMaterializedDineinCounters, mergeMaterializedDineinReservationCounters, mergeMaterializedDineinTopEditedChecks, mergeMaterializedDineinUpcomingReservations, pruneDocumentDineinMaps, } from './materialized-dinein-accumulator-helpers';
5
5
  import { assertMaterializedDocumentSize } from './materialized-document-size';
6
6
  import { dineinEntryPageOf, materializedDineinEntryPageKey } from './materialized-dinein-entry-pages';
@@ -171,7 +171,7 @@ export function buildTenantDineinCloseDayPlan(input) {
171
171
  const existing = storedReportsByScope.get(dineinScopeKey(compiledDateDoc));
172
172
  const next = {
173
173
  ...compiledDateDoc,
174
- _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
174
+ _id: existing?._id ?? new ObjectID().toHexString(),
175
175
  iteration: Number(existing?.iteration ?? 0) + 1,
176
176
  };
177
177
  if (force || needsMaterializedDineinReportWrite(existing, next))
@@ -259,7 +259,7 @@ export function buildTenantDineinCloseDayPlan(input) {
259
259
  for (const key of keys)
260
260
  counters[key] = folded.counters[key];
261
261
  const report = {
262
- _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
262
+ _id: existing?._id ?? new ObjectID().toHexString(),
263
263
  tenant_id: tenantId,
264
264
  brand_id: existing?.brand_id ?? compiledDateDoc?.brand_id ?? '',
265
265
  scope_context: scope,
@@ -297,7 +297,7 @@ export function buildTenantDineinCloseDayPlan(input) {
297
297
  const existing = storedEntriesByPage.get(key) ?? null;
298
298
  const next = {
299
299
  ...compiled,
300
- _id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
300
+ _id: existing?._id ?? new ObjectID().toHexString(),
301
301
  iteration: Number(existing?.iteration ?? 0) + 1,
302
302
  };
303
303
  if (existing == null || force || needsMaterializedDineinEntriesWrite(existing, next)) {
@@ -1,6 +1,6 @@
1
1
  import { MATERIALIZED_DINEIN_ENTRY_SCOPES, MATERIALIZED_DINEIN_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DINEIN_HOUR_KEYS, MATERIALIZED_DINEIN_REPORT_MAX_BSON_BYTES, MATERIALIZED_DINEIN_TOP_EDITED_CHECK_LIMIT, MATERIALIZED_DINEIN_UNKNOWN_TOKEN, MATERIALIZED_DINEIN_UPCOMING_RESERVATION_LIMIT, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
2
- import { MATERIALIZED_TENANT_DINEIN_ENTRIES_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_dinein_entries';
3
- import { MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_dinein_reports';
2
+ import { MATERIALIZED_TENANT_DINEIN_ENTRIES_SCHEMA_VERSION } from '../../constants/MATERIALIZED_SCHEMA_VERSIONS';
3
+ import { MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION } from '../../constants/MATERIALIZED_SCHEMA_VERSIONS';
4
4
  import { MATERIALIZED_DINEIN_REPORT_KEY_FILTERS, } from '../../schemas/types/materialized_tenant_dinein_reports';
5
5
  import { freshMaterializedDineinCounters, freshMaterializedDineinReservationCounters, mergeMaterializedDineinTopEditedChecks, mergeMaterializedDineinUpcomingReservations, pruneDocumentDineinMaps, } from './materialized-dinein-accumulator-helpers';
6
6
  import { addDineinOrderToCounters, classifyMaterializedDineinOrigin, freshDineinCoverage, isDeletedSplitParent, isDineinOrder, itemLineDelta, itemQuantityOf, splitRoleOf, toDineinDate as toDate, toDineinInt as num, validRatingOf, } from './materialized-dinein-classification-helpers';
@@ -1,4 +1,4 @@
1
- import mongoose from 'mongoose';
1
+ import ObjectID from 'bson-objectid';
2
2
  import { MATERIALIZED_DISCOUNT_ENTRY_PAGE_ALL, MATERIALIZED_DISCOUNT_REPORT_SCOPES, MATERIALIZED_REPORT_KEY_FILTER_ENUM, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
3
3
  import { accumulateOrderDiscounts, toDiscountHourLabel, } from './materialized-discount-accumulator-helpers';
4
4
  import { createEmptyDiscountOrderCounters } from './materialized-discount-classification-helpers';
@@ -156,7 +156,7 @@ export function compileTenantDiscountReports(input) {
156
156
  const docWatermark = watermark ?? sourceWatermark;
157
157
  const isDateScoped = scope === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE || scope === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR;
158
158
  reports.push({
159
- _id: new mongoose.Types.ObjectId().toHexString(),
159
+ _id: new ObjectID().toHexString(),
160
160
  tenant_id: tenantId,
161
161
  brand_id: brandId,
162
162
  scope_context: scope,
@@ -179,7 +179,7 @@ export function compileTenantDiscountReports(input) {
179
179
  // Entries documents exist only when there is at least one row.
180
180
  if (Object.keys(entrySections).length > 0) {
181
181
  entriesDocs.push({
182
- _id: new mongoose.Types.ObjectId().toHexString(),
182
+ _id: new ObjectID().toHexString(),
183
183
  tenant_id: tenantId,
184
184
  brand_id: brandId,
185
185
  scope_context: scope,
@@ -1,4 +1,4 @@
1
- import mongoose from 'mongoose';
1
+ import { calculateObjectSize } from 'bson';
2
2
  /**
3
3
  * The BSON SIZE BUDGET of a materialized report/entry/geo document.
4
4
  *
@@ -31,7 +31,7 @@ import mongoose from 'mongoose';
31
31
  export function materializedDocumentBsonBytes(document) {
32
32
  if (document == null)
33
33
  return 0;
34
- return mongoose.mongo.BSON.calculateObjectSize(document);
34
+ return calculateObjectSize(document);
35
35
  }
36
36
  /**
37
37
  * Throws when ONE document exceeds its budget.
@@ -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,
@@ -1,4 +1,4 @@
1
- import mongoose from 'mongoose';
1
+ import ObjectID from 'bson-objectid';
2
2
  import { MATERIALIZED_PREP_TIME_ITEM_SCOPES, MATERIALIZED_PREP_TIME_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
3
3
  import { freshMaterializedPrepTimeCounters } from './materialized-prep-time-classification-helpers';
4
4
  import { mergeMaterializedPrepTimeCounters } from './materialized-prep-time-compile-helpers';
@@ -359,7 +359,7 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
359
359
  const report = {
360
360
  // A period document created by the fold gets a fresh id; an existing one keeps
361
361
  // its identity so the upsert targets the same row.
362
- _id: existingDoc?._id ?? new mongoose.Types.ObjectId().toHexString(),
362
+ _id: existingDoc?._id ?? new ObjectID().toHexString(),
363
363
  tenant_id: tenantId,
364
364
  brand_id: existingDoc?.brand_id ?? reports[0]?.brand_id ?? '',
365
365
  scope_context: scope,
@@ -470,7 +470,7 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
470
470
  }
471
471
  }
472
472
  const report = {
473
- _id: existingDoc?._id ?? new mongoose.Types.ObjectId().toHexString(),
473
+ _id: existingDoc?._id ?? new ObjectID().toHexString(),
474
474
  tenant_id: tenantId,
475
475
  brand_id: existingDoc?.brand_id ?? entries[0]?.brand_id ?? '',
476
476
  scope_context: scope,
@@ -1,4 +1,4 @@
1
- import mongoose from 'mongoose';
1
+ import ObjectID from 'bson-objectid';
2
2
  import { MATERIALIZED_PREP_TIME_ITEM_SCOPES, MATERIALIZED_PREP_TIME_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, PREP_TIME_KEY_FILTERS, } from '../../constants/MATERIALIZED_REPORTS';
3
3
  import { accumulateOrderPrepTime, freshMaterializedPrepTimeCounters, } from './materialized-prep-time-classification-helpers';
4
4
  import { getDayIndexFromLocalDate, getMaterializedPeriodScopeFields } from './scope-helpers';
@@ -169,7 +169,7 @@ export function compileTenantPrepTimeReports(input) {
169
169
  const docWatermark = watermark ?? sourceWatermark;
170
170
  const isDateScoped = scopeContext === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
171
171
  reports.push({
172
- _id: new mongoose.Types.ObjectId().toHexString(),
172
+ _id: new ObjectID().toHexString(),
173
173
  tenant_id: tenantId,
174
174
  brand_id: brandId,
175
175
  scope_context: scopeContext,
@@ -187,7 +187,7 @@ export function compileTenantPrepTimeReports(input) {
187
187
  });
188
188
  if (itemScopes.has(scopeContext) && Object.keys(entrySections).length > 0) {
189
189
  entries.push({
190
- _id: new mongoose.Types.ObjectId().toHexString(),
190
+ _id: new ObjectID().toHexString(),
191
191
  tenant_id: tenantId,
192
192
  brand_id: brandId,
193
193
  scope_context: scopeContext,
@@ -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
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * PURE materialized-report COMPILE entrypoint.
3
+ *
4
+ * This module is the workers-service Piscina worker's toolkit surface. Its
5
+ * transitive import graph contains NO mongoose, NO schema model, NO DB
6
+ * initializer and NO model registration: every close-day plan/compile helper
7
+ * was moved to pure modules (schema-version constants live in
8
+ * `constants/MATERIALIZED_SCHEMA_VERSIONS`, ObjectIds are generated with
9
+ * `bson-objectid`, document size uses the `bson` calculator).
10
+ *
11
+ * Import the ROOT toolkit barrel for the service-facing API; import THIS module
12
+ * (or its built `lib/helpers/materialized-reports/report-compile.js` path) from
13
+ * a worker.
14
+ */
15
+ export { MATERIALIZED_DINEIN_ORIGIN_ENUM, MATERIALIZED_DINEIN_REPORT_SCOPES, MATERIALIZED_DINEIN_SPLIT_PARENT_DELETE_REASON, MATERIALIZED_DINEIN_UNKNOWN_TOKEN, MATERIALIZED_PREP_TIME_ITEM_SCOPES, MATERIALIZED_PREP_TIME_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
16
+ export { MATERIALIZED_CREDIT_BILLS_SCHEMA_VERSION, MATERIALIZED_TENANT_DINEIN_ENTRIES_SCHEMA_VERSION, MATERIALIZED_TENANT_DINEIN_REPORTS_SCHEMA_VERSION, } from '../../constants/MATERIALIZED_SCHEMA_VERSIONS';
17
+ export { SALES_OVERVIEW_KEY_FILTERS } from '../../constants/MATERIALIZED_REPORTS';
18
+ export { REPORT_SALE_TYPE_ENUM } from '../../constants/ANALYTICS';
19
+ export { compileTenantPrepTimeReports } from './materialized-prep-time-compile-helpers';
20
+ export { buildTenantPrepTimeCloseDayPlan, getMaterializedPrepTimePeriodDayScopeFields, getMaterializedPrepTimeReportPeriodDayScopeFields, } from './materialized-prep-time-close-day-helpers';
21
+ export { compileTenantDiscountReports } from './materialized-discount-compile-helpers';
22
+ export { buildTenantDiscountCloseDayPlan } from './materialized-discount-close-day-helpers';
23
+ export { compileTenantDeliveryReports } from './materialized-delivery-compile-helpers';
24
+ export { buildTenantDeliveryCloseDayPlan } from './materialized-delivery-close-day-helpers';
25
+ export { compileTenantDeliveryGeoCellsWithStats } from './materialized-tenant-delivery-geo-helpers';
26
+ export { compileTenantDialerReports } from './materialized-dialer-compile-helpers';
27
+ export { buildTenantDialerCloseDayPlan } from './materialized-dialer-close-day-helpers';
28
+ export { compileTenantDineinReports } from './materialized-dinein-compile-helpers';
29
+ export { buildTenantDineinCloseDayPlan, foldMaterializedDineinPeriodCounters, } from './materialized-dinein-close-day-helpers';
30
+ export { pruneDocumentDineinMaps } from './materialized-dinein-accumulator-helpers';
31
+ export { dineinKeyPassesFilters } from './materialized-dinein-key-helpers';
32
+ export { mergeMaterializedDineinEntryPages } from './materialized-dinein-entry-pages';
33
+ export { getMaterializedPeriodScopeFields, parseMaterializedLocalDate } from './scope-helpers';
34
+ export { REPORT_KEY_ENUM, REPORT_MODE_ENUM, buildReportDetailsResponse, buildReportMeta, buildReportResponse, } from '../reporting-contract/report-contract';
35
+ export { convertOrdersToMaterializedTenantSalesOverviewReports, sumMaterializedTenantSalesOverviewReports, } from './tenant-sales-overview-helpers';
36
+ export { buildTenantSalesOverviewCloseDayPlan } from './tenant-sales-overview-close-day-helpers';
37
+ export { applyCreditResolutionToReportDocuments, buildMaterializedCreditBillSelectionFromScopeGroups, computeMaterializedCreditBillTotalsFromReportsByKey, getMaterializedCreditBillScopeRefsForDay, getMaterializedCreditBillScopeRefsForIssue, resolveCreditBillDay, } from './materialized-credit-bills-helpers';
38
+ export { normalizeOverviewAggregatedShape } from '../reporting-contract/overview-contract';