mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 23:33:29 +00:00
88 lines
3.6 KiB
TypeScript
88 lines
3.6 KiB
TypeScript
/**
|
|
* v1.21.2 (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, requireRecordAuth } from "@/lib/api-handler";
|
|
import { annotate } from "@/lib/logging/context";
|
|
import { checkAnalyticsReadRateLimit } from "@/lib/rate-limit";
|
|
import { requireModuleEnabled } from "@/lib/modules/gate";
|
|
import { requireAssistantSurface } from "@/lib/feature-flags";
|
|
import { measurementTypeEnum } from "@/lib/validations/measurement";
|
|
import { buildCoachReadStrip } from "@/lib/insights/derived/coach-read";
|
|
import { resolveServerLocale } from "@/lib/i18n/server-locale";
|
|
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) => {
|
|
// v1.37.0 — MANAGE-level read: computed over the whole record, with no
|
|
// provider anywhere on the path.
|
|
const { user } = await requireRecordAuth("manage", "record");
|
|
|
|
const m = await requireModuleEnabled(user.id, "insights");
|
|
if (!m.enabled) return m.response;
|
|
|
|
// The strip is the ambient Coach presence on the metric page, so it
|
|
// gates on the Coach assistant matrix too: an operator who turns the
|
|
// Coach off must not see a "Coach read" line linger.
|
|
await requireAssistantSurface("coach");
|
|
|
|
// 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;
|
|
|
|
// Line 2 of the strip is a finished sentence the clients print verbatim, so
|
|
// the reader's language is decided here — same resolution the correlations
|
|
// and analytics routes use (cookie, then the stored preference, then the
|
|
// Accept-Language header). Without it the strip answered a German page in
|
|
// English, one line under the other.
|
|
const locale = await resolveServerLocale({ userLocale: user.locale ?? null });
|
|
|
|
const strip = await buildCoachReadStrip(user.id, metric, locale);
|
|
|
|
annotate({
|
|
action: { name: "insights.coach-read" },
|
|
meta: {
|
|
metric,
|
|
locale,
|
|
has_baseline: strip.baseline !== null,
|
|
learning: strip.learning,
|
|
has_driver: strip.driver !== null,
|
|
},
|
|
});
|
|
|
|
return apiSuccess(strip);
|
|
});
|