@managesome/eposmatic-toolkit 0.4.29 → 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.
- package/esm/constants/MATERIALIZED_REPORTS.js +9 -12
- package/esm/helpers/materialized-reports/index.js +1 -0
- package/esm/helpers/materialized-reports/materialized-credit-bills-helpers.js +129 -20
- package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +10 -3
- package/esm/helpers/materialized-reports/materialized-discount-close-day-helpers.js +251 -240
- package/esm/helpers/materialized-reports/materialized-discount-compile-helpers.js +2 -1
- package/esm/helpers/materialized-reports/materialized-discount-source-helpers.js +176 -0
- package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +64 -0
- package/esm/helpers/materialized-reports/materialized-integer-contract.js +30 -4
- package/esm/helpers/materialized-reports/materialized-report-drilldown-rows.js +4 -2
- package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +7 -0
- package/esm/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +284 -43
- package/esm/helpers/materialized-reports/tenant-sales-overview-helpers.js +4 -4
- package/esm/schemas/models/materialized_report_drilldown_rows.js +39 -12
- package/esm/schemas/models/materialized_tenant_discount_entries.js +18 -12
- package/esm/schemas/models/materialized_tenant_discount_sources.js +1 -1
- package/esm/schemas/models/orders.js +5 -0
- package/esm/schemas/models/tenant_calendar.js +12 -0
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +9 -12
- package/lib/constants/MATERIALIZED_REPORTS.js +11 -14
- package/lib/helpers/materialized-reports/index.d.ts +1 -0
- package/lib/helpers/materialized-reports/index.js +1 -0
- package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.d.ts +38 -0
- package/lib/helpers/materialized-reports/materialized-credit-bills-helpers.js +129 -19
- package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +10 -3
- package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +23 -61
- package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +250 -239
- package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.d.ts +4 -4
- package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +1 -0
- package/lib/helpers/materialized-reports/materialized-discount-source-helpers.d.ts +17 -0
- package/lib/helpers/materialized-reports/materialized-discount-source-helpers.js +178 -0
- package/lib/helpers/materialized-reports/materialized-index-bootstrap.d.ts +7 -0
- package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +68 -0
- package/lib/helpers/materialized-reports/materialized-integer-contract.d.ts +3 -1
- package/lib/helpers/materialized-reports/materialized-integer-contract.js +30 -4
- package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.d.ts +2 -2
- package/lib/helpers/materialized-reports/materialized-report-drilldown-rows.js +4 -2
- package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +7 -0
- package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.d.ts +8 -0
- package/lib/helpers/materialized-reports/tenant-sales-overview-close-day-helpers.js +283 -42
- package/lib/helpers/materialized-reports/tenant-sales-overview-helpers.js +4 -4
- package/lib/schemas/models/materialized_report_drilldown_rows.d.ts +40 -6
- package/lib/schemas/models/materialized_report_drilldown_rows.js +39 -12
- package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +33 -29
- package/lib/schemas/models/materialized_tenant_discount_entries.js +18 -12
- package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +6 -6
- package/lib/schemas/models/materialized_tenant_discount_sources.js +1 -1
- package/lib/schemas/models/orders.d.ts +5 -0
- package/lib/schemas/models/orders.js +5 -0
- package/lib/schemas/models/tenant_calendar.d.ts +13 -0
- package/lib/schemas/models/tenant_calendar.js +13 -1
- package/lib/schemas/types/materialized_report_drilldown_rows.d.ts +8 -0
- package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +9 -5
- package/lib/tests/materialized-reports/materialized-credit-bills-helpers.test.js +56 -1
- package/lib/tests/materialized-reports/materialized-discount-classification.test.js +17 -0
- package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +60 -54
- package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.js +14 -13
- package/lib/tests/materialized-reports/materialized-discount-sources.test.js +76 -0
- package/lib/tests/materialized-reports/materialized-integer-contract.test.js +11 -0
- package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.js +2 -2
- package/lib/tests/materialized-reports/tenant-sales-overview-close-day-helpers.test.js +152 -7
- package/lib/tests/materialized-reports/tenant-sales-overview-helpers.test.js +89 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
|
-
import { MATERIALIZED_DISCOUNT_REPORT_SCOPES, MATERIALIZED_REPORT_KEY_FILTER_ENUM, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { MATERIALIZED_DISCOUNT_ENTRY_PAGE_ALL, MATERIALIZED_DISCOUNT_REPORT_SCOPES, MATERIALIZED_REPORT_KEY_FILTER_ENUM, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
3
|
import { accumulateOrderDiscounts, toDiscountHourLabel, } from './materialized-discount-accumulator-helpers';
|
|
4
4
|
import { createEmptyDiscountOrderCounters } from './materialized-discount-classification-helpers';
|
|
5
5
|
import { buildMaterializedReportKey } from './key-helpers';
|
|
@@ -184,6 +184,7 @@ export function compileTenantDiscountReports(input) {
|
|
|
184
184
|
brand_id: brandId,
|
|
185
185
|
scope_context: scope,
|
|
186
186
|
scope_field: scopeField,
|
|
187
|
+
page: isDateScoped ? MATERIALIZED_DISCOUNT_ENTRY_PAGE_ALL : localDate,
|
|
187
188
|
day_index: isDateScoped ? dayIndex : null,
|
|
188
189
|
sale_type_key_filters: [
|
|
189
190
|
MATERIALIZED_REPORT_KEY_FILTER_ENUM.ORDER_STATUS,
|
|
@@ -423,3 +423,179 @@ export function buildDiscountSourcesForScope(input) {
|
|
|
423
423
|
a.source_name.localeCompare(b.source_name) ||
|
|
424
424
|
a.level.localeCompare(b.level));
|
|
425
425
|
}
|
|
426
|
+
const sourceDeltaMetrics = (doc) => ({
|
|
427
|
+
order_count: doc.order_count ?? 0,
|
|
428
|
+
row_count: doc.row_count ?? 0,
|
|
429
|
+
discount_amount: doc.discount_amount ?? 0,
|
|
430
|
+
free_item_count: doc.free_item_count ?? 0,
|
|
431
|
+
free_item_value: doc.free_item_value ?? 0,
|
|
432
|
+
covered_amount: doc.covered_amount ?? 0,
|
|
433
|
+
percentage_row_count: doc.percentage_row_count ?? 0,
|
|
434
|
+
percentage_rate_sum: doc.percentage_rate_sum ?? 0,
|
|
435
|
+
fixed_row_count: doc.fixed_row_count ?? 0,
|
|
436
|
+
});
|
|
437
|
+
const applySourceDelta = (target, delta, sign) => {
|
|
438
|
+
for (const key of Object.keys(target)) {
|
|
439
|
+
target[key] += sign * (delta[key] ?? 0);
|
|
440
|
+
if (!Number.isFinite(target[key]) || target[key] < 0)
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
return true;
|
|
444
|
+
};
|
|
445
|
+
const sourceKindOf = (sourceClass, metrics) => {
|
|
446
|
+
if (sourceClass === MATERIALIZED_DISCOUNT_SOURCE_CLASS_ENUM.COMPLIMENTARY) {
|
|
447
|
+
return MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.COMPLIMENTARY;
|
|
448
|
+
}
|
|
449
|
+
if (metrics.free_item_count > 0 && metrics.free_item_count === metrics.row_count) {
|
|
450
|
+
return MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.FREE_ITEM;
|
|
451
|
+
}
|
|
452
|
+
return MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM.DISCOUNT;
|
|
453
|
+
};
|
|
454
|
+
/**
|
|
455
|
+
* Adds or subtracts one DAY's source contribution from a PERIOD source document.
|
|
456
|
+
*
|
|
457
|
+
* This is the INCREMENTAL source projection: a new close adds the day's
|
|
458
|
+
* contribution; a re-close subtracts the stored day first and adds the fresh
|
|
459
|
+
* one. No period entry rows are read or rebuilt. Returns:
|
|
460
|
+
* - a new document when the source survives;
|
|
461
|
+
* - `null` when the source becomes empty (the caller deletes it);
|
|
462
|
+
* - `null` when the delta cannot be applied safely (negative metrics,
|
|
463
|
+
* identity/schema/scope mismatch) — the caller must fail the close.
|
|
464
|
+
*/
|
|
465
|
+
/**
|
|
466
|
+
* Whether `existing` currently contains at least `delta`'s additive metrics
|
|
467
|
+
* (so a subtraction can be applied without going negative).
|
|
468
|
+
*/
|
|
469
|
+
export function discountSourceDeltaCovers(existing, delta) {
|
|
470
|
+
if (existing == null || delta == null)
|
|
471
|
+
return false;
|
|
472
|
+
const left = sourceDeltaMetrics(existing);
|
|
473
|
+
const right = sourceDeltaMetrics(delta);
|
|
474
|
+
for (const key of Object.keys(left)) {
|
|
475
|
+
if (left[key] < right[key])
|
|
476
|
+
return false;
|
|
477
|
+
}
|
|
478
|
+
for (const keyMetric of delta.key_metrics ?? []) {
|
|
479
|
+
const current = (existing.key_metrics ?? []).find((entry) => entry.key === keyMetric.key);
|
|
480
|
+
if (current == null)
|
|
481
|
+
return false;
|
|
482
|
+
for (const field of [
|
|
483
|
+
'order_count',
|
|
484
|
+
'row_count',
|
|
485
|
+
'discount_amount',
|
|
486
|
+
'free_item_count',
|
|
487
|
+
'free_item_value',
|
|
488
|
+
'covered_amount',
|
|
489
|
+
'percentage_row_count',
|
|
490
|
+
'percentage_rate_sum',
|
|
491
|
+
'fixed_row_count',
|
|
492
|
+
]) {
|
|
493
|
+
if ((current[field] ?? 0) < (keyMetric[field] ?? 0))
|
|
494
|
+
return false;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
export function applyDiscountSourceDelta(existing, delta, direction, computedAt) {
|
|
500
|
+
if (delta == null)
|
|
501
|
+
return null;
|
|
502
|
+
if (existing == null) {
|
|
503
|
+
if (direction < 0)
|
|
504
|
+
return null;
|
|
505
|
+
return {
|
|
506
|
+
...delta,
|
|
507
|
+
computed_at: computedAt,
|
|
508
|
+
createdAt: delta.createdAt ?? computedAt,
|
|
509
|
+
updatedAt: computedAt,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
if (existing.tenant_id !== delta.tenant_id ||
|
|
513
|
+
existing.brand_id !== delta.brand_id ||
|
|
514
|
+
existing.scope_context !== delta.scope_context ||
|
|
515
|
+
existing.scope_field !== delta.scope_field ||
|
|
516
|
+
existing.source_key !== delta.source_key ||
|
|
517
|
+
existing.level !== delta.level ||
|
|
518
|
+
existing.schema_version !== delta.schema_version ||
|
|
519
|
+
(existing.sale_type_key_filters ?? []).join(':') !== (delta.sale_type_key_filters ?? []).join(':')) {
|
|
520
|
+
return null;
|
|
521
|
+
}
|
|
522
|
+
const totals = sourceDeltaMetrics(existing);
|
|
523
|
+
if (!applySourceDelta(totals, sourceDeltaMetrics(delta), direction))
|
|
524
|
+
return null;
|
|
525
|
+
const byKey = new Map();
|
|
526
|
+
for (const keyMetric of existing.key_metrics ?? []) {
|
|
527
|
+
byKey.set(keyMetric.key, {
|
|
528
|
+
order_count: keyMetric.order_count,
|
|
529
|
+
row_count: keyMetric.row_count,
|
|
530
|
+
discount_amount: keyMetric.discount_amount,
|
|
531
|
+
free_item_count: keyMetric.free_item_count,
|
|
532
|
+
free_item_value: keyMetric.free_item_value,
|
|
533
|
+
covered_amount: keyMetric.covered_amount,
|
|
534
|
+
percentage_row_count: keyMetric.percentage_row_count,
|
|
535
|
+
percentage_rate_sum: keyMetric.percentage_rate_sum,
|
|
536
|
+
fixed_row_count: keyMetric.fixed_row_count,
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
for (const keyMetric of delta.key_metrics ?? []) {
|
|
540
|
+
const current = byKey.get(keyMetric.key) ?? {
|
|
541
|
+
order_count: 0,
|
|
542
|
+
row_count: 0,
|
|
543
|
+
discount_amount: 0,
|
|
544
|
+
free_item_count: 0,
|
|
545
|
+
free_item_value: 0,
|
|
546
|
+
covered_amount: 0,
|
|
547
|
+
percentage_row_count: 0,
|
|
548
|
+
percentage_rate_sum: 0,
|
|
549
|
+
fixed_row_count: 0,
|
|
550
|
+
};
|
|
551
|
+
if (!applySourceDelta(current, {
|
|
552
|
+
order_count: keyMetric.order_count,
|
|
553
|
+
row_count: keyMetric.row_count,
|
|
554
|
+
discount_amount: keyMetric.discount_amount,
|
|
555
|
+
free_item_count: keyMetric.free_item_count,
|
|
556
|
+
free_item_value: keyMetric.free_item_value,
|
|
557
|
+
covered_amount: keyMetric.covered_amount,
|
|
558
|
+
percentage_row_count: keyMetric.percentage_row_count,
|
|
559
|
+
percentage_rate_sum: keyMetric.percentage_rate_sum,
|
|
560
|
+
fixed_row_count: keyMetric.fixed_row_count,
|
|
561
|
+
}, direction)) {
|
|
562
|
+
return null;
|
|
563
|
+
}
|
|
564
|
+
byKey.set(keyMetric.key, current);
|
|
565
|
+
}
|
|
566
|
+
const keyMetrics = [...byKey.entries()]
|
|
567
|
+
.filter(([, metrics]) => Object.values(metrics).some((value) => typeof value === 'number' && value !== 0))
|
|
568
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
569
|
+
.map(([key, metrics]) => ({ key, ...metrics }));
|
|
570
|
+
const empty = totals.row_count === 0 &&
|
|
571
|
+
totals.order_count === 0 &&
|
|
572
|
+
totals.discount_amount === 0 &&
|
|
573
|
+
totals.free_item_count === 0 &&
|
|
574
|
+
totals.free_item_value === 0 &&
|
|
575
|
+
totals.covered_amount === 0 &&
|
|
576
|
+
totals.percentage_row_count === 0 &&
|
|
577
|
+
totals.percentage_rate_sum === 0 &&
|
|
578
|
+
totals.fixed_row_count === 0;
|
|
579
|
+
if (empty || keyMetrics.length === 0)
|
|
580
|
+
return null;
|
|
581
|
+
return {
|
|
582
|
+
...existing,
|
|
583
|
+
kind: sourceKindOf(existing.source_class, totals),
|
|
584
|
+
rate_kind: rateKindOf(totals),
|
|
585
|
+
percentage_row_count: totals.percentage_row_count,
|
|
586
|
+
percentage_rate_sum: totals.percentage_rate_sum,
|
|
587
|
+
fixed_row_count: totals.fixed_row_count,
|
|
588
|
+
order_count: totals.order_count,
|
|
589
|
+
row_count: totals.row_count,
|
|
590
|
+
discount_amount: totals.discount_amount,
|
|
591
|
+
free_item_count: totals.free_item_count,
|
|
592
|
+
free_item_value: totals.free_item_value,
|
|
593
|
+
covered_amount: totals.covered_amount,
|
|
594
|
+
key_metrics: keyMetrics,
|
|
595
|
+
source_watermark: direction > 0
|
|
596
|
+
? new Date(Math.max(new Date(existing.source_watermark).getTime(), new Date(delta.source_watermark).getTime()))
|
|
597
|
+
: existing.source_watermark,
|
|
598
|
+
computed_at: computedAt,
|
|
599
|
+
updatedAt: computedAt,
|
|
600
|
+
};
|
|
601
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { MATERIALIZED_TENANT_CREDIT_BILLS_COLLECTION_NAME, MATERIALIZED_TENANT_CREDIT_BILLS_INDEX_OPTIONS, } from '../../schemas/models/materialized_credit_bills';
|
|
2
|
+
import { MATERIALIZED_TENANT_DISCOUNT_ENTRIES_INDEX_OPTIONS, MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_NAME, } from '../../schemas/models/materialized_tenant_discount_entries';
|
|
3
|
+
import { MATERIALIZED_TENANT_DISCOUNT_REPORTS_INDEX_OPTIONS, MATERIALIZED_TENANT_DISCOUNT_REPORTS_COLLECTION_NAME, } from '../../schemas/models/materialized_tenant_discount_reports';
|
|
4
|
+
import { MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS, MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME, } from '../../schemas/models/materialized_tenant_discount_sources';
|
|
5
|
+
import { MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME, MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_reports_iterations';
|
|
6
|
+
import { MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME, MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_INDEX_OPTIONS, } from '../../schemas/models/materialized_tenant_sales_overview_reports';
|
|
7
|
+
import { MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME, MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS, MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME, MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS, } from '../../schemas/models/materialized_report_drilldown_rows';
|
|
8
|
+
import { TENANT_CALENDAR_COLLECTION_NAME, TENANT_CALENDAR_INDEX_OPTIONS } from '../../schemas/models/tenant_calendar';
|
|
9
|
+
/**
|
|
10
|
+
* BOOTSTRAP the indexes the materialized reads/writes assume.
|
|
11
|
+
*
|
|
12
|
+
* The services run with `autoIndex: false`, and the model schemas deliberately
|
|
13
|
+
* do not declare these compound indexes. Until now the only creator was a
|
|
14
|
+
* manual simulation script; a fresh deployment therefore failed every
|
|
15
|
+
* `.hint()` read and could create duplicate scope/year documents. This helper
|
|
16
|
+
* is called after `mongoose.connect` by items/workers/operations and fails the
|
|
17
|
+
* startup if an expected unique index is missing afterwards.
|
|
18
|
+
*
|
|
19
|
+
* It creates indexes with `createIndexes` (never drops unrelated indexes).
|
|
20
|
+
*/
|
|
21
|
+
const INDEXES = {
|
|
22
|
+
[MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_COLLECTION_NAME]: MATERIALIZED_TENANT_SALES_OVERVIEW_REPORTS_INDEX_OPTIONS,
|
|
23
|
+
[MATERIALIZED_TENANT_CREDIT_BILLS_COLLECTION_NAME]: MATERIALIZED_TENANT_CREDIT_BILLS_INDEX_OPTIONS,
|
|
24
|
+
[MATERIALIZED_TENANT_DISCOUNT_REPORTS_COLLECTION_NAME]: MATERIALIZED_TENANT_DISCOUNT_REPORTS_INDEX_OPTIONS,
|
|
25
|
+
[MATERIALIZED_TENANT_DISCOUNT_ENTRIES_COLLECTION_NAME]: MATERIALIZED_TENANT_DISCOUNT_ENTRIES_INDEX_OPTIONS,
|
|
26
|
+
[MATERIALIZED_TENANT_DISCOUNT_SOURCES_COLLECTION_NAME]: MATERIALIZED_TENANT_DISCOUNT_SOURCES_INDEX_OPTIONS,
|
|
27
|
+
[MATERIALIZED_TENANT_REPORTS_ITERATIONS_COLLECTION_NAME]: MATERIALIZED_TENANT_REPORTS_ITERATIONS_INDEX_OPTIONS,
|
|
28
|
+
[MATERIALIZED_REPORT_DRILLDOWN_ROWS_COLLECTION_NAME]: MATERIALIZED_REPORT_DRILLDOWN_ROWS_INDEX_OPTIONS,
|
|
29
|
+
[MATERIALIZED_REPORT_DRILLDOWN_SCOPES_COLLECTION_NAME]: MATERIALIZED_REPORT_DRILLDOWN_SCOPES_INDEX_OPTIONS,
|
|
30
|
+
[TENANT_CALENDAR_COLLECTION_NAME]: TENANT_CALENDAR_INDEX_OPTIONS,
|
|
31
|
+
};
|
|
32
|
+
export const MATERIALIZED_REPORT_INDEX_COLLECTIONS = Object.keys(INDEXES);
|
|
33
|
+
export async function ensureMaterializedReportIndexes(connection, options = {}) {
|
|
34
|
+
const db = connection.db;
|
|
35
|
+
if (db == null)
|
|
36
|
+
throw new Error('ensureMaterializedReportIndexes: no database connection');
|
|
37
|
+
const collections = options.collections ?? MATERIALIZED_REPORT_INDEX_COLLECTIONS;
|
|
38
|
+
for (const collectionName of collections) {
|
|
39
|
+
const definitions = INDEXES[collectionName];
|
|
40
|
+
if (definitions == null) {
|
|
41
|
+
throw new Error('ensureMaterializedReportIndexes: unknown collection ' + collectionName);
|
|
42
|
+
}
|
|
43
|
+
const collection = db.collection(collectionName);
|
|
44
|
+
const indexes = definitions.filter((definition) => definition.name !== '_id_');
|
|
45
|
+
try {
|
|
46
|
+
await collection.createIndexes(indexes);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
throw new Error('ensureMaterializedReportIndexes: failed to create indexes for ' +
|
|
50
|
+
collectionName +
|
|
51
|
+
': ' +
|
|
52
|
+
(error instanceof Error ? error.message : String(error)));
|
|
53
|
+
}
|
|
54
|
+
const existing = await collection.listIndexes().toArray();
|
|
55
|
+
const names = new Set(existing.map((index) => index.name));
|
|
56
|
+
const missing = indexes.filter((index) => !names.has(index.name));
|
|
57
|
+
if (missing.length > 0) {
|
|
58
|
+
throw new Error('ensureMaterializedReportIndexes: ' +
|
|
59
|
+
collectionName +
|
|
60
|
+
' is missing expected indexes ' +
|
|
61
|
+
missing.map((index) => index.name).join(', '));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -43,21 +43,43 @@ const IDENTIFIER_FIELD = /^(scope_field|day_index|key|_id|.*_id)$/;
|
|
|
43
43
|
* integer contract applies to money/rate/count/duration leaves. Only reject a
|
|
44
44
|
* numeric-looking string when it sits in a field that is not one of these.
|
|
45
45
|
*/
|
|
46
|
-
const TEXT_FIELD = /(name|label|title|reason|note|description|reference|code|currency|platform|status|type|source_key|order_number)$/i;
|
|
46
|
+
const TEXT_FIELD = /(name|label|title|reason|note|description|reference|reference_number|code|currency|platform|status|type|source_key|order_number)$/i;
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
48
|
+
* BSON values: only IDENTITY types are ignored. A numeric BSON value
|
|
49
|
+
* (Decimal128 / Long / Double / Int32) is storage of a number and must obey the
|
|
50
|
+
* same integer contract as a JS number — otherwise a Decimal128 decimal would
|
|
51
|
+
* bypass the validator entirely.
|
|
49
52
|
*
|
|
50
53
|
* Duck-typed on `_bsontype` so the toolkit keeps no mongoose dependency
|
|
51
54
|
* (it is published to the frontends too).
|
|
52
55
|
*/
|
|
53
|
-
const
|
|
56
|
+
const BSON_NUMERIC_TYPES = new Set([
|
|
57
|
+
'Decimal128',
|
|
58
|
+
'Long',
|
|
59
|
+
'Double',
|
|
60
|
+
'Int32',
|
|
61
|
+
'NumberDecimal',
|
|
62
|
+
'NumberDouble',
|
|
63
|
+
'NumberLong',
|
|
64
|
+
'NumberInt',
|
|
65
|
+
]);
|
|
66
|
+
const getBsonType = (value) => {
|
|
67
|
+
if (typeof value !== 'object' || value === null)
|
|
68
|
+
return null;
|
|
69
|
+
const type = value._bsontype;
|
|
70
|
+
return typeof type === 'string' ? type : null;
|
|
71
|
+
};
|
|
72
|
+
const isBsonNumeric = (value) => {
|
|
73
|
+
const type = getBsonType(value);
|
|
74
|
+
return type != null && BSON_NUMERIC_TYPES.has(type);
|
|
75
|
+
};
|
|
54
76
|
/** Dates, buffers and other non-numeric leaves are ignored. */
|
|
55
77
|
const isIgnoredLeaf = (value) => value === null ||
|
|
56
78
|
typeof value === 'boolean' ||
|
|
57
79
|
value instanceof Date ||
|
|
58
80
|
value instanceof RegExp ||
|
|
59
81
|
(typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) ||
|
|
60
|
-
|
|
82
|
+
(getBsonType(value) != null && !isBsonNumeric(value));
|
|
61
83
|
const childPath = (parent, key) => (parent.length === 0 ? key : `${parent}.${key}`);
|
|
62
84
|
/**
|
|
63
85
|
* Collects every numeric leaf that violates the integer-only contract.
|
|
@@ -73,6 +95,10 @@ export function findNonIntegerNumericLeaves(document) {
|
|
|
73
95
|
const walk = (node, path) => {
|
|
74
96
|
if (node === undefined)
|
|
75
97
|
return;
|
|
98
|
+
if (isBsonNumeric(node)) {
|
|
99
|
+
violations.push({ path, value: node, reason: 'bson-numeric' });
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
76
102
|
if (isIgnoredLeaf(node))
|
|
77
103
|
return;
|
|
78
104
|
if (typeof node === 'number') {
|
|
@@ -66,7 +66,7 @@ export const getDrilldownRowsKey = (reportType) => ROW_SPECS[reportType].rowsKey
|
|
|
66
66
|
* usable time anchor are skipped and counted; the writer can decide whether to
|
|
67
67
|
* fail loudly.
|
|
68
68
|
*/
|
|
69
|
-
export function buildMaterializedDrilldownRows(reportType, document) {
|
|
69
|
+
export function buildMaterializedDrilldownRows(reportType, document, sourceLocalDate = null) {
|
|
70
70
|
if (document == null)
|
|
71
71
|
return { rows: [], skipped: 0 };
|
|
72
72
|
const spec = ROW_SPECS[reportType];
|
|
@@ -117,6 +117,7 @@ export function buildMaterializedDrilldownRows(reportType, document) {
|
|
|
117
117
|
cursor_id: cursorId,
|
|
118
118
|
time,
|
|
119
119
|
data: row,
|
|
120
|
+
source_local_date: sourceLocalDate,
|
|
120
121
|
schema_version: MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION,
|
|
121
122
|
computed_at: computedAt,
|
|
122
123
|
});
|
|
@@ -136,7 +137,7 @@ export function buildMaterializedDrilldownRowsFromDocuments(reportType, document
|
|
|
136
137
|
return { rows, skipped };
|
|
137
138
|
}
|
|
138
139
|
/** Scope metadata (row count + delivery off-app count) for the flat reader. */
|
|
139
|
-
export function buildMaterializedDrilldownScope(reportType, document, rows) {
|
|
140
|
+
export function buildMaterializedDrilldownScope(reportType, document, rows, sourceLocalDate = null) {
|
|
140
141
|
if (document == null)
|
|
141
142
|
return null;
|
|
142
143
|
const tenantId = asString(document.tenant_id);
|
|
@@ -156,6 +157,7 @@ export function buildMaterializedDrilldownScope(reportType, document, rows) {
|
|
|
156
157
|
scope_field: scopeField,
|
|
157
158
|
row_count: rows.length,
|
|
158
159
|
off_app_order_count: offApp,
|
|
160
|
+
source_local_date: sourceLocalDate,
|
|
159
161
|
schema_version: MATERIALIZED_REPORT_DRILLDOWN_ROWS_SCHEMA_VERSION,
|
|
160
162
|
computed_at: toDate(document.computed_at) ?? new Date(),
|
|
161
163
|
};
|
|
@@ -640,6 +640,13 @@ const decodeMaterializedReportScopeGroups = (selection) => {
|
|
|
640
640
|
// never disagree.
|
|
641
641
|
const window = resolveTimeOfDayWindow(selection);
|
|
642
642
|
const weekdayFilter = getActiveWeekdayFilter(selection.weekdays);
|
|
643
|
+
// A half-set / invalid / unservable time window must NEVER silently fall
|
|
644
|
+
// through to the full period: the caller asked for a time refinement, so an
|
|
645
|
+
// unresolvable one is an invalid selection (the read parser turns this null
|
|
646
|
+
// into a 400).
|
|
647
|
+
if ((selection.timeStart != null || selection.timeEnd != null) && window == null) {
|
|
648
|
+
return null;
|
|
649
|
+
}
|
|
643
650
|
if (window != null || weekdayFilter != null) {
|
|
644
651
|
const days = getDayKeysFromCalendarSelection(selection);
|
|
645
652
|
if (days.length === 0)
|