@managesome/eposmatic-toolkit 0.4.29 → 0.4.31

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 (66) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +9 -12
  2. package/esm/helpers/materialized-reports/index.js +1 -0
  3. package/esm/helpers/materialized-reports/materialized-credit-bills-helpers.js +129 -20
  4. package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +10 -3
  5. package/esm/helpers/materialized-reports/materialized-discount-close-day-helpers.js +251 -240
  6. package/esm/helpers/materialized-reports/materialized-discount-compile-helpers.js +2 -1
  7. package/esm/helpers/materialized-reports/materialized-discount-source-helpers.js +176 -0
  8. package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +83 -0
  9. package/esm/helpers/materialized-reports/materialized-integer-contract.js +30 -4
  10. package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +4 -2
  11. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +7 -0
  12. package/esm/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +284 -43
  13. package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +4 -4
  14. package/esm/schemas/models/materialized_report_drilldown_rows.js +39 -12
  15. package/esm/schemas/models/materialized_tenant_discount_entries.js +18 -12
  16. package/esm/schemas/models/materialized_tenant_discount_sources.js +1 -1
  17. package/esm/schemas/models/orders.js +5 -0
  18. package/esm/schemas/models/tenant_active_jobs.js +7 -0
  19. package/esm/schemas/models/tenant_calendar.js +12 -0
  20. package/lib/constants/MATERIALIZED_REPORTS.d.ts +9 -12
  21. package/lib/constants/MATERIALIZED_REPORTS.js +11 -14
  22. package/lib/helpers/materialized-reports/index.d.ts +1 -0
  23. package/lib/helpers/materialized-reports/index.js +1 -0
  24. package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.d.ts +38 -0
  25. package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.js +129 -19
  26. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +10 -3
  27. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +23 -61
  28. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +250 -239
  29. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.d.ts +4 -4
  30. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +1 -0
  31. package/lib/helpers/materialized-reports/materialized-discount-source-helpers.d.ts +17 -0
  32. package/lib/helpers/materialized-reports/materialized-discount-source-helpers.js +178 -0
  33. package/lib/helpers/materialized-reports/materialized-index-bootstrap.d.ts +7 -0
  34. package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +87 -0
  35. package/lib/helpers/materialized-reports/materialized-integer-contract.d.ts +3 -1
  36. package/lib/helpers/materialized-reports/materialized-integer-contract.js +30 -4
  37. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.d.ts +2 -2
  38. package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +4 -2
  39. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +7 -0
  40. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.d.ts +8 -0
  41. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +283 -42
  42. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +4 -4
  43. package/lib/schemas/models/materialized_report_drilldown_rows.d.ts +40 -6
  44. package/lib/schemas/models/materialized_report_drilldown_rows.js +39 -12
  45. package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +33 -29
  46. package/lib/schemas/models/materialized_tenant_discount_entries.js +18 -12
  47. package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +6 -6
  48. package/lib/schemas/models/materialized_tenant_discount_sources.js +1 -1
  49. package/lib/schemas/models/orders.d.ts +5 -0
  50. package/lib/schemas/models/orders.js +5 -0
  51. package/lib/schemas/models/tenant_active_jobs.d.ts +7 -0
  52. package/lib/schemas/models/tenant_active_jobs.js +8 -1
  53. package/lib/schemas/models/tenant_calendar.d.ts +13 -0
  54. package/lib/schemas/models/tenant_calendar.js +13 -1
  55. package/lib/schemas/types/materialized_report_drilldown_rows.d.ts +8 -0
  56. package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +9 -5
  57. package/lib/tests/materialized-reports/materialized-credit-bills-helpers.test.js +56 -1
  58. package/lib/tests/materialized-reports/materialized-discount-classification.test.js +17 -0
  59. package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +60 -54
  60. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.js +14 -13
  61. package/lib/tests/materialized-reports/materialized-discount-sources.test.js +76 -0
  62. package/lib/tests/materialized-reports/materialized-integer-contract.test.js +11 -0
  63. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.js +2 -2
  64. package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.js +152 -7
  65. package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +89 -0
  66. package/package.json +1 -1
@@ -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
  ];
@@ -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
  ];
@@ -16870,6 +16870,11 @@ export declare const ORDERS_COLLECTION_INDEXES: {
16870
16870
  localDate: number;
16871
16871
  createdAt: number;
16872
16872
  };
