@managesome/eposmatic-toolkit 0.3.11 → 0.4.0
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.
- package/esm/constants/MATERIALIZED_REPORTS.js +105 -0
- package/esm/helpers/materialized-reports/index.js +5 -0
- package/esm/helpers/materialized-reports/key-helpers.js +59 -0
- package/esm/helpers/materialized-reports/materialized-tenant-reports-iterations-helpers.js +60 -0
- package/esm/helpers/materialized-reports/scope-helpers.js +107 -0
- package/esm/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +251 -0
- package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +860 -0
- package/esm/schemas/models/materialized_tenant_reports_iterations.js +82 -0
- package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +399 -0
- package/esm/schemas/types/materialized_tenant_reports_iterations.js +1 -0
- package/esm/schemas/types/materialized_tenant_sales_overview_reports.js +1 -0
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +96 -0
- package/lib/constants/MATERIALIZED_REPORTS.js +108 -0
- package/lib/helpers/materialized-reports/index.d.ts +5 -0
- package/lib/helpers/materialized-reports/index.js +21 -0
- package/lib/helpers/materialized-reports/key-helpers.d.ts +35 -0
- package/lib/helpers/materialized-reports/key-helpers.js +64 -0
- package/lib/helpers/materialized-reports/materialized-tenant-reports-iterations-helpers.d.ts +66 -0
- package/lib/helpers/materialized-reports/materialized-tenant-reports-iterations-helpers.js +63 -0
- package/lib/helpers/materialized-reports/scope-helpers.d.ts +57 -0
- package/lib/helpers/materialized-reports/scope-helpers.js +114 -0
- package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.d.ts +125 -0
- package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +256 -0
- package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.d.ts +223 -0
- package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +875 -0
- package/lib/schemas/models/materialized_tenant_reports_iterations.d.ts +80 -0
- package/lib/schemas/models/materialized_tenant_reports_iterations.js +85 -0
- package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +569 -0
- package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +402 -0
- package/lib/schemas/types/materialized_tenant_reports_iterations.d.ts +62 -0
- package/lib/schemas/types/materialized_tenant_reports_iterations.js +2 -0
- package/lib/schemas/types/materialized_tenant_sales_overview_reports.d.ts +242 -0
- package/lib/schemas/types/materialized_tenant_sales_overview_reports.js +3 -0
- package/lib/tests/materialized-reports/fixtures.d.ts +81 -0
- package/lib/tests/materialized-reports/fixtures.js +87 -0
- package/lib/tests/materialized-reports/fixtures.test.d.ts +1 -0
- package/lib/tests/materialized-reports/fixtures.test.js +123 -0
- package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-helpers.test.js +75 -0
- package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-schema.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-tenant-reports-iterations-schema.test.js +73 -0
- package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +207 -0
- package/lib/tests/materialized-reports/sales-overview-baseline.test.d.ts +1 -0
- package/lib/tests/materialized-reports/sales-overview-baseline.test.js +326 -0
- package/lib/tests/materialized-reports/sales-overview-reference.d.ts +63 -0
- package/lib/tests/materialized-reports/sales-overview-reference.js +131 -0
- package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.js +335 -0
- package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +885 -0
- package/package.json +5 -2
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_INDEXES = exports.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME = exports.MaterializedTenantSalesOverviewReportSchema = exports.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_SCHEMA_VERSION = void 0;
|
|
4
|
+
const mongoose_1 = require("mongoose");
|
|
5
|
+
const constants_1 = require("../../constants");
|
|
6
|
+
const ENUMS_1 = require("../../constants/ENUMS");
|
|
7
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
8
|
+
/**
|
|
9
|
+
* Mongoose schema for `materialized_tenant_sales_overview_reports` — the
|
|
10
|
+
* materialized Reports > Overview data (one document per tenant × scope
|
|
11
|
+
* context × scope value), maintained by the day-end materialization job and
|
|
12
|
+
* read by the analytics API. Scope slices are stamped from the order's
|
|
13
|
+
* business day (`order.localDate`); hour buckets use the punch hour for sales
|
|
14
|
+
* and the settlement hour for credit-bill collections (see
|
|
15
|
+
* `MATERIALIZED_REPORTS.md`). The report's time-bias toggle (Punched At /
|
|
16
|
+
* `createdAt`) is out of scope. `scope_field` formats are documented on
|
|
17
|
+
* `MATERIALIZED_REPORT_SCOPE_ENUM` and in `MATERIALIZED_REPORTS.md`.
|
|
18
|
+
* Credit bills (`PAY_LATER`) live in each `sale_type_metrics` key's
|
|
19
|
+
* `CREDIT_BILLS` slot as a ledger (issued + settled tracking / received);
|
|
20
|
+
* gross stays sales-only and report views are derived by the frontend
|
|
21
|
+
* formatter. Iteration history (every structural write's full post-write
|
|
22
|
+
* snapshot) lives in the shared `materialized_tenant_reports_iterations` collection.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Version of the key grammar + metric rules of this collection. Bump when
|
|
26
|
+
* `sale_type_key_filters`, their order, or metric semantics change —
|
|
27
|
+
* historical documents must then be re-materialized (readers may branch on
|
|
28
|
+
* this value during a rollout).
|
|
29
|
+
*/
|
|
30
|
+
exports.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_SCHEMA_VERSION = 1;
|
|
31
|
+
const ReportDiscountByTypeSchema = new mongoose_1.Schema({
|
|
32
|
+
special_discount: {
|
|
33
|
+
type: Number, // cents — Σ order_discount_details.special_discount
|
|
34
|
+
required: true,
|
|
35
|
+
},
|
|
36
|
+
loyalty_discount: {
|
|
37
|
+
type: Number, // cents — Σ order_discount_details.loyalty_discount
|
|
38
|
+
required: true,
|
|
39
|
+
},
|
|
40
|
+
voucher_discount: {
|
|
41
|
+
type: Number, // cents — Σ order_discount_details.voucher_discount
|
|
42
|
+
required: true,
|
|
43
|
+
},
|
|
44
|
+
}, { _id: false, minimize: false });
|
|
45
|
+
const ReportOrderMetricSchema = new mongoose_1.Schema({
|
|
46
|
+
count: {
|
|
47
|
+
type: Number,
|
|
48
|
+
required: true,
|
|
49
|
+
min: 0,
|
|
50
|
+
},
|
|
51
|
+
gross_amount: {
|
|
52
|
+
type: Number, // cents — Σ amount_earned of the bucket's orders
|
|
53
|
+
required: true,
|
|
54
|
+
},
|
|
55
|
+
discount_amount: {
|
|
56
|
+
type: Number, // cents — Σ total_discount_applied
|
|
57
|
+
required: true,
|
|
58
|
+
},
|
|
59
|
+
discount_by_type: {
|
|
60
|
+
type: ReportDiscountByTypeSchema,
|
|
61
|
+
required: true,
|
|
62
|
+
},
|
|
63
|
+
gst_amount: {
|
|
64
|
+
type: Number, // cents — Σ gst_amount
|
|
65
|
+
required: true,
|
|
66
|
+
},
|
|
67
|
+
tax_amount: {
|
|
68
|
+
type: Number, // cents — Σ tax_amount
|
|
69
|
+
required: true,
|
|
70
|
+
},
|
|
71
|
+
tip_amount: {
|
|
72
|
+
type: Number, // cents — Σ tip_amount
|
|
73
|
+
required: true,
|
|
74
|
+
},
|
|
75
|
+
tax_invoice_provider_charges: {
|
|
76
|
+
type: Number, // cents — Σ tax_invoice_provider_charges
|
|
77
|
+
required: true,
|
|
78
|
+
},
|
|
79
|
+
delivery_charges: {
|
|
80
|
+
type: Number, // cents — Σ delivery_details.delivery_cost
|
|
81
|
+
required: true,
|
|
82
|
+
},
|
|
83
|
+
}, { _id: false, minimize: false });
|
|
84
|
+
const ReportPaymentMetricSchema = new mongoose_1.Schema({
|
|
85
|
+
payment_type: {
|
|
86
|
+
type: String,
|
|
87
|
+
// enum: ALL_PAYMENT_TYPE_NAMES, — intentionally NOT enforced: live stores
|
|
88
|
+
// contain legacy/aggregator values (CARD_PAYMENT, FOODPANDA, PLAZA, …)
|
|
89
|
+
// that must pass through reporting untouched.
|
|
90
|
+
required: true,
|
|
91
|
+
},
|
|
92
|
+
count: {
|
|
93
|
+
type: Number,
|
|
94
|
+
required: true,
|
|
95
|
+
min: 0,
|
|
96
|
+
},
|
|
97
|
+
gross_amount: {
|
|
98
|
+
type: Number, // cents — net of refunds & card charges; may be negative
|
|
99
|
+
required: true,
|
|
100
|
+
},
|
|
101
|
+
amount_due: {
|
|
102
|
+
type: Number,
|
|
103
|
+
required: true,
|
|
104
|
+
min: 0,
|
|
105
|
+
},
|
|
106
|
+
amount_paid: {
|
|
107
|
+
type: Number,
|
|
108
|
+
required: true,
|
|
109
|
+
min: 0,
|
|
110
|
+
},
|
|
111
|
+
amount_refunded: {
|
|
112
|
+
type: Number,
|
|
113
|
+
required: true,
|
|
114
|
+
min: 0,
|
|
115
|
+
},
|
|
116
|
+
card_charges: {
|
|
117
|
+
type: Number,
|
|
118
|
+
required: true,
|
|
119
|
+
min: 0,
|
|
120
|
+
},
|
|
121
|
+
}, { _id: false, minimize: false });
|
|
122
|
+
const ReportSaleTypeMetricSchema = new mongoose_1.Schema({
|
|
123
|
+
orders: {
|
|
124
|
+
type: ReportOrderMetricSchema,
|
|
125
|
+
required: true,
|
|
126
|
+
},
|
|
127
|
+
payments: {
|
|
128
|
+
type: [ReportPaymentMetricSchema],
|
|
129
|
+
default: [],
|
|
130
|
+
},
|
|
131
|
+
}, { _id: false, minimize: false });
|
|
132
|
+
const ReportCreditBillSchema = new mongoose_1.Schema({
|
|
133
|
+
order_id: {
|
|
134
|
+
type: String,
|
|
135
|
+
required: true,
|
|
136
|
+
},
|
|
137
|
+
payment_id: {
|
|
138
|
+
type: String,
|
|
139
|
+
required: true,
|
|
140
|
+
},
|
|
141
|
+
amount: {
|
|
142
|
+
type: Number, // cents — the bill's amount at issue
|
|
143
|
+
required: true,
|
|
144
|
+
},
|
|
145
|
+
// Cumulative amount received against this bill so far (0 while unpaid;
|
|
146
|
+
// less than `amount` while partially paid). Replaces the old `is_settled`
|
|
147
|
+
// flag: the bill is settled when `received_amount >= amount` (`settled_at`
|
|
148
|
+
// is stamped by the collection patch). Never an event delta, so patches
|
|
149
|
+
// stay idempotent.
|
|
150
|
+
received_amount: {
|
|
151
|
+
type: Number,
|
|
152
|
+
required: true,
|
|
153
|
+
default: 0,
|
|
154
|
+
},
|
|
155
|
+
credit_note: {
|
|
156
|
+
type: String, // optional note/reference recorded with the collection
|
|
157
|
+
default: null,
|
|
158
|
+
},
|
|
159
|
+
issued_at: {
|
|
160
|
+
type: mongoose_1.Schema.Types.Date,
|
|
161
|
+
required: true,
|
|
162
|
+
},
|
|
163
|
+
// Stamped when the bill is fully collected (received_amount >= amount).
|
|
164
|
+
settled_at: {
|
|
165
|
+
type: mongoose_1.Schema.Types.Date,
|
|
166
|
+
default: null,
|
|
167
|
+
},
|
|
168
|
+
}, { _id: false, minimize: false });
|
|
169
|
+
const ReportReceivedCreditBillSchema = new mongoose_1.Schema({
|
|
170
|
+
order_id: {
|
|
171
|
+
type: String,
|
|
172
|
+
required: true,
|
|
173
|
+
},
|
|
174
|
+
payment_id: {
|
|
175
|
+
type: String,
|
|
176
|
+
required: true,
|
|
177
|
+
},
|
|
178
|
+
amount: {
|
|
179
|
+
type: Number, // cents — the bill's amount at issue
|
|
180
|
+
required: true,
|
|
181
|
+
},
|
|
182
|
+
// Cumulative amount received for this bill as of the collection compile
|
|
183
|
+
// (≤ `amount` while partially paid; reaches `amount` when fully collected).
|
|
184
|
+
received_amount: {
|
|
185
|
+
type: Number,
|
|
186
|
+
required: true,
|
|
187
|
+
},
|
|
188
|
+
credit_note: {
|
|
189
|
+
type: String, // optional note/reference recorded with the collection
|
|
190
|
+
default: null,
|
|
191
|
+
},
|
|
192
|
+
issued_local_date: {
|
|
193
|
+
type: String, // 'YYYY-MM-DD' — the sale's day (bill issue day)
|
|
194
|
+
required: true,
|
|
195
|
+
},
|
|
196
|
+
// Collection timestamp (`order_settled_at`) when known — stamped onto the
|
|
197
|
+
// issue-day bill's `settled_at` by the collection patch.
|
|
198
|
+
settled_at: {
|
|
199
|
+
type: mongoose_1.Schema.Types.Date,
|
|
200
|
+
default: null,
|
|
201
|
+
},
|
|
202
|
+
}, { _id: false, minimize: false });
|
|
203
|
+
const ReportCreditBillsSchema = new mongoose_1.Schema({
|
|
204
|
+
issued: {
|
|
205
|
+
count: {
|
|
206
|
+
type: Number,
|
|
207
|
+
required: true,
|
|
208
|
+
min: 0,
|
|
209
|
+
default: 0,
|
|
210
|
+
},
|
|
211
|
+
amount: {
|
|
212
|
+
type: Number,
|
|
213
|
+
required: true,
|
|
214
|
+
default: 0,
|
|
215
|
+
},
|
|
216
|
+
// How much of the issued bills has been collected since — patched on
|
|
217
|
+
// settlement (tracking only; the money is counted on the collection day).
|
|
218
|
+
settled_count: {
|
|
219
|
+
type: Number,
|
|
220
|
+
required: true,
|
|
221
|
+
min: 0,
|
|
222
|
+
default: 0,
|
|
223
|
+
},
|
|
224
|
+
settled_amount: {
|
|
225
|
+
type: Number,
|
|
226
|
+
required: true,
|
|
227
|
+
default: 0,
|
|
228
|
+
},
|
|
229
|
+
bills: {
|
|
230
|
+
type: [ReportCreditBillSchema],
|
|
231
|
+
default: [],
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
received: {
|
|
235
|
+
count: {
|
|
236
|
+
type: Number,
|
|
237
|
+
required: true,
|
|
238
|
+
min: 0,
|
|
239
|
+
default: 0,
|
|
240
|
+
},
|
|
241
|
+
amount: {
|
|
242
|
+
type: Number,
|
|
243
|
+
required: true,
|
|
244
|
+
default: 0,
|
|
245
|
+
},
|
|
246
|
+
bills: {
|
|
247
|
+
type: [ReportReceivedCreditBillSchema],
|
|
248
|
+
default: [],
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
}, { _id: false, minimize: false });
|
|
252
|
+
// Credit bills carry the PAY_LATER ledger (issued + settled tracking /
|
|
253
|
+
// received) — report views (including the Credit Bills sale-type numbers) are
|
|
254
|
+
// derived by the frontend formatter, not stored separately.
|
|
255
|
+
const ReportKeyMetricsSchema = new mongoose_1.Schema({
|
|
256
|
+
[constants_1.REPORT_SALE_TYPE_ENUM.TOTAL_SALES]: {
|
|
257
|
+
type: ReportSaleTypeMetricSchema,
|
|
258
|
+
required: true,
|
|
259
|
+
},
|
|
260
|
+
[constants_1.REPORT_SALE_TYPE_ENUM.AMOUNT_COLLECTED]: {
|
|
261
|
+
type: ReportSaleTypeMetricSchema,
|
|
262
|
+
required: true,
|
|
263
|
+
},
|
|
264
|
+
[constants_1.REPORT_SALE_TYPE_ENUM.CREDIT_BILLS]: {
|
|
265
|
+
type: ReportCreditBillsSchema,
|
|
266
|
+
required: true,
|
|
267
|
+
default: () => ({
|
|
268
|
+
issued: { count: 0, amount: 0, settled_count: 0, settled_amount: 0, bills: [] },
|
|
269
|
+
received: { count: 0, amount: 0, bills: [] },
|
|
270
|
+
}),
|
|
271
|
+
},
|
|
272
|
+
}, { _id: false, minimize: false });
|
|
273
|
+
exports.MaterializedTenantSalesOverviewReportSchema = new mongoose_1.Schema({
|
|
274
|
+
_id: {
|
|
275
|
+
type: mongoose_1.Schema.Types.ObjectId,
|
|
276
|
+
required: true,
|
|
277
|
+
immutable: true,
|
|
278
|
+
},
|
|
279
|
+
tenant_id: {
|
|
280
|
+
type: String,
|
|
281
|
+
required: true,
|
|
282
|
+
immutable: true,
|
|
283
|
+
ref: 'tenants',
|
|
284
|
+
},
|
|
285
|
+
brand_id: {
|
|
286
|
+
type: String,
|
|
287
|
+
required: true,
|
|
288
|
+
},
|
|
289
|
+
scope_context: {
|
|
290
|
+
type: String,
|
|
291
|
+
enum: MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_REPORT_SCOPES,
|
|
292
|
+
required: true,
|
|
293
|
+
},
|
|
294
|
+
scope_field: {
|
|
295
|
+
type: String, // format depends on scope_context — see MATERIALIZED_REPORT_SCOPE_ENUM
|
|
296
|
+
required: true,
|
|
297
|
+
},
|
|
298
|
+
// Denormalized weekday of scope_field — set ONLY on LOCAL_HOUR /
|
|
299
|
+
// LOCAL_DATE documents (MATERIALIZED_REPORT_SCOPES_WITH_DAY_INDEX);
|
|
300
|
+
// null elsewhere. Weekday analysis reads date/hour slices instead of an
|
|
301
|
+
// all-time accumulator document (see MATERIALIZED_REPORTS.md).
|
|
302
|
+
day_index: {
|
|
303
|
+
type: String,
|
|
304
|
+
enum: ENUMS_1.ALL_DAY_INDEX,
|
|
305
|
+
default: null,
|
|
306
|
+
},
|
|
307
|
+
sale_type_key_filters: {
|
|
308
|
+
type: [String],
|
|
309
|
+
enum: MATERIALIZED_REPORTS_1.ALL_MATERIALIZED_REPORT_KEY_FILTERS,
|
|
310
|
+
required: true,
|
|
311
|
+
default: () => [...MATERIALIZED_REPORTS_1.SALES_OVERVIEW_KEY_FILTERS],
|
|
312
|
+
},
|
|
313
|
+
// Map key = the ordered filter combination, e.g. "PLACED:POS_WALK-IN" —
|
|
314
|
+
// the grammar is declared by `sale_type_key_filters` and must be built /
|
|
315
|
+
// parsed through the shared key helper, never ad hoc.
|
|
316
|
+
sale_type_metrics: {
|
|
317
|
+
type: Map,
|
|
318
|
+
of: ReportKeyMetricsSchema,
|
|
319
|
+
required: true,
|
|
320
|
+
default: {},
|
|
321
|
+
},
|
|
322
|
+
// Versioned writes: bumped by `updateMaterializedTenantSalesOverviewReport`
|
|
323
|
+
// on metric changes (replacement / period deltas) AND on received-payment
|
|
324
|
+
// patches that change the ledger; 1 = first materialization. Every bump is
|
|
325
|
+
// recorded in the shared `materialized_tenant_reports_iterations` history
|
|
326
|
+
// (full post-write snapshot + reason) — this document carries no embedded
|
|
327
|
+
// snapshot.
|
|
328
|
+
iteration: {
|
|
329
|
+
type: Number,
|
|
330
|
+
required: true,
|
|
331
|
+
min: 1,
|
|
332
|
+
default: 1,
|
|
333
|
+
},
|
|
334
|
+
schema_version: {
|
|
335
|
+
type: Number,
|
|
336
|
+
required: true,
|
|
337
|
+
immutable: true,
|
|
338
|
+
default: exports.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_SCHEMA_VERSION,
|
|
339
|
+
},
|
|
340
|
+
source_watermark: {
|
|
341
|
+
type: mongoose_1.Schema.Types.Date,
|
|
342
|
+
required: true,
|
|
343
|
+
},
|
|
344
|
+
computed_at: {
|
|
345
|
+
type: mongoose_1.Schema.Types.Date,
|
|
346
|
+
required: true,
|
|
347
|
+
},
|
|
348
|
+
createdAt: {
|
|
349
|
+
type: mongoose_1.Schema.Types.Date,
|
|
350
|
+
required: true,
|
|
351
|
+
default: () => new Date(),
|
|
352
|
+
immutable: true,
|
|
353
|
+
},
|
|
354
|
+
updatedAt: {
|
|
355
|
+
type: mongoose_1.Schema.Types.Date,
|
|
356
|
+
required: true,
|
|
357
|
+
default: () => new Date(),
|
|
358
|
+
},
|
|
359
|
+
}, {
|
|
360
|
+
timestamps: true,
|
|
361
|
+
versionKey: false,
|
|
362
|
+
minimize: false,
|
|
363
|
+
});
|
|
364
|
+
exports.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME = 'materialized_tenant_sales_overview_reports';
|
|
365
|
+
/**
|
|
366
|
+
* Describes the indexes for `materialized_tenant_sales_overview_reports`.
|
|
367
|
+
* Values are Mongo key patterns that can be passed straight to `.hint()`.
|
|
368
|
+
* - `_id`: unique primary key.
|
|
369
|
+
* - `tenant_id__scope_context__scope_field`: UNIQUE index — one document per
|
|
370
|
+
* (tenant, scope context, scope value); also serves the standardized query
|
|
371
|
+
* (`tenant_id` + `scope_context` + `scope_field`).
|
|
372
|
+
* - `brand_id__scope_context__scope_field`: brand-level reads across the
|
|
373
|
+
* brand's tenants (sum across their tenant documents).
|
|
374
|
+
* - `tenant_id__scope_context__day_index__scope_field` (+ brand variant):
|
|
375
|
+
* weekday queries over LOCAL_HOUR / LOCAL_DATE slices
|
|
376
|
+
* (`day_index` + `scope_field` range).
|
|
377
|
+
*/
|
|
378
|
+
exports.MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_INDEXES = {
|
|
379
|
+
_id: { _id: 1 }, // Unique Index
|
|
380
|
+
tenant_id__scope_context__scope_field: {
|
|
381
|
+
tenant_id: 1,
|
|
382
|
+
scope_context: 1,
|
|
383
|
+
scope_field: 1,
|
|
384
|
+
}, // unique — one document per (tenant, scope context, scope value)
|
|
385
|
+
brand_id__scope_context__scope_field: {
|
|
386
|
+
brand_id: 1,
|
|
387
|
+
scope_context: 1,
|
|
388
|
+
scope_field: 1,
|
|
389
|
+
}, // brand-level reads across the brand's tenants
|
|
390
|
+
tenant_id__scope_context__scope_field__day_index: {
|
|
391
|
+
tenant_id: 1,
|
|
392
|
+
scope_context: 1,
|
|
393
|
+
scope_field: 1,
|
|
394
|
+
day_index: 1,
|
|
395
|
+
}, // weekday queries over LOCAL_HOUR / LOCAL_DATE slices
|
|
396
|
+
brand_id__scope_context__scope_field__day_index: {
|
|
397
|
+
brand_id: 1,
|
|
398
|
+
scope_context: 1,
|
|
399
|
+
scope_field: 1,
|
|
400
|
+
day_index: 1,
|
|
401
|
+
}, // brand-level weekday queries over LOCAL_HOUR / LOCAL_DATE slices
|
|
402
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { MATERIALIZED_REPORT_TYPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { IMaterializedTenantSalesOverviewReport } from './materialized_tenant_sales_overview_reports';
|
|
3
|
+
/**
|
|
4
|
+
* Document types for `materialized_tenant_reports_iterations` — the shared iteration
|
|
5
|
+
* history of EVERY materialized report collection (see
|
|
6
|
+
* `MATERIALIZED_REPORTS.md`, "Iteration history").
|
|
7
|
+
*
|
|
8
|
+
* One row per VERSIONED write of a report document; `snapshot` is the full
|
|
9
|
+
* report document AFTER the write, so `(report_id, iteration)` identifies a
|
|
10
|
+
* stored version of that document:
|
|
11
|
+
* - first materialization → `iteration: 1`;
|
|
12
|
+
* - replacement (day/hour write) / reconcile deltas (`subtract` +
|
|
13
|
+
* `additions`) → the report's bumped `iteration`;
|
|
14
|
+
* - received-payment patches THAT CHANGE THE LEDGER are versioned too — they
|
|
15
|
+
* bump `iteration` and append rows (no-op patches write nothing, keeping
|
|
16
|
+
* replays churn-free);
|
|
17
|
+
* - every row carries the backend-generated `reason` the iteration happened
|
|
18
|
+
* (e.g. `NEW_DAY`, `RECLOSE`, `RECONCILE`, `CREDIT_BILL_COLLECTED`).
|
|
19
|
+
*
|
|
20
|
+
* Rows are append-only: a replay rewrites the SAME `(report_id, iteration)`
|
|
21
|
+
* row (idempotent), and a rollback re-applies an older snapshot as a NEW
|
|
22
|
+
* versioned write (with its own row) — the audit trail is never rewritten.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Every materialized report document shape a history row can snapshot —
|
|
26
|
+
* extend this union as report collections land.
|
|
27
|
+
*/
|
|
28
|
+
export type IMaterializedReportSnapshot = IMaterializedTenantSalesOverviewReport;
|
|
29
|
+
/**
|
|
30
|
+
* One persisted iteration row. `snapshot` is untyped in storage (Mixed) since
|
|
31
|
+
* the collection serves every report kind — narrow it via `report_type`.
|
|
32
|
+
*/
|
|
33
|
+
export interface IMaterializedTenantReportIteration<TReportSnapshot = IMaterializedReportSnapshot> {
|
|
34
|
+
_id: string;
|
|
35
|
+
/** Which report collection `report_id` belongs to (`MATERIALIZED_REPORT_TYPE_ENUM`). */
|
|
36
|
+
report_type: MATERIALIZED_REPORT_TYPE_ENUM;
|
|
37
|
+
/** `_id` of the materialized report document this row snapshots. */
|
|
38
|
+
report_id: string;
|
|
39
|
+
/**
|
|
40
|
+
* The report's structural iteration this snapshot represents — equals
|
|
41
|
+
* `snapshot.iteration`; denormalized so history reads can sort/filter
|
|
42
|
+
* without digging into the Mixed snapshot (1 = first materialization).
|
|
43
|
+
*/
|
|
44
|
+
iteration: number;
|
|
45
|
+
/** Snapshot schema version — lets a rollback pick rows it can interpret. */
|
|
46
|
+
schema_version: number;
|
|
47
|
+
/**
|
|
48
|
+
* Why the iteration happened — backend-generated free string (e.g.
|
|
49
|
+
* `NEW_DAY`, `RECLOSE`, `RECONCILE`, `CREDIT_BILL_COLLECTED`).
|
|
50
|
+
*/
|
|
51
|
+
reason: string;
|
|
52
|
+
/** The full report document as written (post-write state). */
|
|
53
|
+
snapshot: TReportSnapshot;
|
|
54
|
+
createdAt: Date;
|
|
55
|
+
updatedAt: Date;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A new history row built by `buildMaterializedTenantReportIteration` — `_id` is assigned
|
|
59
|
+
* by MongoDB on the write (`replaceOne` upserts keyed by
|
|
60
|
+
* `(report_id, iteration)` keep idempotent replays on the same row).
|
|
61
|
+
*/
|
|
62
|
+
export type IMaterializedTenantReportIterationInput<TReportSnapshot = IMaterializedReportSnapshot> = Omit<IMaterializedTenantReportIteration<TReportSnapshot>, '_id'>;
|