- components/hire/: 4-step Hire Wizard (Person/Position/Vertrag/ Zusammenfassung) matching sec4.4, with a HireWizardProvider context so it can be opened both from the global "+ Neueinstellung" button and from a "Fortsetzen" link on a saved draft. - actions/hireDrafts.ts: save/delete hire_drafts (owner-scoped RLS already in place from Phase 1). Dashboard now shows the "Entwuerfe" card the Phase 1 plan deferred, since the wizard it depends on now exists. - lib/positions.ts: shared open-positions loader (position number, org breadcrumb, resolved manager name) used by both the wizard and (later) the Positions page. Two real bugs found via live testing and fixed in supabase/functions.sql: 1. hire_employee/rehire_employee: a two-branch CASE returning bare string literals defaults to `text`, not the target enum, so `status = case when ... then 'Geplant' else 'Aktiv' end` failed against the employment_status column. Fixed with an explicit ::employment_status cast on the whole CASE expression. 2. Postgres precedence gotcha: ->> and || sit at the *same* precedence tier and left-associate, so `payload->>'first_name' || ' ' || payload->>'last_name'` does not group the way it reads - it tries to apply ->> to an intermediate text value and fails with "operator does not exist: text ->> unknown". Fixed by parenthesizing every ->>'...' expression that participates in a || chain. Also fixed: hire_employee referenced v_position.title outside the branch that assigns v_position, raising "record not assigned" whenever a hire wasn't tied to a position_id; extracted a v_job_title variable instead. Verified live end-to-end: wizard search -> select position -> submit creates the employee, closes the position, and writes matching employee_history + audit_log rows atomically.
238 lines
9.6 KiB
TypeScript
238 lines
9.6 KiB
TypeScript
import Link from "next/link";
|
|
import { DraftsCard } from "@/components/dashboard/DraftsCard";
|
|
import { actionBadgeStyle } from "@/lib/colors";
|
|
import { fmtDate } from "@/lib/format";
|
|
import { createClient } from "@/lib/supabase/server";
|
|
|
|
function isoDate(d: Date): string {
|
|
return d.toISOString().slice(0, 10);
|
|
}
|
|
|
|
const TONE_TEXT: Record<string, string> = {
|
|
default: "text-ink",
|
|
success: "text-success-text",
|
|
danger: "text-danger-text",
|
|
warning: "text-warning-text",
|
|
brand: "text-brand-700",
|
|
};
|
|
|
|
const DOT_STYLES: Record<string, string> = {
|
|
Eintritt: "bg-success-text",
|
|
Wiedereintritt: "bg-success-text",
|
|
Rückkehr: "bg-success-text",
|
|
Austritt: "bg-danger-text",
|
|
Versetzung: "bg-info-text",
|
|
Beförderung: "bg-purple-text",
|
|
Reorganisation: "bg-purple-text",
|
|
Karenz: "bg-warning-text",
|
|
Vertragsänderung: "bg-warning-text",
|
|
Stammdatenänderung: "bg-warning-text",
|
|
Gehaltsanpassung: "bg-warning-text",
|
|
};
|
|
|
|
const KIND_LABEL = { hire: "Eintritt", exit: "Austritt", return: "Karenz-Rückkehr" } as const;
|
|
|
|
export default async function DashboardPage() {
|
|
const supabase = await createClient();
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser();
|
|
const { data: drafts } = user
|
|
? await supabase
|
|
.from("hire_drafts")
|
|
.select("id, step, payload, updated_at")
|
|
.eq("created_by", user.id)
|
|
.order("updated_at", { ascending: false })
|
|
: { data: [] };
|
|
|
|
const today = new Date();
|
|
const todayIso = isoDate(today);
|
|
const yearStart = isoDate(new Date(today.getFullYear(), 0, 1));
|
|
const yearEnd = isoDate(new Date(today.getFullYear(), 11, 31));
|
|
const in60 = new Date(today);
|
|
in60.setDate(in60.getDate() + 60);
|
|
const in60Iso = isoDate(in60);
|
|
|
|
const [
|
|
activeCountRes,
|
|
karenzCountRes,
|
|
hiresYtdRes,
|
|
exitsYtdRes,
|
|
openPositionsRes,
|
|
fteRowsRes,
|
|
divisionsRes,
|
|
headcountRowsRes,
|
|
upcomingHiresRes,
|
|
upcomingExitsRes,
|
|
upcomingReturnsRes,
|
|
historyRes,
|
|
] = await Promise.all([
|
|
supabase.from("employees_directory").select("id", { count: "exact", head: true }).in("status", ["Aktiv", "Karenz"]),
|
|
supabase.from("employees_directory").select("id", { count: "exact", head: true }).eq("status", "Karenz"),
|
|
supabase
|
|
.from("employees_directory")
|
|
.select("id", { count: "exact", head: true })
|
|
.gte("entry_date", yearStart)
|
|
.lte("entry_date", yearEnd),
|
|
supabase
|
|
.from("employees_directory")
|
|
.select("id", { count: "exact", head: true })
|
|
.gte("exit_date", yearStart)
|
|
.lte("exit_date", yearEnd),
|
|
supabase.from("positions").select("id", { count: "exact", head: true }).eq("status", "open"),
|
|
supabase.from("employees_directory").select("weekly_hours").in("status", ["Aktiv", "Karenz"]),
|
|
supabase.from("divisions").select("id, name"),
|
|
supabase.from("employees_directory").select("division_id").in("status", ["Aktiv", "Karenz"]),
|
|
supabase
|
|
.from("employees_directory")
|
|
.select("id, first_name, last_name, entry_date")
|
|
.eq("status", "Geplant")
|
|
.gte("entry_date", todayIso)
|
|
.lte("entry_date", in60Iso),
|
|
supabase
|
|
.from("employees_directory")
|
|
.select("id, first_name, last_name, exit_date")
|
|
.not("exit_date", "is", null)
|
|
.gte("exit_date", todayIso)
|
|
.lte("exit_date", in60Iso),
|
|
supabase
|
|
.from("employees_directory")
|
|
.select("id, first_name, last_name, karenz_return_date")
|
|
.eq("status", "Karenz")
|
|
.not("karenz_return_date", "is", null)
|
|
.gte("karenz_return_date", todayIso)
|
|
.lte("karenz_return_date", in60Iso),
|
|
supabase
|
|
.from("employee_history")
|
|
.select("id, employee_id, event_date, event_type, description")
|
|
.order("event_date", { ascending: false })
|
|
.order("created_at", { ascending: false })
|
|
.limit(10),
|
|
]);
|
|
|
|
const fte = (fteRowsRes.data ?? []).reduce((sum, row) => sum + Number(row.weekly_hours), 0) / 38.5;
|
|
|
|
const headcountByDivision = new Map<string, number>();
|
|
for (const row of headcountRowsRes.data ?? []) {
|
|
if (!row.division_id) continue;
|
|
headcountByDivision.set(row.division_id, (headcountByDivision.get(row.division_id) ?? 0) + 1);
|
|
}
|
|
const divisionBars = (divisionsRes.data ?? [])
|
|
.map((d) => ({ name: d.name, count: headcountByDivision.get(d.id) ?? 0 }))
|
|
.sort((a, b) => b.count - a.count);
|
|
const maxDivisionCount = Math.max(1, ...divisionBars.map((d) => d.count));
|
|
|
|
type UpcomingItem = { id: string; label: string; date: string; kind: keyof typeof KIND_LABEL };
|
|
const upcoming: UpcomingItem[] = [
|
|
...(upcomingHiresRes.data ?? []).map((e) => ({
|
|
id: e.id,
|
|
label: `${e.first_name} ${e.last_name}`,
|
|
date: e.entry_date,
|
|
kind: "hire" as const,
|
|
})),
|
|
...(upcomingExitsRes.data ?? []).map((e) => ({
|
|
id: e.id,
|
|
label: `${e.first_name} ${e.last_name}`,
|
|
date: e.exit_date!,
|
|
kind: "exit" as const,
|
|
})),
|
|
...(upcomingReturnsRes.data ?? []).map((e) => ({
|
|
id: e.id,
|
|
label: `${e.first_name} ${e.last_name}`,
|
|
date: e.karenz_return_date!,
|
|
kind: "return" as const,
|
|
})),
|
|
]
|
|
.sort((a, b) => a.date.localeCompare(b.date))
|
|
.slice(0, 8);
|
|
|
|
const historyEmployeeIds = Array.from(new Set((historyRes.data ?? []).map((h) => h.employee_id)));
|
|
const historyEmployeesRes = historyEmployeeIds.length
|
|
? await supabase.from("employees_directory").select("id, first_name, last_name").in("id", historyEmployeeIds)
|
|
: { data: [] as { id: string; first_name: string; last_name: string }[] };
|
|
const employeeNameById = new Map((historyEmployeesRes.data ?? []).map((e) => [e.id, `${e.first_name} ${e.last_name}`]));
|
|
|
|
const kpis = [
|
|
{ label: "Aktive Mitarbeiter:innen", value: activeCountRes.count ?? 0, tone: "default" },
|
|
{ label: "FTE", value: fte.toFixed(1), tone: "default" },
|
|
{ label: "Eintritte (Jahr)", value: hiresYtdRes.count ?? 0, tone: "success" },
|
|
{ label: "Austritte (Jahr)", value: exitsYtdRes.count ?? 0, tone: "danger" },
|
|
{ label: "In Karenz", value: karenzCountRes.count ?? 0, tone: "warning" },
|
|
{ label: "Offene Positionen", value: openPositionsRes.count ?? 0, tone: "brand" },
|
|
];
|
|
|
|
return (
|
|
<div className="flex flex-col gap-6">
|
|
{drafts && drafts.length > 0 && <DraftsCard drafts={drafts} />}
|
|
<div className="grid grid-cols-2 gap-4 sm:grid-cols-3 lg:grid-cols-6">
|
|
{kpis.map((kpi) => (
|
|
<div key={kpi.label} className="rounded border border-border bg-white p-4">
|
|
<div className="text-xs font-semibold uppercase tracking-wide text-ink-muted">{kpi.label}</div>
|
|
<div className={`mt-2 text-2xl font-extrabold ${TONE_TEXT[kpi.tone]}`}>{kpi.value}</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 gap-4 lg:grid-cols-3">
|
|
<div className="rounded border border-border bg-white p-4">
|
|
<h2 className="mb-3 text-sm font-bold text-ink">Headcount nach Bereich</h2>
|
|
<div className="flex flex-col gap-2">
|
|
{divisionBars.map((d) => (
|
|
<div key={d.name}>
|
|
<div className="mb-0.5 flex justify-between text-xs text-ink-body">
|
|
<span>{d.name}</span>
|
|
<span className="font-semibold">{d.count}</span>
|
|
</div>
|
|
<div className="h-2 rounded bg-surface">
|
|
<div className="h-2 rounded bg-brand-500" style={{ width: `${(d.count / maxDivisionCount) * 100}%` }} />
|
|
</div>
|
|
</div>
|
|
))}
|
|
{divisionBars.length === 0 && <p className="text-sm text-ink-muted">Keine Daten vorhanden.</p>}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="rounded border border-border bg-white p-4">
|
|
<h2 className="mb-3 text-sm font-bold text-ink">Anstehend (60 Tage)</h2>
|
|
<ul className="flex flex-col divide-y divide-border">
|
|
{upcoming.map((item) => (
|
|
<li key={`${item.kind}-${item.id}`}>
|
|
<Link
|
|
href={`/employees/${item.id}`}
|
|
className="flex items-center justify-between py-2 text-sm hover:text-brand-700"
|
|
>
|
|
<span>
|
|
<span className="font-semibold text-ink">{item.label}</span>
|
|
<span className="ml-2 text-xs text-ink-muted">{KIND_LABEL[item.kind]}</span>
|
|
</span>
|
|
<span className="text-ink-muted">{fmtDate(item.date)}</span>
|
|
</Link>
|
|
</li>
|
|
))}
|
|
{upcoming.length === 0 && <p className="py-2 text-sm text-ink-muted">Keine anstehenden Ereignisse.</p>}
|
|
</ul>
|
|
</div>
|
|
|
|
<div className="rounded border border-border bg-white p-4">
|
|
<h2 className="mb-3 text-sm font-bold text-ink">Letzte Aktivitäten</h2>
|
|
<ul className="flex flex-col divide-y divide-border">
|
|
{(historyRes.data ?? []).map((h) => (
|
|
<li key={h.id} className="py-2">
|
|
<div className="flex items-center gap-2">
|
|
<span className={`inline-block h-2 w-2 shrink-0 rounded-full ${DOT_STYLES[h.event_type] ?? "bg-ink-muted"}`} />
|
|
<span className="text-sm font-semibold text-ink">{employeeNameById.get(h.employee_id) ?? "Unbekannt"}</span>
|
|
<span className={`rounded-full px-2 py-0.5 text-xs font-semibold ${actionBadgeStyle(h.event_type)}`}>
|
|
{h.event_type}
|
|
</span>
|
|
</div>
|
|
<p className="mt-1 text-xs text-ink-muted">{h.description}</p>
|
|
</li>
|
|
))}
|
|
{(historyRes.data ?? []).length === 0 && <p className="py-2 text-sm text-ink-muted">Keine Aktivitäten vorhanden.</p>}
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|