forked from fahrPool/fahrPool
131 lines
5.7 KiB
TypeScript
131 lines
5.7 KiB
TypeScript
"use server";
|
|
|
|
import { redirect } from "next/navigation";
|
|
import { revalidatePath } from "next/cache";
|
|
import { createAdminClient } from "@/lib/supabase/admin";
|
|
import { loadProfile } from "@/lib/auth/profile";
|
|
import { validatePasswort } from "@/lib/auth/passwort";
|
|
import { protokolliere } from "@/lib/audit/protokollieren";
|
|
import { legeNutzerInFirmaAn } from "@/lib/nutzerverwaltung/nutzer-anlegen";
|
|
import { pruefeSuperadmin } from "@/lib/plattform/superadmin";
|
|
import { pruefeLoeschBestaetigung, validateFirmaAnlegen, validateFirmenAdmin } from "@/lib/plattform/validate";
|
|
import { ladeFirmaFuerPlattform } from "@/lib/plattform/queries";
|
|
import { loescheFirmenDateien } from "@/lib/plattform/firma-loeschen";
|
|
|
|
function zurueck(pfad: string, fehler?: string): never {
|
|
redirect(fehler ? `${pfad}?error=${encodeURIComponent(fehler)}` : pfad);
|
|
}
|
|
|
|
async function sicherSuperadmin(): Promise<string> {
|
|
const profil = await loadProfile();
|
|
if (!pruefeSuperadmin(profil) || profil.status !== "ok") {
|
|
redirect("/login");
|
|
}
|
|
return profil.profile.id;
|
|
}
|
|
|
|
export async function legeFirmaAn(formData: FormData): Promise<void> {
|
|
const superadminId = await sicherSuperadmin();
|
|
const v = validateFirmaAnlegen({
|
|
firmenname: formData.get("firmenname"),
|
|
name: formData.get("name"),
|
|
email: formData.get("email"),
|
|
passwort: formData.get("passwort"),
|
|
});
|
|
if (!v.valid) zurueck("/plattform", v.error);
|
|
|
|
const admin = createAdminClient(superadminId);
|
|
const { data: firma, error } = await admin.from("firmen").insert({ name: v.firmenname }).select("id").single();
|
|
if (error || !firma) zurueck("/plattform", "Firma konnte nicht angelegt werden.");
|
|
|
|
const nutzer = await legeNutzerInFirmaAn(admin, { firmaId: firma.id, rolle: "admin", akteurId: superadminId, ...v.admin });
|
|
if (!nutzer.ok) {
|
|
// Keine Firma ohne Admin zuruecklassen.
|
|
const { error: loeschFehler } = await admin.from("firmen").delete().eq("id", firma.id);
|
|
if (loeschFehler) {
|
|
console.error(`Rollback: Firma ${firma.id} konnte nicht geloescht werden: ${loeschFehler.message}`);
|
|
}
|
|
zurueck("/plattform", nutzer.fehler);
|
|
}
|
|
revalidatePath("/plattform");
|
|
zurueck(`/plattform/${firma.id}`);
|
|
}
|
|
|
|
export async function setzeFirmaAktiv(firmaId: string, aktiv: boolean): Promise<void> {
|
|
const superadminId = await sicherSuperadmin();
|
|
const { error } = await createAdminClient(superadminId).from("firmen").update({ aktiv }).eq("id", firmaId);
|
|
revalidatePath("/plattform");
|
|
zurueck(`/plattform/${firmaId}`, error ? "Status konnte nicht geändert werden." : undefined);
|
|
}
|
|
|
|
// Endgueltiges Loeschen einer deaktivierten Firma. Erst die Storage-Dateien
|
|
// (solange die Auftrags-IDs noch existieren), dann atomar alle DB-Daten. Scheitert
|
|
// der DB-Teil, bleibt die Firma deaktiviert bestehen und das Loeschen laesst
|
|
// sich wiederholen.
|
|
export async function loescheFirma(firmaId: string, formData: FormData): Promise<void> {
|
|
const superadminId = await sicherSuperadmin();
|
|
const admin = createAdminClient(superadminId);
|
|
const firma = await ladeFirmaFuerPlattform(admin, firmaId);
|
|
if (!firma) zurueck("/plattform", "Firma nicht gefunden.");
|
|
if (firma.aktiv) zurueck(`/plattform/${firmaId}`, "Zum Löschen die Firma zuerst deaktivieren.");
|
|
if (!pruefeLoeschBestaetigung(formData.get("bestaetigung"), firma.name)) {
|
|
zurueck(`/plattform/${firmaId}`, "Der eingegebene Name stimmt nicht.");
|
|
}
|
|
|
|
const dateien = await loescheFirmenDateien(admin, firmaId);
|
|
if (!dateien.ok) {
|
|
console.error(`Firma ${firmaId}: Dateien loeschen fehlgeschlagen: ${dateien.fehler}`);
|
|
zurueck(`/plattform/${firmaId}`, "Dateien konnten nicht gelöscht werden. Bitte erneut versuchen.");
|
|
}
|
|
|
|
const { error } = await admin.rpc("firma_endgueltig_loeschen", { p_firma_id: firmaId, p_akteur_id: superadminId });
|
|
if (error) {
|
|
console.error(`Firma ${firmaId}: Loeschen fehlgeschlagen: ${error.message}`);
|
|
zurueck(`/plattform/${firmaId}`, "Firma konnte nicht gelöscht werden.");
|
|
}
|
|
|
|
revalidatePath("/plattform");
|
|
redirect(`/plattform?hinweis=${encodeURIComponent(`Firma ${firma.name} wurde gelöscht.`)}`);
|
|
}
|
|
|
|
export async function legeFirmenAdminAn(firmaId: string, formData: FormData): Promise<void> {
|
|
const superadminId = await sicherSuperadmin();
|
|
const v = validateFirmenAdmin({ name: formData.get("name"), email: formData.get("email"), passwort: formData.get("passwort") });
|
|
if (!v.valid) zurueck(`/plattform/${firmaId}`, v.error);
|
|
const nutzer = await legeNutzerInFirmaAn(createAdminClient(superadminId), {
|
|
firmaId,
|
|
akteurId: superadminId,
|
|
rolle: "admin",
|
|
name: v.name,
|
|
email: v.email,
|
|
passwort: v.passwort,
|
|
});
|
|
zurueck(`/plattform/${firmaId}`, nutzer.ok ? undefined : nutzer.fehler);
|
|
}
|
|
|
|
export async function setzeAdminPasswort(firmaId: string, nutzerId: string, formData: FormData): Promise<void> {
|
|
const superadminId = await sicherSuperadmin();
|
|
const v = validatePasswort(formData.get("passwort"));
|
|
if (!v.valid) zurueck(`/plattform/${firmaId}`, v.error);
|
|
|
|
const admin = createAdminClient(superadminId);
|
|
// Nur Admins der angegebenen Firma, nie andere Rollen oder Superadmins.
|
|
const { data: ziel } = await admin
|
|
.from("profiles")
|
|
.select("id")
|
|
.eq("id", nutzerId)
|
|
.eq("firma_id", firmaId)
|
|
.eq("role", "admin")
|
|
.maybeSingle();
|
|
if (!ziel) zurueck(`/plattform/${firmaId}`, "Nutzer nicht gefunden.");
|
|
|
|
const { error } = await admin.auth.admin.updateUserById(nutzerId, {
|
|
password: v.passwort,
|
|
app_metadata: { must_change_password: true },
|
|
});
|
|
if (!error) {
|
|
await protokolliere(admin, { firmaId, akteurId: superadminId, aktion: "passwort_gesetzt", nutzerId });
|
|
}
|
|
zurueck(`/plattform/${firmaId}`, error ? `Passwort setzen fehlgeschlagen: ${error.message}` : undefined);
|
|
}
|