@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,9 +1,14 @@
|
|
|
1
1
|
import { ALL_DAY_INDEX } from '../../constants/ENUMS';
|
|
2
|
-
import { DELIVERY_KEY_FILTERS, MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { DELIVERY_KEY_FILTERS, MATERIALIZED_DELIVERY_AREA_LIMIT, MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES, MATERIALIZED_DELIVERY_ENTRY_SCOPES, MATERIALIZED_DELIVERY_OTHER_TOKEN, MATERIALIZED_DELIVERY_REPORT_MAX_BSON_BYTES, MATERIALIZED_DELIVERY_RIDER_LIMIT, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
3
|
import { DELIVERY_STATUS_ENUM, RIDER_EARNINGS_STATUS_ENUM } from '../../constants/enums/delivery';
|
|
4
4
|
import { MATERIALIZED_TENANT_DELIVERY_ENTRIES_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_delivery_entries';
|
|
5
5
|
import { MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_delivery_reports';
|
|
6
|
-
import {
|
|
6
|
+
import { mergeMaterializedDeliveryCounters } from './materialized-delivery-accumulator-helpers';
|
|
7
|
+
import { addDelivery, addDeliveryOrder, buildMaterializedDeliveryKey, DELIVERY_UNKNOWN_TOKEN, finalizeDeliveryAccumulator, freshDeliveryAccumulator, freshDeliveryCounters, isDeliveryOrder, MATERIALIZED_DELIVERY_KEY_SEPARATOR, parseMaterializedDeliveryKey, } from './materialized-delivery-classification-helpers';
|
|
8
|
+
import { deliveryEntryPageOf, splitMaterializedDeliveryEntryPage } from './materialized-delivery-entry-pages';
|
|
9
|
+
import { emptyMaterializedDeliveryAreaRow, emptyMaterializedDeliveryRiderRow, mergeMaterializedDeliveryAreaRow, mergeMaterializedDeliveryRiderRow, } from './materialized-delivery-rows';
|
|
10
|
+
import { assertMaterializedDocumentSize } from './materialized-document-size';
|
|
11
|
+
import { getMaterializedPeriodScopeFields } from './scope-helpers';
|
|
7
12
|
const watermarkOf = (row, fallback) => {
|
|
8
13
|
const raw = row?.updatedAt ?? row?.createdAt;
|
|
9
14
|
const time = raw == null ? NaN : new Date(raw).getTime();
|
|
@@ -16,31 +21,189 @@ const minutesBetween = (from, to) => {
|
|
|
16
21
|
return null;
|
|
17
22
|
return Math.round((b - a) / 60000);
|
|
18
23
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
/**
|
|
25
|
+
* `YYYY-MM-DD HH:MM` (minute known) or `YYYY-MM-DD HH` (hour only) of the JOINED
|
|
26
|
+
* ORDER's tenant-local time.
|
|
27
|
+
*
|
|
28
|
+
* This is the ONLY local-time source of the report. The delivery write path carries no
|
|
29
|
+
* `localHour`/`localMinute`, and deriving one from the delivery's `createdAt` on the
|
|
30
|
+
* server would report UTC as if it were tenant-local. Minutes are preserved because
|
|
31
|
+
* half-hour tenant offsets (e.g. +05:30) cannot be reconstructed from an hour alone.
|
|
32
|
+
*
|
|
33
|
+
* When `localMinute` is absent, the label stays HOUR-ONLY. This is deliberate: emitting
|
|
34
|
+
* a synthetic `:00` would make the UI derive an offset from a minute that does not exist
|
|
35
|
+
* (e.g. 23:00 - 20:50 = 2h10m instead of the tenant's whole-hour offset). The UI's
|
|
36
|
+
* hour-only branch derives the offset from the whole hour and is correct for that row.
|
|
37
|
+
* A missing local hour leaves the field null - unmeasured, never hour 0.
|
|
38
|
+
*/
|
|
39
|
+
const orderHourLabel = (order) => {
|
|
40
|
+
if (order == null)
|
|
29
41
|
return null;
|
|
30
|
-
const
|
|
31
|
-
|
|
42
|
+
const hour = Number(order.localHour);
|
|
43
|
+
const day = String(order.localDate ?? '');
|
|
44
|
+
if (!Number.isFinite(hour) || day === '')
|
|
45
|
+
return null;
|
|
46
|
+
const whole = Math.floor(hour);
|
|
47
|
+
if (whole < 0 || whole > 23)
|
|
32
48
|
return null;
|
|
33
|
-
|
|
49
|
+
const hourLabel = String(whole).padStart(2, '0');
|
|
50
|
+
if (order.localMinute == null)
|
|
51
|
+
return day + ' ' + hourLabel;
|
|
52
|
+
const minuteRaw = Number(order.localMinute);
|
|
53
|
+
if (!Number.isFinite(minuteRaw) || minuteRaw < 0 || minuteRaw > 59) {
|
|
54
|
+
return day + ' ' + hourLabel;
|
|
55
|
+
}
|
|
56
|
+
return day + ' ' + hourLabel + ':' + String(Math.floor(minuteRaw)).padStart(2, '0');
|
|
34
57
|
};
|
|
35
|
-
/** The
|
|
58
|
+
/** The normalized AREA of an order - the city component of its key (`city`, then `state`). */
|
|
59
|
+
export function deliveryAreaOfOrder(order) {
|
|
60
|
+
const value = order?.delivery_details?.city ?? order?.delivery_details?.state ?? null;
|
|
61
|
+
return value == null ? null : String(value);
|
|
62
|
+
}
|
|
63
|
+
/** The key of an ORDER - status, platform and city, all from the order itself. */
|
|
36
64
|
export function deliveryKeyOfOrder(order) {
|
|
37
|
-
return buildMaterializedDeliveryKey(order?.
|
|
65
|
+
return buildMaterializedDeliveryKey(order?.order_status, order?.platform, deliveryAreaOfOrder(order));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Builds the resolver that bounds the CITY dimension to top-N + `__OTHER__`.
|
|
69
|
+
*
|
|
70
|
+
* The CITY is part of the key grammar (`ORDER_STATUS:PLATFORM:CITY`), so an unbounded city set is an
|
|
71
|
+
* unbounded DOCUMENT, not merely a long leaderboard. The top-N cities (by delivery
|
|
72
|
+
* orders, `count DESC, city ASC`) keep their own keys; every other city is resolved to
|
|
73
|
+
* the reserved `__OTHER__` component, which merges them per platform. The area rows and
|
|
74
|
+
* the by-area read then see a bounded `__OTHER__` area, and the totals still reconcile
|
|
75
|
+
* because the merge happens before the adoption sets are finalized.
|
|
76
|
+
*/
|
|
77
|
+
export function buildDeliveryCityResolver(orders, limit = MATERIALIZED_DELIVERY_AREA_LIMIT) {
|
|
78
|
+
const counts = new Map();
|
|
79
|
+
for (const order of orders ?? []) {
|
|
80
|
+
const area = deliveryAreaOfOrder(order);
|
|
81
|
+
if (area == null)
|
|
82
|
+
continue;
|
|
83
|
+
counts.set(area, (counts.get(area) ?? 0) + 1);
|
|
84
|
+
}
|
|
85
|
+
const ranked = [...counts.entries()].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
|
|
86
|
+
const kept = new Set(ranked.slice(0, Math.max(0, limit)).map(([area]) => area));
|
|
87
|
+
return (area) => {
|
|
88
|
+
if (area == null)
|
|
89
|
+
return null;
|
|
90
|
+
return kept.has(area) ? area : MATERIALIZED_DELIVERY_OTHER_TOKEN;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Bounds the RIDER leaderboard at the DOCUMENT level: top-N riders by record count
|
|
95
|
+
* (`count DESC, rider id ASC`), and every other rider's rows - in WHICHEVER key they
|
|
96
|
+
* appear - are folded into that key's `__OTHER__` row.
|
|
97
|
+
*
|
|
98
|
+
* Pruning per key would bound each key but not the document; a rider working several
|
|
99
|
+
* cities would be kept several times. This runs after the accumulators are finalized and
|
|
100
|
+
* before the counter rows are assembled, so the stored document is genuinely top-N.
|
|
101
|
+
*/
|
|
102
|
+
export function pruneDocumentDeliveryRiders(countersByKey, limit = MATERIALIZED_DELIVERY_RIDER_LIMIT) {
|
|
103
|
+
const countersValues = countersByKey instanceof Map ? [...countersByKey.values()] : Object.values(countersByKey ?? {});
|
|
104
|
+
const totals = new Map();
|
|
105
|
+
for (const counters of countersValues) {
|
|
106
|
+
for (const [riderId, row] of Object.entries(counters.riders ?? {})) {
|
|
107
|
+
totals.set(riderId, (totals.get(riderId) ?? 0) + (Number(row?.count) || 0));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (totals.size <= limit)
|
|
111
|
+
return;
|
|
112
|
+
const ranked = [...totals.entries()].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
|
|
113
|
+
const kept = new Set(ranked.slice(0, Math.max(0, limit)).map(([riderId]) => riderId));
|
|
114
|
+
for (const counters of countersValues) {
|
|
115
|
+
const riders = counters.riders;
|
|
116
|
+
if (riders == null)
|
|
117
|
+
continue;
|
|
118
|
+
const dropped = Object.entries(riders).filter(([riderId]) => !kept.has(riderId));
|
|
119
|
+
if (dropped.length === 0)
|
|
120
|
+
continue;
|
|
121
|
+
const other = riders[MATERIALIZED_DELIVERY_OTHER_TOKEN] ?? emptyMaterializedDeliveryRiderRow();
|
|
122
|
+
for (const [riderId, row] of dropped) {
|
|
123
|
+
mergeMaterializedDeliveryRiderRow(other, row);
|
|
124
|
+
delete riders[riderId];
|
|
125
|
+
}
|
|
126
|
+
// Deterministic display label: the overflow row must not inherit the first
|
|
127
|
+
// merged rider's name (day-doc order differs between live fold and close fold).
|
|
128
|
+
other.rider_name = 'Other riders';
|
|
129
|
+
riders[MATERIALIZED_DELIVERY_OTHER_TOKEN] = other;
|
|
130
|
+
counters.riders = riders;
|
|
131
|
+
}
|
|
38
132
|
}
|
|
39
133
|
/**
|
|
40
|
-
*
|
|
134
|
+
* Bounds the CITY dimension of a FOLDED period document.
|
|
135
|
+
*
|
|
136
|
+
* Each DAY document already resolves cities to top-N + `__OTHER__`, but a month has
|
|
137
|
+
* 28-31 such resolvers: the union can exceed the top-N even though every day is bounded.
|
|
138
|
+
* Re-rank the folded city totals and merge every dropped city's key into the matching
|
|
139
|
+
* `ORDER_STATUS:PLATFORM:__OTHER__` key. The area rows are remapped with the key, so the
|
|
140
|
+
* by-area read and the totals stay consistent.
|
|
141
|
+
*/
|
|
142
|
+
export function pruneDocumentDeliveryAreas(countersByKey, limit = MATERIALIZED_DELIVERY_AREA_LIMIT) {
|
|
143
|
+
if (countersByKey == null)
|
|
144
|
+
return {};
|
|
145
|
+
const cityOrders = new Map();
|
|
146
|
+
for (const counters of Object.values(countersByKey)) {
|
|
147
|
+
for (const [city, row] of Object.entries(counters.areas ?? {})) {
|
|
148
|
+
cityOrders.set(city, (cityOrders.get(city) ?? 0) + (Number(row?.delivery_orders) || 0));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (cityOrders.size <= limit)
|
|
152
|
+
return countersByKey;
|
|
153
|
+
const ranked = [...cityOrders.entries()].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
|
|
154
|
+
const kept = new Set(ranked.slice(0, Math.max(1, limit)).map(([city]) => city));
|
|
155
|
+
// The overflow bucket and the explicit unknown area must always exist.
|
|
156
|
+
kept.add(MATERIALIZED_DELIVERY_OTHER_TOKEN);
|
|
157
|
+
kept.add(DELIVERY_UNKNOWN_TOKEN);
|
|
158
|
+
const result = {};
|
|
159
|
+
const targetKeyOf = (city, key) => {
|
|
160
|
+
if (kept.has(city))
|
|
161
|
+
return key;
|
|
162
|
+
const parsed = parseMaterializedDeliveryKey(key);
|
|
163
|
+
if (parsed == null)
|
|
164
|
+
return key;
|
|
165
|
+
return (parsed.order_status +
|
|
166
|
+
MATERIALIZED_DELIVERY_KEY_SEPARATOR +
|
|
167
|
+
parsed.platform +
|
|
168
|
+
MATERIALIZED_DELIVERY_KEY_SEPARATOR +
|
|
169
|
+
MATERIALIZED_DELIVERY_OTHER_TOKEN);
|
|
170
|
+
};
|
|
171
|
+
for (const [key, counters] of Object.entries(countersByKey)) {
|
|
172
|
+
const parsed = parseMaterializedDeliveryKey(key);
|
|
173
|
+
const city = parsed?.city ?? DELIVERY_UNKNOWN_TOKEN;
|
|
174
|
+
const targetKey = targetKeyOf(city, key);
|
|
175
|
+
if (result[targetKey] == null)
|
|
176
|
+
result[targetKey] = freshDeliveryCounters();
|
|
177
|
+
if (targetKey === key) {
|
|
178
|
+
mergeMaterializedDeliveryCounters(result[targetKey], counters);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
// A dropped city's AREA row must also move to __OTHER__, or by_area would still
|
|
182
|
+
// expose the dropped city through the key that was supposed to be gone.
|
|
183
|
+
const remapped = {
|
|
184
|
+
...counters,
|
|
185
|
+
areas: {},
|
|
186
|
+
};
|
|
187
|
+
const otherArea = emptyMaterializedDeliveryAreaRow();
|
|
188
|
+
for (const row of Object.values(counters.areas ?? {})) {
|
|
189
|
+
mergeMaterializedDeliveryAreaRow(otherArea, row);
|
|
190
|
+
}
|
|
191
|
+
remapped.areas = { [MATERIALIZED_DELIVERY_OTHER_TOKEN]: otherArea };
|
|
192
|
+
mergeMaterializedDeliveryCounters(result[targetKey], remapped);
|
|
193
|
+
}
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Compiles one business day into:
|
|
198
|
+
*
|
|
199
|
+
* - the `localDate` COUNTER report document (the close-day plan re-folds the period
|
|
200
|
+
* counters from the sibling day documents);
|
|
201
|
+
* - ONE ENTRY PAGE per entry scope (date/ week / ISO week / month). The day page uses
|
|
202
|
+
* `page='all'`; each period page is partitioned by the source date, so a document is
|
|
203
|
+
* bounded by ONE day's rows. There is no quarter/semester/year entry document.
|
|
41
204
|
*
|
|
42
205
|
* Returns null when the day has NEITHER a delivery order NOR a delivery - an emptied day
|
|
43
|
-
* writes nothing, and the close-day planner deletes
|
|
206
|
+
* writes nothing, and the close-day planner deletes the pages it used to own.
|
|
44
207
|
*/
|
|
45
208
|
export function compileTenantDeliveryReports(input) {
|
|
46
209
|
if (input == null)
|
|
@@ -55,13 +218,33 @@ export function compileTenantDeliveryReports(input) {
|
|
|
55
218
|
return null;
|
|
56
219
|
}
|
|
57
220
|
const computedAt = input.computedAt ?? new Date();
|
|
58
|
-
const orders = (Array.isArray(input.orders) ? input.orders : []).filter((row) => String(row?.tenant_id ?? tenantId) === tenantId &&
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return null;
|
|
221
|
+
const orders = (Array.isArray(input.orders) ? input.orders : []).filter((row) => String(row?.tenant_id ?? tenantId) === tenantId &&
|
|
222
|
+
isDeliveryOrder(row) &&
|
|
223
|
+
String(row?.localDate ?? localDate) === localDate);
|
|
62
224
|
const ordersById = new Map();
|
|
63
225
|
for (const order of orders)
|
|
64
226
|
ordersById.set(String(order._id), order);
|
|
227
|
+
/**
|
|
228
|
+
* SOURCE DAY: the JOINED ORDER's localDate. A delivery whose order is staged for
|
|
229
|
+
* this day is always included, even if the delivery document itself was created
|
|
230
|
+
* after midnight. A delivery with no staged order is included only when its own
|
|
231
|
+
* `localDate` is this day; that is the explicit unattributable fallback.
|
|
232
|
+
*/
|
|
233
|
+
const deliveries = (Array.isArray(input.deliveries) ? input.deliveries : []).filter((row) => {
|
|
234
|
+
if (String(row?.tenant_id ?? tenantId) !== tenantId)
|
|
235
|
+
return false;
|
|
236
|
+
const orderId = row?.order_id == null ? '' : String(row.order_id);
|
|
237
|
+
const order = orderId === '' ? null : ordersById.get(orderId);
|
|
238
|
+
if (order != null)
|
|
239
|
+
return String(order.localDate ?? localDate) === localDate;
|
|
240
|
+
return String(row?.localDate ?? '') === localDate;
|
|
241
|
+
});
|
|
242
|
+
if (orders.length === 0 && deliveries.length === 0)
|
|
243
|
+
return null;
|
|
244
|
+
// Bound the CITY dimension (it is part of the key grammar) BEFORE accumulating, so
|
|
245
|
+
// both stages and the area rows agree on the top-N + `__OTHER__` resolution.
|
|
246
|
+
const resolveArea = buildDeliveryCityResolver(orders);
|
|
247
|
+
const keyOfOrder = (order) => buildMaterializedDeliveryKey(order?.order_status, order?.platform, resolveArea(deliveryAreaOfOrder(order)));
|
|
65
248
|
const accumulators = new Map();
|
|
66
249
|
const accumulatorFor = (key) => {
|
|
67
250
|
const existing = accumulators.get(key);
|
|
@@ -75,7 +258,10 @@ export function compileTenantDeliveryReports(input) {
|
|
|
75
258
|
// The ORDERS stage first: adoption is a property of the order population, and a
|
|
76
259
|
// delivery that arrives for an unseen order must not invent one.
|
|
77
260
|
for (const order of orders) {
|
|
78
|
-
addDeliveryOrder
|
|
261
|
+
// The RESOLVED key is passed explicitly: recomputing it inside addDeliveryOrder
|
|
262
|
+
// would use the raw city and put the area row under a city the key folded away.
|
|
263
|
+
const key = keyOfOrder(order);
|
|
264
|
+
addDeliveryOrder(accumulatorFor(key), order, key);
|
|
79
265
|
watermarks.push(watermarkOf(order, computedAt));
|
|
80
266
|
}
|
|
81
267
|
const rowsByKey = new Map();
|
|
@@ -84,21 +270,28 @@ export function compileTenantDeliveryReports(input) {
|
|
|
84
270
|
const order = orderId == null ? null : (ordersById.get(orderId) ?? null);
|
|
85
271
|
// A delivery whose order was NOT staged keys on its own (absent) dimensions, so it
|
|
86
272
|
// lands in UNKNOWN rather than being dropped or attributed to a neighbouring city.
|
|
87
|
-
const key = order == null ? buildMaterializedDeliveryKey(null, null) :
|
|
273
|
+
const key = order == null ? buildMaterializedDeliveryKey(null, null, null) : keyOfOrder(order);
|
|
88
274
|
addDelivery(accumulatorFor(key), delivery, order, key);
|
|
89
275
|
watermarks.push(watermarkOf(delivery, computedAt));
|
|
90
276
|
const details = order?.delivery_details ?? {};
|
|
91
|
-
const row = buildEntryRow(delivery, order, details);
|
|
277
|
+
const row = buildEntryRow(delivery, order, details, localDate);
|
|
92
278
|
const bucket = rowsByKey.get(key);
|
|
93
279
|
if (bucket == null)
|
|
94
280
|
rowsByKey.set(key, [row]);
|
|
95
281
|
else
|
|
96
282
|
bucket.push(row);
|
|
97
283
|
}
|
|
284
|
+
const finalized = new Map();
|
|
285
|
+
for (const [key, accumulator] of accumulators) {
|
|
286
|
+
finalized.set(key, finalizeDeliveryAccumulator(accumulator));
|
|
287
|
+
}
|
|
288
|
+
// The rider leaderboard is bounded at the DOCUMENT level, after the adoption figures
|
|
289
|
+
// are derived (they depend on the per-key order sets, not on the rider map).
|
|
290
|
+
pruneDocumentDeliveryRiders(finalized);
|
|
98
291
|
const deliveryCounters = {};
|
|
99
292
|
const deliveryCounterRows = [];
|
|
100
|
-
for (const key of [...
|
|
101
|
-
const counters =
|
|
293
|
+
for (const key of [...finalized.keys()].sort()) {
|
|
294
|
+
const counters = finalized.get(key);
|
|
102
295
|
deliveryCounters[key] = counters;
|
|
103
296
|
deliveryCounterRows.push({ key, counters });
|
|
104
297
|
}
|
|
@@ -116,47 +309,81 @@ export function compileTenantDeliveryReports(input) {
|
|
|
116
309
|
offAppCount += Number(counters?.adoption?.off_app_count) || 0;
|
|
117
310
|
}
|
|
118
311
|
const watermark = watermarks.reduce((latest, current) => (current.getTime() > latest.getTime() ? current : latest), computedAt);
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
312
|
+
const reports = [
|
|
313
|
+
{
|
|
314
|
+
_id: undefined,
|
|
315
|
+
tenant_id: tenantId,
|
|
316
|
+
brand_id: brandId,
|
|
317
|
+
scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
318
|
+
scope_field: localDate,
|
|
319
|
+
day_index: dayIndexOf(localDate),
|
|
320
|
+
sale_type_key_filters: [...DELIVERY_KEY_FILTERS],
|
|
321
|
+
delivery_counters: deliveryCounters,
|
|
322
|
+
delivery_counter_rows: deliveryCounterRows,
|
|
323
|
+
schema_version: MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION,
|
|
324
|
+
source_watermark: watermark,
|
|
325
|
+
computed_at: computedAt,
|
|
326
|
+
createdAt: computedAt,
|
|
327
|
+
updatedAt: computedAt,
|
|
328
|
+
},
|
|
329
|
+
];
|
|
330
|
+
// ---- ENTRY PAGES (one page per source day, max scope LOCAL_MONTH) ----
|
|
331
|
+
// Every entry scope carries the SAME day rows; the difference is the scope value and
|
|
332
|
+
// the page identity. A quarter/semester/year entry document does not exist.
|
|
333
|
+
const periodFields = getMaterializedPeriodScopeFields(localDate);
|
|
334
|
+
if (periodFields == null)
|
|
335
|
+
return null;
|
|
336
|
+
const entrySections = Object.keys(sections).length === 0 ? null : sections;
|
|
337
|
+
const entries = [];
|
|
338
|
+
for (const scope of MATERIALIZED_DELIVERY_ENTRY_SCOPES) {
|
|
339
|
+
const scopeField = periodFields[scope];
|
|
340
|
+
const page = deliveryEntryPageOf(scope, localDate);
|
|
341
|
+
if (typeof scopeField !== 'string' || scopeField === '' || page == null)
|
|
342
|
+
return null;
|
|
343
|
+
const pageDoc = {
|
|
344
|
+
_id: undefined,
|
|
345
|
+
tenant_id: tenantId,
|
|
346
|
+
brand_id: brandId,
|
|
347
|
+
scope_context: scope,
|
|
348
|
+
scope_field: scopeField,
|
|
349
|
+
page,
|
|
350
|
+
day_index: scope === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE ? dayIndexOf(localDate) : null,
|
|
351
|
+
sale_type_key_filters: [...DELIVERY_KEY_FILTERS],
|
|
352
|
+
delivery_entries: entrySections,
|
|
353
|
+
off_app_order_count: offAppCount,
|
|
354
|
+
schema_version: MATERIALIZED_TENANT_DELIVERY_ENTRIES_SCHEMA_VERSION,
|
|
355
|
+
source_watermark: watermark,
|
|
356
|
+
computed_at: computedAt,
|
|
357
|
+
createdAt: computedAt,
|
|
358
|
+
updatedAt: computedAt,
|
|
359
|
+
};
|
|
360
|
+
entries.push(...splitMaterializedDeliveryEntryPage(pageDoc));
|
|
361
|
+
}
|
|
362
|
+
// BSON BUDGETS: fail at COMPILE time, naming the collection + day + scope, rather
|
|
363
|
+
// than at insert time inside the close transaction.
|
|
364
|
+
assertMaterializedDocumentSize(reports[0], {
|
|
365
|
+
label: `materialized_tenant_delivery_reports:${tenantId}:${localDate}:localDate:${localDate}`,
|
|
366
|
+
maxBytes: MATERIALIZED_DELIVERY_REPORT_MAX_BSON_BYTES,
|
|
367
|
+
});
|
|
368
|
+
for (const page of entries) {
|
|
369
|
+
assertMaterializedDocumentSize(page, {
|
|
370
|
+
label: 'materialized_tenant_delivery_entries:' +
|
|
371
|
+
tenantId +
|
|
372
|
+
':' +
|
|
373
|
+
localDate +
|
|
374
|
+
':' +
|
|
375
|
+
String(page.scope_context) +
|
|
376
|
+
':' +
|
|
377
|
+
page.scope_field +
|
|
378
|
+
':page=' +
|
|
379
|
+
page.page,
|
|
380
|
+
maxBytes: MATERIALIZED_DELIVERY_ENTRY_MAX_BSON_BYTES,
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
return { reports, entries };
|
|
157
384
|
}
|
|
158
385
|
/** Builds one statement row from a delivery plus its joined order. */
|
|
159
|
-
function buildEntryRow(delivery, order, details) {
|
|
386
|
+
function buildEntryRow(delivery, order, details, localDate) {
|
|
160
387
|
const createdAt = new Date(delivery?.createdAt ?? Date.now());
|
|
161
388
|
const orderCreatedAt = order?.createdAt == null ? null : new Date(order.createdAt);
|
|
162
389
|
const deliveredAt = delivery?.delivered_at == null ? null : new Date(delivery.delivered_at);
|
|
@@ -183,13 +410,19 @@ function buildEntryRow(delivery, order, details) {
|
|
|
183
410
|
has_dropoff_proof: delivery?.dropoff_food_image_url != null,
|
|
184
411
|
polyline_point_count: Array.isArray(delivery?.polyline) ? delivery.polyline.length : 0,
|
|
185
412
|
platform: order?.platform == null ? DELIVERY_UNKNOWN_TOKEN : String(order.platform),
|
|
186
|
-
|
|
413
|
+
order_status: order?.order_status == null ? DELIVERY_UNKNOWN_TOKEN : String(order.order_status),
|
|
414
|
+
// The key resolves city, then state. The row must use the same fallback or a
|
|
415
|
+
// state-only order would sit under one area in the counters and another in the UI.
|
|
416
|
+
city: details?.city != null ? String(details.city) : details?.state != null ? String(details.state) : null,
|
|
187
417
|
state: details?.state == null ? null : String(details.state),
|
|
188
|
-
|
|
418
|
+
source_local_date: String(order?.localDate ?? delivery?.localDate ?? localDate),
|
|
419
|
+
source_day_unattributed: order == null,
|
|
189
420
|
// Money is integer CENTS.
|
|
190
421
|
net_sale_amount: Math.round(Number(order?.net_sale_amount ?? order?.amount_earned) || 0),
|
|
191
422
|
collected_amount: Math.round(Number(order?.amount_collected) || 0),
|
|
192
|
-
delivery_charge: Math.round(Number(details?.delivery_cost
|
|
423
|
+
delivery_charge: Math.round(Number(details?.delivery_cost ??
|
|
424
|
+
details?.delivery_charge ??
|
|
425
|
+
details?.delivery_charge_amount) || 0),
|
|
193
426
|
order_created_at: orderCreatedAt ?? createdAt,
|
|
194
427
|
assigned_at: delivery?.assigned_at == null ? null : new Date(delivery.assigned_at),
|
|
195
428
|
picked_up_at: delivery?.picked_up_at == null ? null : new Date(delivery.picked_up_at),
|
|
@@ -203,7 +436,7 @@ function buildEntryRow(delivery, order, details) {
|
|
|
203
436
|
status_updated_count: delivery?.status_updated_count == null ? null : Number(delivery.status_updated_count) || 0,
|
|
204
437
|
created_at: createdAt,
|
|
205
438
|
created_at_mins: delivery?.createdAt_mins == null ? null : Number(delivery.createdAt_mins) || 0,
|
|
206
|
-
local_hour:
|
|
439
|
+
local_hour: orderHourLabel(order),
|
|
207
440
|
};
|
|
208
441
|
}
|
|
209
442
|
/** Weekday denormalization for a date document. */
|