Files
HealthLog/src/app/api/export/route.ts
T
Marc-André Bombeck 67207d725e chore(release): v1.4.41 — iOS perf hotfix, soft-delete completeness, tree hygiene
Eight touch-disjoint waves landed on develop before this release:

- W-INSIGHTS-HOT + W-RECONCILE-INSIGHTS — extract the timeout-stub
  persist helper and wire all seven *-status routes through it, closing
  the recurring ~14 s warm response the iOS client sees on dashboard
  open once the AI provider has stalled once that day.
- W-DELETED-2 — close the remaining three soft-delete reader-tier gaps
  (export bundle, gamification achievements, doctor-report aggregator)
  with a five-route integration-test pin.
- W-ORG — types consolidation into src/types/analytics.ts and
  src/types/backups.ts; prompt directory unification.
- W-FRONTEND-FACTORY — queryKey factory expansion + ESLint rule lift.
- W-IOS-COORD — POST /api/auth/check-user four-branch onboarding
  discovery, identifier matched verbatim, per-IP rate-limited.
- W-SIMPLIFIER — today-intake projection helper extract; unused tx?
  params dropped; 13 knip-flagged dead exports trimmed.
- W-PERF-OPS — v1.4.39 W-SUM legacy-NULL UNION discovery arm retired.
- W-PROCESS-DOCS — pg.Pool sizing operator doc; ESLint rule
  formalisation; knip CI gate staged for v1.4.42 flip.

No migration, no env-var change, no API contract break for iOS v0.5.4.
The new check-user endpoint is an additive surface. AP-2 .p8 key is
installed; SB-5 time-sensitive APNs is live as of this deploy.

pnpm test --run green at 4732 passing / 1 skipped.
pnpm typecheck, pnpm lint, pnpm knip green.
2026-05-21 18:36:17 +02:00

149 lines
4.6 KiB
TypeScript

import { prisma } from "@/lib/db";
import { apiHandler, requireAuth } from "@/lib/api-handler";
import { annotate } from "@/lib/logging/context";
import { auditLog } from "@/lib/auth/audit";
import { apiError, getClientIp } from "@/lib/api-response";
import { checkRateLimit } from "@/lib/rate-limit";
import {
toCSV,
formatMeasurementsForExport,
formatMedicationsForExport,
formatIntakeEventsForExport,
formatMoodEntriesForExport,
} from "@/lib/export";
import { resolveUserTimezone } from "@/lib/tz/resolver";
import { NextRequest, NextResponse } from "next/server";
/**
* Export user data.
* Query params:
* format: "csv" | "json" (default: json)
* type: "measurements" | "medications" | "intake" | "all" (default: all)
*
* Re-authentication is required via session (user must be logged in).
*/
export const GET = apiHandler(async (request: NextRequest) => {
const { user } = await requireAuth();
const rl = await checkRateLimit(`export:${user.id}`, 10, 60 * 60 * 1000);
if (!rl.allowed) {
return apiError("Maximum 10 exports per hour", 429);
}
annotate({ action: { name: "export.download" } });
const { searchParams } = new URL(request.url);
const format = searchParams.get("format") ?? "json";
const type = searchParams.get("type") ?? "all";
if (!["csv", "json"].includes(format)) {
return apiError("Format must be csv or json", 422);
}
if (
!["measurements", "medications", "intake", "mood", "all"].includes(type)
) {
return apiError(
"Type must be measurements, medications, intake, mood, or all",
422,
);
}
const userId = user.id;
const userTz = await resolveUserTimezone(userId);
const data: Record<string, unknown> = {};
if (type === "measurements" || type === "all") {
const measurements = await prisma.measurement.findMany({
// v1.4.41 W-DELETED-2 — exclude soft-deleted measurements from
// the legacy /api/export endpoint so CSV + JSON downloads stay
// consistent with the live measurement reads.
where: { userId, deletedAt: null },
orderBy: { measuredAt: "desc" },
});
data.measurements =
format === "csv"
? toCSV(formatMeasurementsForExport(measurements, userTz))
: formatMeasurementsForExport(measurements, userTz);
}
if (type === "medications" || type === "all") {
const medications = await prisma.medication.findMany({
where: { userId },
include: { schedules: true },
orderBy: { createdAt: "desc" },
});
data.medications =
format === "csv"
? toCSV(formatMedicationsForExport(medications))
: formatMedicationsForExport(medications);
}
if (type === "intake" || type === "all") {
const events = await prisma.medicationIntakeEvent.findMany({
where: { userId },
include: { medication: { select: { name: true } } },
orderBy: { scheduledFor: "desc" },
});
data.intakeEvents =
format === "csv"
? toCSV(formatIntakeEventsForExport(events, userTz))
: formatIntakeEventsForExport(events, userTz);
}
if (type === "mood" || type === "all") {
const moodEntries = await prisma.moodEntry.findMany({
where: { userId },
orderBy: { moodLoggedAt: "desc" },
});
data.moodEntries =
format === "csv"
? toCSV(formatMoodEntriesForExport(moodEntries, userTz))
: formatMoodEntriesForExport(moodEntries, userTz);
}
await auditLog("export.download", {
userId,
ipAddress: getClientIp(request),
details: { format, type },
});
annotate({ meta: { export_format: format, export_type: type } });
if (format === "csv") {
// For CSV with all types, combine into separate sections
const csvParts: string[] = [];
if (data.measurements) {
csvParts.push("# Measurements\n" + data.measurements);
}
if (data.medications) {
csvParts.push("# Medications\n" + data.medications);
}
if (data.intakeEvents) {
csvParts.push("# Intake Events\n" + data.intakeEvents);
}
if (data.moodEntries) {
csvParts.push("# Mood Entries\n" + data.moodEntries);
}
const csvContent = csvParts.join("\n\n");
return new NextResponse(csvContent, {
status: 200,
headers: {
"Content-Type": "text/csv; charset=utf-8",
"Content-Disposition": `attachment; filename="healthlog-export-${new Date().toISOString().slice(0, 10)}.csv"`,
},
});
}
return NextResponse.json(
{ data },
{
status: 200,
headers: {
"Content-Disposition": `attachment; filename="healthlog-export-${new Date().toISOString().slice(0, 10)}.json"`,
},
},
);
});