@managesome/eposmatic-toolkit 0.4.28 → 0.4.30

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 (89) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +9 -12
  2. package/esm/helpers/materialized-reports/index.js +2 -0
  3. package/esm/helpers/materialized-reports/materialized-credit-bills-helpers.js +178 -40
  4. package/esm/helpers/materialized-reports/materialized-day-input-signature.js +103 -0
  5. package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +10 -3
  6. package/esm/helpers/materialized-reports/materialized-discount-close-day-helpers.js +299 -227
  7. package/esm/helpers/materialized-reports/materialized-discount-compile-helpers.js +20 -1
  8. package/esm/helpers/materialized-reports/materialized-discount-source-helpers.js +176 -0
  9. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +64 -0
  10. package/esm/helpers/materialized-reports/materialized-integer-contract.js +30 -4
  11. package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +4 -2
  12. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +7 -0
  13. package/esm/helpers/materialized-reports/scope-helpers.js +27 -0
  14. package/esm/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +400 -46
  15. package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +28 -6
  16. package/esm/schemas/models/materialized_credit_bills.js +8 -0
  17. package/esm/schemas/models/materialized_report_drilldown_rows.js +39 -12
  18. package/esm/schemas/models/materialized_tenant_discount_entries.js +18 -12
  19. package/esm/schemas/models/materialized_tenant_discount_reports.js +9 -0
  20. package/esm/schemas/models/materialized_tenant_discount_sources.js +1 -1
  21. package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +9 -0
  22. package/esm/schemas/models/orders.js +5 -0
  23. package/esm/schemas/models/tenant_active_jobs.js +8 -0
  24. package/esm/schemas/models/tenant_calendar.js +12 -0
  25. package/lib/constants/MATERIALIZED_REPORTS.d.ts +9 -12
  26. package/lib/constants/MATERIALIZED_REPORTS.js +11 -14
  27. package/lib/helpers/materialized-reports/index.d.ts +2 -0
  28. package/lib/helpers/materialized-reports/index.js +2 -0
  29. package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.d.ts +66 -2
  30. package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.js +179 -39
  31. package/lib/helpers/materialized-reports/materialized-day-input-signature.d.ts +66 -0
  32. package/lib/helpers/materialized-reports/materialized-day-input-signature.js +108 -0
  33. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +10 -3
  34. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +41 -60
  35. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +298 -226
  36. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.d.ts +4 -4
  37. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +19 -0
  38. package/lib/helpers/materialized-reports/materialized-discount-source-helpers.d.ts +17 -0
  39. package/lib/helpers/materialized-reports/materialized-discount-source-helpers.js +178 -0
  40. package/lib/helpers/materialized-reports/materialized-index-bootstrap.d.ts +7 -0
  41. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +68 -0
  42. package/lib/helpers/materialized-reports/materialized-integer-contract.d.ts +3 -1
  43. package/lib/helpers/materialized-reports/materialized-integer-contract.js +30 -4
  44. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.d.ts +2 -2
  45. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +4 -2
  46. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +7 -0
  47. package/lib/helpers/materialized-reports/scope-helpers.d.ts +22 -0
  48. package/lib/helpers/materialized-reports/scope-helpers.js +28 -0
  49. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.d.ts +49 -3
  50. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +398 -44
  51. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.d.ts +4 -1
  52. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +28 -6
  53. package/lib/schemas/models/materialized_credit_bills.d.ts +3 -0
  54. package/lib/schemas/models/materialized_credit_bills.js +8 -0
  55. package/lib/schemas/models/materialized_report_drilldown_rows.d.ts +40 -6
  56. package/lib/schemas/models/materialized_report_drilldown_rows.js +39 -12
  57. package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +33 -29
  58. package/lib/schemas/models/materialized_tenant_discount_entries.js +18 -12
  59. package/lib/schemas/models/materialized_tenant_discount_reports.d.ts +3 -0
  60. package/lib/schemas/models/materialized_tenant_discount_reports.js +9 -0
  61. package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +6 -6
  62. package/lib/schemas/models/materialized_tenant_discount_sources.js +1 -1
  63. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +3 -0
  64. package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +9 -0
  65. package/lib/schemas/models/orders.d.ts +5 -0
  66. package/lib/schemas/models/orders.js +5 -0
  67. package/lib/schemas/models/tenant_active_jobs.d.ts +3 -0
  68. package/lib/schemas/models/tenant_active_jobs.js +8 -0
  69. package/lib/schemas/models/tenant_calendar.d.ts +13 -0
  70. package/lib/schemas/models/tenant_calendar.js +13 -1
  71. package/lib/schemas/types/materialized_credit_bills.d.ts +6 -0
  72. package/lib/schemas/types/materialized_report_drilldown_rows.d.ts +8 -0
  73. package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +9 -5
  74. package/lib/schemas/types/materialized_tenant_discount_reports.d.ts +6 -0
  75. package/lib/schemas/types/materialized_tenant_reports_iterations.d.ts +9 -0
  76. package/lib/schemas/types/materialized_tenant_sales_overview_reports.d.ts +14 -0
  77. package/lib/schemas/types/tenant_active_jobs.d.ts +8 -0
  78. package/lib/tests/materialized-reports/materialized-credit-bills-helpers.test.js +56 -1
  79. package/lib/tests/materialized-reports/materialized-day-input-signature.test.d.ts +1 -0
  80. package/lib/tests/materialized-reports/materialized-day-input-signature.test.js +81 -0
  81. package/lib/tests/materialized-reports/materialized-discount-classification.test.js +17 -0
  82. package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +148 -53
  83. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.js +14 -13
  84. package/lib/tests/materialized-reports/materialized-discount-sources.test.js +76 -0
  85. package/lib/tests/materialized-reports/materialized-integer-contract.test.js +11 -0
  86. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.js +2 -2
  87. package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.js +311 -4
  88. package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +89 -0
  89. package/package.json +1 -1
