merge: wave/v1220-strip into release/v1.21.2

This commit is contained in:
Marc-André Bombeck committed 2026-06-26 23:58:51 +02:00
commit 089a5254aa
11 files changed
+576

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
/**
* v1.22.0 (A1) — "Coach read" strip route.
*
* `GET /api/insights/coach-read?metric=<MeasurementType>` serves the two
* server-authoritative lines a metric sub-page renders above its chart: the
* own-baseline placement (median ± k·MAD, today's reading within / above /
* below) and the single strongest lagged association whose outcome is the
* metric. Pure compute over the baseline + correlation engines — no provider
* call, no cache table — so web and iOS decode the SAME resolved DTO.
*
* Mirrors the `/api/insights/derived` precedent: `apiHandler` wrapper, Zod
* `safeParse` on the query (unknown `metric` → 422 via `returnAllZodIssues`),
* cookie OR Bearer auth, `userId` narrowed from the session (never a query
* field), insights-module gate, and the shared analytics-read budget.
*/
import { NextRequest } from "next/server";
import { z } from "zod/v4";
import { apiError, apiSuccess, returnAllZodIssues } from "@/lib/api-response";
import { apiHandler, requireAuth } from "@/lib/api-handler";
import { annotate } from "@/lib/logging/context";
import { checkAnalyticsReadRateLimit } from "@/lib/rate-limit";
import { requireModuleEnabled } from "@/lib/modules/gate";
import { measurementTypeEnum } from "@/lib/validations/measurement";
import { buildCoachReadStrip } from "@/lib/insights/derived/coach-read";
import type { MeasurementType } from "@/generated/prisma/client";
export const dynamic = "force-dynamic";
const coachReadQuerySchema = z.object({
metric: measurementTypeEnum,
});
export const GET = apiHandler(async (request: NextRequest) => {
const { user } = await requireAuth();
const m = await requireModuleEnabled(user.id, "insights");
if (!m.enabled) return m.response;
// Shared analytics-read budget — generous; caps a runaway navigation loop.
const rl = await checkAnalyticsReadRateLimit(user.id);
if (!rl.allowed) {
return apiError("Too many analytics requests. Please retry later.", 429);
}
const parsed = coachReadQuerySchema.safeParse({
metric: request.nextUrl.searchParams.get("metric"),
});
if (!parsed.success) {
annotate({
action: { name: "insights.coach-read.invalid-metric" },
meta: { issue_count: parsed.error.issues.length },
});
return returnAllZodIssues(parsed.error, 422);
}
const metric = parsed.data.metric as MeasurementType;
const strip = await buildCoachReadStrip(user.id, metric);
annotate({
action: { name: "insights.coach-read" },
meta: {
metric,
has_baseline: strip.baseline !== null,
learning: strip.learning,
has_driver: strip.driver !== null,
},
});
return apiSuccess(strip);
});
+8
View File
@@ -14,6 +14,7 @@ import { SlugInsightStatusCard } from "@/components/insights/slug-insight-status
import { MeasurementDiversityNudge } from "@/components/insights/measurement-diversity-nudge";
import { MetricEmptyState } from "@/components/insights/metric-empty-state";
import { MetricStatStrip } from "@/components/insights/metric-stat-strip";
import { CoachReadStrip } from "@/components/insights/derived/coach-read-strip";
import { MetricTargetSummary } from "@/components/insights/metric-target-summary";
import { SubPageShell } from "@/components/insights/sub-page-shell";
import { getBpTargets } from "@/lib/analytics/bp-targets";
@@ -111,6 +112,13 @@ export default function InsightsBlutdruckPage() {
description={t("insights.subPage.blutdruckDescription")}
explainerMetric="bloodPressure"
coachLaunch
coachReadStrip={
<CoachReadStrip
metricType="BLOOD_PRESSURE_SYS"
unit="mmHg"
fractionDigits={0}
/>
}
statStrip={
// v1.12.7 — blood pressure is two series, but they share ONE card
// with the systolic / diastolic columns side by side (stacking only
+8
View File
@@ -13,6 +13,7 @@ import { HealthChartDynamic } from "@/components/charts/health-chart-dynamic";
import { SlugInsightStatusCard } from "@/components/insights/slug-insight-status-card";
import { MetricEmptyState } from "@/components/insights/metric-empty-state";
import { MetricStatStrip } from "@/components/insights/metric-stat-strip";
import { CoachReadStrip } from "@/components/insights/derived/coach-read-strip";
import { MetricCorrelationCard } from "@/components/insights/metric-correlation-card";
import { MeasurementDiversityNudge } from "@/components/insights/measurement-diversity-nudge";
import { MetricTargetSummary } from "@/components/insights/metric-target-summary";
@@ -133,6 +134,13 @@ export default function InsightsPulsPage() {
windowStats={statsByType?.PULSE ?? null}
/>
}
coachReadStrip={
<CoachReadStrip
metricType={hasRestingHr ? "RESTING_HEART_RATE" : "PULSE"}
unit="bpm"
fractionDigits={0}
/>
}
diversityNudge={
<MeasurementDiversityNudge
measurementType="PULSE"
+2
View File
@@ -13,6 +13,7 @@ import { HealthChartDynamic } from "@/components/charts/health-chart-dynamic";
import { SlugInsightStatusCard } from "@/components/insights/slug-insight-status-card";
import { MetricEmptyState } from "@/components/insights/metric-empty-state";
import { MetricStatStrip } from "@/components/insights/metric-stat-strip";
import { CoachReadStrip } from "@/components/insights/derived/coach-read-strip";
import { MetricCorrelationCard } from "@/components/insights/metric-correlation-card";
import { MeasurementDiversityNudge } from "@/components/insights/measurement-diversity-nudge";
import { MetricTargetSummary } from "@/components/insights/metric-target-summary";
@@ -88,6 +89,7 @@ export default function InsightsGewichtPage() {
windowStats={statsByType?.WEIGHT ?? null}
/>
}
coachReadStrip={<CoachReadStrip metricType="WEIGHT" unit="kg" />}
diversityNudge={
<MeasurementDiversityNudge
measurementType="WEIGHT"