@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
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
1
|
import mongoose from 'mongoose';
|
|
3
|
-
import { DELIVERY_KEY_FILTERS, MATERIALIZED_DELIVERY_REPORT_SCOPES, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
4
|
-
import {
|
|
5
|
-
import { getMaterializedPeriodScopeFields } from './scope-helpers';
|
|
6
|
-
import { freshDeliveryCounters } from './materialized-delivery-classification-helpers';
|
|
2
|
+
import { DELIVERY_KEY_FILTERS, MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DELIVERY_REPORT_MAX_BSON_BYTES, MATERIALIZED_DELIVERY_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
|
+
import { MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_delivery_reports';
|
|
7
4
|
import { mergeMaterializedDeliveryCounters } from './materialized-delivery-accumulator-helpers';
|
|
5
|
+
import { freshDeliveryCounters } from './materialized-delivery-classification-helpers';
|
|
6
|
+
import { pruneDocumentDeliveryAreas, pruneDocumentDeliveryRiders } from './materialized-delivery-compile-helpers';
|
|
7
|
+
import { deliveryEntryPageOf, materializedDeliveryEntryPageKey } from './materialized-delivery-entry-pages';
|
|
8
|
+
import { assertMaterializedDocumentSize } from './materialized-document-size';
|
|
9
|
+
import { buildMaterializedTenantReportIteration } from './materialized-tenant-reports-iterations-helpers';
|
|
10
|
+
import { enumerateMaterializedPeriodDays, getMaterializedPeriodScopeFields } from './scope-helpers';
|
|
8
11
|
/**
|
|
9
12
|
* M4 close-day helpers.
|
|
10
13
|
*
|
|
@@ -20,50 +23,27 @@ import { mergeMaterializedDeliveryCounters } from './materialized-delivery-accum
|
|
|
20
23
|
* A folded document that inherited the STORED document watermark would look unchanged to
|
|
21
24
|
* the write guard and be skipped forever, freezing the period at its first closed day.
|
|
22
25
|
* That bug shipped once in the prep time report; it is designed out here from the start.
|
|
26
|
+
*
|
|
27
|
+
* ## ENTRY pages are NOT folded
|
|
28
|
+
*
|
|
29
|
+
* `materialized_tenant_delivery_entries` is page-partitioned by source day (the discounts
|
|
30
|
+
* split). The plan never reads, merges or folds another day's page: the caller fetches
|
|
31
|
+
* exactly the closing day's pages, each is replaced by its page identity, and a fetched
|
|
32
|
+
* page the day no longer owns is deleted. There is no quarter/semester/year entry scope.
|
|
23
33
|
*/
|
|
24
|
-
/** Period scopes this report materializes (everything but the day scope). */
|
|
34
|
+
/** Period COUNTER scopes this report materializes (everything but the day scope). */
|
|
25
35
|
export const MATERIALIZED_DELIVERY_PERIOD_SCOPES = MATERIALIZED_DELIVERY_REPORT_SCOPES.filter((scope) => String(scope) !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
|
|
26
|
-
/** True when the scope is the plain day scope. */
|
|
27
|
-
export const isMaterializedDeliveryDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
|
|
28
|
-
const deliveryScopeKey = (doc) => String(doc.scope_context) + '|' + String(doc.scope_field);
|
|
29
36
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* same `entry_key` twice means the fold saw it twice.
|
|
37
|
+
* Every `localDate` label whose DAY counter document can contribute to a REPORT period
|
|
38
|
+
* the closing day rolls into - week through YEAR. The worker uses it to fetch exactly
|
|
39
|
+
* the sibling DAY counters the period re-fold needs.
|
|
34
40
|
*/
|
|
35
|
-
export function
|
|
36
|
-
|
|
37
|
-
const seenByKey = new Map();
|
|
38
|
-
const absorb = (input) => {
|
|
39
|
-
if (input == null)
|
|
40
|
-
return;
|
|
41
|
-
for (const [key, section] of Object.entries(input)) {
|
|
42
|
-
const rows = rowsByKey.get(key) ?? [];
|
|
43
|
-
const seen = seenByKey.get(key) ?? new Set();
|
|
44
|
-
for (const row of section?.rows ?? []) {
|
|
45
|
-
const identity = String(row.entry_key ?? row.delivery_id ?? '');
|
|
46
|
-
if (identity !== '') {
|
|
47
|
-
if (seen.has(identity))
|
|
48
|
-
continue;
|
|
49
|
-
seen.add(identity);
|
|
50
|
-
}
|
|
51
|
-
rows.push(row);
|
|
52
|
-
}
|
|
53
|
-
rowsByKey.set(key, rows);
|
|
54
|
-
seenByKey.set(key, seen);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
absorb(target);
|
|
58
|
-
absorb(source);
|
|
59
|
-
const out = {};
|
|
60
|
-
for (const [key, rows] of rowsByKey) {
|
|
61
|
-
rows.sort((a, b) => new Date(b.delivered_at ?? b.created_at).getTime() - new Date(a.delivered_at ?? a.created_at).getTime() ||
|
|
62
|
-
(String(a.delivery_id) < String(b.delivery_id) ? 1 : -1));
|
|
63
|
-
out[key] = { rows };
|
|
64
|
-
}
|
|
65
|
-
return out;
|
|
41
|
+
export function getMaterializedDeliveryReportPeriodDayScopeFields(localDate) {
|
|
42
|
+
return enumerateMaterializedPeriodDays(localDate, MATERIALIZED_DELIVERY_PERIOD_SCOPES);
|
|
66
43
|
}
|
|
44
|
+
/** True when the scope is the plain day scope. */
|
|
45
|
+
export const isMaterializedDeliveryDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
|
|
46
|
+
const deliveryScopeKey = (doc) => String(doc.scope_context) + '|' + String(doc.scope_field);
|
|
67
47
|
/** True when the stored document already carries this exact content. */
|
|
68
48
|
export function needsMaterializedDeliveryReportWrite(existing, next) {
|
|
69
49
|
if (existing == null)
|
|
@@ -102,25 +82,6 @@ export function foldMaterializedDeliveryPeriodCounters(input) {
|
|
|
102
82
|
}
|
|
103
83
|
return folded;
|
|
104
84
|
}
|
|
105
|
-
/** Folds the statement rows of every day document belonging to one period scope. */
|
|
106
|
-
export function foldMaterializedDeliveryPeriodRows(input) {
|
|
107
|
-
const { periodScope, periodScopeField, dayDocuments } = input;
|
|
108
|
-
let merged = {};
|
|
109
|
-
let offApp = 0;
|
|
110
|
-
for (const dayDocument of dayDocuments ?? []) {
|
|
111
|
-
if (!isMaterializedDeliveryDayScope(String(dayDocument?.scope_context)))
|
|
112
|
-
continue;
|
|
113
|
-
const fields = getMaterializedPeriodScopeFields(String(dayDocument.scope_field));
|
|
114
|
-
if (fields == null)
|
|
115
|
-
continue;
|
|
116
|
-
if (fields[periodScope] !== periodScopeField)
|
|
117
|
-
continue;
|
|
118
|
-
merged = mergeMaterializedDeliveryEntrySections(merged, dayDocument.delivery_entries ?? null);
|
|
119
|
-
// Off-app orders cannot be rows, so the period total is a SUM of the day counts.
|
|
120
|
-
offApp += Number(dayDocument.off_app_order_count) || 0;
|
|
121
|
-
}
|
|
122
|
-
return { sections: merged, offApp };
|
|
123
|
-
}
|
|
124
85
|
/**
|
|
125
86
|
* Builds the close-day plan for ONE tenant-day.
|
|
126
87
|
*
|
|
@@ -138,6 +99,7 @@ export function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
138
99
|
if (typeof reason !== 'string' || reason.length === 0)
|
|
139
100
|
return null;
|
|
140
101
|
const computedAt = input.computedAt ?? new Date();
|
|
102
|
+
const force = input.force === true;
|
|
141
103
|
const compiledReports = (reports ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
142
104
|
const compiledEntries = (entries ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
143
105
|
const storedReports = (input.existing ?? []).filter((doc) => String(doc?.tenant_id) === tenantId);
|
|
@@ -155,9 +117,14 @@ export function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
155
117
|
const storedReportsByScope = new Map();
|
|
156
118
|
for (const doc of storedReports)
|
|
157
119
|
storedReportsByScope.set(deliveryScopeKey(doc), doc);
|
|
158
|
-
const
|
|
159
|
-
for (const doc of storedEntries)
|
|
160
|
-
|
|
120
|
+
const storedEntriesByPage = new Map();
|
|
121
|
+
for (const doc of storedEntries) {
|
|
122
|
+
// Legacy documents without a page are inconsistent input - the caller must only
|
|
123
|
+
// fetch page-partitioned documents (the reader keeps no legacy fallback).
|
|
124
|
+
if (typeof doc.page !== 'string' || doc.page === '')
|
|
125
|
+
return null;
|
|
126
|
+
storedEntriesByPage.set(materializedDeliveryEntryPageKey(doc), doc);
|
|
127
|
+
}
|
|
161
128
|
const addWrite = (report) => {
|
|
162
129
|
const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
|
|
163
130
|
const iteration = buildMaterializedTenantReportIteration(persisted, {
|
|
@@ -190,23 +157,24 @@ export function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
190
157
|
_id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
|
|
191
158
|
iteration: Number(existing?.iteration ?? 0) + 1,
|
|
192
159
|
};
|
|
193
|
-
if (needsMaterializedDeliveryReportWrite(existing, next))
|
|
160
|
+
if (force || needsMaterializedDeliveryReportWrite(existing, next))
|
|
194
161
|
addWrite(next);
|
|
195
162
|
else
|
|
196
163
|
skippedDocuments.push(existing);
|
|
197
164
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
165
|
+
// ---- ENTRY PAGES: exactly the pages the closing day owns ----
|
|
166
|
+
// A page is replaced wholesale; a page with no compiled counterpart is stale and is
|
|
167
|
+
// deleted. No other day's page is in `existingEntries`, so nothing else is touched.
|
|
168
|
+
const compiledEntriesByPage = new Map();
|
|
169
|
+
for (const doc of compiledEntries) {
|
|
170
|
+
const expectedPage = deliveryEntryPageOf(doc.scope_context, localDate);
|
|
171
|
+
if (expectedPage == null || (doc.page !== expectedPage && !String(doc.page ?? '').startsWith(expectedPage + '#'))) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
const key = materializedDeliveryEntryPageKey(doc);
|
|
175
|
+
if (compiledEntriesByPage.has(key))
|
|
176
|
+
return null; // duplicate page - data issue
|
|
177
|
+
compiledEntriesByPage.set(key, doc);
|
|
210
178
|
}
|
|
211
179
|
// ---- DAY deletion: a stored day doc that no longer compiled ----
|
|
212
180
|
const compiledDateKeys = new Set(compiledReports
|
|
@@ -223,28 +191,12 @@ export function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
223
191
|
staleDocuments.push(doc);
|
|
224
192
|
removedDates.push(String(doc.scope_field));
|
|
225
193
|
}
|
|
226
|
-
const compiledDateEntryKeys = new Set(compiledEntries
|
|
227
|
-
.filter((doc) => isMaterializedDeliveryDayScope(String(doc.scope_context)))
|
|
228
|
-
.map((doc) => deliveryScopeKey(doc)));
|
|
229
|
-
for (const doc of storedEntries) {
|
|
230
|
-
if (!isMaterializedDeliveryDayScope(String(doc.scope_context)))
|
|
231
|
-
continue;
|
|
232
|
-
if (String(doc.scope_field) !== localDate)
|
|
233
|
-
continue;
|
|
234
|
-
if (compiledDateEntryKeys.has(deliveryScopeKey(doc)))
|
|
235
|
-
continue;
|
|
236
|
-
staleEntryDocuments.push(doc);
|
|
237
|
-
}
|
|
238
194
|
// ---- PERIOD re-fold ----
|
|
239
195
|
const nextDayDocs = storedReports.filter((doc) => isMaterializedDeliveryDayScope(String(doc.scope_context)) && String(doc.scope_field) !== localDate);
|
|
240
196
|
if (compiledDateDoc != null) {
|
|
241
197
|
nextDayDocs.push(writes.find((w) => isMaterializedDeliveryDayScope(String(w.report.scope_context)))?.report ??
|
|
242
198
|
compiledDateDoc);
|
|
243
199
|
}
|
|
244
|
-
const nextDayEntryDocs = storedEntries.filter((doc) => isMaterializedDeliveryDayScope(String(doc.scope_context)) && String(doc.scope_field) !== localDate);
|
|
245
|
-
const compiledDateEntryDoc = entryWrites.find((w) => isMaterializedDeliveryDayScope(String(w.report.scope_context)))?.report;
|
|
246
|
-
if (compiledDateEntryDoc != null)
|
|
247
|
-
nextDayEntryDocs.push(compiledDateEntryDoc);
|
|
248
200
|
const touchedPeriods = new Map();
|
|
249
201
|
const noteDay = (day) => {
|
|
250
202
|
const fields = getMaterializedPeriodScopeFields(day);
|
|
@@ -261,11 +213,15 @@ export function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
261
213
|
for (const day of removedDates)
|
|
262
214
|
noteDay(day);
|
|
263
215
|
for (const { scope, field } of touchedPeriods.values()) {
|
|
264
|
-
|
|
216
|
+
let folded = foldMaterializedDeliveryPeriodCounters({
|
|
265
217
|
periodScope: scope,
|
|
266
218
|
periodScopeField: field,
|
|
267
219
|
dayDocuments: nextDayDocs,
|
|
268
220
|
});
|
|
221
|
+
// Each DAY is bounded to top-N cities + riders. The period fold unions 28-31 such
|
|
222
|
+
// bounds, so re-bound BOTH maps at the DOCUMENT level after the merge.
|
|
223
|
+
folded = pruneDocumentDeliveryAreas(folded);
|
|
224
|
+
pruneDocumentDeliveryRiders(folded);
|
|
269
225
|
const existing = storedReportsByScope.get(scope + '|' + field);
|
|
270
226
|
const keys = Object.keys(folded).sort();
|
|
271
227
|
if (keys.length === 0) {
|
|
@@ -286,71 +242,87 @@ export function buildTenantDeliveryCloseDayPlan(input) {
|
|
|
286
242
|
scope_context: scope,
|
|
287
243
|
scope_field: field,
|
|
288
244
|
day_index: null,
|
|
289
|
-
|
|
245
|
+
// Page-model documents carry the CURRENT key dimensions. Never inherit a
|
|
246
|
+
// pre-page (v1) filter array: the reader's `$all` gate would keep the period
|
|
247
|
+
// document invisible forever.
|
|
248
|
+
sale_type_key_filters: [...DELIVERY_KEY_FILTERS],
|
|
290
249
|
delivery_counters: counters,
|
|
291
250
|
delivery_counter_rows: counterRows,
|
|
292
251
|
iteration: existing?.iteration,
|
|
293
|
-
|
|
294
|
-
//
|
|
295
|
-
|
|
252
|
+
// A folded period is always a CURRENT-shape document. v1 data must be wiped and
|
|
253
|
+
// re-closed; it is never upgraded in place.
|
|
254
|
+
schema_version: compiledDateDoc?.schema_version ?? MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION,
|
|
255
|
+
// FROM THE DAYS THAT BELONG TO THIS PERIOD - never the stored watermark, and never
|
|
256
|
+
// an unrelated day from another period. Using every sibling day would rewrite a
|
|
257
|
+
// period whenever any other month moved.
|
|
258
|
+
source_watermark: watermarkOfDays(nextDayDocs.filter((dayDoc) => {
|
|
259
|
+
const fields = getMaterializedPeriodScopeFields(String(dayDoc?.scope_field));
|
|
260
|
+
return fields != null && fields[scope] === field;
|
|
261
|
+
}), existing?.source_watermark ?? computedAt),
|
|
296
262
|
computed_at: computedAt,
|
|
297
263
|
createdAt: existing?.createdAt ?? computedAt,
|
|
298
264
|
updatedAt: computedAt,
|
|
299
265
|
};
|
|
300
266
|
const next = { ...report, iteration: Number(existing?.iteration ?? 0) + 1 };
|
|
301
|
-
if (needsMaterializedDeliveryReportWrite(existing, next))
|
|
267
|
+
if (force || needsMaterializedDeliveryReportWrite(existing, next))
|
|
302
268
|
addWrite(next);
|
|
303
269
|
else
|
|
304
270
|
skippedDocuments.push(existing);
|
|
305
271
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
const existing = storedEntriesByScope.get(scope + '|' + field);
|
|
313
|
-
const keys = Object.keys(sections);
|
|
314
|
-
if (keys.length === 0 && offApp === 0) {
|
|
315
|
-
if (existing != null)
|
|
316
|
-
staleEntryDocuments.push(existing);
|
|
317
|
-
continue;
|
|
318
|
-
}
|
|
319
|
-
let watermark = existing?.source_watermark ?? null;
|
|
320
|
-
for (const dayDoc of nextDayEntryDocs) {
|
|
321
|
-
const fields = getMaterializedPeriodScopeFields(String(dayDoc.scope_field));
|
|
322
|
-
if (fields == null)
|
|
323
|
-
continue;
|
|
324
|
-
if (fields[scope] !== field)
|
|
325
|
-
continue;
|
|
326
|
-
const time = new Date(dayDoc.source_watermark ?? 0).getTime();
|
|
327
|
-
if (Number.isNaN(time))
|
|
328
|
-
continue;
|
|
329
|
-
if (watermark == null || time > new Date(watermark).getTime())
|
|
330
|
-
watermark = new Date(time);
|
|
331
|
-
}
|
|
332
|
-
const report = {
|
|
272
|
+
// ---- entry PAGE writes (replace by page identity) ----
|
|
273
|
+
for (const compiled of compiledEntriesByPage.values()) {
|
|
274
|
+
const key = materializedDeliveryEntryPageKey(compiled);
|
|
275
|
+
const existing = storedEntriesByPage.get(key) ?? null;
|
|
276
|
+
const next = {
|
|
277
|
+
...compiled,
|
|
333
278
|
_id: existing?._id ?? new mongoose.Types.ObjectId().toHexString(),
|
|
334
|
-
|
|
335
|
-
brand_id: existing?.brand_id ?? compiledDateEntries?.brand_id ?? '',
|
|
336
|
-
scope_context: scope,
|
|
337
|
-
scope_field: field,
|
|
338
|
-
day_index: null,
|
|
339
|
-
sale_type_key_filters: existing?.sale_type_key_filters ?? [...DELIVERY_KEY_FILTERS],
|
|
340
|
-
delivery_entries: Object.keys(sections).length === 0 ? null : sections,
|
|
341
|
-
off_app_order_count: offApp,
|
|
342
|
-
iteration: existing?.iteration,
|
|
343
|
-
schema_version: existing?.schema_version ?? 1,
|
|
344
|
-
source_watermark: watermark ?? computedAt,
|
|
345
|
-
computed_at: computedAt,
|
|
346
|
-
createdAt: existing?.createdAt ?? computedAt,
|
|
347
|
-
updatedAt: computedAt,
|
|
279
|
+
iteration: Number(existing?.iteration ?? 0) + 1,
|
|
348
280
|
};
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
281
|
+
if (existing == null || force || needsMaterializedDeliveryEntriesWrite(existing, next)) {
|
|
282
|
+
if (!addEntryWrite(next))
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
353
286
|
skippedEntryDocuments.push(existing);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
// Anything fetched but not recompiled is a page this day no longer owns (an emptied
|
|
290
|
+
// day, or a row set that disappeared): delete exactly that page.
|
|
291
|
+
for (const doc of storedEntriesByPage.values()) {
|
|
292
|
+
if (compiledEntriesByPage.has(materializedDeliveryEntryPageKey(doc)))
|
|
293
|
+
continue;
|
|
294
|
+
staleEntryDocuments.push(doc);
|
|
295
|
+
}
|
|
296
|
+
// BSON BUDGETS: a counter fold or one day's page can exceed its budget even when the
|
|
297
|
+
// compile looked fine. Fail here, naming the collection + day + scope, instead of at
|
|
298
|
+
// bulkWrite inside the close.
|
|
299
|
+
for (const write of writes) {
|
|
300
|
+
assertMaterializedDocumentSize(write.report, {
|
|
301
|
+
label: 'materialized_tenant_delivery_reports:' +
|
|
302
|
+
tenantId +
|
|
303
|
+
':' +
|
|
304
|
+
localDate +
|
|
305
|
+
':' +
|
|
306
|
+
String(write.report.scope_context) +
|
|
307
|
+
':' +
|
|
308
|
+
write.report.scope_field,
|
|
309
|
+
maxBytes: MATERIALIZED_DELIVERY_REPORT_MAX_BSON_BYTES,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
for (const write of entryWrites) {
|
|
313
|
+
assertMaterializedDocumentSize(write.report, {
|
|
314
|
+
label: 'materialized_tenant_delivery_entries:' +
|
|
315
|
+
tenantId +
|
|
316
|
+
':' +
|
|
317
|
+
localDate +
|
|
318
|
+
':' +
|
|
319
|
+
String(write.report.scope_context) +
|
|
320
|
+
':' +
|
|
321
|
+
write.report.scope_field +
|
|
322
|
+
':page=' +
|
|
323
|
+
write.report.page,
|
|
324
|
+
maxBytes: MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES,
|
|
325
|
+
});
|
|
354
326
|
}
|
|
355
327
|
return {
|
|
356
328
|
writes,
|