@managesome/eposmatic-toolkit 0.4.52 → 0.4.54
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 +106 -2
- package/esm/constants/enums/delivery.js +13 -0
- package/esm/helpers/materialized-reports/index.js +6 -0
- package/esm/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +8 -62
- package/esm/helpers/materialized-reports/materialized-delivery-classification-helpers.js +95 -73
- package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +118 -146
- package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +304 -71
- package/esm/helpers/materialized-reports/materialized-delivery-entry-pages.js +307 -0
- package/esm/helpers/materialized-reports/materialized-delivery-index-migration.js +103 -0
- package/esm/helpers/materialized-reports/materialized-delivery-rows.js +109 -0
- package/esm/helpers/materialized-reports/materialized-delivery-wipe-targets.js +32 -0
- package/esm/helpers/materialized-reports/materialized-document-size.js +65 -0
- package/esm/helpers/materialized-reports/materialized-index-bootstrap.js +31 -16
- package/esm/helpers/materialized-reports/materialized-integer-contract.js +10 -1
- package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +3 -91
- package/esm/helpers/materialized-reports/materialized-tenant-delivery-geo-helpers.js +597 -0
- package/esm/helpers/materialized-reports/report-platform/definition.js +508 -0
- package/esm/helpers/materialized-reports/report-platform/index.js +4 -0
- package/esm/helpers/materialized-reports/report-platform/lifecycle/budget.js +27 -0
- package/esm/helpers/materialized-reports/report-platform/lifecycle/cap-metadata.js +12 -0
- package/esm/helpers/materialized-reports/report-platform/lifecycle/entry-pages.js +121 -0
- package/esm/helpers/materialized-reports/report-platform/lifecycle/flat-projection.js +14 -0
- package/esm/helpers/materialized-reports/report-platform/lifecycle/index.js +8 -0
- package/esm/helpers/materialized-reports/report-platform/lifecycle/iteration-row.js +10 -0
- package/esm/helpers/materialized-reports/report-platform/lifecycle/run-stats.js +42 -0
- package/esm/helpers/materialized-reports/report-platform/lifecycle/scope-refs.js +28 -0
- package/esm/helpers/materialized-reports/report-platform/lifecycle/top-n.js +24 -0
- package/esm/helpers/materialized-reports/report-platform/registry.js +23 -0
- package/esm/helpers/materialized-reports/report-platform/validate-report-registry.js +214 -0
- package/esm/helpers/materialized-reports/scope-helpers.js +98 -0
- package/esm/helpers/materialized-reports/tenant-delivery-formatter.js +70 -20
- package/esm/schemas/models/applied_order_discounts.js +103 -0
- package/esm/schemas/models/index.js +12 -10
- package/esm/schemas/models/materialized_tenant_delivery_entries.js +33 -12
- package/esm/schemas/models/materialized_tenant_delivery_geo_cells.js +83 -0
- package/esm/schemas/models/materialized_tenant_delivery_geo_scopes.js +35 -0
- package/esm/schemas/models/materialized_tenant_delivery_reports.js +10 -1
- package/esm/schemas/types/applied_order_discounts.js +1 -0
- package/esm/schemas/types/index.js +14 -12
- package/esm/schemas/types/materialized_tenant_delivery_geo_cells.js +1 -0
- package/esm/schemas/types/materialized_tenant_delivery_geo_scopes.js +1 -0
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +95 -1
- package/lib/constants/MATERIALIZED_REPORTS.js +108 -4
- package/lib/constants/enums/delivery.d.ts +9 -0
- package/lib/constants/enums/delivery.js +14 -1
- package/lib/helpers/materialized-reports/index.d.ts +6 -0
- package/lib/helpers/materialized-reports/index.js +6 -0
- package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +8 -62
- package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +36 -9
- package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +94 -72
- package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +23 -26
- package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +121 -150
- package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +44 -5
- package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +306 -69
- package/lib/helpers/materialized-reports/materialized-delivery-entry-pages.d.ts +128 -0
- package/lib/helpers/materialized-reports/materialized-delivery-entry-pages.js +322 -0
- package/lib/helpers/materialized-reports/materialized-delivery-index-migration.d.ts +48 -0
- package/lib/helpers/materialized-reports/materialized-delivery-index-migration.js +108 -0
- package/lib/helpers/materialized-reports/materialized-delivery-rows.d.ts +15 -0
- package/lib/helpers/materialized-reports/materialized-delivery-rows.js +115 -0
- package/lib/helpers/materialized-reports/materialized-delivery-wipe-targets.d.ts +17 -0
- package/lib/helpers/materialized-reports/materialized-delivery-wipe-targets.js +35 -0
- package/lib/helpers/materialized-reports/materialized-document-size.d.ts +47 -0
- package/lib/helpers/materialized-reports/materialized-document-size.js +73 -0
- package/lib/helpers/materialized-reports/materialized-index-bootstrap.js +28 -13
- package/lib/helpers/materialized-reports/materialized-integer-contract.js +10 -1
- package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +2 -90
- package/lib/helpers/materialized-reports/materialized-tenant-delivery-geo-helpers.d.ts +186 -0
- package/lib/helpers/materialized-reports/materialized-tenant-delivery-geo-helpers.js +620 -0
- package/lib/helpers/materialized-reports/report-platform/definition.d.ts +133 -0
- package/lib/helpers/materialized-reports/report-platform/definition.js +511 -0
- package/lib/helpers/materialized-reports/report-platform/index.d.ts +4 -0
- package/lib/helpers/materialized-reports/report-platform/index.js +20 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/budget.d.ts +8 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/budget.js +31 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/cap-metadata.d.ts +8 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/cap-metadata.js +16 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/entry-pages.d.ts +40 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/entry-pages.js +130 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/flat-projection.d.ts +22 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/flat-projection.js +17 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/index.d.ts +8 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/index.js +24 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/iteration-row.d.ts +13 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/iteration-row.js +13 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/run-stats.d.ts +20 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/run-stats.js +47 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/scope-refs.d.ts +13 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/scope-refs.js +31 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/top-n.d.ts +16 -0
- package/lib/helpers/materialized-reports/report-platform/lifecycle/top-n.js +29 -0
- package/lib/helpers/materialized-reports/report-platform/registry.d.ts +13 -0
- package/lib/helpers/materialized-reports/report-platform/registry.js +31 -0
- package/lib/helpers/materialized-reports/report-platform/validate-report-registry.d.ts +20 -0
- package/lib/helpers/materialized-reports/report-platform/validate-report-registry.js +218 -0
- package/lib/helpers/materialized-reports/scope-helpers.d.ts +8 -0
- package/lib/helpers/materialized-reports/scope-helpers.js +99 -0
- package/lib/helpers/materialized-reports/tenant-delivery-formatter.d.ts +41 -0
- package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +70 -20
- package/lib/helpers/order/payment-collection.d.ts +25 -0
- package/lib/helpers/order/payment-collection.js +51 -0
- package/lib/schemas/models/applied_order_discounts.d.ts +80 -0
- package/lib/schemas/models/applied_order_discounts.js +106 -0
- package/lib/schemas/models/index.d.ts +12 -10
- package/lib/schemas/models/index.js +12 -10
- package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +31 -9
- package/lib/schemas/models/materialized_tenant_delivery_entries.js +32 -11
- package/lib/schemas/models/materialized_tenant_delivery_geo_cells.d.ts +129 -0
- package/lib/schemas/models/materialized_tenant_delivery_geo_cells.js +86 -0
- package/lib/schemas/models/materialized_tenant_delivery_geo_scopes.d.ts +71 -0
- package/lib/schemas/models/materialized_tenant_delivery_geo_scopes.js +38 -0
- package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +10 -1
- package/lib/schemas/models/materialized_tenant_delivery_reports.js +10 -1
- package/lib/schemas/models/materialized_tenant_discount_sources.d.ts +15 -15
- package/lib/schemas/types/applied_order_discounts.d.ts +22 -0
- package/lib/schemas/types/applied_order_discounts.js +2 -0
- package/lib/schemas/types/index.d.ts +14 -12
- package/lib/schemas/types/index.js +14 -12
- package/lib/schemas/types/materialized_tenant_delivery_entries.d.ts +36 -4
- package/lib/schemas/types/materialized_tenant_delivery_geo_cells.d.ts +178 -0
- package/lib/schemas/types/materialized_tenant_delivery_geo_cells.js +2 -0
- package/lib/schemas/types/materialized_tenant_delivery_geo_scopes.d.ts +21 -0
- package/lib/schemas/types/materialized_tenant_delivery_geo_scopes.js +2 -0
- package/lib/schemas/types/materialized_tenant_delivery_reports.d.ts +49 -7
- package/lib/tests/materialized-reports/analytics-growth-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/analytics-overview-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/analytics-performance-helpers.test.d.ts +1 -0
- package/lib/tests/materialized-reports/characterization/delivery.characterization.test.d.ts +1 -0
- package/lib/tests/materialized-reports/characterization/delivery.characterization.test.js +229 -0
- package/lib/tests/materialized-reports/characterization/discount.characterization.test.d.ts +1 -0
- package/lib/tests/materialized-reports/characterization/discount.characterization.test.js +99 -0
- package/lib/tests/materialized-reports/characterization/fixtures.d.ts +30 -0
- package/lib/tests/materialized-reports/characterization/fixtures.js +402 -0
- package/lib/tests/materialized-reports/characterization/golden.d.ts +18 -0
- package/lib/tests/materialized-reports/characterization/golden.js +127 -0
- package/lib/tests/materialized-reports/characterization/overview.characterization.test.d.ts +1 -0
- package/lib/tests/materialized-reports/characterization/overview.characterization.test.js +69 -0
- package/lib/tests/materialized-reports/characterization/prep-time.characterization.test.d.ts +1 -0
- package/lib/tests/materialized-reports/characterization/prep-time.characterization.test.js +102 -0
- package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.js +56 -19
- package/lib/tests/materialized-reports/materialized-delivery-budgets.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-delivery-budgets.test.js +91 -0
- package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +271 -15
- package/lib/tests/materialized-reports/materialized-delivery-final-audit.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-delivery-final-audit.test.js +128 -0
- package/lib/tests/materialized-reports/materialized-delivery-fold.test.js +42 -5
- package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +13 -1
- package/lib/tests/materialized-reports/materialized-delivery-geo.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-delivery-geo.test.js +446 -0
- package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +117 -25
- package/lib/tests/materialized-reports/materialized-delivery-practical.test.d.ts +1 -0
- package/lib/tests/materialized-reports/materialized-delivery-practical.test.js +344 -0
- package/lib/tests/materialized-reports/materialized-integer-contract.test.js +14 -0
- package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +1 -1
- package/lib/tests/materialized-reports/report-platform/lifecycle-primitives.test.d.ts +1 -0
- package/lib/tests/materialized-reports/report-platform/lifecycle-primitives.test.js +177 -0
- package/lib/tests/materialized-reports/report-platform/registry-conformance.test.d.ts +1 -0
- package/lib/tests/materialized-reports/report-platform/registry-conformance.test.js +214 -0
- package/lib/tests/materialized-reports/report-platform/registry-validation.test.d.ts +1 -0
- package/lib/tests/materialized-reports/report-platform/registry-validation.test.js +103 -0
- package/lib/tests/offer-item-resolution.test.js +1 -4
- package/lib/tests/payment-collection-timestamps.test.d.ts +1 -0
- package/lib/tests/payment-collection-timestamps.test.js +71 -0
- package/package.json +6 -2
|
@@ -4,19 +4,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.isMaterializedDeliveryDayScope = exports.MATERIALIZED_DELIVERY_PERIOD_SCOPES = void 0;
|
|
7
|
-
exports.
|
|
7
|
+
exports.getMaterializedDeliveryReportPeriodDayScopeFields = getMaterializedDeliveryReportPeriodDayScopeFields;
|
|
8
8
|
exports.needsMaterializedDeliveryReportWrite = needsMaterializedDeliveryReportWrite;
|
|
9
9
|
exports.needsMaterializedDeliveryEntriesWrite = needsMaterializedDeliveryEntriesWrite;
|
|
10
10
|
exports.foldMaterializedDeliveryPeriodCounters = foldMaterializedDeliveryPeriodCounters;
|
|
11
|
-
exports.foldMaterializedDeliveryPeriodRows = foldMaterializedDeliveryPeriodRows;
|
|
12
11
|
exports.buildTenantDeliveryCloseDayPlan = buildTenantDeliveryCloseDayPlan;
|
|
13
|
-
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
14
12
|
const mongoose_1 = __importDefault(require("mongoose"));
|
|
15
|
-
const
|
|
13
|
+
const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
|
|
14
|
+
const materialized_tenant_delivery_reports_1 = require("../../schemas/models/materialized_tenant_delivery_reports");
|
|
15
|
+
const materialized_delivery_accumulator_helpers_1 = require("./materialized-delivery-accumulator-helpers");
|
|
16
|
+
const materialized_delivery_classification_helpers_1 = require("./materialized-delivery-classification-helpers");
|
|
17
|
+
const materialized_delivery_compile_helpers_1 = require("./materialized-delivery-compile-helpers");
|
|
18
|
+
const materialized_delivery_entry_pages_1 = require("./materialized-delivery-entry-pages");
|
|
19
|
+
const materialized_document_size_1 = require("./materialized-document-size");
|
|
16
20
|
const materialized_tenant_reports_iterations_helpers_1 = require("./materialized-tenant-reports-iterations-helpers");
|
|
17
21
|
const scope_helpers_1 = require("./scope-helpers");
|
|
18
|
-
const materialized_delivery_classification_helpers_1 = require("./materialized-delivery-classification-helpers");
|
|
19
|
-
const materialized_delivery_accumulator_helpers_1 = require("./materialized-delivery-accumulator-helpers");
|
|
20
22
|
/**
|
|
21
23
|
* M4 close-day helpers.
|
|
22
24
|
*
|
|
@@ -32,51 +34,28 @@ const materialized_delivery_accumulator_helpers_1 = require("./materialized-deli
|
|
|
32
34
|
* A folded document that inherited the STORED document watermark would look unchanged to
|
|
33
35
|
* the write guard and be skipped forever, freezing the period at its first closed day.
|
|
34
36
|
* That bug shipped once in the prep time report; it is designed out here from the start.
|
|
37
|
+
*
|
|
38
|
+
* ## ENTRY pages are NOT folded
|
|
39
|
+
*
|
|
40
|
+
* `materialized_tenant_delivery_entries` is page-partitioned by source day (the discounts
|
|
41
|
+
* split). The plan never reads, merges or folds another day's page: the caller fetches
|
|
42
|
+
* exactly the closing day's pages, each is replaced by its page identity, and a fetched
|
|
43
|
+
* page the day no longer owns is deleted. There is no quarter/semester/year entry scope.
|
|
35
44
|
*/
|
|
36
|
-
/** Period scopes this report materializes (everything but the day scope). */
|
|
37
|
-
exports.MATERIALIZED_DELIVERY_PERIOD_SCOPES =
|
|
45
|
+
/** Period COUNTER scopes this report materializes (everything but the day scope). */
|
|
46
|
+
exports.MATERIALIZED_DELIVERY_PERIOD_SCOPES = MATERIALIZED_REPORTS_1.MATERIALIZED_DELIVERY_REPORT_SCOPES.filter((scope) => String(scope) !== MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
|
|
47
|
+
/**
|
|
48
|
+
* Every `localDate` label whose DAY counter document can contribute to a REPORT period
|
|
49
|
+
* the closing day rolls into - week through YEAR. The worker uses it to fetch exactly
|
|
50
|
+
* the sibling DAY counters the period re-fold needs.
|
|
51
|
+
*/
|
|
52
|
+
function getMaterializedDeliveryReportPeriodDayScopeFields(localDate) {
|
|
53
|
+
return (0, scope_helpers_1.enumerateMaterializedPeriodDays)(localDate, exports.MATERIALIZED_DELIVERY_PERIOD_SCOPES);
|
|
54
|
+
}
|
|
38
55
|
/** True when the scope is the plain day scope. */
|
|
39
56
|
const isMaterializedDeliveryDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
|
|
40
57
|
exports.isMaterializedDeliveryDayScope = isMaterializedDeliveryDayScope;
|
|
41
58
|
const deliveryScopeKey = (doc) => String(doc.scope_context) + '|' + String(doc.scope_field);
|
|
42
|
-
/**
|
|
43
|
-
* Merges TWO period entry sections, keyed on `entry_key`.
|
|
44
|
-
*
|
|
45
|
-
* Rows are deduplicated by identity: a source delivery belongs to exactly one day, so the
|
|
46
|
-
* same `entry_key` twice means the fold saw it twice.
|
|
47
|
-
*/
|
|
48
|
-
function mergeMaterializedDeliveryEntrySections(target, source) {
|
|
49
|
-
const rowsByKey = new Map();
|
|
50
|
-
const seenByKey = new Map();
|
|
51
|
-
const absorb = (input) => {
|
|
52
|
-
if (input == null)
|
|
53
|
-
return;
|
|
54
|
-
for (const [key, section] of Object.entries(input)) {
|
|
55
|
-
const rows = rowsByKey.get(key) ?? [];
|
|
56
|
-
const seen = seenByKey.get(key) ?? new Set();
|
|
57
|
-
for (const row of section?.rows ?? []) {
|
|
58
|
-
const identity = String(row.entry_key ?? row.delivery_id ?? '');
|
|
59
|
-
if (identity !== '') {
|
|
60
|
-
if (seen.has(identity))
|
|
61
|
-
continue;
|
|
62
|
-
seen.add(identity);
|
|
63
|
-
}
|
|
64
|
-
rows.push(row);
|
|
65
|
-
}
|
|
66
|
-
rowsByKey.set(key, rows);
|
|
67
|
-
seenByKey.set(key, seen);
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
absorb(target);
|
|
71
|
-
absorb(source);
|
|
72
|
-
const out = {};
|
|
73
|
-
for (const [key, rows] of rowsByKey) {
|
|
74
|
-
rows.sort((a, b) => new Date(b.delivered_at ?? b.created_at).getTime() - new Date(a.delivered_at ?? a.created_at).getTime() ||
|
|
75
|
-
(String(a.delivery_id) < String(b.delivery_id) ? 1 : -1));
|
|
76
|
-
out[key] = { rows };
|
|
77
|
-
}
|
|
78
|
-
return out;
|
|
79
|
-
}
|
|
80
59
|
/** True when the stored document already carries this exact content. */
|
|
81
60
|
function needsMaterializedDeliveryReportWrite(existing, next) {
|
|
82
61
|
if (existing == null)
|
|
@@ -115,25 +94,6 @@ function foldMaterializedDeliveryPeriodCounters(input) {
|
|
|
115
94
|
}
|
|
116
95
|
return folded;
|
|
117
96
|
}
|
|
118
|
-
/** Folds the statement rows of every day document belonging to one period scope. */
|
|
119
|
-
function foldMaterializedDeliveryPeriodRows(input) {
|
|
120
|
-
const { periodScope, periodScopeField, dayDocuments } = input;
|
|
121
|
-
let merged = {};
|
|
122
|
-
let offApp = 0;
|
|
123
|
-
for (const dayDocument of dayDocuments ?? []) {
|
|
124
|
-
if (!(0, exports.isMaterializedDeliveryDayScope)(String(dayDocument?.scope_context)))
|
|
125
|
-
continue;
|
|
126
|
-
const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(String(dayDocument.scope_field));
|
|
127
|
-
if (fields == null)
|
|
128
|
-
continue;
|
|
129
|
-
if (fields[periodScope] !== periodScopeField)
|
|
130
|
-
continue;
|
|
131
|
-
merged = mergeMaterializedDeliveryEntrySections(merged, dayDocument.delivery_entries ?? null);
|
|
132
|
-
// Off-app orders cannot be rows, so the period total is a SUM of the day counts.
|
|
133
|
-
offApp += Number(dayDocument.off_app_order_count) || 0;
|
|
134
|
-
}
|
|
135
|
-
return { sections: merged, offApp };
|
|
136
|
-
}
|
|
137
97
|
/**
|
|
138
98
|
* Builds the close-day plan for ONE tenant-day.
|
|
139
99
|
*
|
|
@@ -151,6 +111,7 @@ function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
151
111
|
if (typeof reason !== 'string' || reason.length === 0)
|
|
152
112
|
return null;
|
|
153
113
|
const computedAt = input.computedAt ?? new Date();
|
|
114
|
+
const force = input.force === true;
|
|
154
115
|
const compiledReports = (reports ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
155
116
|
const compiledEntries = (entries ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
156
117
|
const storedReports = (input.existing ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
@@ -168,13 +129,18 @@ function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
168
129
|
const storedReportsByScope = new Map();
|
|
169
130
|
for (const doc of storedReports)
|
|
170
131
|
storedReportsByScope.set(deliveryScopeKey(doc), doc);
|
|
171
|
-
const
|
|
172
|
-
for (const doc of storedEntries)
|
|
173
|
-
|
|
132
|
+
const storedEntriesByPage = new Map();
|
|
133
|
+
for (const doc of storedEntries) {
|
|
134
|
+
// Legacy documents without a page are inconsistent input - the caller must only
|
|
135
|
+
// fetch page-partitioned documents (the reader keeps no legacy fallback).
|
|
136
|
+
if (typeof doc.page !== 'string' || doc.page === '')
|
|
137
|
+
return null;
|
|
138
|
+
storedEntriesByPage.set((0, materialized_delivery_entry_pages_1.materializedDeliveryEntryPageKey)(doc), doc);
|
|
139
|
+
}
|
|
174
140
|
const addWrite = (report) => {
|
|
175
141
|
const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
|
|
176
142
|
const iteration = (0, materialized_tenant_reports_iterations_helpers_1.buildMaterializedTenantReportIteration)(persisted, {
|
|
177
|
-
reportType:
|
|
143
|
+
reportType: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_REPORTS,
|
|
178
144
|
reason,
|
|
179
145
|
computedAt,
|
|
180
146
|
});
|
|
@@ -186,7 +152,7 @@ function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
186
152
|
const addEntryWrite = (report) => {
|
|
187
153
|
const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
|
|
188
154
|
const iteration = (0, materialized_tenant_reports_iterations_helpers_1.buildMaterializedTenantReportIteration)(persisted, {
|
|
189
|
-
reportType:
|
|
155
|
+
reportType: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_TYPE_ENUM.TENANT_DELIVERY_ENTRIES,
|
|
190
156
|
reason,
|
|
191
157
|
computedAt,
|
|
192
158
|
});
|
|
@@ -203,23 +169,24 @@ function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
203
169
|
_id: existing?._id ?? new mongoose_1.default.Types.ObjectId().toHexString(),
|
|
204
170
|
iteration: Number(existing?.iteration ?? 0) + 1,
|
|
205
171
|
};
|
|
206
|
-
if (needsMaterializedDeliveryReportWrite(existing, next))
|
|
172
|
+
if (force || needsMaterializedDeliveryReportWrite(existing, next))
|
|
207
173
|
addWrite(next);
|
|
208
174
|
else
|
|
209
175
|
skippedDocuments.push(existing);
|
|
210
176
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
177
|
+
// ---- ENTRY PAGES: exactly the pages the closing day owns ----
|
|
178
|
+
// A page is replaced wholesale; a page with no compiled counterpart is stale and is
|
|
179
|
+
// deleted. No other day's page is in `existingEntries`, so nothing else is touched.
|
|
180
|
+
const compiledEntriesByPage = new Map();
|
|
181
|
+
for (const doc of compiledEntries) {
|
|
182
|
+
const expectedPage = (0, materialized_delivery_entry_pages_1.deliveryEntryPageOf)(doc.scope_context, localDate);
|
|
183
|
+
if (expectedPage == null || (doc.page !== expectedPage && !String(doc.page ?? '').startsWith(expectedPage + '#'))) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
const key = (0, materialized_delivery_entry_pages_1.materializedDeliveryEntryPageKey)(doc);
|
|
187
|
+
if (compiledEntriesByPage.has(key))
|
|
188
|
+
return null; // duplicate page - data issue
|
|
189
|
+
compiledEntriesByPage.set(key, doc);
|
|
223
190
|
}
|
|
224
191
|
// ---- DAY deletion: a stored day doc that no longer compiled ----
|
|
225
192
|
const compiledDateKeys = new Set(compiledReports
|
|
@@ -236,28 +203,12 @@ function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
236
203
|
staleDocuments.push(doc);
|
|
237
204
|
removedDates.push(String(doc.scope_field));
|
|
238
205
|
}
|
|
239
|
-
const compiledDateEntryKeys = new Set(compiledEntries
|
|
240
|
-
.filter((doc) => (0, exports.isMaterializedDeliveryDayScope)(String(doc.scope_context)))
|
|
241
|
-
.map((doc) => deliveryScopeKey(doc)));
|
|
242
|
-
for (const doc of storedEntries) {
|
|
243
|
-
if (!(0, exports.isMaterializedDeliveryDayScope)(String(doc.scope_context)))
|
|
244
|
-
continue;
|
|
245
|
-
if (String(doc.scope_field) !== localDate)
|
|
246
|
-
continue;
|
|
247
|
-
if (compiledDateEntryKeys.has(deliveryScopeKey(doc)))
|
|
248
|
-
continue;
|
|
249
|
-
staleEntryDocuments.push(doc);
|
|
250
|
-
}
|
|
251
206
|
// ---- PERIOD re-fold ----
|
|
252
207
|
const nextDayDocs = storedReports.filter((doc) => (0, exports.isMaterializedDeliveryDayScope)(String(doc.scope_context)) && String(doc.scope_field) !== localDate);
|
|
253
208
|
if (compiledDateDoc != null) {
|
|
254
209
|
nextDayDocs.push(writes.find((w) => (0, exports.isMaterializedDeliveryDayScope)(String(w.report.scope_context)))?.report ??
|
|
255
210
|
compiledDateDoc);
|
|
256
211
|
}
|
|
257
|
-
const nextDayEntryDocs = storedEntries.filter((doc) => (0, exports.isMaterializedDeliveryDayScope)(String(doc.scope_context)) && String(doc.scope_field) !== localDate);
|
|
258
|
-
const compiledDateEntryDoc = entryWrites.find((w) => (0, exports.isMaterializedDeliveryDayScope)(String(w.report.scope_context)))?.report;
|
|
259
|
-
if (compiledDateEntryDoc != null)
|
|
260
|
-
nextDayEntryDocs.push(compiledDateEntryDoc);
|
|
261
212
|
const touchedPeriods = new Map();
|
|
262
213
|
const noteDay = (day) => {
|
|
263
214
|
const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(day);
|
|
@@ -274,11 +225,15 @@ function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
274
225
|
for (const day of removedDates)
|
|
275
226
|
noteDay(day);
|
|
276
227
|
for (const { scope, field } of touchedPeriods.values()) {
|
|
277
|
-
|
|
228
|
+
let folded = foldMaterializedDeliveryPeriodCounters({
|
|
278
229
|
periodScope: scope,
|
|
279
230
|
periodScopeField: field,
|
|
280
231
|
dayDocuments: nextDayDocs,
|
|
281
232
|
});
|
|
233
|
+
// Each DAY is bounded to top-N cities + riders. The period fold unions 28-31 such
|
|
234
|
+
// bounds, so re-bound BOTH maps at the DOCUMENT level after the merge.
|
|
235
|
+
folded = (0, materialized_delivery_compile_helpers_1.pruneDocumentDeliveryAreas)(folded);
|
|
236
|
+
(0, materialized_delivery_compile_helpers_1.pruneDocumentDeliveryRiders)(folded);
|
|
282
237
|
const existing = storedReportsByScope.get(scope + '|' + field);
|
|
283
238
|
const keys = Object.keys(folded).sort();
|
|
284
239
|
if (keys.length === 0) {
|
|
@@ -299,71 +254,87 @@ function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
299
254
|
scope_context: scope,
|
|
300
255
|
scope_field: field,
|
|
301
256
|
day_index: null,
|
|
302
|
-
|
|
257
|
+
// Page-model documents carry the CURRENT key dimensions. Never inherit a
|
|
258
|
+
// pre-page (v1) filter array: the reader's `$all` gate would keep the period
|
|
259
|
+
// document invisible forever.
|
|
260
|
+
sale_type_key_filters: [...MATERIALIZED_REPORTS_1.DELIVERY_KEY_FILTERS],
|
|
303
261
|
delivery_counters: counters,
|
|
304
262
|
delivery_counter_rows: counterRows,
|
|
305
263
|
iteration: existing?.iteration,
|
|
306
|
-
|
|
307
|
-
//
|
|
308
|
-
|
|
264
|
+
// A folded period is always a CURRENT-shape document. v1 data must be wiped and
|
|
265
|
+
// re-closed; it is never upgraded in place.
|
|
266
|
+
schema_version: compiledDateDoc?.schema_version ?? materialized_tenant_delivery_reports_1.MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION,
|
|
267
|
+
// FROM THE DAYS THAT BELONG TO THIS PERIOD - never the stored watermark, and never
|
|
268
|
+
// an unrelated day from another period. Using every sibling day would rewrite a
|
|
269
|
+
// period whenever any other month moved.
|
|
270
|
+
source_watermark: watermarkOfDays(nextDayDocs.filter((dayDoc) => {
|
|
271
|
+
const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(String(dayDoc?.scope_field));
|
|
272
|
+
return fields != null && fields[scope] === field;
|
|
273
|
+
}), existing?.source_watermark ?? computedAt),
|
|
309
274
|
computed_at: computedAt,
|
|
310
275
|
createdAt: existing?.createdAt ?? computedAt,
|
|
311
276
|
updatedAt: computedAt,
|
|
312
277
|
};
|
|
313
278
|
const next = { ...report, iteration: Number(existing?.iteration ?? 0) + 1 };
|
|
314
|
-
if (needsMaterializedDeliveryReportWrite(existing, next))
|
|
279
|
+
if (force || needsMaterializedDeliveryReportWrite(existing, next))
|
|
315
280
|
addWrite(next);
|
|
316
281
|
else
|
|
317
282
|
skippedDocuments.push(existing);
|
|
318
283
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
const existing = storedEntriesByScope.get(scope + '|' + field);
|
|
326
|
-
const keys = Object.keys(sections);
|
|
327
|
-
if (keys.length === 0 && offApp === 0) {
|
|
328
|
-
if (existing != null)
|
|
329
|
-
staleEntryDocuments.push(existing);
|
|
330
|
-
continue;
|
|
331
|
-
}
|
|
332
|
-
let watermark = existing?.source_watermark ?? null;
|
|
333
|
-
for (const dayDoc of nextDayEntryDocs) {
|
|
334
|
-
const fields = (0, scope_helpers_1.getMaterializedPeriodScopeFields)(String(dayDoc.scope_field));
|
|
335
|
-
if (fields == null)
|
|
336
|
-
continue;
|
|
337
|
-
if (fields[scope] !== field)
|
|
338
|
-
continue;
|
|
339
|
-
const time = new Date(dayDoc.source_watermark ?? 0).getTime();
|
|
340
|
-
if (Number.isNaN(time))
|
|
341
|
-
continue;
|
|
342
|
-
if (watermark == null || time > new Date(watermark).getTime())
|
|
343
|
-
watermark = new Date(time);
|
|
344
|
-
}
|
|
345
|
-
const report = {
|
|
284
|
+
// ---- entry PAGE writes (replace by page identity) ----
|
|
285
|
+
for (const compiled of compiledEntriesByPage.values()) {
|
|
286
|
+
const key = (0, materialized_delivery_entry_pages_1.materializedDeliveryEntryPageKey)(compiled);
|
|
287
|
+
const existing = storedEntriesByPage.get(key) ?? null;
|
|
288
|
+
const next = {
|
|
289
|
+
...compiled,
|
|
346
290
|
_id: existing?._id ?? new mongoose_1.default.Types.ObjectId().toHexString(),
|
|
347
|
-
|
|
348
|
-
brand_id: existing?.brand_id ?? compiledDateEntries?.brand_id ?? '',
|
|
349
|
-
scope_context: scope,
|
|
350
|
-
scope_field: field,
|
|
351
|
-
day_index: null,
|
|
352
|
-
sale_type_key_filters: existing?.sale_type_key_filters ?? [...MATERIALIZED_REPORTS_2.DELIVERY_KEY_FILTERS],
|
|
353
|
-
delivery_entries: Object.keys(sections).length === 0 ? null : sections,
|
|
354
|
-
off_app_order_count: offApp,
|
|
355
|
-
iteration: existing?.iteration,
|
|
356
|
-
schema_version: existing?.schema_version ?? 1,
|
|
357
|
-
source_watermark: watermark ?? computedAt,
|
|
358
|
-
computed_at: computedAt,
|
|
359
|
-
createdAt: existing?.createdAt ?? computedAt,
|
|
360
|
-
updatedAt: computedAt,
|
|
291
|
+
iteration: Number(existing?.iteration ?? 0) + 1,
|
|
361
292
|
};
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
293
|
+
if (existing == null || force || needsMaterializedDeliveryEntriesWrite(existing, next)) {
|
|
294
|
+
if (!addEntryWrite(next))
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
366
298
|
skippedEntryDocuments.push(existing);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// Anything fetched but not recompiled is a page this day no longer owns (an emptied
|
|
302
|
+
// day, or a row set that disappeared): delete exactly that page.
|
|
303
|
+
for (const doc of storedEntriesByPage.values()) {
|
|
304
|
+
if (compiledEntriesByPage.has((0, materialized_delivery_entry_pages_1.materializedDeliveryEntryPageKey)(doc)))
|
|
305
|
+
continue;
|
|
306
|
+
staleEntryDocuments.push(doc);
|
|
307
|
+
}
|
|
308
|
+
// BSON BUDGETS: a counter fold or one day's page can exceed its budget even when the
|
|
309
|
+
// compile looked fine. Fail here, naming the collection + day + scope, instead of at
|
|
310
|
+
// bulkWrite inside the close.
|
|
311
|
+
for (const write of writes) {
|
|
312
|
+
(0, materialized_document_size_1.assertMaterializedDocumentSize)(write.report, {
|
|
313
|
+
label: 'materialized_tenant_delivery_reports:' +
|
|
314
|
+
tenantId +
|
|
315
|
+
':' +
|
|
316
|
+
localDate +
|
|
317
|
+
':' +
|
|
318
|
+
String(write.report.scope_context) +
|
|
319
|
+
':' +
|
|
320
|
+
write.report.scope_field,
|
|
321
|
+
maxBytes: MATERIALIZED_REPORTS_1.MATERIALIZED_DELIVERY_REPORT_MAX_BSON_BYTES,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
for (const write of entryWrites) {
|
|
325
|
+
(0, materialized_document_size_1.assertMaterializedDocumentSize)(write.report, {
|
|
326
|
+
label: 'materialized_tenant_delivery_entries:' +
|
|
327
|
+
tenantId +
|
|
328
|
+
':' +
|
|
329
|
+
localDate +
|
|
330
|
+
':' +
|
|
331
|
+
String(write.report.scope_context) +
|
|
332
|
+
':' +
|
|
333
|
+
write.report.scope_field +
|
|
334
|
+
':page=' +
|
|
335
|
+
write.report.page,
|
|
336
|
+
maxBytes: MATERIALIZED_REPORTS_1.MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES,
|
|
337
|
+
});
|
|
367
338
|
}
|
|
368
339
|
return {
|
|
369
340
|
writes,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IMaterializedTenantDeliveryEntries } from '../../schemas/types/materialized_tenant_delivery_entries';
|
|
2
|
-
import { IMaterializedTenantDeliveryReport } from '../../schemas/types/materialized_tenant_delivery_reports';
|
|
2
|
+
import { IMaterializedDeliveryCounters, IMaterializedTenantDeliveryReport } from '../../schemas/types/materialized_tenant_delivery_reports';
|
|
3
3
|
import { IDeliveryOrderSource, TDeliverySource } from './materialized-delivery-classification-helpers';
|
|
4
4
|
/**
|
|
5
5
|
* M4 delivery - the day compile.
|
|
@@ -27,13 +27,52 @@ export interface IDeliveryDayCompileInput {
|
|
|
27
27
|
deliveries?: TDeliverySource[];
|
|
28
28
|
computedAt?: Date;
|
|
29
29
|
}
|
|
30
|
-
/** The
|
|
31
|
-
export declare function
|
|
30
|
+
/** The normalized AREA of an order - the city component of its key (`city`, then `state`). */
|
|
31
|
+
export declare function deliveryAreaOfOrder(order: Pick<IDeliveryOrderSource, 'delivery_details'>): string | null;
|
|
32
|
+
/** The key of an ORDER - status, platform and city, all from the order itself. */
|
|
33
|
+
export declare function deliveryKeyOfOrder(order: Pick<IDeliveryOrderSource, 'order_status' | 'platform' | 'delivery_details'>): string;
|
|
32
34
|
/**
|
|
33
|
-
*
|
|
35
|
+
* Builds the resolver that bounds the CITY dimension to top-N + `__OTHER__`.
|
|
36
|
+
*
|
|
37
|
+
* The CITY is part of the key grammar (`ORDER_STATUS:PLATFORM:CITY`), so an unbounded city set is an
|
|
38
|
+
* unbounded DOCUMENT, not merely a long leaderboard. The top-N cities (by delivery
|
|
39
|
+
* orders, `count DESC, city ASC`) keep their own keys; every other city is resolved to
|
|
40
|
+
* the reserved `__OTHER__` component, which merges them per platform. The area rows and
|
|
41
|
+
* the by-area read then see a bounded `__OTHER__` area, and the totals still reconcile
|
|
42
|
+
* because the merge happens before the adoption sets are finalized.
|
|
43
|
+
*/
|
|
44
|
+
export declare function buildDeliveryCityResolver(orders: readonly IDeliveryOrderSource[], limit?: number): (area: string | null) => string | null;
|
|
45
|
+
/**
|
|
46
|
+
* Bounds the RIDER leaderboard at the DOCUMENT level: top-N riders by record count
|
|
47
|
+
* (`count DESC, rider id ASC`), and every other rider's rows - in WHICHEVER key they
|
|
48
|
+
* appear - are folded into that key's `__OTHER__` row.
|
|
49
|
+
*
|
|
50
|
+
* Pruning per key would bound each key but not the document; a rider working several
|
|
51
|
+
* cities would be kept several times. This runs after the accumulators are finalized and
|
|
52
|
+
* before the counter rows are assembled, so the stored document is genuinely top-N.
|
|
53
|
+
*/
|
|
54
|
+
export declare function pruneDocumentDeliveryRiders(countersByKey: Map<string, IMaterializedDeliveryCounters> | Record<string, IMaterializedDeliveryCounters>, limit?: number): void;
|
|
55
|
+
/**
|
|
56
|
+
* Bounds the CITY dimension of a FOLDED period document.
|
|
57
|
+
*
|
|
58
|
+
* Each DAY document already resolves cities to top-N + `__OTHER__`, but a month has
|
|
59
|
+
* 28-31 such resolvers: the union can exceed the top-N even though every day is bounded.
|
|
60
|
+
* Re-rank the folded city totals and merge every dropped city's key into the matching
|
|
61
|
+
* `ORDER_STATUS:PLATFORM:__OTHER__` key. The area rows are remapped with the key, so the
|
|
62
|
+
* by-area read and the totals stay consistent.
|
|
63
|
+
*/
|
|
64
|
+
export declare function pruneDocumentDeliveryAreas(countersByKey: Record<string, IMaterializedDeliveryCounters>, limit?: number): Record<string, IMaterializedDeliveryCounters>;
|
|
65
|
+
/**
|
|
66
|
+
* Compiles one business day into:
|
|
67
|
+
*
|
|
68
|
+
* - the `localDate` COUNTER report document (the close-day plan re-folds the period
|
|
69
|
+
* counters from the sibling day documents);
|
|
70
|
+
* - ONE ENTRY PAGE per entry scope (date/ week / ISO week / month). The day page uses
|
|
71
|
+
* `page='all'`; each period page is partitioned by the source date, so a document is
|
|
72
|
+
* bounded by ONE day's rows. There is no quarter/semester/year entry document.
|
|
34
73
|
*
|
|
35
74
|
* Returns null when the day has NEITHER a delivery order NOR a delivery - an emptied day
|
|
36
|
-
* writes nothing, and the close-day planner deletes
|
|
75
|
+
* writes nothing, and the close-day planner deletes the pages it used to own.
|
|
37
76
|
*/
|
|
38
77
|
export declare function compileTenantDeliveryReports(input: IDeliveryDayCompileInput): {
|
|
39
78
|
reports: IMaterializedTenantDeliveryReport[];
|