@@ -18,6 +18,7 @@ const constants_1 = require("../../constants");
18
18
  const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
19
19
  const materialized_tenant_sales_overview_reports_1 = require("../../schemas/models/materialized_tenant_sales_overview_reports");
20
20
  const key_helpers_1 = require("./key-helpers");
21
+ const materialized_day_input_signature_1 = require("./materialized-day-input-signature");
21
22
  const materialized_credit_bills_helpers_1 = require("./materialized-credit-bills-helpers");
22
23
  Object.defineProperty(exports, "isMaterializedCreditBillPayment", { enumerable: true, get: function () { return materialized_credit_bills_helpers_1.isMaterializedCreditBillPayment; } });
23
24
  const scope_helpers_1 = require("./scope-helpers");
@@ -50,16 +51,16 @@ function isPaymentIncludedInMaterializedSalesOverview(payment) {
50
51
  function getMaterializedPaymentGross(payment) {
51
52
  const reportedType = normalizeMaterializedPaymentType(payment.payment_type);
52
53
  const isUnpaid = reportedType === constants_1.PAYMENT_TYPE_ENUM.UNPAID;
53
- // Money is integer CENTS: the result is rounded to the cent at this
54
- // boundary, because it feeds a materialized document.
54
+ // Money is integer CENTS. Do NOT round: a fractional source amount must fail
55
+ // the materialized-write integer gate, not be silently changed here.
55
56
  if ((payment.amount_refunded ?? 0) === 0) {
56
57
  let gross = isUnpaid ? (payment.amount_due ?? 0) : Math.min(payment.amount_paid ?? 0, payment.amount_due ?? 0);
57
58
  if (payment.card_details != null) {
58
59
  gross -= payment.card_details.card_charges ?? 0;
59
60
  }
60
- return Math.round(gross);
61
+ return gross;
61
62
  }
62
- return -Math.round(isUnpaid ? (payment.amount_due ?? 0) : (payment.amount_refunded ?? 0));
63
+ return -(isUnpaid ? (payment.amount_due ?? 0) : (payment.amount_refunded ?? 0));
63
64
  }
64
65
  const createEmptyOrderMetric = () => ({
65
66
  count: 0,
@@ -519,13 +520,26 @@ function convertOrdersToMaterializedTenantSalesOverviewReports(localDate, orders
519
520
  // `source_watermark` = max `order.updatedAt` folded in; a day with no
520
521
  // orders folds nothing → epoch. Stable across recompiles, so the
521
522
  // watermark guard skips unchanged (order-less) days instead of churning.
522
- const watermark = tenantOrders.reduce((latest, order) => {
523
+ //
524
+ // `source_input_signature` is the SET-shaped companion: the watermark only
525
+ // advances, so a delete / move that removes the maximum-`updatedAt` order
526
+ // would leave the incoming watermark LOWER than the stored one and the
527
+ // guard would skip the correction. The digest changes with the input set,
528
+ // so the close is still applied. Both are computed from the SAME
529
+ // `tenantOrders` array the compile consumed.
530
+ // Both change-detection signals describe the orders this day ACTUALLY
531
+ // materializes: a deleted order must never keep the day "fresh" (or
532
+ // contribute a watermark the remaining orders cannot reproduce).
533
+ const consideredOrders = tenantOrders.filter((order) => order.is_deleted === false);
534
+ const watermark = consideredOrders.reduce((latest, order) => {
523
535
  const updatedAt = new Date(order.updatedAt ?? now);
524
536
  return updatedAt.getTime() > latest.getTime() ? updatedAt : latest;
525
537
  }, new Date(0));
538
+ const sourceInputSignature = (0, materialized_day_input_signature_1.buildMaterializedDayInputSignature)(consideredOrders) ?? undefined;
526
539
  for (const { doc, metrics } of [...periodDocs, ...hourDocs.values()]) {
527
540
  doc.sale_type_metrics = finalizeMetrics(metrics);
528
541
  doc.source_watermark = watermark;
542
+ doc.source_input_signature = sourceInputSignature;
529
543
  materializedReports.push(doc);
530
544
  }
531
545
  }
@@ -749,7 +763,10 @@ function subtractMaterializedTenantSalesOverviewReports(minuend, subtrahend) {
749
763
  * so replays stay churn-free;
750
764
  * 4. `source_watermark` advances to the max of the existing and the incoming
751
765
  * (`replacement` / `additions`) watermarks — monotonic, never regressed
752
- * (rollbacks / replays keep the previously folded maximum).
766
+ * (rollbacks / replays keep the previously folded maximum);
767
+ * 5. `source_input_signature` follows a `replacement` (the fresh day compile)
768
+ * and is KEPT on period accumulates — a period's payload is a running sum,
769
+ * not one day's input set.
753
770
  *
754
771
  * Returns a new JSON report; the input is not mutated.
755
772
  */
@@ -807,6 +824,11 @@ function updateMaterializedTenantSalesOverviewReport(existing, updates, options
807
824
  // Reconcile artifacts: a fully-subtracted key is pruned so accumulated
808
825
  // period documents keep the same key set as day / hour reads.
809
826
  sale_type_metrics: pruneZeroReportKeys(finalizeMetrics(metrics)),
827
+ // The day-input digest travels with a REPLACEMENT write (the freshly
828
+ // compiled day/hour document) so the next close can compare it. Period
829
+ // accumulates keep the stored value: their payload is a running sum, not
830
+ // one day's input set.
831
+ source_input_signature: updates.replacement?.source_input_signature ?? existing.source_input_signature,
810
832
  // Version metric writes AND ledger-changing received-payment patches (both
811
833
  // append a history row via the caller). No-op patches bump nothing, so
812
834
  // replays stay churn-free.
@@ -109,6 +109,7 @@ export declare const MaterializedTenantCreditBillsReportSchema: Schema<any, impo
109
109
  schema_version: number;
110
110
  source_watermark: NativeDate;
111
111
  computed_at: NativeDate;
112
+ source_input_signature?: string | null | undefined;
112
113
  }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
113
114
  createdAt: NativeDate;
114
115
  updatedAt: NativeDate;
@@ -196,6 +197,7 @@ export declare const MaterializedTenantCreditBillsReportSchema: Schema<any, impo
196
197
  schema_version: number;
197
198
  source_watermark: NativeDate;
198
199
  computed_at: NativeDate;
200
+ source_input_signature?: string | null | undefined;
199
201
  }>> & import("mongoose").FlatRecord<{
200
202
  createdAt: NativeDate;
201
203
  updatedAt: NativeDate;
@@ -283,6 +285,7 @@ export declare const MaterializedTenantCreditBillsReportSchema: Schema<any, impo
283
285
  schema_version: number;
284
286
  source_watermark: NativeDate;
285
287
  computed_at: NativeDate;
288
+ source_input_signature?: string | null | undefined;
286
289
  }> & Required<{
287
290
  _id: import("mongoose").Types.ObjectId;
288
291
  }> & {
@@ -204,6 +204,14 @@ exports.MaterializedTenantCreditBillsReportSchema = new mongoose_1.Schema({
204
204
  type: mongoose_1.Schema.Types.Date,
205
205
  required: true,
206
206
  },
207
+ // Day-input digest (`v1:<count>:<hash>`) — set-shaped change detection
208
+ // for the credit ledger, mirroring the sales-overview reports. A deleted /
209
+ // moved order must remove its bill even when the watermark regresses.
210
+ source_input_signature: {
211
+ type: String,
212
+ required: false,
213
+ default: undefined,
214
+ },
207
215
  computed_at: {
208
216
  type: mongoose_1.Schema.Types.Date,
209
217
  required: true,
@@ -42,7 +42,7 @@ export declare const MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES: {
42
42
  readonly _id: {
43
43
  readonly _id: 1;
44
44
  };
45
- readonly identity: {
45
+ readonly tenant_id__report_type__scope_context__scope_field__order_id__cursor_id: {
46
46
  readonly tenant_id: 1;
47
47
  readonly report_type: 1;
48
48
  readonly scope_context: 1;
@@ -50,7 +50,7 @@ export declare const MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES: {
50
50
  readonly order_id: 1;
51
51
  readonly cursor_id: 1;
52
52
  };
53
- readonly read_asc: {
53
+ readonly tenant_id__report_type__scope_context__scope_field__time__order_id__cursor_id: {
54
54
  readonly tenant_id: 1;
55
55
  readonly report_type: 1;
56
56
  readonly scope_context: 1;
@@ -59,7 +59,7 @@ export declare const MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES: {
59
59
  readonly order_id: 1;
60
60
  readonly cursor_id: 1;
61
61
  };
62
- readonly read_desc: {
62
+ readonly tenant_id__report_type__scope_context__scope_field__time__cursor_id: {
63
63
  readonly tenant_id: 1;
64
64
  readonly report_type: 1;
65
65
  readonly scope_context: 1;
@@ -67,6 +67,13 @@ export declare const MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES: {
67
67
  readonly time: -1;
68
68
  readonly cursor_id: -1;
69
69
  };
70
+ readonly tenant_id__report_type__scope_context__scope_field__source_local_date: {
71
+ readonly tenant_id: 1;
72
+ readonly report_type: 1;
73
+ readonly scope_context: 1;
74
+ readonly scope_field: 1;
75
+ readonly source_local_date: 1;
76
+ };
70
77
  };
71
78
  export declare const MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS: ({
72
79
  key: {
@@ -102,19 +109,36 @@ export declare const MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS: ({
102
109
  };
103
110
  name: string;
104
111
  unique?: undefined;
112
+ } | {
113
+ key: {
114
+ readonly tenant_id: 1;
115
+ readonly report_type: 1;
116
+ readonly scope_context: 1;
117
+ readonly scope_field: 1;
118
+ readonly source_local_date: 1;
119
+ };
120
+ name: string;
121
+ unique?: undefined;
105
122
  })[];
106
123
  export declare const MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_INDEXES: {
107
124
  readonly _id: {
108
125
  readonly _id: 1;
109
126
  };
110
- readonly identity: {
127
+ readonly tenant_id__report_type__scope_context__scope_field: {
111
128
  readonly tenant_id: 1;
112
129
  readonly report_type: 1;
113
130
  readonly scope_context: 1;
114
131
  readonly scope_field: 1;
115
132
  };
133
+ readonly tenant_id__report_type__scope_context__scope_field__source_local_date: {
134
+ readonly tenant_id: 1;
135
+ readonly report_type: 1;
136
+ readonly scope_context: 1;
137
+ readonly scope_field: 1;
138
+ readonly source_local_date: 1;
139
+ };
116
140
  };
117
- export declare const MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS: {
141
+ export declare const MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS: ({
118
142
  key: {
119
143
  readonly tenant_id: 1;
120
144
  readonly report_type: 1;
@@ -123,7 +147,17 @@ export declare const MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS: {
123
147
  };
124
148
  name: string;
125
149
  unique: boolean;
126
- }[];
150
+ } | {
151
+ key: {
152
+ readonly tenant_id: 1;
153
+ readonly report_type: 1;
154
+ readonly scope_context: 1;
155
+ readonly scope_field: 1;
156
+ readonly source_local_date: 1;
157
+ };
158
+ name: string;
159
+ unique?: undefined;
160
+ })[];
127
161
  /** The report-type values the flat reader accepts. */
128
162
  export declare const ALL_DRILLDOWN_REPORT_TYPE_VALUES: import("../types/materialized_report_drilldown_rows").TDrilldownReportType[];
129
163
  /** Kept explicit so the JSON/demo upload can validate a file's report type. */
@@ -43,6 +43,9 @@ exports.MaterializedReportDrilldownRowSchema = new mongoose_1.Schema({
43
43
  // The original row is polymorphic across reports; the report_type
44
44
  // discriminator says which shape it is.
45
45
  data: { type: mongoose_1.Schema.Types.Mixed, required: true },
46
+ // Null only on legacy rows written before the incremental flat projection.
47
+ // The writer rebuilds a touched scope once and stamps every rebuilt row.
48
+ source_local_date: { type: String, default: null },
46
49
  schema_version: {
47
50
  type: Number,
48
51
  required: true,
@@ -69,6 +72,8 @@ exports.MaterializedReportDrilldownScopeSchema = new mongoose_1.Schema({
69
72
  scope_field: { type: String, required: true, immutable: true },
70
73
  row_count: { type: Number, required: true, min: 0, default: 0 },
71
74
  off_app_order_count: { type: Number, required: true, min: 0, default: 0 },
75
+ /** Last day whose close updated this scope (not an identity). */
76
+ source_local_date: { type: String, default: null },
72
77
  schema_version: {
73
78
  type: Number,
74
79
  required: true,
@@ -87,7 +92,7 @@ exports.MaterializedReportDrilldownScopeSchema = new mongoose_1.Schema({
87
92
  */
88
93
  exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES = {
89
94
  _id: { _id: 1 },
90
- identity: {
95
+ tenant_id__report_type__scope_context__scope_field__order_id__cursor_id: {
91
96
  tenant_id: 1,
92
97
  report_type: 1,
93
98
  scope_context: 1,
@@ -95,7 +100,7 @@ exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES = {
95
100
  order_id: 1,
96
101
  cursor_id: 1,
97
102
  },
98
- read_asc: {
103
+ tenant_id__report_type__scope_context__scope_field__time__order_id__cursor_id: {
99
104
  tenant_id: 1,
100
105
  report_type: 1,
101
106
  scope_context: 1,
@@ -104,7 +109,7 @@ exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES = {
104
109
  order_id: 1,
105
110
  cursor_id: 1,
106
111
  },
107
- read_desc: {
112
+ tenant_id__report_type__scope_context__scope_field__time__cursor_id: {
108
113
  tenant_id: 1,
109
114
  report_type: 1,
110
115
  scope_context: 1,
@@ -112,37 +117,59 @@ exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES = {
112
117
  time: -1,
113
118
  cursor_id: -1,
114
119
  },
120
+ tenant_id__report_type__scope_context__scope_field__source_local_date: {
121
+ tenant_id: 1,
122
+ report_type: 1,
123
+ scope_context: 1,
124
+ scope_field: 1,
125
+ source_local_date: 1,
126
+ },
115
127
  };
116
128
  exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS = [
117
129
  {
118
- key: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.identity,
119
- name: 'tenant_id__report_type__scope__order_id__cursor_id',
130
+ key: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.tenant_id__report_type__scope_context__scope_field__order_id__cursor_id,
131
+ name: 'tenant_id__report_type__scope_context__scope_field__order_id__cursor_id',
120
132
  unique: true,
121
133
  },
122
134
  {
123
- key: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.read_asc,
124
- name: 'tenant_id__report_type__scope__time_desc__order_id__cursor_id',
135
+ key: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.tenant_id__report_type__scope_context__scope_field__time__order_id__cursor_id,
136
+ name: 'tenant_id__report_type__scope_context__scope_field__time__order_id__cursor_id',
137
+ },
138
+ {
139
+ key: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.tenant_id__report_type__scope_context__scope_field__time__cursor_id,
140
+ name: 'tenant_id__report_type__scope_context__scope_field__time__cursor_id',
125
141
  },
126
142
  {
127
- key: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.read_desc,
128
- name: 'tenant_id__report_type__scope__time_desc__cursor_id_desc',
143
+ key: exports.MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_INDEXES.tenant_id__report_type__scope_context__scope_field__source_local_date,
144
+ name: 'tenant_id__report_type__scope_context__scope_field__source_local_date',
129
145
  },
130
146
  ];
131
147
  exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_INDEXES = {
132
148
  _id: { _id: 1 },
133
- identity: {
149
+ tenant_id__report_type__scope_context__scope_field: {
134
150
  tenant_id: 1,
135
151
  report_type: 1,
136
152
  scope_context: 1,
137
153
  scope_field: 1,
138
154
  },
155
+ tenant_id__report_type__scope_context__scope_field__source_local_date: {
156
+ tenant_id: 1,
157
+ report_type: 1,
158
+ scope_context: 1,
159
+ scope_field: 1,
160
+ source_local_date: 1,
161
+ },
139
162
  };
140
163
  exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS = [
141
164
  {
142
- key: exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_INDEXES.identity,
143
- name: 'tenant_id__report_type__scope',
165
+ key: exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_INDEXES.tenant_id__report_type__scope_context__scope_field,
166
+ name: 'tenant_id__report_type__scope_context__scope_field',
144
167
  unique: true,
145
168
  },
169
+ {
170
+ key: exports.MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_INDEXES.tenant_id__report_type__scope_context__scope_field__source_local_date,
171
+ name: 'tenant_id__report_type__scope_context__scope_field__source_local_date',
172
+ },
146
173
  ];
147
174
  /** The report-type values the flat reader accepts. */
148
175
  exports.ALL_DRILLDOWN_REPORT_TYPE_VALUES = materialized_report_drilldown_rows_1.ALL_DRILLDOWN_REPORT_TYPES;
@@ -5,10 +5,12 @@ import { Schema } from 'mongoose';
5
5
  * SPLIT). See `schemas/types/materialized_tenant_discount_entries.ts` and
6
6
  * `docs/report-discounts-concept.md`.
7
7
  *
8
- * Scopes are capped at `LOCAL_MONTH` so a document can never grow unboundedly.
9
- * Rows are slim (ids + discount facts); each carries `entry_key`, its stable
10
- * identity INSIDE an order, so the `(time, order_id, entry_key)` cursor can
11
- * never collide. Amounts are CENTS; reads pin `schema_version`.
8
+ * Documents are PAGE-PARTITIONED by source day: day/hour scopes use
9
+ * `page='all'`; a period scope uses `page=<source localDate>`, so each document
10
+ * is bounded by one day's rows. Rows are slim (ids + discount facts); each
11
+ * carries `entry_key`, its stable identity INSIDE an order, so the
12
+ * `(time, order_id, entry_key)` cursor can never collide. Amounts are CENTS;
13
+ * reads pin `schema_version`.
12
14
  */
13
15
  /**
14
16
  * Version of the discount ENTRY rules of this collection. Bump when row
@@ -33,20 +35,20 @@ declare const DiscountEntrySchema: Schema<any, import("mongoose").Model<any, any
33
35
  platform: string;
34
36
  slot: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SLOT_ENUM | null;
35
37
  item_id: string;
38
+ kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
36
39
  sub_kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
37
- discount_id: string;
38
- reference_number: string;
39
- catalog_item_id: string;
40
- covered_amount: number;
41
- order_amount: number;
42
40
  level: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_LEVEL_ENUM;
43
41
  entry_key: string;
44
- kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
42
+ catalog_item_id: string;
45
43
  catalog_auto: boolean;
46
44
  item_name: string;
47
45
  rate: number;
48
46
  item_value: number;
47
+ discount_id: string;
48
+ reference_number: string;
49
49
  complimentary_full: boolean;
50
+ covered_amount: number;
51
+ order_amount: number;
50
52
  order_taker_id: string;
51
53
  order_taker_name: string;
52
54
  created_at: NativeDate;
@@ -65,20 +67,20 @@ declare const DiscountEntrySchema: Schema<any, import("mongoose").Model<any, any
65
67
  platform: string;
66
68
  slot: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SLOT_ENUM | null;
67
69
  item_id: string;
70
+ kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
68
71
  sub_kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
69
- discount_id: string;
70
- reference_number: string;
71
- catalog_item_id: string;
72
- covered_amount: number;
73
- order_amount: number;
74
72
  level: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_LEVEL_ENUM;
75
73
  entry_key: string;
76
- kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
74
+ catalog_item_id: string;
77
75
  catalog_auto: boolean;
78
76
  item_name: string;
79
77
  rate: number;
80
78
  item_value: number;
79
+ discount_id: string;
80
+ reference_number: string;
81
81
  complimentary_full: boolean;
82
+ covered_amount: number;
83
+ order_amount: number;
82
84
  order_taker_id: string;
83
85
  order_taker_name: string;
84
86
  created_at: NativeDate;
@@ -97,20 +99,20 @@ declare const DiscountEntrySchema: Schema<any, import("mongoose").Model<any, any
97
99
  platform: string;
98
100
  slot: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SLOT_ENUM | null;
99
101
  item_id: string;
102
+ kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
100
103
  sub_kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
101
- discount_id: string;
102
- reference_number: string;
103
- catalog_item_id: string;
104
- covered_amount: number;
105
- order_amount: number;
106
104
  level: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_LEVEL_ENUM;
107
105
  entry_key: string;
108
- kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
106
+ catalog_item_id: string;
109
107
  catalog_auto: boolean;
110
108
  item_name: string;
111
109
  rate: number;
112
110
  item_value: number;
111
+ discount_id: string;
112
+ reference_number: string;
113
113
  complimentary_full: boolean;
114
+ covered_amount: number;
115
+ order_amount: number;
114
116
  order_taker_id: string;
115
117
  order_taker_name: string;
116
118
  created_at: NativeDate;
@@ -141,6 +143,7 @@ export declare const MaterializedTenantDiscountEntriesSchema: Schema<any, import
141
143
  schema_version: number;
142
144
  source_watermark: NativeDate;
143
145
  computed_at: NativeDate;
146
+ page: string;
144
147
  discount_entries: any;
145
148
  }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
146
149
  createdAt: NativeDate;
@@ -159,6 +162,7 @@ export declare const MaterializedTenantDiscountEntriesSchema: Schema<any, import
159
162
  schema_version: number;
160
163
  source_watermark: NativeDate;
161
164
  computed_at: NativeDate;
165
+ page: string;
162
166
  discount_entries: any;
163
167
  }>> & import("mongoose").FlatRecord<{
164
168
  createdAt: NativeDate;
@@ -177,6 +181,7 @@ export declare const MaterializedTenantDiscountEntriesSchema: Schema<any, import
177
181
  schema_version: number;
178
182
  source_watermark: NativeDate;
179
183
  computed_at: NativeDate;
184
+ page: string;
180
185
  discount_entries: any;
181
186
  }> & Required<{
182
187
  _id: import("mongoose").Types.ObjectId;
@@ -186,20 +191,19 @@ export declare const MaterializedTenantDiscountEntriesSchema: Schema<any, import
186
191
  export { DiscountEntrySchema };
187
192
  export declare const MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_NAME = "materialized_tenant_discount_entries";
188
193
  /**
189
- * Indexes for `materialized_tenant_discount_entries` — the same single
190
- * UNIQUE compound identity index as every materialized report collection. The
191
- * `{ tenant_id, scope_context, scope_field }` prefix also serves the
192
- * drill-down's month-document resolution.
194
+ * Indexes for `materialized_tenant_discount_entries` — ONE unique index per
195
+ * page. The `{ tenant_id, scope_context, scope_field, page }` prefix serves
196
+ * both page replacement and the operations multi-page aggregation.
193
197
  */
194
198
  export declare const MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_INDEXES: {
195
199
  _id: {
196
200
  _id: number;
197
201
  };
198
- tenant_id__scope_context__scope_field__day_index: {
202
+ tenant_id__scope_context__scope_field__page: {
199
203
  tenant_id: number;
200
204
  scope_context: number;
201
205
  scope_field: number;
202
- day_index: number;
206
+ page: number;
203
207
  };
204
208
  };
205
209
  export declare const MATERIALIZED_TENANT_DISCOUNT_ENTRIES_INDEX_OPTIONS: {
@@ -207,7 +211,7 @@ export declare const MATERIALIZED_TENANT_DISCOUNT_ENTRIES_INDEX_OPTIONS: {
207
211
  tenant_id: number;
208
212
  scope_context: number;
209
213
  scope_field: number;
210
- day_index: number;
214
+ page: number;
211
215
  };
212
216
  name: string;
213
217
  unique: boolean;
@@ -10,10 +10,12 @@ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
10
10
  * SPLIT). See `schemas/types/materialized_tenant_discount_entries.ts` and
11
11
  * `docs/report-discounts-concept.md`.
12
12
  *
13
- * Scopes are capped at `LOCAL_MONTH` so a document can never grow unboundedly.
14
- * Rows are slim (ids + discount facts); each carries `entry_key`, its stable
15
- * identity INSIDE an order, so the `(time, order_id, entry_key)` cursor can
16
- * never collide. Amounts are CENTS; reads pin `schema_version`.
13
+ * Documents are PAGE-PARTITIONED by source day: day/hour scopes use
14
+ * `page='all'`; a period scope uses `page=<source localDate>`, so each document
15
+ * is bounded by one day's rows. Rows are slim (ids + discount facts); each
16
+ * carries `entry_key`, its stable identity INSIDE an order, so the
17
+ * `(time, order_id, entry_key)` cursor can never collide. Amounts are CENTS;
18
+ * reads pin `schema_version`.
17
19
  */
18
20
  /**
19
21
  * Version of the discount ENTRY rules of this collection. Bump when row
@@ -89,6 +91,11 @@ exports.MaterializedTenantDiscountEntriesSchema = new mongoose_1.Schema({
89
91
  type: String,
90
92
  required: true,
91
93
  },
94
+ page: {
95
+ type: String,
96
+ required: true,
97
+ immutable: true,
98
+ },
92
99
  day_index: {
93
100
  type: String,
94
101
  default: null,
@@ -148,24 +155,23 @@ exports.MaterializedTenantDiscountEntriesSchema = new mongoose_1.Schema({
148
155
  });
149
156
  exports.MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_NAME = 'materialized_tenant_discount_entries';
150
157
  /**
151
- * Indexes for `materialized_tenant_discount_entries` — the same single
152
- * UNIQUE compound identity index as every materialized report collection. The
153
- * `{ tenant_id, scope_context, scope_field }` prefix also serves the
154
- * drill-down's month-document resolution.
158
+ * Indexes for `materialized_tenant_discount_entries` — ONE unique index per
159
+ * page. The `{ tenant_id, scope_context, scope_field, page }` prefix serves
160
+ * both page replacement and the operations multi-page aggregation.
155
161
  */
156
162
  exports.MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_INDEXES = {
157
163
  _id: { _id: 1 },
158
- tenant_id__scope_context__scope_field__day_index: {
164
+ tenant_id__scope_context__scope_field__page: {
159
165
  tenant_id: 1,
160
166
  scope_context: 1,
161
167
  scope_field: 1,
162
- day_index: 1,
168
+ page: 1,
163
169
  },
164
170
  };
165
171
  exports.MATERIALIZED_TENANT_DISCOUNT_ENTRIES_INDEX_OPTIONS = [
166
172
  {
167
- key: { tenant_id: 1, scope_context: 1, scope_field: 1, day_index: 1 },
168
- name: 'tenant_id__scope_context__scope_field__day_index',
173
+ key: { tenant_id: 1, scope_context: 1, scope_field: 1, page: 1 },
174
+ name: 'tenant_id__scope_context__scope_field__page',
169
175
  unique: true,
170
176
  },
171
177
  ];
@@ -61,6 +61,7 @@ export declare const MaterializedTenantDiscountReportSchema: Schema<any, import(
61
61
  sub_kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
62
62
  resolved_amount: number;
63
63
  }>;
64
+ source_input_signature?: string | null | undefined;
64
65
  discount_counters?: any;
65
66
  discount_counter_rows?: any;
66
67
  }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
@@ -99,6 +100,7 @@ export declare const MaterializedTenantDiscountReportSchema: Schema<any, import(
99
100
  sub_kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
100
101
  resolved_amount: number;
101
102
  }>;
103
+ source_input_signature?: string | null | undefined;
102
104
  discount_counters?: any;
103
105
  discount_counter_rows?: any;
104
106
  }>> & import("mongoose").FlatRecord<{
@@ -137,6 +139,7 @@ export declare const MaterializedTenantDiscountReportSchema: Schema<any, import(
137
139
  sub_kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
138
140
  resolved_amount: number;
139
141
  }>;
142
+ source_input_signature?: string | null | undefined;
140
143
  discount_counters?: any;
141
144
  discount_counter_rows?: any;
142
145
  }> & Required<{
@@ -210,6 +210,15 @@ exports.MaterializedTenantDiscountReportSchema = new mongoose_1.Schema({
210
210
  type: mongoose_1.Schema.Types.Date,
211
211
  required: true,
212
212
  },
213
+ // Day-input digest (`v1:<count>:<hash>`) — set-shaped change detection
214
+ // for the discount counters, mirroring the sales-overview reports. A
215
+ // deleted / moved order must remove its discount even when the watermark
216
+ // regresses.
217
+ source_input_signature: {
218
+ type: String,
219
+ required: false,
220
+ default: undefined,
221
+ },
213
222
  computed_at: {
214
223
  type: mongoose_1.Schema.Types.Date,
215
224
  required: true,
@@ -83,11 +83,11 @@ export declare const MaterializedTenantDiscountSourceSchema: Schema<any, import(
83
83
  schema_version: number;
84
84
  source_watermark: NativeDate;
85
85
  computed_at: NativeDate;
86
+ kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
86
87
  sub_kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
87
- covered_amount: number;
88
88
  level: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_LEVEL_ENUM;
89
- kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
90
89
  complimentary_full: boolean;
90
+ covered_amount: number;
91
91
  row_count: number;
92
92
  free_item_count: number;
93
93
  free_item_value: number;
@@ -152,11 +152,11 @@ export declare const MaterializedTenantDiscountSourceSchema: Schema<any, import(
152
152
  schema_version: number;
153
153
  source_watermark: NativeDate;
154
154
  computed_at: NativeDate;
155
+ kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
155
156
  sub_kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
156
- covered_amount: number;
157
157
  level: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_LEVEL_ENUM;
158
- kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
159
158
  complimentary_full: boolean;
159
+ covered_amount: number;
160
160
  row_count: number;
161
161
  free_item_count: number;
162
162
  free_item_value: number;
@@ -221,11 +221,11 @@ export declare const MaterializedTenantDiscountSourceSchema: Schema<any, import(
221
221
  schema_version: number;
222
222
  source_watermark: NativeDate;
223
223
  computed_at: NativeDate;
224
+ kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
224
225
  sub_kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_SUB_KIND_ENUM;
225
- covered_amount: number;
226
226
  level: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_LEVEL_ENUM;
227
- kind: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM;
228
227
  complimentary_full: boolean;
228
+ covered_amount: number;
229
229
  row_count: number;
230
230
  free_item_count: number;
231
231
  free_item_value: number;
@@ -167,6 +167,6 @@ exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS = [
167
167
  },
168
168
  {
169
169
  key: exports.MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_INDEXES.tenant_id__scope_context__scope_field__discount_amount,
170
- name: 'tenant_id__scope_context__scope_field__discount_amount_desc',
170
+ name: 'tenant_id__scope_context__scope_field__discount_amount',
171
171
  },
172
172
  ];