From 476ba98eae5af18ddee0e0d5659e8dfea43dfcd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Sun, 17 May 2026 12:53:35 +0200 Subject: [PATCH] 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. --- src/app/api/analytics/route.ts | 35 ++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/app/api/analytics/route.ts b/src/app/api/analytics/route.ts index d92dd8b29..d04551d9d 100644 --- a/src/app/api/analytics/route.ts +++ b/src/app/api/analytics/route.ts @@ -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,