refactor(analytics): hoist cumulative metric-key lookup out of nested ternary

Pulls the v1.4.36 W4c MeasurementType -> SourcePriorityMetricKey
chain out of the dashboard per-type loop into a typed switch helper.
Same call sites, same null fallback for the unmapped TIME_IN_DAYLIGHT
branch.
This commit is contained in:
Marc-André Bombeck committed 2026-05-17 12:53:35 +02:00
1 parent 384179228a
commit 476ba98eae
1 file changed
+25 -10
+25 -10
View File
@@ -29,6 +29,7 @@ import type {
} from "@/generated/prisma/client";
import { measurementTypeEnum } from "@/lib/validations/measurement";
import { pickCanonicalSourceRows } from "@/lib/analytics/source-priority";
import type { SourcePriorityMetricKey } from "@/lib/validations/source-priority";
import { ensureUserRollupsFresh } from "@/lib/measurements/rollups";
import {
isCumulativeDaySumType,
@@ -37,6 +38,29 @@ import {
export const dynamic = "force-dynamic";
/**
* v1.4.36 W4c — cumulative MeasurementType → SourcePriorityMetricKey
* for `pickCanonicalSourceRows`. Returns `null` for types without a
* dedicated priority ladder (e.g. TIME_IN_DAYLIGHT), which fall
* through the picker's "no ladder" pass-through branch.
*/
function cumulativeMetricKey(
type: MeasurementType,
): SourcePriorityMetricKey | null {
switch (type) {
case "ACTIVITY_STEPS":
return "steps";
case "ACTIVE_ENERGY_BURNED":
return "activeEnergy";
case "WALKING_RUNNING_DISTANCE":
return "walkingRunningDistance";
case "FLIGHTS_CLIMBED":
return "flightsClimbed";
default:
return null;
}
}
/**
* v1.4.33 C1 — pull `?slice=…` from either a NextRequest (the
* route's production wrapper) or a plain `Request` (the integration
@@ -234,16 +258,7 @@ async function buildAnalyticsResponse(user: AuthedUser) {
// through `pickCanonicalSourceRows`'s "no ladder"
// pass-through branch — we ALWAYS bucket-and-sum it
// regardless of source.
const metricKey =
type === "ACTIVITY_STEPS"
? ("steps" as const)
: type === "ACTIVE_ENERGY_BURNED"
? ("activeEnergy" as const)
: type === "WALKING_RUNNING_DISTANCE"
? ("walkingRunningDistance" as const)
: type === "FLIGHTS_CLIMBED"
? ("flightsClimbed" as const)
: null;
const metricKey = cumulativeMetricKey(type);
const canonicalRows = metricKey
? pickCanonicalSourceRows(
measurements,