Files
alpenwerk-hr/app/(app)/reports/page.tsx
Andrei Laas 2a0ab1d3dc
Some checks failed
CI / Migrationen auf leerer Datenbank (push) Has been cancelled
CI / Ausrollen (push) Has been cancelled
CI / Lint, Typen, Tests, Build (push) Has been cancelled
Berichte: Mehrfachauswahl in allen Kriterien
Jedes Auswahlkriterium der Berichte nimmt mehrere Werte: die weiteren
Kriterien, Organisationseinheit, Standort, Kostenstelle, Ereignistyp und
dessen Untertypen. Mehrere Werte eines Kriteriums verbinden sich mit
"oder", verschiedene Kriterien mit "und". In der Adresszeile trennt "|",
weil ein Wert des Personenkreises selbst ein Komma enthaelt; ein einzelner
Wert liest sich wie bisher, gespeicherte Berichte gelten unveraendert.

Ein Untertyp schraenkt nur seinen eigenen Ereignistyp ein. Exporte und
Zielsystem-Dateien uebernehmen dieselbe Auswahl.

Geprueft: lint, tsc, types:check, 955 Tests, Waechter.
2026-10-29 08:35:27 +01:00

159 lines
5.4 KiB
TypeScript

import { Suspense } from "react";
import { ReportsPageClient } from "@/components/reports/ReportsPageClient";
import {
aggregateEvents,
aggregateReport,
parseEventDateParam,
parseEventGroupDimension,
parseEventSplitDimension,
parseEventSubtypes,
parseEventTypes,
parseGroupDimension,
parseIsoDateParam,
parseMeasure,
parseMode,
parseSplitDimension,
sumValues,
totalForRows,
} from "@/lib/reports";
import { parseCriteria } from "@/lib/report-criteria";
import { loadEventHistory, loadOrgLookups, loadSnapshotEmployees } from "@/lib/reports-data";
import { currentUserId } from "@/lib/auth/session";
import { withUser } from "@/lib/db";
// Nur die Parameter, die diese Seite selbst auswertet. Die Auswahlkriterien
// stehen ebenfalls in der Adresszeile, werden aber geschlossen von
// parseCriteria gelesen — siehe lib/report-criteria.ts.
type SearchParams = Record<string, string | string[] | undefined> & {
mode?: string;
measure?: string;
group?: string;
split?: string;
division?: string;
location?: string;
kostenstelle?: string;
status?: string;
asOf?: string;
eventType?: string;
eventSubtype?: string;
from?: string;
to?: string;
};
export default async function ReportsPage({ searchParams }: { searchParams: Promise<SearchParams> }) {
const params = await searchParams;
const mode = parseMode(params.mode);
// Both modes are parsed up front so the data load can start before
// anything is awaited. They group by different dimension sets, so the
// parsed values stay separate rather than being widened into one type.
const asOf = parseIsoDateParam(params.asOf);
const eventGroup = parseEventGroupDimension(params.group);
const eventSplit = parseEventSplitDimension(params.split);
const eventTypes = parseEventTypes(params.eventType);
const eventSubtypes = parseEventSubtypes(params.eventSubtype, eventTypes);
const from = parseEventDateParam(params.from);
const to = parseEventDateParam(params.to);
const measure = parseMeasure(params.measure);
const group = parseGroupDimension(params.group);
const split = parseSplitDimension(params.split);
const criteria = parseCriteria((k) => (typeof params[k] === "string" ? (params[k] as string) : undefined));
// The report data depends on neither the org lookups nor on who is signed
// in, so all three go out together. Against a remote database a round trip
// costs about as much as the query itself, so sequential waves would
// dominate this page's cost.
const userId = await currentUserId();
// Alles in einer Transaktion — dort gilt der Sitzungskontext, und der
// Lesestand ist über alle Abfragen hinweg derselbe.
const { lookups, units, locations, events, employees, savedReports, kostenstellen } = await withUser(userId, async (tx) => {
const [{ lookups, units, locations }, events, employees, savedReports, kostenstellen] = await Promise.all([
loadOrgLookups(tx),
mode === "events"
? loadEventHistory(tx, {
eventTypes,
eventSubtypes,
division: params.division,
location: params.location,
from,
to,
})
: Promise.resolve([]),
mode === "snapshot"
? loadSnapshotEmployees(tx, {
division: params.division,
location: params.location,
kostenstelle: params.kostenstelle,
status: params.status,
criteria,
asOf,
})
: Promise.resolve([]),
userId
? tx
.selectFrom("saved_reports")
.select(["id", "name", "config"])
.where("created_by", "=", userId)
.orderBy("created_at", "desc")
.execute()
: Promise.resolve([]),
// Für den Filter: alle Kostenstellen, auch beendete — ein Stichtag in
// der Vergangenheit kann eine meinen, die es heute nicht mehr gibt.
tx.selectFrom("cost_centers").select(["id", "code", "name"]).orderBy("code").execute(),
]);
return { lookups, units, locations, events, employees, savedReports, kostenstellen };
});
if (mode === "events") {
const rows = aggregateEvents(events, eventGroup, eventSplit, lookups);
return (
<Suspense>
<ReportsPageClient
mode="events"
eventGroup={eventGroup}
eventSplit={eventSplit ?? ""}
eventTypes={eventTypes}
eventSubtypes={eventSubtypes}
eventFilters={{ division: params.division ?? "", location: params.location ?? "", from: from ?? "", to: to ?? "" }}
rows={rows}
total={sumValues(rows)}
recordCount={events.length}
units={units}
locations={locations}
savedReports={savedReports}
/>
</Suspense>
);
}
const rows = aggregateReport(employees, measure, group, split, lookups, asOf);
return (
<Suspense>
<ReportsPageClient
mode="snapshot"
measure={measure}
group={group}
split={split ?? ""}
asOf={asOf ?? ""}
filters={{
division: params.division ?? "",
location: params.location ?? "",
kostenstelle: params.kostenstelle ?? "",
status: params.status ?? "",
}}
criteria={criteria}
rows={rows}
total={totalForRows(rows, measure)}
recordCount={employees.length}
units={units}
locations={locations}
kostenstellen={kostenstellen}
savedReports={savedReports}
/>
</Suspense>
);
}