16873
+ tenant_id__is_deleted__order_settled_at: {
16874
+ tenant_id: number;
16875
+ is_deleted: number;
16876
+ order_settled_at: number;
16877
+ };
16873
16878
  tenant_id__is_deleted__consumer_phone__createdAt: {
16874
16879
  tenant_id: number;
16875
16880
  is_deleted: number;
@@ -1038,6 +1038,11 @@ exports.ORDERS_COLLECTION_INDEXES = {
1038
1038
  localDate: 1,
1039
1039
  createdAt: -1,
1040
1040
  }, // partial index (partialFilterExpression: { localDate: { $exists: true } }) — per-tenant local-date order lookups filtered by is_deleted, newest first
1041
+ tenant_id__is_deleted__order_settled_at: {
1042
+ tenant_id: 1,
1043
+ is_deleted: 1,
1044
+ order_settled_at: -1,
1045
+ }, // deployed settlement-window index — prior-day credit bills collected in a day's window
1041
1046
  tenant_id__is_deleted__consumer_phone__createdAt: {
1042
1047
  tenant_id: 1,
1043
1048
  is_deleted: 1,
@@ -74,3 +74,10 @@ export declare const TENANT_ACTIVE_JOBS_COLLECTION_INDEXES: {
74
74
  tenant_id: number;
75
75
  };
76
76
  };
77
+ export declare const TENANT_ACTIVE_JOBS_INDEX_OPTIONS: {
78
+ key: {
79
+ tenant_id: number;
80
+ };
81
+ name: string;
82
+ unique: boolean;
83
+ }[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TENANT_ACTIVE_JOBS_COLLECTION_INDEXES = exports.TENANT_ACTIVE_JOBS_COLLECTION_NAME = exports.TenantActiveJobSchema = void 0;
3
+ exports.TENANT_ACTIVE_JOBS_INDEX_OPTIONS = exports.TENANT_ACTIVE_JOBS_COLLECTION_INDEXES = exports.TENANT_ACTIVE_JOBS_COLLECTION_NAME = exports.TenantActiveJobSchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const constants_1 = require("../../constants");
6
6
  exports.TenantActiveJobSchema = new mongoose_1.Schema({
@@ -86,3 +86,10 @@ exports.TENANT_ACTIVE_JOBS_COLLECTION_INDEXES = {
86
86
  _id: { _id: 1 }, // Unique Index
87
87
  tenant_id: { tenant_id: 1 }, // Unique Index — one document per tenant
88
88
  };
89
+ exports.TENANT_ACTIVE_JOBS_INDEX_OPTIONS = [
90
+ {
91
+ key: exports.TENANT_ACTIVE_JOBS_COLLECTION_INDEXES.tenant_id,
92
+ name: 'tenant_id',
93
+ unique: true,
94
+ },
95
+ ];
@@ -85,3 +85,16 @@ export declare const TENANT_CALENDAR_COLLECTION_INDEXES: {
85
85
  localYear: number;
86
86
  };
87
87
  };
88
+ /**
89
+ * Index OPTIONS the bootstrapper must create. `tenant_calendar` has no report
90
+ * history, but it DOES need the unique (tenant_id, localYear) invariant: a
91
+ * concurrent/retried upsert must never create a second year document.
92
+ */
93
+ export declare const TENANT_CALENDAR_INDEX_OPTIONS: {
94
+ key: {
95
+ tenant_id: number;
96
+ localYear: number;
97
+ };
98
+ name: string;
99
+ unique: boolean;
100
+ }[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TENANT_CALENDAR_COLLECTION_INDEXES = exports.TENANT_CALENDAR_COLLECTION_NAME = exports.TenantCalendarSchema = void 0;
3
+ exports.TENANT_CALENDAR_INDEX_OPTIONS = exports.TENANT_CALENDAR_COLLECTION_INDEXES = exports.TENANT_CALENDAR_COLLECTION_NAME = exports.TenantCalendarSchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const TenantCalendarDaySchema = new mongoose_1.Schema({
6
6
  total_orders: {
@@ -90,3 +90,15 @@ exports.TENANT_CALENDAR_COLLECTION_INDEXES = {
90
90
  _id: { _id: 1 }, // Unique Index
91
91
  tenant_id__localYear: { tenant_id: 1, localYear: -1 }, // unique per (tenant_id, localYear)
92
92
  };
93
+ /**
94
+ * Index OPTIONS the bootstrapper must create. `tenant_calendar` has no report
95
+ * history, but it DOES need the unique (tenant_id, localYear) invariant: a
96
+ * concurrent/retried upsert must never create a second year document.
97
+ */
98
+ exports.TENANT_CALENDAR_INDEX_OPTIONS = [
99
+ {
100
+ key: { tenant_id: 1, localYear: -1 },
101
+ name: 'tenant_id__localYear',
102
+ unique: true,
103
+ },
104
+ ];
@@ -38,6 +38,12 @@ export interface IMaterializedReportDrilldownRow {
38
38
  time: Date;
39
39
  /** The original row payload, unchanged. */
40
40
  data: Record<string, unknown>;
41
+ /**
42
+ * The business day this row's contribution belongs to. Day/hour rows equal
43
+ * their own day; period rows equal the day whose close added them. Added for
44
+ * incremental flat rebuilds; legacy rows may be null until a guarded rebuild.
45
+ */
46
+ source_local_date?: string | null;
41
47
  schema_version: number;
42
48
  computed_at: Date;
43
49
  createdAt?: Date;
@@ -57,6 +63,8 @@ export interface IMaterializedReportDrilldownScope {
57
63
  scope_field: string;
58
64
  row_count: number;
59
65
  off_app_order_count: number;
66
+ /** Last day whose close updated this scope (migration/audit marker). */
67
+ source_local_date?: string | null;
60
68
  schema_version: number;
61
69
  computed_at: Date;
62
70
  createdAt?: Date;
@@ -5,9 +5,10 @@ import { MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM, MATERIALIZED_DISCOUNT_LEVEL_ENUM
5
5
  * discount rows behind the discounts report's drill-down (owner decision (a):
6
6
  * SPLIT — the report documents hold counters only).
7
7
  *
8
- * ONE document per (tenant, scope context, scope value), scopes CAPPED AT
9
- * `LOCAL_MONTH` (`MATERIALIZED_DISCOUNT_REPORT_SCOPES`) so a month bounds how
10
- * many rows a document accumulates. Each document carries a per-key map
8
+ * ONE PAGE per (tenant, scope context, scope value, source day), scopes CAPPED
9
+ * AT `LOCAL_MONTH` (`MATERIALIZED_DISCOUNT_REPORT_SCOPES`). Day/hour scopes use
10
+ * `page = 'all'`; a period scope uses `page = <source localDate>`, so every
11
+ * document is bounded by one day's rows. Each page carries a per-key map
11
12
  * `discount_entries: { [sale_type_key]: { rows: [...] } }` — the same
12
13
  * `ORDER_STATUS:PLATFORM` key grammar as every materialized report, so platform
13
14
  * / status filters apply to the rows too.
@@ -93,8 +94,9 @@ export interface IMaterializedDiscountKeySection {
93
94
  rows: IMaterializedDiscountEntry[];
94
95
  }
95
96
  /**
96
- * One `materialized_tenant_discount_entries` document — tenant × scope context ×
97
- * scope value, month-capped.
97
+ * One `materialized_tenant_discount_entries` PAGE — tenant × scope context ×
98
+ * scope value × source day. Day/hour pages use `page = 'all'`; period pages use
99
+ * the source `localDate`, so a month is bounded by its days.
98
100
  */
99
101
  export interface IMaterializedTenantDiscountEntries {
100
102
  _id: string;
@@ -102,6 +104,8 @@ export interface IMaterializedTenantDiscountEntries {
102
104
  brand_id: string;
103
105
  scope_context: MATERIALIZED_REPORT_SCOPE_ENUM;
104
106
  scope_field: string;
107
+ /** `'all'` for day/hour scopes; source `YYYY-MM-DD` for period scopes. */
108
+ page: string;
105
109
  day_index?: DAY_INDEX_ENUM | null;
106
110
  sale_type_key_filters: MATERIALIZED_REPORT_KEY_FILTER_ENUM[];
107
111
  discount_entries: Record<string, IMaterializedDiscountKeySection> | null;
@@ -254,6 +254,60 @@ describe('resolveCreditBillDay (delta resolution + carry-forward + settlements)'
254
254
  issued_local_hour: '2026-10-01 20',
255
255
  });
256
256
  });
257
+ it('patches the STORED issue section when the order status changed (status drift)', () => {
258
+ const oldKey = `COMPLETED:POS_WALK-IN`;
259
+ const stored = makeCreditDoc(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE, '2026-10-01', {
260
+ [oldKey]: { issued: [issuedEntry()], received: [] },
261
+ });
262
+ // The order is now DELIVERED; the collection candidate's current key is
263
+ // DELIVERED:POS_WALK-IN while the issue entry lives under COMPLETED.
264
+ const resolution = expectNonNull((0, materialized_credit_bills_helpers_1.resolveCreditBillDay)({
265
+ tenantId: TENANT,
266
+ localDate: '2026-10-03',
267
+ issued: [],
268
+ events: [
269
+ makeEventCandidate({
270
+ order_status: constants_1.ORDER_STATUS_ENUM.DELIVERED,
271
+ candidate_cumulative: 1000,
272
+ }),
273
+ ],
274
+ stored: [stored],
275
+ computedAt: NOW,
276
+ }));
277
+ expect(resolution.settled_bills[0].stored_key).toBe(oldKey);
278
+ const patched = expectNonNull((0, materialized_credit_bills_helpers_1.updateMaterializedCreditBillsReport)(stored, { settled_bills: resolution.settled_bills }, { computedAt: NOW }));
279
+ expect(patched.credit_bills?.[oldKey]?.issued[0]).toMatchObject({
280
+ received_amount: 1000,
281
+ settled_local_date: '2026-10-03',
282
+ });
283
+ expect(patched.credit_bills?.[`DELIVERED:POS_WALK-IN`]).toBeUndefined();
284
+ expect(patched.iteration).toBe(2);
285
+ });
286
+ it('updates the SAME bill cumulative for a PARTIAL receipt (no second receivable)', () => {
287
+ const stored = makeCreditDoc(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE, '2026-10-01', {
288
+ [KEY]: { issued: [issuedEntry()], received: [] },
289
+ });
290
+ const resolution = expectNonNull((0, materialized_credit_bills_helpers_1.resolveCreditBillDay)({
291
+ tenantId: TENANT,
292
+ localDate: '2026-10-03',
293
+ issued: [],
294
+ events: [makeEventCandidate({ candidate_cumulative: 400 })],
295
+ stored: [stored],
296
+ computedAt: NOW,
297
+ }));
298
+ expect(resolution.settled_bills).toEqual([]);
299
+ expect(resolution.receipt_bills).toHaveLength(1);
300
+ const patched = expectNonNull((0, materialized_credit_bills_helpers_1.updateMaterializedCreditBillsReport)(stored, { receipt_bills: resolution.receipt_bills }, { computedAt: NOW }));
301
+ expect(patched.credit_bills?.[KEY]?.issued).toHaveLength(1);
302
+ expect(patched.credit_bills?.[KEY]?.issued[0]).toMatchObject({
303
+ amount: 1000,
304
+ received_amount: 400,
305
+ settled_at: null,
306
+ });
307
+ // Replaying the same receipt is idempotent — no new bill, no new iteration.
308
+ const replay = expectNonNull((0, materialized_credit_bills_helpers_1.updateMaterializedCreditBillsReport)(patched, { receipt_bills: resolution.receipt_bills }, { computedAt: NOW }));
309
+ expect(replay.iteration).toBe(patched.iteration);
310
+ });
257
311
  it('keeps a stored same-day delta and never re-emits a settled transition (idempotent re-close)', () => {
258
312
  const stored = makeCreditDoc(MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE, '2026-10-03', {
259
313
  [KEY]: {
@@ -342,6 +396,7 @@ describe('buildMaterializedCreditBillDayReports (month-capped scopes)', () => {
342
396
  },
343
397
  ],
344
398
  settled_bills: [],
399
+ receipt_bills: [],
345
400
  };
346
401
  it('materializes hour/date/week/ISO-week/month documents with the entries on every scope', () => {
347
402
  const docs = expectNonNull((0, materialized_credit_bills_helpers_1.buildMaterializedCreditBillDayReports)({
@@ -381,7 +436,7 @@ describe('buildMaterializedCreditBillDayReports (month-capped scopes)', () => {
381
436
  tenantId: TENANT,
382
437
  brandId: 'brand-1',
383
438
  localDate: '2026-10-01',
384
- resolution: { issued: [], received_events: [], settled_bills: [] },
439
+ resolution: { issued: [], received_events: [], settled_bills: [], receipt_bills: [] },
385
440
  computedAt: NOW,
386
441
  }));
387
442
  expect(docs).toEqual([]);
@@ -266,6 +266,23 @@ describe('materialized discounts — sub-kind classifier (synthesized coverage)'
266
266
  expect(resolved.perSlot.get(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL)).toBe(580);
267
267
  expect(resolved.unallocated).toBe(0);
268
268
  });
269
+ it('caps several FIXED slots at the headline instead of over-allocating', () => {
270
+ const details = {
271
+ special_discount: 500,
272
+ special_discount_id: 'MANUAL_Rs 500',
273
+ is_special_discount_percentage: false,
274
+ loyalty_discount: 300,
275
+ loyalty_discount_id: '6aa472763689e5f08dd0599f',
276
+ is_loyalty_discount_percentage: false,
277
+ total_discount: 600,
278
+ };
279
+ const resolved = (0, materialized_discount_classification_helpers_1.resolveRowMoney)(details, 600);
280
+ expect(resolved.perSlot.get(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.SPECIAL)).toBe(500);
281
+ expect(resolved.perSlot.get(MATERIALIZED_REPORTS_1.MATERIALIZED_DISCOUNT_SLOT_ENUM.LOYALTY)).toBe(100);
282
+ expect(resolved.allocated).toBe(600);
283
+ expect(resolved.unallocated).toBe(0);
284
+ expect(resolved.row_total).toBe(600);
285
+ });
269
286
  it('books money with NO usable slot as unattributed (never dropped)', () => {
270
287
  const details = { total_discount: 8717.5 };
271
288
  const resolved = (0, materialized_discount_classification_helpers_1.resolveRowMoney)(details, 8717.5);