@managesome/eposmatic-toolkit 0.4.7 → 0.4.8

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 (51) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +33 -0
  2. package/esm/helpers/materialized-reports/index.js +1 -0
  3. package/esm/helpers/materialized-reports/materialized-credit-bills-helpers.js +982 -0
  4. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +276 -33
  5. package/esm/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +327 -41
  6. package/esm/helpers/materialized-reports/tenant-sales-overview-formatter.js +72 -11
  7. package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +118 -235
  8. package/esm/schemas/models/index.js +1 -0
  9. package/esm/schemas/models/materialized_credit_bills.js +265 -0
  10. package/esm/schemas/models/materialized_tenant_reports_iterations.js +13 -0
  11. package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +39 -97
  12. package/esm/schemas/types/index.js +1 -0
  13. package/esm/schemas/types/materialized_credit_bills.js +1 -0
  14. package/lib/constants/MATERIALIZED_REPORTS.d.ts +28 -2
  15. package/lib/constants/MATERIALIZED_REPORTS.js +34 -1
  16. package/lib/helpers/materialized-reports/index.d.ts +1 -0
  17. package/lib/helpers/materialized-reports/index.js +1 -0
  18. package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.d.ts +307 -0
  19. package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.js +1003 -0
  20. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +36 -2
  21. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +278 -34
  22. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.d.ts +66 -8
  23. package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +326 -40
  24. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.d.ts +18 -2
  25. package/lib/helpers/materialized-reports/tenant-sales-overview-formatter.js +72 -11
  26. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.d.ts +46 -37
  27. package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +119 -238
  28. package/lib/helpers/types/materialized-reports-apis.d.ts +32 -5
  29. package/lib/schemas/models/index.d.ts +1 -0
  30. package/lib/schemas/models/index.js +1 -0
  31. package/lib/schemas/models/materialized_credit_bills.d.ts +337 -0
  32. package/lib/schemas/models/materialized_credit_bills.js +268 -0
  33. package/lib/schemas/models/materialized_tenant_reports_iterations.d.ts +20 -6
  34. package/lib/schemas/models/materialized_tenant_reports_iterations.js +14 -1
  35. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +37 -165
  36. package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +40 -98
  37. package/lib/schemas/types/index.d.ts +1 -0
  38. package/lib/schemas/types/index.js +1 -0
  39. package/lib/schemas/types/materialized_credit_bills.d.ts +139 -0
  40. package/lib/schemas/types/materialized_credit_bills.js +2 -0
  41. package/lib/schemas/types/materialized_tenant_sales_overview_reports.d.ts +32 -77
  42. package/lib/tests/materialized-reports/materialized-credit-bills-helpers.test.d.ts +1 -0
  43. package/lib/tests/materialized-reports/materialized-credit-bills-helpers.test.js +715 -0
  44. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.js +96 -0
  45. package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-schema.test.js +9 -0
  46. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +23 -24
  47. package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.js +327 -22
  48. package/lib/tests/materialized-reports/tenant-sales-overview-formatter-aggregated.test.js +27 -2
  49. package/lib/tests/materialized-reports/tenant-sales-overview-formatter.test.js +12 -27
  50. package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +79 -146
  51. package/package.json +1 -1
