Übergabestand 2026-10-14
Alpenwerk HR — HR-Stammdatenverwaltung Mitarbeiter:innen, Organisationsstruktur, Planstellen, Eintritt, Austritt, Versetzung, Beförderung, Karenz, On- und Offboarding, Berichte, Exporte, Massenimport und ein lückenloser Nachweis jeder Änderung. Next.js 16, React 19 und TypeScript; PostgreSQL über Kysely; Anmeldung ausschliesslich über Microsoft Entra ID. Die Fachlogik liegt in der Datenbank, die Zugriffsschranke ist Row Level Security. Betrieb auf Windows Server: siehe deploy/windows/README.md. Sicherheitskonzept: docs/sicherheitskonzept.md.
This commit is contained in:
132
app/api/export/report/route.ts
Normal file
132
app/api/export/report/route.ts
Normal file
@@ -0,0 +1,132 @@
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
import { exportFilename, exportResponseHeaders, toCsv, toXlsx, type ExportColumn } from "@/lib/export";
|
||||
import { parseCriteria } from "@/lib/report-criteria";
|
||||
import {
|
||||
aggregateEvents,
|
||||
aggregateReport,
|
||||
EVENT_GROUP_LABELS,
|
||||
GROUP_LABELS,
|
||||
MEASURE_LABELS,
|
||||
parseEventDateParam,
|
||||
parseEventGroupDimension,
|
||||
parseEventSplitDimension,
|
||||
parseEventType,
|
||||
parseGroupDimension,
|
||||
parseIsoDateParam,
|
||||
parseMeasure,
|
||||
parseMode,
|
||||
parseSplitDimension,
|
||||
sortKeysForDimension,
|
||||
sumValues,
|
||||
totalForRows,
|
||||
type EventGroupDimension,
|
||||
type GroupDimension,
|
||||
type Measure,
|
||||
type ReportRow,
|
||||
} from "@/lib/reports";
|
||||
import { loadEventHistory, loadOrgLookups, loadSnapshotEmployees } from "@/lib/reports-data";
|
||||
import { requireHrUser } from "@/lib/auth/require-hr";
|
||||
import { withUser } from "@/lib/db";
|
||||
|
||||
// Exports exactly the pivot table currently on screen (same mode/measure or
|
||||
// event-type/group/split/filters, read from the query string the client
|
||||
// already keeps in the URL) as a flat table — one row per group, one column
|
||||
// per split value if a split is active.
|
||||
export async function GET(request: NextRequest) {
|
||||
const gate = await requireHrUser();
|
||||
if ("denied" in gate) return gate.denied;
|
||||
|
||||
const params = request.nextUrl.searchParams;
|
||||
const format = params.get("format") === "xlsx" ? "xlsx" : "csv";
|
||||
const mode = parseMode(params.get("mode"));
|
||||
// Eine Transaktion für Nachschlagewerte und Daten: dort gilt der
|
||||
// Sitzungskontext, und beide sehen denselben Lesestand.
|
||||
const { rows, columns, filenameBase } = await withUser(gate.userId, async (tx) => {
|
||||
const { lookups } = await loadOrgLookups(tx);
|
||||
let rows: ReportRow[];
|
||||
let columns: ExportColumn<ReportRow>[];
|
||||
let filenameBase: string;
|
||||
|
||||
if (mode === "events") {
|
||||
const group = parseEventGroupDimension(params.get("group"));
|
||||
const split = parseEventSplitDimension(params.get("split"));
|
||||
const eventType = parseEventType(params.get("eventType"));
|
||||
const events = await loadEventHistory(tx, {
|
||||
eventType: eventType ?? undefined,
|
||||
division: params.get("division") ?? undefined,
|
||||
location: params.get("location") ?? undefined,
|
||||
from: parseEventDateParam(params.get("from")),
|
||||
to: parseEventDateParam(params.get("to")),
|
||||
});
|
||||
rows = aggregateEvents(events, group, split, lookups);
|
||||
columns = eventReportColumns(rows, group, split, sumValues(rows));
|
||||
filenameBase = `ereignisse-${eventType ?? "alle"}-${group}`;
|
||||
} else {
|
||||
const measure = parseMeasure(params.get("measure"));
|
||||
const group = parseGroupDimension(params.get("group"));
|
||||
const split = parseSplitDimension(params.get("split"));
|
||||
const asOf = parseIsoDateParam(params.get("asOf"));
|
||||
const employees = await loadSnapshotEmployees(tx, {
|
||||
division: params.get("division") ?? undefined,
|
||||
location: params.get("location") ?? undefined,
|
||||
status: params.get("status") ?? undefined,
|
||||
criteria: parseCriteria((k) => params.get(k)),
|
||||
asOf,
|
||||
});
|
||||
rows = aggregateReport(employees, measure, group, split, lookups, asOf);
|
||||
columns = snapshotReportColumns(rows, measure, group, split, totalForRows(rows, measure));
|
||||
filenameBase = `bericht-${measure}-${group}`;
|
||||
}
|
||||
|
||||
return { rows, columns, filenameBase };
|
||||
});
|
||||
|
||||
const filename = exportFilename(filenameBase, format);
|
||||
|
||||
const body = format === "xlsx" ? await toXlsx(rows, columns, "Bericht") : toCsv(rows, columns);
|
||||
// TS 5.9's Uint8Array<ArrayBufferLike> vs DOM's BlobPart/ArrayBuffer<> generic
|
||||
// mismatch (microsoft/TypeScript#59417) — a real Uint8Array works fine here.
|
||||
return new NextResponse(new Blob([body as BlobPart]), { headers: exportResponseHeaders(filename, format) });
|
||||
}
|
||||
|
||||
function snapshotReportColumns(
|
||||
rows: ReportRow[],
|
||||
measure: Measure,
|
||||
group: GroupDimension,
|
||||
split: GroupDimension | null,
|
||||
total: number
|
||||
): ExportColumn<ReportRow>[] {
|
||||
const columns: ExportColumn<ReportRow>[] = [{ header: GROUP_LABELS[group], get: (r) => r.key }];
|
||||
if (split) {
|
||||
const splitKeys = sortKeysForDimension(Array.from(new Set(rows.flatMap((r) => r.split?.map((s) => s.key) ?? []))), split);
|
||||
for (const key of splitKeys) {
|
||||
columns.push({ header: key, get: (r) => Math.round((r.split?.find((s) => s.key === key)?.value ?? 0) * 100) / 100 });
|
||||
}
|
||||
}
|
||||
columns.push(
|
||||
{ header: MEASURE_LABELS[measure], get: (r) => Math.round(r.value * 100) / 100 },
|
||||
{ header: "Anzahl", get: (r) => r.count },
|
||||
{ header: "Anteil (%)", get: (r) => (total > 0 ? Math.round((r.value / total) * 1000) / 10 : 0) }
|
||||
);
|
||||
return columns;
|
||||
}
|
||||
|
||||
function eventReportColumns(
|
||||
rows: ReportRow[],
|
||||
group: EventGroupDimension,
|
||||
split: EventGroupDimension | null,
|
||||
total: number
|
||||
): ExportColumn<ReportRow>[] {
|
||||
const columns: ExportColumn<ReportRow>[] = [{ header: EVENT_GROUP_LABELS[group], get: (r) => r.key }];
|
||||
if (split) {
|
||||
const splitKeys = Array.from(new Set(rows.flatMap((r) => r.split?.map((s) => s.key) ?? [])));
|
||||
for (const key of splitKeys) {
|
||||
columns.push({ header: key, get: (r) => r.split?.find((s) => s.key === key)?.value ?? 0 });
|
||||
}
|
||||
}
|
||||
columns.push(
|
||||
{ header: "Anzahl", get: (r) => r.count },
|
||||
{ header: "Anteil (%)", get: (r) => (total > 0 ? Math.round((r.value / total) * 1000) / 10 : 0) }
|
||||
);
|
||||
return columns;
|
||||
}
|
||||
Reference in New Issue
Block a user