"use server"; import { revalidatePath } from "next/cache"; import { currentUserId } from "@/lib/auth/session"; import { runMutation, type ActionResult } from "@/lib/db/rpc"; const POSITION_PATHS = ["/positions", "/orgchart", "/"]; async function callRpc( fn: "create_position" | "update_position" | "delete_position", payload: Record, revalidate: string[] ): Promise { 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 createPosition(payload: { org_unit_id: string; job_title: string; is_chief: boolean; valid_from: string; }): Promise { return callRpc("create_position", payload, POSITION_PATHS); } /** * Ändert eine Planstelle. * * Weggelassene Felder bleiben, wie sie sind. `valid_to: null` beendet die * Befristung ausdrücklich — deshalb ist es hier `string | null` und nicht * optional: „nicht mitgeschickt" und „auf leer setzen" müssen unterscheidbar * bleiben. */ export async function updatePosition(payload: { position_id: string; org_unit_id?: string; job_title?: string; is_chief?: boolean; valid_from?: string; valid_to?: string | null; }): Promise { return callRpc("update_position", payload, POSITION_PATHS); } export async function deletePosition(positionId: string): Promise { return callRpc("delete_position", { position_id: positionId }, POSITION_PATHS); }