@@ -0,0 +1,337 @@
1
+ import { Schema } from 'mongoose';
2
+ /**
3
+ * Mongoose schema for `materialized_tenant_credit_bills` — the SHARED
4
+ * credit-bill ledger of the materialized reports (see `MATERIALIZED_REPORTS.md`).
5
+ *
6
+ * ONE document per (tenant, scope context, scope value), scopes CAPPED AT
7
+ * `LOCAL_MONTH` (`MATERIALIZED_CREDIT_BILLS_SCOPES`) so a document can never
8
+ * grow unboundedly: a month bounds its bill volume by construction. Each
9
+ * document carries a per-key `credit_bills` map of slim `issued` / `received`
10
+ * entries (ids + credit facts — the order document stays the single source of
11
+ * the sale). See the type module `schemas/types/materialized_credit_bills.ts`
12
+ * for the entry contract. Amounts are CENTS; reads pin `schema_version`.
13
+ */
14
+ /**
15
+ * Version of the credit-entry rules of this collection. Bump when entry
16
+ * semantics change (e.g. signed REFUND / WRITE_OFF deltas) — historical
17
+ * documents must then be re-materialized. PRE-RELEASE: the collection has no
18
+ * live tenants/consumers yet, so every change so far folds into version `1`.
19
+ */
20
+ export declare const MATERIALIZED_CREDIT_BILLS_SCHEMA_VERSION = 1;
21
+ export declare const MaterializedTenantCreditBillsReportSchema: Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
22
+ timestamps: true;
23
+ versionKey: false;
24
+ minimize: false;
25
+ }, {
26
+ createdAt: NativeDate;
27
+ updatedAt: NativeDate;
28
+ } & {
29
+ _id: import("mongoose").Types.ObjectId;
30
+ tenant_id: string;
31
+ createdAt: NativeDate;
32
+ updatedAt: NativeDate;
33
+ brand_id: string;
34
+ day_index: import("../../constants/ENUMS").DAY_INDEX_ENUM;
35
+ iteration: number;
36
+ scope_context: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_SCOPE_ENUM;
37
+ scope_field: string;
38
+ sale_type_key_filters: string[];
39
+ credit_bills: Map<string, {
40
+ issued: import("mongoose").Types.DocumentArray<{
41
+ order_id: string;
42
+ payment_id: string;
43
+ amount: number;
44
+ issued_at: NativeDate;
45
+ issued_local_hour: string;
46
+ received_at_issue: number;
47
+ received_amount: number;
48
+ settled_at: NativeDate;
49
+ settled_local_date: string;
50
+ credit_note: string;
51
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
52
+ order_id: string;
53
+ payment_id: string;
54
+ amount: number;
55
+ issued_at: NativeDate;
56
+ issued_local_hour: string;
57
+ received_at_issue: number;
58
+ received_amount: number;
59
+ settled_at: NativeDate;
60
+ settled_local_date: string;
61
+ credit_note: string;
62
+ }> & {
63
+ order_id: string;
64
+ payment_id: string;
65
+ amount: number;
66
+ issued_at: NativeDate;
67
+ issued_local_hour: string;
68
+ received_at_issue: number;
69
+ received_amount: number;
70
+ settled_at: NativeDate;
71
+ settled_local_date: string;
72
+ credit_note: string;
73
+ }>;
74
+ received: import("mongoose").Types.DocumentArray<{
75
+ order_id: string;
76
+ payment_id: string;
77
+ amount: number;
78
+ received_at: NativeDate;
79
+ received_amount: number;
80
+ credit_note: string;
81
+ event_local_date: string;
82
+ received_delta: number;
83
+ event_local_hour: string;
84
+ issued_local_date: string;
85
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
86
+ order_id: string;
87
+ payment_id: string;
88
+ amount: number;
89
+ received_at: NativeDate;
90
+ received_amount: number;
91
+ credit_note: string;
92
+ event_local_date: string;
93
+ received_delta: number;
94
+ event_local_hour: string;
95
+ issued_local_date: string;
96
+ }> & {
97
+ order_id: string;
98
+ payment_id: string;
99
+ amount: number;
100
+ received_at: NativeDate;
101
+ received_amount: number;
102
+ credit_note: string;
103
+ event_local_date: string;
104
+ received_delta: number;
105
+ event_local_hour: string;
106
+ issued_local_date: string;
107
+ }>;
108
+ }>;
109
+ schema_version: number;
110
+ source_watermark: NativeDate;
111
+ computed_at: NativeDate;
112
+ }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
113
+ createdAt: NativeDate;
114
+ updatedAt: NativeDate;
115
+ } & {
116
+ _id: import("mongoose").Types.ObjectId;
117
+ tenant_id: string;
118
+ createdAt: NativeDate;
119
+ updatedAt: NativeDate;
120
+ brand_id: string;
121
+ day_index: import("../../constants/ENUMS").DAY_INDEX_ENUM;
122
+ iteration: number;
123
+ scope_context: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_SCOPE_ENUM;
124
+ scope_field: string;
125
+ sale_type_key_filters: string[];
126
+ credit_bills: Map<string, {
127
+ issued: import("mongoose").Types.DocumentArray<{
128
+ order_id: string;
129
+ payment_id: string;
130
+ amount: number;
131
+ issued_at: NativeDate;
132
+ issued_local_hour: string;
133
+ received_at_issue: number;
134
+ received_amount: number;
135
+ settled_at: NativeDate;
136
+ settled_local_date: string;
137
+ credit_note: string;
138
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
139
+ order_id: string;
140
+ payment_id: string;
141
+ amount: number;
142
+ issued_at: NativeDate;
143
+ issued_local_hour: string;
144
+ received_at_issue: number;
145
+ received_amount: number;
146
+ settled_at: NativeDate;
147
+ settled_local_date: string;
148
+ credit_note: string;
149
+ }> & {
150
+ order_id: string;
151
+ payment_id: string;
152
+ amount: number;
153
+ issued_at: NativeDate;
154
+ issued_local_hour: string;
155
+ received_at_issue: number;
156
+ received_amount: number;
157
+ settled_at: NativeDate;
158
+ settled_local_date: string;
159
+ credit_note: string;
160
+ }>;
161
+ received: import("mongoose").Types.DocumentArray<{
162
+ order_id: string;
163
+ payment_id: string;
164
+ amount: number;
165
+ received_at: NativeDate;
166
+ received_amount: number;
167
+ credit_note: string;
168
+ event_local_date: string;
169
+ received_delta: number;
170
+ event_local_hour: string;
171
+ issued_local_date: string;
172
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
173
+ order_id: string;
174
+ payment_id: string;
175
+ amount: number;
176
+ received_at: NativeDate;
177
+ received_amount: number;
178
+ credit_note: string;
179
+ event_local_date: string;
180
+ received_delta: number;
181
+ event_local_hour: string;
182
+ issued_local_date: string;
183
+ }> & {
184
+ order_id: string;
185
+ payment_id: string;
186
+ amount: number;
187
+ received_at: NativeDate;
188
+ received_amount: number;
189
+ credit_note: string;
190
+ event_local_date: string;
191
+ received_delta: number;
192
+ event_local_hour: string;
193
+ issued_local_date: string;
194
+ }>;
195
+ }>;
196
+ schema_version: number;
197
+ source_watermark: NativeDate;
198
+ computed_at: NativeDate;
199
+ }>> & import("mongoose").FlatRecord<{
200
+ createdAt: NativeDate;
201
+ updatedAt: NativeDate;
202
+ } & {
203
+ _id: import("mongoose").Types.ObjectId;
204
+ tenant_id: string;
205
+ createdAt: NativeDate;
206
+ updatedAt: NativeDate;
207
+ brand_id: string;
208
+ day_index: import("../../constants/ENUMS").DAY_INDEX_ENUM;
209
+ iteration: number;
210
+ scope_context: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_SCOPE_ENUM;
211
+ scope_field: string;
212
+ sale_type_key_filters: string[];
213
+ credit_bills: Map<string, {
214
+ issued: import("mongoose").Types.DocumentArray<{
215
+ order_id: string;
216
+ payment_id: string;
217
+ amount: number;
218
+ issued_at: NativeDate;
219
+ issued_local_hour: string;
220
+ received_at_issue: number;
221
+ received_amount: number;
222
+ settled_at: NativeDate;
223
+ settled_local_date: string;
224
+ credit_note: string;
225
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
226
+ order_id: string;
227
+ payment_id: string;
228
+ amount: number;
229
+ issued_at: NativeDate;
230
+ issued_local_hour: string;
231
+ received_at_issue: number;
232
+ received_amount: number;
233
+ settled_at: NativeDate;
234
+ settled_local_date: string;
235
+ credit_note: string;
236
+ }> & {
237
+ order_id: string;
238
+ payment_id: string;
239
+ amount: number;
240
+ issued_at: NativeDate;
241
+ issued_local_hour: string;
242
+ received_at_issue: number;
243
+ received_amount: number;
244
+ settled_at: NativeDate;
245
+ settled_local_date: string;
246
+ credit_note: string;
247
+ }>;
248
+ received: import("mongoose").Types.DocumentArray<{
249
+ order_id: string;
250
+ payment_id: string;
251
+ amount: number;
252
+ received_at: NativeDate;
253
+ received_amount: number;
254
+ credit_note: string;
255
+ event_local_date: string;
256
+ received_delta: number;
257
+ event_local_hour: string;
258
+ issued_local_date: string;
259
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
260
+ order_id: string;
261
+ payment_id: string;
262
+ amount: number;
263
+ received_at: NativeDate;
264
+ received_amount: number;
265
+ credit_note: string;
266
+ event_local_date: string;
267
+ received_delta: number;
268
+ event_local_hour: string;
269
+ issued_local_date: string;
270
+ }> & {
271
+ order_id: string;
272
+ payment_id: string;
273
+ amount: number;
274
+ received_at: NativeDate;
275
+ received_amount: number;
276
+ credit_note: string;
277
+ event_local_date: string;
278
+ received_delta: number;
279
+ event_local_hour: string;
280
+ issued_local_date: string;
281
+ }>;
282
+ }>;
283
+ schema_version: number;
284
+ source_watermark: NativeDate;
285
+ computed_at: NativeDate;
286
+ }> & Required<{
287
+ _id: import("mongoose").Types.ObjectId;
288
+ }> & {
289
+ __v: number;
290
+ }>;
291
+ export declare const MATERIALIZED_TENANT_CREDIT_BILLS_COLLECTION_NAME = "materialized_tenant_credit_bills";
292
+ /**
293
+ * Describes the indexes for `materialized_tenant_credit_bills`. Values are
294
+ * Mongo key patterns that can be passed straight to `.hint()`.
295
+ *
296
+ * ONE compound index carries the whole read story (plus `_id`) — same shape
297
+ * as the other materialized collections:
298
+ * `tenant_id__scope_context__scope_field__day_index` — UNIQUE:
299
+ * - identity — one document per (tenant, scope context, scope value);
300
+ * `day_index` is a deterministic function of `scope_field` (null for period
301
+ * scopes), so including it keeps the uniqueness exactly on that identity;
302
+ * - the drill-down read — the `{ tenant_id, scope_context, scope_field }`
303
+ * prefix serves exact and ranged scope-field queries (covering-set
304
+ * resolution) for single tenants and `$in` tenant lists.
305
+ *
306
+ * There are NO `brand_id` indexes: brand-level reads always pass the brand's
307
+ * explicit tenant list (`tenant_id: { $in }`).
308
+ */
309
+ export declare const MATERIALIZED_TENANT_CREDIT_BILLS_COLLECTION_INDEXES: {
310
+ _id: {
311
+ _id: number;
312
+ };
313
+ tenant_id__scope_context__scope_field__day_index: {
314
+ tenant_id: number;
315
+ scope_context: number;
316
+ scope_field: number;
317
+ day_index: number;
318
+ };
319
+ };
320
+ /**
321
+ * Index OPTIONS the materializer must CREATE — the identity index is a UNIQUE
322
+ * business invariant (one document per tenant × scope context × scope value;
323
+ * the close-day writes upsert against it and silently degrade without it),
324
+ * so it must be created with `unique: true`. The key patterns above are what
325
+ * the read `.hint()` consumes; these options are what creation must apply.
326
+ * The materializer boot should assert them (`listIndexes`, fail closed).
327
+ */
328
+ export declare const MATERIALIZED_TENANT_CREDIT_BILLS_INDEX_OPTIONS: {
329
+ key: {
330
+ tenant_id: number;
331
+ scope_context: number;
332
+ scope_field: number;
333
+ day_index: number;
334
+ };
335
+ name: string;
336
+ unique: boolean;
337
+ }[];
@@ -0,0 +1,268 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MATERIALIZED_TENANT_CREDIT_BILLS_INDEX_OPTIONS = exports.MATERIALIZED_TENANT_CREDIT_BILLS_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_CREDIT_BILLS_COLLECTION_NAME = exports.MaterializedTenantCreditBillsReportSchema = exports.MATERIALIZED_CREDIT_BILLS_SCHEMA_VERSION = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ const ENUMS_1 = require("../../constants/ENUMS");
6
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
7
+ /**
8
+ * Mongoose schema for `materialized_tenant_credit_bills` — the SHARED
9
+ * credit-bill ledger of the materialized reports (see `MATERIALIZED_REPORTS.md`).
10
+ *
11
+ * ONE document per (tenant, scope context, scope value), scopes CAPPED AT
12
+ * `LOCAL_MONTH` (`MATERIALIZED_CREDIT_BILLS_SCOPES`) so a document can never
13
+ * grow unboundedly: a month bounds its bill volume by construction. Each
14
+ * document carries a per-key `credit_bills` map of slim `issued` / `received`
15
+ * entries (ids + credit facts — the order document stays the single source of
16
+ * the sale). See the type module `schemas/types/materialized_credit_bills.ts`
17
+ * for the entry contract. Amounts are CENTS; reads pin `schema_version`.
18
+ */
19
+ /**
20
+ * Version of the credit-entry rules of this collection. Bump when entry
21
+ * semantics change (e.g. signed REFUND / WRITE_OFF deltas) — historical
22
+ * documents must then be re-materialized. PRE-RELEASE: the collection has no
23
+ * live tenants/consumers yet, so every change so far folds into version `1`.
24
+ */
25
+ exports.MATERIALIZED_CREDIT_BILLS_SCHEMA_VERSION = 1;
26
+ const CreditBillIssuedEntrySchema = new mongoose_1.Schema({
27
+ order_id: {
28
+ type: String,
29
+ required: true,
30
+ },
31
+ payment_id: {
32
+ type: String,
33
+ required: true,
34
+ },
35
+ amount: {
36
+ type: Number, // cents — the bill's amount at issue
37
+ required: true,
38
+ min: 0,
39
+ },
40
+ issued_at: {
41
+ type: mongoose_1.Schema.Types.Date, // punch time (order.createdAt) — NEVER order_settled_at
42
+ required: true,
43
+ },
44
+ issued_local_hour: {
45
+ type: String, // 'YYYY-MM-DD HH' punch hour (null when unknown)
46
+ default: null,
47
+ },
48
+ // Cents received BEFORE the issue day closed (prepaid partials +
49
+ // same-day settled credit). Carried forward on re-closes — never
50
+ // re-anchored to the current payment state.
51
+ received_at_issue: {
52
+ type: Number,
53
+ required: true,
54
+ default: 0,
55
+ min: 0,
56
+ },
57
+ // Cumulative received so far (NOW state) — patched by collection closes
58
+ // on the ISSUE-scope documents (bounded fan-out).
59
+ received_amount: {
60
+ type: Number,
61
+ required: true,
62
+ default: 0,
63
+ min: 0,
64
+ },
65
+ settled_at: {
66
+ type: mongoose_1.Schema.Types.Date, // the settling receipt's timestamp when known
67
+ default: null,
68
+ },
69
+ settled_local_date: {
70
+ type: String, // 'YYYY-MM-DD' — the settling receipt event's day
71
+ default: null,
72
+ },
73
+ credit_note: {
74
+ type: String, // optional note/reference recorded with the collection
75
+ default: null,
76
+ },
77
+ }, { _id: false, minimize: false });
78
+ const CreditBillReceivedEntrySchema = new mongoose_1.Schema({
79
+ order_id: {
80
+ type: String,
81
+ required: true,
82
+ },
83
+ payment_id: {
84
+ type: String,
85
+ required: true,
86
+ },
87
+ event_local_date: {
88
+ type: String, // 'YYYY-MM-DD' — collection business day (entry identity)
89
+ required: true,
90
+ },
91
+ amount: {
92
+ type: Number, // cents — the bill's amount at issue (receipt context)
93
+ required: true,
94
+ min: 0,
95
+ },
96
+ // Cumulative received right AFTER this event — the idempotent re-close
97
+ // anchor (`prior = received_amount − received_delta`).
98
+ received_amount: {
99
+ type: Number,
100
+ required: true,
101
+ default: 0,
102
+ min: 0,
103
+ },
104
+ // Cents collected BY this event (incremental — resolved against the
105
+ // stored pre-event cumulative at close-plan time). REFUND / WRITE_OFF
106
+ // would carry a SIGNED delta — reserved, unimplemented.
107
+ received_delta: {
108
+ type: Number,
109
+ required: true,
110
+ default: 0,
111
+ },
112
+ received_at: {
113
+ type: mongoose_1.Schema.Types.Date, // collection timestamp (order_settled_at) when known — ordering anchor
114
+ default: null,
115
+ },
116
+ event_local_hour: {
117
+ type: String, // 'YYYY-MM-DD HH' tenant-local collection hour (null when unknown)
118
+ default: null,
119
+ },
120
+ credit_note: {
121
+ type: String,
122
+ default: null,
123
+ },
124
+ issued_local_date: {
125
+ type: String, // 'YYYY-MM-DD' — the bill's issue day (display anchor)
126
+ required: true,
127
+ },
128
+ }, { _id: false, minimize: false });
129
+ const CreditBillKeySectionSchema = new mongoose_1.Schema({
130
+ issued: {
131
+ type: [CreditBillIssuedEntrySchema],
132
+ default: [],
133
+ },
134
+ received: {
135
+ type: [CreditBillReceivedEntrySchema],
136
+ default: [],
137
+ },
138
+ }, { _id: false, minimize: false });
139
+ exports.MaterializedTenantCreditBillsReportSchema = new mongoose_1.Schema({
140
+ _id: {
141
+ type: mongoose_1.Schema.Types.ObjectId,
142
+ required: true,
143
+ immutable: true,
144
+ },
145
+ tenant_id: {
146
+ type: String,
147
+ required: true,
148
+ immutable: true,
149
+ ref: 'tenants',
150
+ },
151
+ brand_id: {
152
+ type: String,
153
+ required: true,
154
+ },
155
+ scope_context: {
156
+ type: String,
157
+ enum: MATERIALIZED_REPORTS_1.MATERIALIZED_CREDIT_BILLS_SCOPES,
158
+ required: true,
159
+ },
160
+ scope_field: {
161
+ type: String, // format depends on scope_context — see MATERIALIZED_REPORT_SCOPE_ENUM
162
+ required: true,
163
+ },
164
+ // Denormalized weekday of scope_field — set ONLY on LOCAL_HOUR /
165
+ // LOCAL_DATE documents (MATERIALIZED_REPORT_SCOPES_WITH_DAY_INDEX);
166
+ // null elsewhere.
167
+ day_index: {
168
+ type: String,
169
+ enum: ENUMS_1.ALL_DAY_INDEX,
170
+ default: null,
171
+ },
172
+ sale_type_key_filters: {
173
+ type: [String],
174
+ enum: MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_REPORT_KEY_FILTERS,
175
+ required: true,
176
+ default: () => [...MATERIALIZED_REPORTS_1.SALES_OVERVIEW_KEY_FILTERS],
177
+ },
178
+ // Map key = the ordered filter combination, e.g. "COMPLETED:POS_WALK-IN" —
179
+ // the grammar is declared by `sale_type_key_filters` and must be built /
180
+ // parsed through the shared key helper, never ad hoc.
181
+ credit_bills: {
182
+ type: Map,
183
+ of: CreditBillKeySectionSchema,
184
+ required: true,
185
+ default: {},
186
+ },
187
+ // Versioned writes: bumped on replacement / period deltas / settlement
188
+ // patches; 1 = first materialization. Every bump is recorded in the
189
+ // shared `materialized_tenant_reports_iterations` history (full post-write
190
+ // snapshot + reason) — this document carries no embedded snapshot.
191
+ iteration: {
192
+ type: Number,
193
+ required: true,
194
+ min: 1,
195
+ default: 1,
196
+ },
197
+ schema_version: {
198
+ type: Number,
199
+ required: true,
200
+ immutable: true,
201
+ default: exports.MATERIALIZED_CREDIT_BILLS_SCHEMA_VERSION,
202
+ },
203
+ source_watermark: {
204
+ type: mongoose_1.Schema.Types.Date,
205
+ required: true,
206
+ },
207
+ computed_at: {
208
+ type: mongoose_1.Schema.Types.Date,
209
+ required: true,
210
+ },
211
+ createdAt: {
212
+ type: mongoose_1.Schema.Types.Date,
213
+ required: true,
214
+ default: () => new Date(),
215
+ immutable: true,
216
+ },
217
+ updatedAt: {
218
+ type: mongoose_1.Schema.Types.Date,
219
+ required: true,
220
+ default: () => new Date(),
221
+ },
222
+ }, {
223
+ timestamps: true,
224
+ versionKey: false,
225
+ minimize: false,
226
+ });
227
+ exports.MATERIALIZED_TENANT_CREDIT_BILLS_COLLECTION_NAME = 'materialized_tenant_credit_bills';
228
+ /**
229
+ * Describes the indexes for `materialized_tenant_credit_bills`. Values are
230
+ * Mongo key patterns that can be passed straight to `.hint()`.
231
+ *
232
+ * ONE compound index carries the whole read story (plus `_id`) — same shape
233
+ * as the other materialized collections:
234
+ * `tenant_id__scope_context__scope_field__day_index` — UNIQUE:
235
+ * - identity — one document per (tenant, scope context, scope value);
236
+ * `day_index` is a deterministic function of `scope_field` (null for period
237
+ * scopes), so including it keeps the uniqueness exactly on that identity;
238
+ * - the drill-down read — the `{ tenant_id, scope_context, scope_field }`
239
+ * prefix serves exact and ranged scope-field queries (covering-set
240
+ * resolution) for single tenants and `$in` tenant lists.
241
+ *
242
+ * There are NO `brand_id` indexes: brand-level reads always pass the brand's
243
+ * explicit tenant list (`tenant_id: { $in }`).
244
+ */
245
+ exports.MATERIALIZED_TENANT_CREDIT_BILLS_COLLECTION_INDEXES = {
246
+ _id: { _id: 1 }, // Unique Index
247
+ tenant_id__scope_context__scope_field__day_index: {
248
+ tenant_id: 1,
249
+ scope_context: 1,
250
+ scope_field: 1,
251
+ day_index: 1,
252
+ },
253
+ };
254
+ /**
255
+ * Index OPTIONS the materializer must CREATE — the identity index is a UNIQUE
256
+ * business invariant (one document per tenant × scope context × scope value;
257
+ * the close-day writes upsert against it and silently degrade without it),
258
+ * so it must be created with `unique: true`. The key patterns above are what
259
+ * the read `.hint()` consumes; these options are what creation must apply.
260
+ * The materializer boot should assert them (`listIndexes`, fail closed).
261
+ */
262
+ exports.MATERIALIZED_TENANT_CREDIT_BILLS_INDEX_OPTIONS = [
263
+ {
264
+ key: { tenant_id: 1, scope_context: 1, scope_field: 1, day_index: 1 },
265
+ name: 'tenant_id__scope_context__scope_field__day_index',
266
+ unique: true,
267
+ },
268
+ ];
@@ -24,9 +24,9 @@ export declare const MaterializedTenantReportIterationSchema: Schema<any, import
24
24
  createdAt: NativeDate;
