Two reports, four defects, all of them in the way of ordinary use.
A position with a signed starter is not vacant. loadOpenPositions asked "is
anyone on it today?", so three positions whose new holders begin in
September and October were listed as open, labelled "vacant for 2 days".
That same list feeds the hire wizard, so it invited filling a position a
second time — discovered at the partial unique index, after the second
interview. Vacancy now means no assignment that still stands, including one
that has not started. An assignment that ended still frees the position.
Hiring was broken three times over, each fault hidden behind the previous
one:
1. hire_employee cast to ::weekday[], a type that no longer exists — it
was replaced by text plus a CHECK constraint and the function was never
updated. apply_due_pending_changes had the same problem with
::relationship_type, which would have broken the nightly run.
PL/pgSQL resolves types in embedded statements at execution time, so
both functions were created without complaint and failed only in use.
2. Fourteen functions called auth.uid(). The application connects as a
role with no rights on the auth schema, so every write — hire,
transfer, promote, exit, notes, positions — failed with "permission
denied for schema auth". They now use app_current_user_id(), which is
where #23 was heading anyway. Its own fallback also caught only
"function missing" and now catches the privilege error too, so a call
without session context returns null instead of raising.
3. The audit line built a name as `payload->>'a' || ' ' || payload->>'b'`.
`||` binds tighter than `->>`, so Postgres reads
`payload ->> ('a' || ' ' || payload) ->> 'b'`. The ACL failure above
had aborted analysis before the parser ever reached it.
And the wizard collected an email, showed it in the summary, and dropped it:
the server action's signature had no such field. employees.email is NOT
NULL, so every hire that got past the three faults above would have failed
there. It is now passed through and required in step one, rather than
refused by the database at the end of step four.
Verified against the live database, each rolled back: a hire now creates the
employee, the assignment, the history entry and an audit line reading "Probe
Einstellung"; open positions drop from 13 to 10, and the three that
disappear are exactly the ones with a starter.
Migrations rewrite the affected functions in place rather than restating
them — retyping 165 lines of working PL/pgSQL to change two words is the
larger risk. Each one asserts the result afterwards.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
168 lines
5.7 KiB
TypeScript
168 lines
5.7 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { currentUserId } from "@/lib/auth/session";
|
|
import { withUser } from "@/lib/db";
|
|
import { callFunction, runMutation, type ActionResult, type MutationFn } from "@/lib/db/rpc";
|
|
import type { CollectiveAgreement, NoteCategory, RelationshipType, Weekday, WorkerType } from "@/lib/supabase/types";
|
|
|
|
async function callRpc(fn: MutationFn, payload: Record<string, unknown>, revalidate: string[]): Promise<ActionResult> {
|
|
const result = await runMutation(await currentUserId(), fn, payload);
|
|
if (!result.success) return result;
|
|
for (const path of revalidate) revalidatePath(path);
|
|
return result;
|
|
}
|
|
|
|
export async function hireEmployee(payload: {
|
|
first_name: string;
|
|
last_name: string;
|
|
title_prefix?: string[];
|
|
title_suffix?: string[];
|
|
gender: "m" | "w";
|
|
birth_date: string;
|
|
sv_nummer?: string;
|
|
/**
|
|
* Pflicht, weil employees.email NOT NULL ist.
|
|
*
|
|
* Der Assistent hat die Adresse immer erhoben und in der Zusammenfassung
|
|
* angezeigt — sie fehlte nur in dieser Signatur und wurde deshalb
|
|
* stillschweigend verworfen. Jede Einstellung scheiterte danach an der
|
|
* Spaltenbedingung.
|
|
*/
|
|
email: string;
|
|
phone?: string;
|
|
position_id?: string;
|
|
team_id?: string;
|
|
job_title?: string;
|
|
location_id: string;
|
|
entry_date: string;
|
|
contract_type?: "unbefristet" | "befristet";
|
|
contract_end_date?: string;
|
|
employment_type?: "Vollzeit" | "Teilzeit";
|
|
weekly_hours?: number;
|
|
paygrade?: "A" | "B" | "C" | "D" | "E" | "F";
|
|
source: "Intern" | "Extern";
|
|
worker_type?: WorkerType;
|
|
collective_agreement?: CollectiveAgreement;
|
|
work_days?: Weekday[];
|
|
is_betriebsrat?: boolean;
|
|
has_dienstwagen?: boolean;
|
|
is_laterale_fuehrung?: boolean;
|
|
is_c_level?: boolean;
|
|
}): Promise<ActionResult & { employeeId?: string }> {
|
|
// Einzige Mutation, deren Rückgabewert gebraucht wird: die neue
|
|
// Personen-Kennung, damit die Oberfläche direkt auf die Akte springen kann.
|
|
try {
|
|
const employeeId = await withUser(await currentUserId(), (tx) =>
|
|
callFunction(tx, "hire_employee", payload as Record<string, unknown>)
|
|
);
|
|
revalidatePath("/employees");
|
|
revalidatePath("/");
|
|
revalidatePath("/positions");
|
|
return { success: true, employeeId: employeeId as string };
|
|
} catch (err) {
|
|
return { success: false, error: err instanceof Error ? err.message : "Unbekannter Fehler." };
|
|
}
|
|
}
|
|
|
|
export async function terminateEmployee(payload: {
|
|
employee_id: string;
|
|
exit_date: string;
|
|
exit_reason: string;
|
|
note?: string;
|
|
}): Promise<ActionResult> {
|
|
return callRpc("terminate_employee", payload, [`/employees/${payload.employee_id}`, "/employees", "/"]);
|
|
}
|
|
|
|
export async function transferEmployee(payload: {
|
|
employee_id: string;
|
|
effective_date: string;
|
|
/** Die Zielplanstelle; Bereich, Abteilung und Team ergeben sich aus ihrer Einheit. */
|
|
target_position_id: string;
|
|
}): Promise<ActionResult> {
|
|
return callRpc("transfer_employee", payload, [`/employees/${payload.employee_id}`, "/employees"]);
|
|
}
|
|
|
|
export async function promoteEmployee(payload: {
|
|
employee_id: string;
|
|
effective_date: string;
|
|
new_title: string;
|
|
new_paygrade?: "A" | "B" | "C" | "D" | "E" | "F";
|
|
}): Promise<ActionResult> {
|
|
return callRpc("promote_employee", payload, [`/employees/${payload.employee_id}`, "/employees"]);
|
|
}
|
|
|
|
export async function startKarenz(payload: {
|
|
employee_id: string;
|
|
karenz_start_date: string;
|
|
planned_return_date: string;
|
|
absence_type: string;
|
|
note?: string;
|
|
}): Promise<ActionResult> {
|
|
return callRpc("start_karenz", payload, [`/employees/${payload.employee_id}`, "/employees", "/"]);
|
|
}
|
|
|
|
export async function adjustKarenzReturn(payload: {
|
|
employee_id: string;
|
|
new_return_date: string;
|
|
note?: string;
|
|
}): Promise<ActionResult> {
|
|
return callRpc("adjust_karenz_return", payload, [`/employees/${payload.employee_id}`]);
|
|
}
|
|
|
|
export async function recordKarenzReturn(payload: {
|
|
employee_id: string;
|
|
return_date: string;
|
|
employment_mode: "unverändert" | "Vollzeit" | "Teilzeit";
|
|
weekly_hours?: number;
|
|
}): Promise<ActionResult> {
|
|
return callRpc("record_karenz_return", payload, [`/employees/${payload.employee_id}`, "/employees", "/"]);
|
|
}
|
|
|
|
export async function changeEmployeeData(payload: {
|
|
employee_id: string;
|
|
effective_date: string;
|
|
person: Record<string, unknown>;
|
|
contract: Record<string, unknown>;
|
|
role: Record<string, unknown>;
|
|
}): Promise<ActionResult> {
|
|
return callRpc("change_employee_data", payload, [`/employees/${payload.employee_id}`, "/employees"]);
|
|
}
|
|
|
|
export async function rehireEmployee(payload: { employee_id: string; rehire_date: string }): Promise<ActionResult> {
|
|
return callRpc("rehire_employee", payload, [`/employees/${payload.employee_id}`, "/employees", "/"]);
|
|
}
|
|
|
|
export async function addEmployeeDependent(payload: {
|
|
employee_id: string;
|
|
first_name: string;
|
|
last_name: string;
|
|
relationship: RelationshipType;
|
|
sv_nummer?: string;
|
|
birth_date: string;
|
|
effective_date: string;
|
|
}): Promise<ActionResult> {
|
|
return callRpc("add_employee_dependent", payload, [`/employees/${payload.employee_id}`]);
|
|
}
|
|
|
|
export async function deleteEmployeeDependent(payload: {
|
|
dependent_id: string;
|
|
employee_id: string;
|
|
effective_date: string;
|
|
}): Promise<ActionResult> {
|
|
return callRpc("delete_employee_dependent", payload, [`/employees/${payload.employee_id}`]);
|
|
}
|
|
|
|
export async function addEmployeeNote(payload: {
|
|
employee_id: string;
|
|
category: NoteCategory;
|
|
note_text: string;
|
|
due_date?: string;
|
|
}): Promise<ActionResult> {
|
|
return callRpc("add_employee_note", payload, [`/employees/${payload.employee_id}`, "/"]);
|
|
}
|
|
|
|
export async function completeEmployeeNote(payload: { note_id: string; employee_id: string }): Promise<ActionResult> {
|
|
return callRpc("complete_employee_note", payload, [`/employees/${payload.employee_id}`, "/"]);
|
|
}
|