@managesome/eposmatic-toolkit 0.4.10 → 0.4.11
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 +7 -0
- package/esm/constants/enums/delivery.js +8 -0
- package/esm/helpers/materialized-reports/materialized-cash-classification-helpers.js +5 -43
- package/esm/helpers/materialized-reports/materialized-cash-close-day-helpers.js +3 -3
- package/esm/helpers/materialized-reports/materialized-cash-compile-helpers.js +5 -4
- package/esm/helpers/materialized-reports/materialized-delivery-classification-helpers.js +6 -2
- package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +3 -2
- package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +10 -8
- package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +2 -1
- package/esm/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +7 -3
- package/esm/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +2 -1
- package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +15 -14
- package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +2 -2
- package/esm/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +9 -6
- package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +4 -4
- package/esm/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +5 -6
- package/esm/helpers/materialized-reports/tenant-cash-in-out-formatter.js +2 -1
- package/esm/helpers/materialized-reports/tenant-delivery-formatter.js +2 -2
- package/esm/helpers/materialized-reports/tenant-discount-formatter.js +2 -2
- package/esm/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +3 -2
- package/esm/helpers/materialized-reports/tenant-payments-comparison-formatter.js +7 -4
- package/esm/helpers/materialized-reports/tenant-sales-comparison-formatter.js +7 -4
- package/esm/helpers/reporting-contract/overview-contract.js +5 -4
- package/esm/helpers/rider-earnings/rider-earnings.js +0 -6
- package/esm/helpers/rider-reports/rider-daily-report.js +2 -2
- package/esm/helpers/store/common-order-actions.js +3 -2
- package/esm/schemas/models/deliveries.js +11 -5
- package/esm/schemas/models/materialized_credit_bills.js +1 -1
- package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +1 -1
- package/lib/constants/MATERIALIZED_REPORTS.d.ts +6 -0
- package/lib/constants/MATERIALIZED_REPORTS.js +9 -2
- package/lib/constants/enums/delivery.d.ts +7 -0
- package/lib/constants/enums/delivery.js +9 -1
- package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.d.ts +55 -3
- package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.js +4 -42
- package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.d.ts +2 -1
- package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.js +2 -2
- package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.d.ts +3 -2
- package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.js +4 -3
- package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +32 -3
- package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +5 -1
- package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +2 -1
- package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +8 -7
- package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +4 -3
- package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +8 -6
- package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +2 -1
- package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +6 -2
- package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +2 -1
- package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.d.ts +34 -2
- package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +7 -5
- package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +14 -13
- package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.d.ts +2 -1
- package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +1 -1
- package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.d.ts +6 -3
- package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +8 -5
- package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +2 -2
- package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +3 -4
- package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.js +2 -1
- package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +1 -1
- package/lib/helpers/materialized-reports/tenant-discount-formatter.js +1 -1
- package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +3 -2
- package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.js +7 -4
- package/lib/helpers/materialized-reports/tenant-prep-time-formatter.d.ts +2 -1
- package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.js +7 -4
- package/lib/helpers/reporting-contract/overview-contract.js +5 -4
- package/lib/helpers/rider-earnings/rider-earnings.d.ts +0 -7
- package/lib/helpers/rider-earnings/rider-earnings.js +0 -7
- package/lib/helpers/rider-reports/rider-daily-report.d.ts +4 -2
- package/lib/helpers/rider-reports/rider-daily-report.js +1 -1
- package/lib/helpers/store/common-order-actions.js +3 -2
- package/lib/helpers/types/materialized-reports-apis.d.ts +3 -0
- package/lib/schemas/models/deliveries.d.ts +10 -7
- package/lib/schemas/models/deliveries.js +10 -4
- package/lib/schemas/models/materialized_credit_bills.d.ts +3 -3
- package/lib/schemas/models/materialized_credit_bills.js +1 -1
- package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +3 -3
- package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +1 -1
- package/lib/schemas/types/deliveries.d.ts +2 -2
- package/lib/schemas/types/materialized_tenant_sales_overview_reports.d.ts +7 -0
- package/lib/tests/deliveries-schema.test.d.ts +1 -0
- package/lib/tests/deliveries-schema.test.js +70 -0
- package/lib/tests/materialized-reports/materialized-cash-compile.test.js +2 -1
- package/lib/tests/materialized-reports/materialized-cash-formatter.test.js +2 -1
- package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +7 -6
- package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +2 -1
- package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +7 -6
- package/lib/tests/materialized-reports/materialized-reports-scope-group-helpers.test.js +13 -13
- package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.js +2 -1
- package/lib/tests/rider-daily-report.test.js +3 -3
- package/package.json +1 -1
|
@@ -52,6 +52,13 @@ export var MATERIALIZED_REPORT_SCOPE_ENUM;
|
|
|
52
52
|
MATERIALIZED_REPORT_SCOPE_ENUM["LOCAL_YEAR"] = "localYear";
|
|
53
53
|
})(MATERIALIZED_REPORT_SCOPE_ENUM || (MATERIALIZED_REPORT_SCOPE_ENUM = {}));
|
|
54
54
|
export const ALL_MATERIALIZED_REPORT_SCOPES = Object.values(MATERIALIZED_REPORT_SCOPE_ENUM);
|
|
55
|
+
/** Time-window refinement used when compiling/reading a report scope. */
|
|
56
|
+
export var MATERIALIZED_REPORT_WINDOW_KIND_ENUM;
|
|
57
|
+
(function (MATERIALIZED_REPORT_WINDOW_KIND_ENUM) {
|
|
58
|
+
MATERIALIZED_REPORT_WINDOW_KIND_ENUM["RECURRING"] = "RECURRING";
|
|
59
|
+
MATERIALIZED_REPORT_WINDOW_KIND_ENUM["SPAN"] = "SPAN";
|
|
60
|
+
})(MATERIALIZED_REPORT_WINDOW_KIND_ENUM || (MATERIALIZED_REPORT_WINDOW_KIND_ENUM = {}));
|
|
61
|
+
export const ALL_MATERIALIZED_REPORT_WINDOW_KINDS = Object.values(MATERIALIZED_REPORT_WINDOW_KIND_ENUM);
|
|
55
62
|
/**
|
|
56
63
|
* Scopes whose documents carry the denormalized `day_index` weekday field
|
|
57
64
|
* (derived from `scope_field` by the materializer, `DAY_INDEX_ENUM`). All
|
|
@@ -22,6 +22,14 @@ export var RIDER_FEE_TYPE_ENUM;
|
|
|
22
22
|
RIDER_FEE_TYPE_ENUM["DISTANCE_RETURN"] = "DISTANCE_RETURN";
|
|
23
23
|
})(RIDER_FEE_TYPE_ENUM || (RIDER_FEE_TYPE_ENUM = {}));
|
|
24
24
|
export const ALL_RIDER_FEE_TYPES = Object.values(RIDER_FEE_TYPE_ENUM);
|
|
25
|
+
/** Server-owned rider payout lifecycle. */
|
|
26
|
+
export var RIDER_EARNINGS_STATUS_ENUM;
|
|
27
|
+
(function (RIDER_EARNINGS_STATUS_ENUM) {
|
|
28
|
+
RIDER_EARNINGS_STATUS_ENUM["PENDING"] = "PENDING";
|
|
29
|
+
RIDER_EARNINGS_STATUS_ENUM["FINAL"] = "FINAL";
|
|
30
|
+
RIDER_EARNINGS_STATUS_ENUM["VOID"] = "VOID";
|
|
31
|
+
})(RIDER_EARNINGS_STATUS_ENUM || (RIDER_EARNINGS_STATUS_ENUM = {}));
|
|
32
|
+
export const ALL_RIDER_EARNINGS_STATUS = Object.values(RIDER_EARNINGS_STATUS_ENUM);
|
|
25
33
|
// ======================>> <<====================== //
|
|
26
34
|
export var DELIVERY_STATUS_ENUM;
|
|
27
35
|
(function (DELIVERY_STATUS_ENUM) {
|
|
@@ -1,44 +1,4 @@
|
|
|
1
|
-
import { CASH_IN_OUT_REF_ENUM, CASH_IN_OUT_TYPE_ENUM } from '../../constants/enums/cash';
|
|
2
|
-
/**
|
|
3
|
-
* M3 cash - lifecycle classifier + accumulators.
|
|
4
|
-
*
|
|
5
|
-
* ## The classification (decision (i3))
|
|
6
|
-
*
|
|
7
|
-
* Cash ledger rows carry NO explicit lifecycle flag, so provenance is derived from
|
|
8
|
-
* `ref_type` PLUS the note prefix written by the source, with the balance SHAPE as a
|
|
9
|
-
* secondary signal. The source notes are stable, source-verified strings:
|
|
10
|
-
*
|
|
11
|
-
* "Drawer opened - Day ..." -> the opening float
|
|
12
|
-
* "Drawer reopened - ... " -> a reopen re-baseline
|
|
13
|
-
* "Drawer closed - Day ..." -> the count-out
|
|
14
|
-
* "System adjustment - ..." -> an over/short sweep
|
|
15
|
-
*
|
|
16
|
-
* The classifier lives HERE and runs at COMPILE time; the entry rows store the
|
|
17
|
-
* resulting flags, so the read never re-derives them. When the durable
|
|
18
|
-
* `is_lifecycle` write-path stamp lands, only this function changes.
|
|
19
|
-
*
|
|
20
|
-
* ## The three nets, defined EXACTLY
|
|
21
|
-
*
|
|
22
|
-
* - `operator` - pure operator movement. Excludes the float, every reopen, the
|
|
23
|
-
* count-out AND the sweeps. This is the quantity the session
|
|
24
|
-
* document calls `net_movements`, so the two sources must AGREE.
|
|
25
|
-
* - `lifecycle` - float + reopen + count-out, broken out separately.
|
|
26
|
-
* - `system` - the over/short sweeps written at close.
|
|
27
|
-
*
|
|
28
|
-
* NOTE ON THE CONCEPT DOC: section 3.2.2 states the reconciliation as
|
|
29
|
-
* `ledger_net = operator_movement_net + reopen_amount + system_adjustment_net`.
|
|
30
|
-
* That is INCOMPLETE - it omits the float and the count-out, which are also ledger
|
|
31
|
-
* rows. Tracing the close routine shows why they matter: the count-out is written
|
|
32
|
-
* as `CASH_OUT amount = countedCash`, so a fully closed drawer-day nets to ZERO.
|
|
33
|
-
* The identity the harness must assert is therefore:
|
|
34
|
-
*
|
|
35
|
-
* ledger_net = operator.net + lifecycle.float_amount - lifecycle.countout_amount
|
|
36
|
-
* + lifecycle.reopen_amount + system.net
|
|
37
|
-
*
|
|
38
|
-
* which reduces to 0 for a closed day, while BOTH cross-collection checks
|
|
39
|
-
* (`operator.net == sessions.net_movements_amount` and
|
|
40
|
-
* `system.net == -sessions.variance_amount`) hold exactly.
|
|
41
|
-
*/
|
|
1
|
+
import { CASH_DRAWER_STATUS_ENUM, CASH_IN_OUT_REF_ENUM, CASH_IN_OUT_TYPE_ENUM } from '../../constants/enums/cash';
|
|
42
2
|
/** Note prefixes written by the cash drawer repository. */
|
|
43
3
|
export const CASH_OPENING_NOTE_PREFIX = 'Drawer opened';
|
|
44
4
|
export const CASH_REOPEN_NOTE_PREFIX = 'Drawer reopened';
|
|
@@ -238,7 +198,7 @@ export function addCashSession(acc, session) {
|
|
|
238
198
|
if (closing == null) {
|
|
239
199
|
// No closing record: a session still IN_USE is normal; anything else is a
|
|
240
200
|
// data-integrity tripwire.
|
|
241
|
-
if (String(session.status) ===
|
|
201
|
+
if (String(session.status) === CASH_DRAWER_STATUS_ENUM.IN_USE)
|
|
242
202
|
sessions.open_sessions_count += 1;
|
|
243
203
|
else
|
|
244
204
|
sessions.unmatched_close_count += 1;
|
|
@@ -257,7 +217,9 @@ export function addCashSession(acc, session) {
|
|
|
257
217
|
// identity exact again.
|
|
258
218
|
const lifecycles = [closing];
|
|
259
219
|
for (const iteration of Array.isArray(session.reopened_iterations) ? session.reopened_iterations : []) {
|
|
260
|
-
if (iteration != null)
|
|
220
|
+
if (iteration?.closing != null)
|
|
221
|
+
lifecycles.push(iteration.closing);
|
|
222
|
+
else if (iteration != null)
|
|
261
223
|
lifecycles.push(iteration);
|
|
262
224
|
}
|
|
263
225
|
for (const lifecycle of lifecycles) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
|
-
import { CASH_KEY_FILTERS, MATERIALIZED_CASH_REPORT_SCOPES, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { CASH_KEY_FILTERS, MATERIALIZED_CASH_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
3
|
import { buildMaterializedTenantReportIteration } from './materialized-tenant-reports-iterations-helpers';
|
|
4
4
|
import { getMaterializedPeriodScopeFields } from './scope-helpers';
|
|
5
5
|
import { freshCashCounters } from './materialized-cash-classification-helpers';
|
|
@@ -24,9 +24,9 @@ import { mergeMaterializedCashCounters } from './materialized-cash-accumulator-h
|
|
|
24
24
|
* prep time report and is designed out here from the start.
|
|
25
25
|
*/
|
|
26
26
|
/** Period scopes this report materializes (everything but the day scope). */
|
|
27
|
-
export const MATERIALIZED_CASH_PERIOD_SCOPES = MATERIALIZED_CASH_REPORT_SCOPES.filter((scope) => String(scope) !==
|
|
27
|
+
export const MATERIALIZED_CASH_PERIOD_SCOPES = MATERIALIZED_CASH_REPORT_SCOPES.filter((scope) => String(scope) !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
|
|
28
28
|
/** True when the scope is the plain day scope. */
|
|
29
|
-
export const isMaterializedCashDayScope = (scopeContext) => String(scopeContext) ===
|
|
29
|
+
export const isMaterializedCashDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
|
|
30
30
|
const cashScopeKey = (doc) => String(doc.scope_context) + '|' + String(doc.scope_field);
|
|
31
31
|
/**
|
|
32
32
|
* Merges TWO period entry sections, keyed on `entry_key`.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { ALL_DAY_INDEX } from '../../constants/ENUMS';
|
|
1
2
|
import { CASH_IN_OUT_REF_ENUM, CASH_IN_OUT_TYPE_ENUM } from '../../constants/enums/cash';
|
|
2
|
-
import { CASH_KEY_FILTERS } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
|
+
import { CASH_KEY_FILTERS, MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
4
|
import { MATERIALIZED_TENANT_CASH_ENTRIES_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_cash_entries';
|
|
4
5
|
import { MATERIALIZED_TENANT_CASH_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_cash_reports';
|
|
5
6
|
import { addCashMovement, classifyCashMovement, addCashSession, finalizeCashAccumulator, freshCashAccumulator, } from './materialized-cash-classification-helpers';
|
|
@@ -178,7 +179,7 @@ export function compileTenantCashReports(input) {
|
|
|
178
179
|
_id: undefined,
|
|
179
180
|
tenant_id: tenantId,
|
|
180
181
|
brand_id: brandId,
|
|
181
|
-
scope_context:
|
|
182
|
+
scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
182
183
|
scope_field: localDate,
|
|
183
184
|
day_index: dayIndexOf(localDate),
|
|
184
185
|
sale_type_key_filters: [...CASH_KEY_FILTERS],
|
|
@@ -196,7 +197,7 @@ export function compileTenantCashReports(input) {
|
|
|
196
197
|
_id: undefined,
|
|
197
198
|
tenant_id: tenantId,
|
|
198
199
|
brand_id: brandId,
|
|
199
|
-
scope_context:
|
|
200
|
+
scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
200
201
|
scope_field: localDate,
|
|
201
202
|
day_index: dayIndexOf(localDate),
|
|
202
203
|
sale_type_key_filters: [...CASH_KEY_FILTERS],
|
|
@@ -240,5 +241,5 @@ function dayIndexOf(localDate) {
|
|
|
240
241
|
const date = new Date(localDate + 'T00:00:00.000Z');
|
|
241
242
|
if (Number.isNaN(date.getTime()))
|
|
242
243
|
return null;
|
|
243
|
-
return [
|
|
244
|
+
return ALL_DAY_INDEX[date.getUTCDay()] ?? null;
|
|
244
245
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DELIVERY_STATUS_ENUM } from '../../constants/enums/delivery';
|
|
1
|
+
import { DELIVERY_STATUS_ENUM, RIDER_EARNINGS_STATUS_ENUM } from '../../constants/enums/delivery';
|
|
2
2
|
import { MATERIALIZED_DELIVERY_AREA_LIMIT, MATERIALIZED_DELIVERY_DISTANCE_BUCKETS, MATERIALIZED_DELIVERY_RIDER_LIMIT, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
3
|
/**
|
|
4
4
|
* M4 delivery - key grammar, the order join, and the accumulators.
|
|
@@ -259,7 +259,11 @@ export function addDelivery(acc, delivery, order, key) {
|
|
|
259
259
|
// Only a FINAL snapshot is payout. A PENDING snapshot is an estimate shown to
|
|
260
260
|
// the rider before completion and must not inflate the materialized report.
|
|
261
261
|
const earnings = delivery.rider_earnings;
|
|
262
|
-
const revenue = Number(earnings != null
|
|
262
|
+
const revenue = Number(earnings != null
|
|
263
|
+
? earnings.status === RIDER_EARNINGS_STATUS_ENUM.FINAL
|
|
264
|
+
? earnings.amount
|
|
265
|
+
: 0
|
|
266
|
+
: delivery.rider_revenue);
|
|
263
267
|
const hasRevenue = Number.isFinite(revenue);
|
|
264
268
|
if (hasRevenue) {
|
|
265
269
|
counters.money.rider_revenue_amount += revenue;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
1
2
|
import mongoose from 'mongoose';
|
|
2
3
|
import { DELIVERY_KEY_FILTERS, MATERIALIZED_DELIVERY_REPORT_SCOPES, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
4
|
import { buildMaterializedTenantReportIteration } from './materialized-tenant-reports-iterations-helpers';
|
|
@@ -21,9 +22,9 @@ import { mergeMaterializedDeliveryCounters } from './materialized-delivery-accum
|
|
|
21
22
|
* That bug shipped once in the prep time report; it is designed out here from the start.
|
|
22
23
|
*/
|
|
23
24
|
/** Period scopes this report materializes (everything but the day scope). */
|
|
24
|
-
export const MATERIALIZED_DELIVERY_PERIOD_SCOPES = MATERIALIZED_DELIVERY_REPORT_SCOPES.filter((scope) => String(scope) !==
|
|
25
|
+
export const MATERIALIZED_DELIVERY_PERIOD_SCOPES = MATERIALIZED_DELIVERY_REPORT_SCOPES.filter((scope) => String(scope) !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
|
|
25
26
|
/** True when the scope is the plain day scope. */
|
|
26
|
-
export const isMaterializedDeliveryDayScope = (scopeContext) => String(scopeContext) ===
|
|
27
|
+
export const isMaterializedDeliveryDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
|
|
27
28
|
const deliveryScopeKey = (doc) => String(doc.scope_context) + '|' + String(doc.scope_field);
|
|
28
29
|
/**
|
|
29
30
|
* Merges TWO period entry sections, keyed on `entry_key`.
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ALL_DAY_INDEX } from '../../constants/ENUMS';
|
|
2
|
+
import { DELIVERY_KEY_FILTERS, MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
|
+
import { DELIVERY_STATUS_ENUM, RIDER_EARNINGS_STATUS_ENUM } from '../../constants/enums/delivery';
|
|
2
4
|
import { MATERIALIZED_TENANT_DELIVERY_ENTRIES_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_delivery_entries';
|
|
3
5
|
import { MATERIALIZED_TENANT_DELIVERY_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_delivery_reports';
|
|
4
|
-
import { addDelivery, addDeliveryOrder, buildMaterializedDeliveryKey, finalizeDeliveryAccumulator, freshDeliveryAccumulator, isDeliveryOrder, } from './materialized-delivery-classification-helpers';
|
|
6
|
+
import { DELIVERY_UNKNOWN_TOKEN, addDelivery, addDeliveryOrder, buildMaterializedDeliveryKey, finalizeDeliveryAccumulator, freshDeliveryAccumulator, isDeliveryOrder, } from './materialized-delivery-classification-helpers';
|
|
5
7
|
const watermarkOf = (row, fallback) => {
|
|
6
8
|
const raw = row?.updatedAt ?? row?.createdAt;
|
|
7
9
|
const time = raw == null ? NaN : new Date(raw).getTime();
|
|
@@ -120,7 +122,7 @@ export function compileTenantDeliveryReports(input) {
|
|
|
120
122
|
_id: undefined,
|
|
121
123
|
tenant_id: tenantId,
|
|
122
124
|
brand_id: brandId,
|
|
123
|
-
scope_context:
|
|
125
|
+
scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
124
126
|
scope_field: localDate,
|
|
125
127
|
day_index: dayIndexOf(localDate),
|
|
126
128
|
sale_type_key_filters: [...DELIVERY_KEY_FILTERS],
|
|
@@ -138,7 +140,7 @@ export function compileTenantDeliveryReports(input) {
|
|
|
138
140
|
_id: undefined,
|
|
139
141
|
tenant_id: tenantId,
|
|
140
142
|
brand_id: brandId,
|
|
141
|
-
scope_context:
|
|
143
|
+
scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
|
|
142
144
|
scope_field: localDate,
|
|
143
145
|
day_index: dayIndexOf(localDate),
|
|
144
146
|
sale_type_key_filters: [...DELIVERY_KEY_FILTERS],
|
|
@@ -172,7 +174,7 @@ function buildEntryRow(delivery, order, details) {
|
|
|
172
174
|
cancel_reason: delivery?.cancel_reason == null ? null : String(delivery.cancel_reason),
|
|
173
175
|
distance_meters: Number(delivery?.distance_in_meters_from_store) || 0,
|
|
174
176
|
rider_revenue: delivery?.rider_earnings != null
|
|
175
|
-
? delivery.rider_earnings.status ===
|
|
177
|
+
? delivery.rider_earnings.status === RIDER_EARNINGS_STATUS_ENUM.FINAL
|
|
176
178
|
? Number(delivery.rider_earnings.amount) || 0
|
|
177
179
|
: null
|
|
178
180
|
: delivery?.rider_revenue == null
|
|
@@ -182,7 +184,7 @@ function buildEntryRow(delivery, order, details) {
|
|
|
182
184
|
has_pickup_proof: delivery?.pickup_food_image_url != null,
|
|
183
185
|
has_dropoff_proof: delivery?.dropoff_food_image_url != null,
|
|
184
186
|
polyline_point_count: Array.isArray(delivery?.polyline) ? delivery.polyline.length : 0,
|
|
185
|
-
platform: order?.platform == null ?
|
|
187
|
+
platform: order?.platform == null ? DELIVERY_UNKNOWN_TOKEN : String(order.platform),
|
|
186
188
|
city: details?.city == null ? null : String(details.city),
|
|
187
189
|
state: details?.state == null ? null : String(details.state),
|
|
188
190
|
street: details?.street == null ? null : String(details.street),
|
|
@@ -195,7 +197,7 @@ function buildEntryRow(delivery, order, details) {
|
|
|
195
197
|
delivered_at: deliveredAt,
|
|
196
198
|
cancelled_at: delivery?.cancelled_at == null ? null : new Date(delivery.cancelled_at),
|
|
197
199
|
dispatch_lag_minutes: minutesBetween(orderCreatedAt, createdAt),
|
|
198
|
-
complete_minutes: status ===
|
|
200
|
+
complete_minutes: status === DELIVERY_STATUS_ENUM.COMPLETED ? minutesBetween(createdAt, completedAt) : null,
|
|
199
201
|
// The proxy is NOT exact: `updatedAt` moves on ANY later mutation, which is why the
|
|
200
202
|
// metrics capture exists. The row says which one it used.
|
|
201
203
|
completion_is_exact: exact,
|
|
@@ -210,5 +212,5 @@ function dayIndexOf(localDate) {
|
|
|
210
212
|
const date = new Date(localDate + 'T00:00:00.000Z');
|
|
211
213
|
if (Number.isNaN(date.getTime()))
|
|
212
214
|
return null;
|
|
213
|
-
return [
|
|
215
|
+
return ALL_DAY_INDEX[date.getUTCDay()] ?? null;
|
|
214
216
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { MATERIALIZED_DISCOUNT_ENTRY_KIND_ENUM, MATERIALIZED_DISCOUNT_LEVEL_ENUM, MATERIALIZED_DISCOUNT_SLOT_ENUM, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { PAYMENT_TYPE_ENUM } from '../../constants/enums/payments';
|
|
2
3
|
import { createEmptyDiscountOrderCounters, createEmptyDiscountSlotCounter, MATERIALIZED_DISCOUNT_SLOTS, mergeDiscountSlotCounter, readDiscountSlot, resolveRowMoney, } from './materialized-discount-classification-helpers';
|
|
3
|
-
const isComplimentaryPayment = (payment) => payment?.payment_type ===
|
|
4
|
+
const isComplimentaryPayment = (payment) => payment?.payment_type === PAYMENT_TYPE_ENUM.COMPLIMENTARY;
|
|
4
5
|
const nonNegative = (value) => (Number.isFinite(value) && value > 0 ? value : 0);
|
|
5
6
|
/** The line label of a punch hour (null when the hour is unknown). */
|
|
6
7
|
export const toDiscountHourLabel = (localDate, hour) => hour == null || hour < 0 || hour > 23 ? null : `${localDate} ${String(hour).padStart(2, '0')}`;
|
package/esm/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CALENDAR_MODE_ENUM } from '../../constants/REPORT_CALENDAR';
|
|
2
|
-
import { SALES_COMPARISON_BASELINE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { MATERIALIZED_REPORT_SCOPE_ENUM, SALES_COMPARISON_BASELINE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
3
|
import { getSalesComparisonBaselineRange } from './materialized-sales-comparison-baseline-helpers';
|
|
4
4
|
import { getDayKeysFromCalendarSelection, isValidBusinessHourWindow } from './materialized-reports-selection-helpers';
|
|
5
5
|
import { getMaterializedHourScopeField } from './scope-helpers';
|
|
@@ -80,7 +80,9 @@ export function buildHourlyComparisonScopeGroups(selection) {
|
|
|
80
80
|
return {
|
|
81
81
|
days: [...new Set(days)].sort(),
|
|
82
82
|
hours,
|
|
83
|
-
groups: labels.length === 0
|
|
83
|
+
groups: labels.length === 0
|
|
84
|
+
? []
|
|
85
|
+
: [{ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, scope_field: labels }],
|
|
84
86
|
};
|
|
85
87
|
}
|
|
86
88
|
/**
|
|
@@ -106,6 +108,8 @@ export function buildHourlyComparisonBaselineScopeGroups(selection, baseline) {
|
|
|
106
108
|
return {
|
|
107
109
|
days: [...new Set(range.days)].sort(),
|
|
108
110
|
hours,
|
|
109
|
-
groups: labels.length === 0
|
|
111
|
+
groups: labels.length === 0
|
|
112
|
+
? []
|
|
113
|
+
: [{ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, scope_field: labels }],
|
|
110
114
|
};
|
|
111
115
|
}
|
package/esm/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PAYMENT_TYPE_ENUM } from '../../constants/enums/payments';
|
|
1
2
|
import { buildMaterializedReportKey } from './key-helpers';
|
|
2
3
|
import { getMaterializedPaymentGross, isPaymentIncludedInMaterializedSalesOverview, normalizeMaterializedPaymentType, } from './tenant-sales-overview-helpers';
|
|
3
4
|
/**
|
|
@@ -59,7 +60,7 @@ export function addPaymentToPaymentsComparisonCounters(target, payment) {
|
|
|
59
60
|
target.card_charges += cents(payment.card_details?.card_charges);
|
|
60
61
|
if (amountRefunded > 0)
|
|
61
62
|
target.refund_count += 1;
|
|
62
|
-
if (getPaymentsComparisonMethod(payment) ===
|
|
63
|
+
if (getPaymentsComparisonMethod(payment) === PAYMENT_TYPE_ENUM.UNPAID)
|
|
63
64
|
target.unpaid_count += 1;
|
|
64
65
|
}
|
|
65
66
|
/** Adds every counter of `source` into `target` (mutates `target`). */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
|
-
import { MATERIALIZED_PREP_TIME_REPORT_SCOPES, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { MATERIALIZED_PREP_TIME_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
3
|
import { freshMaterializedPrepTimeCounters } from './materialized-prep-time-classification-helpers';
|
|
4
4
|
import { mergeMaterializedPrepTimeCounters } from './materialized-prep-time-compile-helpers';
|
|
5
5
|
import { buildMaterializedTenantReportIteration } from './materialized-tenant-reports-iterations-helpers';
|
|
@@ -21,7 +21,7 @@ import { getMaterializedPeriodScopeFields } from './scope-helpers';
|
|
|
21
21
|
* contribution and re-folds, so a re-close converges instead of drifting.
|
|
22
22
|
*/
|
|
23
23
|
/** Scopes written wholesale for the compiled day (no hour scope in v1). */
|
|
24
|
-
const PREP_TIME_DAY_SCOPES = [
|
|
24
|
+
const PREP_TIME_DAY_SCOPES = [MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE];
|
|
25
25
|
/** Row identity inside a period document: an order holds many items. */
|
|
26
26
|
const prepEntryRowIdentity = (row) => String(row.order_id) + '|' + String(row.entry_key);
|
|
27
27
|
/**
|
|
@@ -117,7 +117,7 @@ export function needsMaterializedPrepTimeEntriesWrite(existing, incoming) {
|
|
|
117
117
|
export function foldMaterializedPrepTimePeriodCounters({ periodScopeField, periodScope, dayDocuments, periodFieldsOfDay, }) {
|
|
118
118
|
const folded = {};
|
|
119
119
|
for (const dayDoc of dayDocuments) {
|
|
120
|
-
if (dayDoc.scope_context !==
|
|
120
|
+
if (dayDoc.scope_context !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
|
|
121
121
|
continue;
|
|
122
122
|
const fields = periodFieldsOfDay(String(dayDoc.scope_field));
|
|
123
123
|
if (fields == null)
|
|
@@ -139,7 +139,7 @@ export function foldMaterializedPrepTimePeriodCounters({ periodScopeField, perio
|
|
|
139
139
|
export function foldMaterializedPrepTimePeriodRows({ periodScopeField, periodScope, dayDocuments, periodFieldsOfDay, }) {
|
|
140
140
|
const sections = {};
|
|
141
141
|
for (const dayDoc of dayDocuments) {
|
|
142
|
-
if (dayDoc.scope_context !==
|
|
142
|
+
if (dayDoc.scope_context !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
|
|
143
143
|
continue;
|
|
144
144
|
const fields = periodFieldsOfDay(String(dayDoc.scope_field));
|
|
145
145
|
if (fields == null)
|
|
@@ -196,7 +196,7 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
196
196
|
const computedAt = input.computedAt ?? new Date();
|
|
197
197
|
const existing = input.existing ?? [];
|
|
198
198
|
const existingEntries = input.existingEntries ?? [];
|
|
199
|
-
const dateDoc = reports.find((doc) => doc.scope_context ===
|
|
199
|
+
const dateDoc = reports.find((doc) => doc.scope_context === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE) ?? null;
|
|
200
200
|
const localDate = dateDoc?.scope_field ?? input.localDate ?? null;
|
|
201
201
|
if (typeof localDate !== 'string' || localDate.length === 0)
|
|
202
202
|
return null;
|
|
@@ -252,20 +252,20 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
252
252
|
const removedDays = [];
|
|
253
253
|
const nextDayDocs = [];
|
|
254
254
|
for (const doc of existing) {
|
|
255
|
-
if (doc.scope_context !==
|
|
255
|
+
if (doc.scope_context !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
|
|
256
256
|
continue;
|
|
257
257
|
if (String(doc.scope_field) === localDate)
|
|
258
258
|
continue;
|
|
259
259
|
nextDayDocs.push(doc);
|
|
260
260
|
}
|
|
261
|
-
const compiledDateDocs = reports.filter((doc) => doc.scope_context ===
|
|
261
|
+
const compiledDateDocs = reports.filter((doc) => doc.scope_context === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
|
|
262
262
|
for (const doc of compiledDateDocs)
|
|
263
263
|
nextDayDocs.push(doc);
|
|
264
264
|
// ONLY THE DAY BEING CLOSED can become stale here. Every other day document is
|
|
265
265
|
// owned by ITS OWN close - treating them as stale would delete the whole history
|
|
266
266
|
// on the next close.
|
|
267
267
|
for (const doc of existing) {
|
|
268
|
-
if (doc.scope_context !==
|
|
268
|
+
if (doc.scope_context !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
|
|
269
269
|
continue;
|
|
270
270
|
if (String(doc.scope_field) !== localDate)
|
|
271
271
|
continue;
|
|
@@ -283,10 +283,11 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
283
283
|
if (fields == null)
|
|
284
284
|
return false;
|
|
285
285
|
for (const scope of MATERIALIZED_PREP_TIME_PERIOD_SCOPES) {
|
|
286
|
-
const
|
|
286
|
+
const periodScope = scope;
|
|
287
|
+
const field = fields[periodScope];
|
|
287
288
|
if (field == null)
|
|
288
289
|
continue;
|
|
289
|
-
touchedPeriods.set(String(scope) + '|' + field, { scope:
|
|
290
|
+
touchedPeriods.set(String(scope) + '|' + field, { scope: periodScope, field });
|
|
290
291
|
}
|
|
291
292
|
return true;
|
|
292
293
|
};
|
|
@@ -379,18 +380,18 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
379
380
|
}
|
|
380
381
|
const nextDayEntries = [];
|
|
381
382
|
for (const doc of existingEntries) {
|
|
382
|
-
if (doc.scope_context !==
|
|
383
|
+
if (doc.scope_context !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
|
|
383
384
|
continue;
|
|
384
385
|
if (String(doc.scope_field) === localDate)
|
|
385
386
|
continue;
|
|
386
387
|
nextDayEntries.push(doc);
|
|
387
388
|
}
|
|
388
389
|
for (const doc of entries) {
|
|
389
|
-
if (doc.scope_context ===
|
|
390
|
+
if (doc.scope_context === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
|
|
390
391
|
nextDayEntries.push(doc);
|
|
391
392
|
}
|
|
392
393
|
for (const doc of existingEntries) {
|
|
393
|
-
if (doc.scope_context !==
|
|
394
|
+
if (doc.scope_context !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
|
|
394
395
|
continue;
|
|
395
396
|
if (String(doc.scope_field) !== localDate)
|
|
396
397
|
continue;
|
|
@@ -455,7 +456,7 @@ export function buildTenantPrepTimeCloseDayPlan(input) {
|
|
|
455
456
|
return null;
|
|
456
457
|
}
|
|
457
458
|
for (const doc of entries) {
|
|
458
|
-
if (doc.scope_context !==
|
|
459
|
+
if (doc.scope_context !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
|
|
459
460
|
continue;
|
|
460
461
|
const existingDoc = existingEntriesByScope.get(prepScopeKey(doc)) ?? null;
|
|
461
462
|
if (existingDoc != null && !needsMaterializedPrepTimeEntriesWrite(existingDoc, doc)) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
|
-
import { MATERIALIZED_PREP_TIME_REPORT_SCOPES, PREP_TIME_KEY_FILTERS } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { MATERIALIZED_PREP_TIME_REPORT_SCOPES, MATERIALIZED_REPORT_SCOPE_ENUM, PREP_TIME_KEY_FILTERS, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
3
|
import { accumulateOrderPrepTime, freshMaterializedPrepTimeCounters, } from './materialized-prep-time-classification-helpers';
|
|
4
4
|
import { getDayIndexFromLocalDate, getMaterializedPeriodScopeFields } from './scope-helpers';
|
|
5
5
|
/**
|
|
@@ -161,7 +161,7 @@ export function compileTenantPrepTimeReports(input) {
|
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
163
|
const docWatermark = watermark ?? sourceWatermark;
|
|
164
|
-
const isDateScoped = scopeContext ===
|
|
164
|
+
const isDateScoped = scopeContext === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
|
|
165
165
|
reports.push({
|
|
166
166
|
_id: new mongoose.Types.ObjectId().toHexString(),
|
|
167
167
|
tenant_id: tenantId,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
1
|
+
import { MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_WINDOW_KIND_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
2
|
import { getMaterializedHourWindowScopeFields } from './materialized-reports-read-helpers';
|
|
3
3
|
/**
|
|
4
4
|
* Smart scope-group compiler for materialized-report reads — turns a
|
|
@@ -24,10 +24,13 @@ import { getMaterializedHourWindowScopeFields } from './materialized-reports-rea
|
|
|
24
24
|
* Returns `null` (never throws) for malformed input — `null` means the
|
|
25
25
|
* caller passed invalid data.
|
|
26
26
|
*/
|
|
27
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Window refinement kinds (see the module docblock).
|
|
29
|
+
* @deprecated use MATERIALIZED_REPORT_WINDOW_KIND_ENUM from constants.
|
|
30
|
+
*/
|
|
28
31
|
export const MATERIALIZED_REPORT_WINDOW_KIND = {
|
|
29
|
-
RECURRING:
|
|
30
|
-
SPAN:
|
|
32
|
+
RECURRING: MATERIALIZED_REPORT_WINDOW_KIND_ENUM.RECURRING,
|
|
33
|
+
SPAN: MATERIALIZED_REPORT_WINDOW_KIND_ENUM.SPAN,
|
|
31
34
|
};
|
|
32
35
|
/* ------------------------------------------------------------------------ */
|
|
33
36
|
/* Date-key math (UTC — keys are timezone-less) */
|
|
@@ -202,7 +205,7 @@ export function buildMaterializedReportScopeGroups(spec) {
|
|
|
202
205
|
const uniqueDates = dayKeys.map((key) => keyToDate(key));
|
|
203
206
|
// A SPAN window reads the CONTINUOUS interval first-day → last-day — a day
|
|
204
207
|
// set with gaps would silently pull the gap days in as full middle days.
|
|
205
|
-
if (window != null && window.kind ===
|
|
208
|
+
if (window != null && window.kind === MATERIALIZED_REPORT_WINDOW_KIND_ENUM.SPAN) {
|
|
206
209
|
for (let index = 1; index < uniqueDates.length; index += 1) {
|
|
207
210
|
if (daysBetween(uniqueDates[index - 1], uniqueDates[index]) !== 1) {
|
|
208
211
|
return null;
|
|
@@ -217,7 +220,7 @@ export function buildMaterializedReportScopeGroups(spec) {
|
|
|
217
220
|
if (!isValidHour(fromHour, 23) || !isValidHour(toHour, 23))
|
|
218
221
|
return null;
|
|
219
222
|
// Recurring: the same hours on every selected day.
|
|
220
|
-
if (kind ===
|
|
223
|
+
if (kind === MATERIALIZED_REPORT_WINDOW_KIND_ENUM.RECURRING) {
|
|
221
224
|
const labels = getMaterializedHourWindowScopeFields(dayKeys, fromHour, toHour);
|
|
222
225
|
return labels == null ? null : [{ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, scope_field: labels }];
|
|
223
226
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ALL_DAY_INDEX } from '../../constants/ENUMS';
|
|
2
|
-
import { MATERIALIZED_REPORT_SCOPE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_WINDOW_KIND_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
3
|
import { CALENDAR_MODE_ENUM, CALENDAR_VIEW_MODE_ENUM } from '../../constants/REPORT_CALENDAR';
|
|
4
|
-
import {
|
|
4
|
+
import { buildMaterializedReportScopeGroups } from './materialized-reports-scope-group-helpers';
|
|
5
5
|
import { getDayIndexFromLocalDate, getMaterializedHourScopeField, getMaterializedPeriodScopeFields, parseMaterializedLocalDate, } from './scope-helpers';
|
|
6
6
|
/**
|
|
7
7
|
* Calendar-selection DECODER for the materialized report READ APIs — the
|
|
@@ -660,8 +660,8 @@ const decodeMaterializedReportScopeGroups = (selection) => {
|
|
|
660
660
|
// never compile it. Weekday + window therefore always compiles
|
|
661
661
|
// RECURRING (the same hours on each matching weekday).
|
|
662
662
|
kind: SPAN_VIEWS.has(view) && weekdayFilter == null
|
|
663
|
-
?
|
|
664
|
-
:
|
|
663
|
+
? MATERIALIZED_REPORT_WINDOW_KIND_ENUM.SPAN
|
|
664
|
+
: MATERIALIZED_REPORT_WINDOW_KIND_ENUM.RECURRING,
|
|
665
665
|
},
|
|
666
666
|
}
|
|
667
667
|
: {}),
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ALL_DAY_INDEX } from '../../constants/ENUMS';
|
|
2
|
+
import { MATERIALIZED_REPORT_WINDOW_KIND_ENUM, SALES_COMPARISON_BASELINE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
|
+
import { buildMaterializedReportScopeGroups, } from './materialized-reports-scope-group-helpers';
|
|
3
4
|
import { MATERIALIZED_REPORT_SPAN_VIEW_MODES, getDayKeysFromCalendarSelection, isValidBusinessHourWindow, } from './materialized-reports-selection-helpers';
|
|
4
5
|
/**
|
|
5
6
|
* Sales Comparison baseline derivation (concept §7).
|
|
@@ -57,11 +58,9 @@ const shiftKeyOneYearBack = (key) => {
|
|
|
57
58
|
const day = Math.min(date.getUTCDate(), lastDayOfMonth);
|
|
58
59
|
return utcDateToKey(new Date(Date.UTC(year, month, day)));
|
|
59
60
|
};
|
|
60
|
-
// `DAY_INDEX_ENUM` values are the three-letter keys (`MON`), not full names.
|
|
61
|
-
const DAY_INDEX_KEYS = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];
|
|
62
61
|
const dayIndexKeyOf = (key) => {
|
|
63
62
|
const date = keyToUtcDate(key);
|
|
64
|
-
return date == null ? null : (
|
|
63
|
+
return date == null ? null : (ALL_DAY_INDEX[date.getUTCDay()] ?? null);
|
|
65
64
|
};
|
|
66
65
|
/** `'HH:mm'` → whole hour, or null (mirrors the decoder's whole-hour rule). */
|
|
67
66
|
const hourOf = (time) => {
|
|
@@ -150,7 +149,7 @@ export function buildSalesComparisonBaselineScopeGroups(selection, baseline) {
|
|
|
150
149
|
spec.window = {
|
|
151
150
|
fromHour: fromHour,
|
|
152
151
|
toHour: toHour,
|
|
153
|
-
kind: span ?
|
|
152
|
+
kind: span ? MATERIALIZED_REPORT_WINDOW_KIND_ENUM.SPAN : MATERIALIZED_REPORT_WINDOW_KIND_ENUM.RECURRING,
|
|
154
153
|
};
|
|
155
154
|
}
|
|
156
155
|
return buildMaterializedReportScopeGroups(spec);
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
1
2
|
import { convertPriceFromCents } from '../utils/price-conversion';
|
|
2
3
|
import { parseMaterializedCashKey } from './materialized-cash-compile-helpers';
|
|
3
4
|
import { freshCashCounters } from './materialized-cash-classification-helpers';
|
|
4
5
|
import { mergeMaterializedCashCounters } from './materialized-cash-accumulator-helpers';
|
|
5
6
|
const toDecimal = (cents) => convertPriceFromCents(cents ?? 0);
|
|
6
|
-
const isDayScope = (scopeContext) => String(scopeContext) ===
|
|
7
|
+
const isDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
|
|
7
8
|
/** True when a key passes the caller-supplied drawer / ref-type filters. */
|
|
8
9
|
function keyPassesFilters(key, filters) {
|
|
9
10
|
const parsed = parseMaterializedCashKey(key);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { convertPriceFromCents } from '../utils/price-conversion';
|
|
2
2
|
import { DELIVERY_UNKNOWN_TOKEN, freshDeliveryCounters, parseMaterializedDeliveryKey, } from './materialized-delivery-classification-helpers';
|
|
3
3
|
import { mergeMaterializedDeliveryCounters } from './materialized-delivery-accumulator-helpers';
|
|
4
|
-
import { MATERIALIZED_DELIVERY_DISTANCE_BUCKETS } from '../../constants/MATERIALIZED_REPORTS';
|
|
4
|
+
import { MATERIALIZED_DELIVERY_DISTANCE_BUCKETS, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
5
5
|
const toDecimal = (cents) => convertPriceFromCents(cents ?? 0);
|
|
6
6
|
const km = (meters) => Math.round(((meters ?? 0) / 1000) * 100) / 100;
|
|
7
7
|
const safeAverage = (total, count) => count > 0 ? Math.round((total / count) * 100) / 100 : null;
|
|
8
|
-
const isDayScope = (scopeContext) => String(scopeContext) ===
|
|
8
|
+
const isDayScope = (scopeContext) => String(scopeContext) === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE;
|
|
9
9
|
/** True when a key passes the caller-supplied platform / city filters. */
|
|
10
10
|
function keyPassesFilters(key, filters) {
|
|
11
11
|
const parsed = parseMaterializedDeliveryKey(key);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ALL_DAY_INDEX } from '../../constants/ENUMS';
|
|
1
|
+
import { ALL_DAY_INDEX, ORDER_STATUS_ENUM } from '../../constants/ENUMS';
|
|
2
2
|
import { MATERIALIZED_DISCOUNT_LEVEL_ENUM, MATERIALIZED_DISCOUNT_SLOT_ENUM, MATERIALIZED_DISCOUNT_SUB_KIND_ENUM, MATERIALIZED_REPORT_KEY_FILTER_ENUM, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_SCOPES_WITH_DAY_INDEX, } from '../../constants/MATERIALIZED_REPORTS';
|
|
3
3
|
import { MATERIALIZED_TENANT_DISCOUNT_REPORTS_SCHEMA_VERSION } from '../../schemas/models/materialized_tenant_discount_reports';
|
|
4
4
|
import { convertPriceFromCents } from '../utils/price-conversion';
|
|
@@ -59,7 +59,7 @@ const isCancelledKey = (components, keyFilters) => {
|
|
|
59
59
|
const statusIndex = keyFilters.indexOf(MATERIALIZED_REPORT_KEY_FILTER_ENUM.ORDER_STATUS);
|
|
60
60
|
if (statusIndex === -1)
|
|
61
61
|
return false;
|
|
62
|
-
return components[statusIndex] ===
|
|
62
|
+
return components[statusIndex] === ORDER_STATUS_ENUM.CANCELLED;
|
|
63
63
|
};
|
|
64
64
|
const emptyScopeAccumulator = () => ({
|
|
65
65
|
summary: {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MATERIALIZED_REPORT_KEY_FILTER_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { ORDER_STATUS_ENUM } from '../../constants/ENUMS';
|
|
2
3
|
import { buildSalesComparisonMetrics, formatSalesComparisonSeries, } from './tenant-sales-comparison-formatter';
|
|
3
4
|
import { addSalesComparisonCounters, createEmptySalesComparisonCounters, } from './materialized-sales-comparison-accumulator-helpers';
|
|
4
5
|
import { parseMaterializedReportKey } from './key-helpers';
|
|
@@ -51,8 +52,8 @@ export const getHourlyComparisonHourLabel = (hour) => {
|
|
|
51
52
|
};
|
|
52
53
|
/** Business-hour index: 06 → 0, …, 23 → 17, 00 → 18, …, 05 → 23. */
|
|
53
54
|
export const getHourlyComparisonBusinessIndex = (hour) => (hour + 18) % 24;
|
|
54
|
-
const isCancelled = (status) => status ===
|
|
55
|
-
const includesCancelled = (filters) => Array.isArray(filters.order_statuses) && filters.order_statuses.includes(
|
|
55
|
+
const isCancelled = (status) => status === ORDER_STATUS_ENUM.CANCELLED;
|
|
56
|
+
const includesCancelled = (filters) => Array.isArray(filters.order_statuses) && filters.order_statuses.includes(ORDER_STATUS_ENUM.CANCELLED);
|
|
56
57
|
const parseKey = (key, keyFilters) => {
|
|
57
58
|
const components = parseMaterializedReportKey(key);
|
|
58
59
|
if (components == null || components.length !== keyFilters.length)
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { MATERIALIZED_REPORT_KEY_FILTER_ENUM, PAYMENTS_COMPARISON_METRIC_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
|
|
2
|
+
import { ORDER_STATUS_ENUM } from '../../constants/ENUMS';
|
|
2
3
|
import { parseMaterializedReportKey } from './key-helpers';
|
|
3
4
|
import { convertPriceFromCents } from '../utils/price-conversion';
|
|
5
|
+
const matchedDocumentsOf = (doc) => ('matched_documents' in doc ? doc.matched_documents : 1);
|
|
6
|
+
const updatedAtOf = (doc) => 'max_updated_at' in doc ? doc.max_updated_at : doc.updatedAt;
|
|
4
7
|
const METRICS = [
|
|
5
8
|
PAYMENTS_COMPARISON_METRIC_ENUM.PAYMENT_AMOUNT,
|
|
6
9
|
PAYMENTS_COMPARISON_METRIC_ENUM.PAYMENT_COUNT,
|
|
@@ -92,8 +95,8 @@ const parseKey = (key, keyFilters) => {
|
|
|
92
95
|
}
|
|
93
96
|
return { payment_method: paymentMethod, order_status: orderStatus };
|
|
94
97
|
};
|
|
95
|
-
const isCancelled = (orderStatus) => orderStatus ===
|
|
96
|
-
const includesCancelled = (filters) => Array.isArray(filters.order_statuses) && filters.order_statuses.includes(
|
|
98
|
+
const isCancelled = (orderStatus) => orderStatus === ORDER_STATUS_ENUM.CANCELLED;
|
|
99
|
+
const includesCancelled = (filters) => Array.isArray(filters.order_statuses) && filters.order_statuses.includes(ORDER_STATUS_ENUM.CANCELLED);
|
|
97
100
|
const matchesFilters = (parsed, filters) => {
|
|
98
101
|
if (filters.payment_methods != null &&
|
|
99
102
|
filters.payment_methods.length > 0 &&
|
|
@@ -119,8 +122,8 @@ const buildPage = (group, filters) => {
|
|
|
119
122
|
for (const doc of docs ?? []) {
|
|
120
123
|
if (doc == null)
|
|
121
124
|
continue;
|
|
122
|
-
matchedDocuments +=
|
|
123
|
-
const updatedAt = doc
|
|
125
|
+
matchedDocuments += matchedDocumentsOf(doc);
|
|
126
|
+
const updatedAt = updatedAtOf(doc);
|
|
124
127
|
if (updatedAt != null) {
|
|
125
128
|
const date = new Date(updatedAt);
|
|
126
129
|
if (!Number.isNaN(date.getTime()) && (maxUpdatedAt == null || date.getTime() > maxUpdatedAt.getTime())) {
|