"use server"; import { revalidatePath } from "next/cache"; import { currentUserId } from "@/lib/auth/session"; import { runMutation, type ActionResult } from "@/lib/db/rpc"; import type { OrgUnitType } from "@/lib/org-art"; const ORG_PFADE = ["/orgchart", "/positions", "/"]; /** * Eine Organisationseinheit unter einer bestehenden anlegen. * * Ein Verschieben bestehender Einheiten gibt es bewusst nicht: * org_units.parent_id trägt kein Datum, ein Verschieben änderte damit jede * Auswertung auf einen vergangenen Stichtag. */ export async function createOrgUnit(payload: { parent_id: string; org_number: string; name: string; unit_type: Exclude; valid_from: string; }): Promise { // Die Einheit entsteht ohne Leitung. Die Leitungsplanstelle wird über // „Planstelle anlegen" erfasst, mit einer Tätigkeit aus dem Katalog statt // eines freien Textes. const result = await runMutation(await currentUserId(), "create_org_unit", payload); if (!result.success) return result; // Die Einheit taucht in drei Ansichten auf, und in allen dreien wäre sie // sonst erst nach dem nächsten harten Neuladen zu sehen. for (const path of ORG_PFADE) revalidatePath(path); return result; } /** Nur die Bezeichnung einer Organisationseinheit berichtigen. */ export async function renameOrgUnit(orgUnitId: string, name: string): Promise { const result = await runMutation(await currentUserId(), "rename_org_unit", { org_unit_id: orgUnitId, name }); if (!result.success) return result; for (const path of ORG_PFADE) revalidatePath(path); return result; } /** Die Art einer Organisationseinheit ändern; gilt sofort, ohne Stichtag. */ export async function changeOrgUnitType(orgUnitId: string, unitType: OrgUnitType): Promise { const result = await runMutation(await currentUserId(), "change_org_unit_type", { org_unit_id: orgUnitId, unit_type: unitType }); if (!result.success) return result; for (const path of ORG_PFADE) revalidatePath(path); return result; } /** * Eine Organisationseinheit entfernen. * * Geht nur, solange nichts darunter hängt und nie etwas hing. Alles andere * weist die Datenbankfunktion mit dem Grund ab; eine Einheit, die bestanden * hat, endet stattdessen mit einem Enddatum (Import der Austauschdatei). */ export async function deleteOrgUnit(orgUnitId: string): Promise { const result = await runMutation(await currentUserId(), "delete_org_unit", { org_unit_id: orgUnitId }); if (!result.success) return result; for (const path of ORG_PFADE) revalidatePath(path); return result; }