25
25
  updatedAt: NativeDate;
26
26
  iteration: number;
27
- report_type: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_OVERVIEW;
28
- report_id: import("mongoose").Types.ObjectId;
29
27
  schema_version: number;
28
+ report_type: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_TYPE_ENUM;
29
+ report_id: import("mongoose").Types.ObjectId;
30
30
  snapshot: any;
31
31
  }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
32
32
  createdAt: NativeDate;
@@ -37,9 +37,9 @@ export declare const MaterializedTenantReportIterationSchema: Schema<any, import
37
37
  createdAt: NativeDate;
38
38
  updatedAt: NativeDate;
39
39
  iteration: number;
40
- report_type: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_OVERVIEW;
41
- report_id: import("mongoose").Types.ObjectId;
42
40
  schema_version: number;
41
+ report_type: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_TYPE_ENUM;
42
+ report_id: import("mongoose").Types.ObjectId;
43
43
  snapshot: any;
44
44
  }>> & import("mongoose").FlatRecord<{
45
45
  createdAt: NativeDate;
@@ -50,9 +50,9 @@ export declare const MaterializedTenantReportIterationSchema: Schema<any, import
50
50
  createdAt: NativeDate;
51
51
  updatedAt: NativeDate;
52
52
  iteration: number;
53
- report_type: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_TYPE_ENUM.TENANT_SALES_OVERVIEW;
54
- report_id: import("mongoose").Types.ObjectId;
55
53
  schema_version: number;
54
+ report_type: import("../../constants/MATERIALIZED_REPORTS").MATERIALIZED_REPORT_TYPE_ENUM;
55
+ report_id: import("mongoose").Types.ObjectId;
56
56
  snapshot: any;
57
57
  }> & Required<{
58
58
  _id: import("mongoose").Types.ObjectId;
@@ -78,3 +78,17 @@ export declare const MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_INDEXES:
78
78
  iteration: number;
79
79
  };
