forked from fahrPool/fahrPool
31 lines
1.2 KiB
TypeScript
31 lines
1.2 KiB
TypeScript
import { loadProfile } from "@/lib/auth/profile";
|
|
import { kannReportsSehen } from "@/lib/auth/roles";
|
|
import { berlinTag } from "@/lib/datum/berlin";
|
|
import { berechneReport, reportZeitraum } from "@/lib/reports/report";
|
|
import { reportCsv } from "@/lib/reports/csv";
|
|
import { ladeReportDaten } from "@/lib/reports/queries";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function GET(request: Request): Promise<Response> {
|
|
const profil = await loadProfile();
|
|
if (profil.status !== "ok" || !kannReportsSehen(profil.profile.role)) {
|
|
return new Response("Keine Berechtigung", { status: 403 });
|
|
}
|
|
const jetzt = new Date();
|
|
const params = Object.fromEntries(new URL(request.url).searchParams);
|
|
const { von, bis } = reportZeitraum(params, berlinTag(jetzt));
|
|
const daten = await ladeReportDaten(von, bis);
|
|
if (daten.status === "fehler") {
|
|
return new Response("Report konnte nicht geladen werden", { status: 500 });
|
|
}
|
|
const report = berechneReport({ ...daten, von, bis, jetzt });
|
|
return new Response(reportCsv(report), {
|
|
headers: {
|
|
"Content-Type": "text/csv; charset=utf-8",
|
|
"Cache-Control": "no-store",
|
|
"Content-Disposition": `attachment; filename="report_${von}_${bis}.csv"`,
|
|
},
|
|
});
|
|
}
|