Files
HealthLog/src/app/api/admin/rollups/recompute/route.ts
T
Marc-André Bombeck 65677c2112 fix(api): bound every JSON body read
Raising the middleware body cap to 512 MB for archive imports removed
the implicit ceiling that protected JSON routes, so any bulk endpoint
would buffer an arbitrarily large body before validation ran. Every
JSON body read under /api now carries an explicit byte cap sized to its
legitimate payload (64 KB for single-record routes up to 16 MB for the
measurement import), webhooks cap before signature work, and oversized
bodies return 413.
2026-06-10 11:36:51 +02:00

114 lines
3.4 KiB
TypeScript

/**
* `POST /api/admin/rollups/recompute` — operator-triggered ad-hoc
* rollup recompute.
*
* Surface added in v1.4.38.7 after the boot-time backfill could not
* be re-triggered without a deploy. A power-user account whose
* `isFullyCovered(coverage)` probe returned `false` (typically
* because a brand-new measurement type landed between the last fold
* and the next analytics read) gets stranded on the live SQL
* aggregator until the next worker boot. This endpoint lets the
* operator force a fold for one user or kick the boot-discovery loop
* across every user without bouncing the worker container.
*
* Body:
* { userId?: string }
*
* - `userId` — single user mode. Synchronously calls
* `recomputeUserRollups(userId)` and returns the row count when
* the fold completes. The call is awaited inside the request, so
* the operator sees the duration in the response.
* - `userId` omitted — calls `enqueueBootTimeRollupBackfill()` to
* re-run the worker-boot discovery (`{ enqueued, skipped }` shape).
*
* Admin gate via `requireAdmin()` (cookie-only — Bearer never elevates
* to admin per the v1.4.25 security boundary).
*/
import type { NextRequest } from "next/server";
import { apiHandler, requireAdmin } from "@/lib/api-handler";
import { auditLog } from "@/lib/auth/audit";
import {
apiError,
apiSuccess,
getClientIp,
safeJson,
} from "@/lib/api-response";
import { annotate } from "@/lib/logging/context";
import {
enqueueBootTimeRollupBackfill,
recomputeUserRollups,
} from "@/lib/rollups/measurement-rollups";
interface RecomputeBody {
userId?: string;
}
export const POST = apiHandler(async (request: NextRequest) => {
const { user } = await requireAdmin();
annotate({ action: { name: "admin.rollups.recompute" } });
const { data: rawBody, error: jsonError } = await safeJson<RecomputeBody>(
request,
{ maxBytes: 64 * 1024 },
);
if (jsonError) return jsonError;
const body: RecomputeBody =
rawBody && typeof rawBody === "object" ? rawBody : {};
const userId =
typeof body.userId === "string" && body.userId.length > 0
? body.userId
: undefined;
if (userId) {
await auditLog("admin.rollups.recompute.start", {
userId: user.id,
ipAddress: getClientIp(request),
details: { targetUserId: userId, mode: "single" },
});
let result: Awaited<ReturnType<typeof recomputeUserRollups>>;
try {
result = await recomputeUserRollups(userId);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
return apiError(message, 500);
}
await auditLog("admin.rollups.recompute.complete", {
userId: user.id,
ipAddress: getClientIp(request),
details: { targetUserId: userId, ...result },
});
return apiSuccess({
mode: "single",
targetUserId: userId,
rowsUpserted: result.rowsUpserted,
durationMs: result.durationMs,
});
}
await auditLog("admin.rollups.recompute.start", {
userId: user.id,
ipAddress: getClientIp(request),
details: { mode: "boot-discovery" },
});
const discovery = await enqueueBootTimeRollupBackfill();
await auditLog("admin.rollups.recompute.complete", {
userId: user.id,
ipAddress: getClientIp(request),
details: { mode: "boot-discovery", ...discovery },
});
return apiSuccess({
mode: "boot-discovery",
enqueued: discovery.enqueued,
skipped: discovery.skipped,
error: discovery.error,
});
});