80
80
  };
81
+ /**
82
+ * Index OPTIONS the materializer must CREATE — `report_id__iteration` is a
83
+ * UNIQUE business invariant: one history row per (report, iteration), and the
84
+ * idempotent upsert key (`replaceOne({ report_id, iteration }, row, { upsert: true })`)
85
+ * silently degrades into duplicates without it.
86
+ */
87
+ export declare const MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS: {
88
+ key: {
89
+ report_id: number;
90
+ iteration: number;
91
+ };
92
+ name: string;
93
+ unique: boolean;
94
+ }[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME = exports.MaterializedTenantReportIterationSchema = void 0;
3
+ exports.MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS = exports.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME = exports.MaterializedTenantReportIterationSchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
6
6
  /**
@@ -83,3 +83,16 @@ exports.MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_INDEXES = {
83
83
  _id: { _id: 1 }, // Unique Index
84
84
  report_id__iteration: { report_id: 1, iteration: -1 }, // unique — one row per (report, iteration)
85
85
  };
86
+ /**
87
+ * Index OPTIONS the materializer must CREATE — `report_id__iteration` is a
88
+ * UNIQUE business invariant: one history row per (report, iteration), and the
89
+ * idempotent upsert key (`replaceOne({ report_id, iteration }, row, { upsert: true })`)
90
+ * silently degrades into duplicates without it.
91
+ */
92
+ exports.MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS = [
93
+ {
94
+ key: { report_id: 1, iteration: -1 },
95
+ name: 'report_id__iteration',
96
+ unique: true,
97
+ },
98
+ ];