mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-11 13:52:20 +00:00
GET /api/topics answers one entry per topic the record holds data for:
a headline value, a rule-based sentence and a 30-day sparkline.
GET /api/topics/{topic} answers a topic's signals with their usual
ranges, side-by-side series for a chosen window, the relationships
between its signals, 90-day trends, what it holds nothing for yet, and
its blocks (training load, lab markers with their trend, screener
trends, the cycle day).
Every figure comes from reads the metric pages already make: the local
daily values, the reconstructed nights, the mood score, computeLabTrend,
the training load and the screener trends. No model is involved. A
relationship says nothing below 14 paired days and is only called a
pattern at p < 0.05; a lab trend needs three readings spanning a year.
The preview is the viewer's own opt-in: with it off both routes answer
404 topics.preview_off. Topics, signals and blocks follow the record's
module switches and the sections the viewer's grant opens; the routes
read across sections, so a scoped grant is refused like the timeline.
/api/auth/me publishes the caller's opt-ins as previewFeatures.
81 lines
2.8 KiB
TypeScript
81 lines
2.8 KiB
TypeScript
/**
|
|
* `GET /api/topics/{topic}` — one topic page (preview).
|
|
*
|
|
* The topic's signals with their usual ranges, the side-by-side series for the
|
|
* chosen window, the relationships between its signals (14 paired days before
|
|
* anything is said), the 90-day trends, the signals it could show but holds
|
|
* nothing for, and its extra blocks (training load, lab markers with their
|
|
* trend, screener trends, today's place in the cycle). Every figure is
|
|
* computed on the server from existing reads; no model is involved.
|
|
*
|
|
* 404 `topics.preview_off` when the viewer has not switched the preview on,
|
|
* `topics.not_available` when the record's module switches do not offer the
|
|
* topic. Admission declares the whole record, like the overview.
|
|
*/
|
|
import { NextRequest } from "next/server";
|
|
|
|
import { apiHandler, requireRecordAuth } from "@/lib/api-handler";
|
|
import { apiError, apiSuccess, returnAllZodIssues } from "@/lib/api-response";
|
|
import { annotate } from "@/lib/logging/context";
|
|
import { checkAnalyticsReadRateLimit } from "@/lib/rate-limit";
|
|
import { hasTopicsPreview, loadTopicDetail } from "@/lib/topics/service";
|
|
import {
|
|
topicDetailQuerySchema,
|
|
topicPathSchema,
|
|
} from "@/lib/validations/topics";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
type RouteParams = { params: Promise<{ topic: string }> };
|
|
|
|
export const GET = apiHandler(
|
|
async (request: NextRequest, { params }: RouteParams) => {
|
|
const { user, actor, grantId } = await requireRecordAuth("read", "record");
|
|
annotate({ action: { name: "topics.detail.read" } });
|
|
|
|
if (!(await hasTopicsPreview(actor.id))) {
|
|
return apiError("Topics are a preview that is switched off", 404, {
|
|
errorCode: "topics.preview_off",
|
|
});
|
|
}
|
|
|
|
const path = topicPathSchema.safeParse(await params);
|
|
if (!path.success) {
|
|
return apiError("No such topic", 404, { errorCode: "topics.not_found" });
|
|
}
|
|
const query = topicDetailQuerySchema.safeParse({
|
|
range: request.nextUrl.searchParams.get("range") ?? undefined,
|
|
});
|
|
if (!query.success) return returnAllZodIssues(query.error, 422);
|
|
|
|
const rl = await checkAnalyticsReadRateLimit(actor.id);
|
|
if (!rl.allowed) {
|
|
return apiError("Too many analytics requests. Please retry later.", 429);
|
|
}
|
|
|
|
const detail = await loadTopicDetail({
|
|
userId: user.id,
|
|
grantId,
|
|
topic: path.data.topic,
|
|
range: query.data.range,
|
|
now: new Date(),
|
|
});
|
|
if (!detail) {
|
|
return apiError("This topic is not available for this record", 404, {
|
|
errorCode: "topics.not_available",
|
|
});
|
|
}
|
|
annotate({
|
|
meta: {
|
|
topic: detail.key,
|
|
topic_range: detail.range,
|
|
topic_status: detail.status,
|
|
topic_relationships_linked: detail.relationships.filter(
|
|
(r) => r.status === "linked",
|
|
).length,
|
|
},
|
|
});
|
|
return apiSuccess(detail);
|
|
},
|
|
);
|