forked from fahrPool/fahrPool
feat: Superadmin kann deaktivierte Firma endgueltig loeschen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
10175513f4
commit
a1f292d449
|
|
@ -5,7 +5,9 @@ import { loadProfile } from "@/lib/auth/profile";
|
|||
import { pruefeSuperadmin } from "@/lib/plattform/superadmin";
|
||||
import { ladeFirmaFuerPlattform, listFirmenAdmins } from "@/lib/plattform/queries";
|
||||
import { formatiereRollen } from "@/lib/plattform/rollen";
|
||||
import { legeFirmenAdminAn, setzeAdminPasswort, setzeFirmaAktiv } from "../actions";
|
||||
import { ladeLoeschUmfang } from "@/lib/plattform/firma-loeschen";
|
||||
import { legeFirmenAdminAn, loescheFirma, setzeAdminPasswort, setzeFirmaAktiv } from "../actions";
|
||||
import { FirmaLoeschenFormular } from "@/components/plattform/FirmaLoeschenFormular";
|
||||
import { Alert } from "@/components/ui/Alert";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
|
|
@ -26,7 +28,10 @@ export default async function PlattformFirmaPage({
|
|||
const admin = createAdminClient();
|
||||
const firma = await ladeFirmaFuerPlattform(admin, id);
|
||||
if (!firma) notFound();
|
||||
const admins = await listFirmenAdmins(admin, id);
|
||||
const [admins, umfang] = await Promise.all([
|
||||
listFirmenAdmins(admin, id),
|
||||
firma.aktiv ? Promise.resolve(null) : ladeLoeschUmfang(admin, id),
|
||||
]);
|
||||
|
||||
return (
|
||||
<div className="mx-auto flex max-w-2xl flex-col gap-6 p-4 md:p-8">
|
||||
|
|
@ -71,8 +76,7 @@ export default async function PlattformFirmaPage({
|
|||
</Card>
|
||||
|
||||
<Card>
|
||||
<h2 className="mb-4 font-semibold text-asphalt-grey">Weiteren Admin anlegen</h2>
|
||||
<form action={legeFirmenAdminAn.bind(null, firma.id)}>
|
||||
<h2 className="mb-4 font-semibold text-asphalt-grey">Weiteren Admin anlegen</h2> <form action={legeFirmenAdminAn.bind(null, firma.id)}>
|
||||
<FormField label="Name" htmlFor="admin-name">
|
||||
<input id="admin-name" name="name" required className={FIELD_CLASSES} />
|
||||
</FormField>
|
||||
|
|
@ -85,6 +89,28 @@ export default async function PlattformFirmaPage({
|
|||
<Button type="submit">Admin anlegen</Button>
|
||||
</form>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<h2 className="mb-4 font-semibold text-status-danger">Firma endgültig löschen</h2>
|
||||
{firma.aktiv || !umfang ? (
|
||||
<p className="text-asphalt-grey">Zum Löschen die Firma zuerst deaktivieren.</p>
|
||||
) : (
|
||||
<>
|
||||
<p className="mb-2 text-asphalt-grey">
|
||||
Das löscht alle Daten, Dateien und Logins dieser Firma unwiderruflich:
|
||||
</p>
|
||||
<ul className="mb-4 list-disc pl-5 text-asphalt-grey">
|
||||
<li>{umfang.nutzer} Nutzer</li>
|
||||
<li>{umfang.auftraege} Aufträge</li>
|
||||
<li>{umfang.fotos} Fotos</li>
|
||||
<li>{umfang.orte} Orte</li>
|
||||
<li>{umfang.fahrzeuge} Fahrzeuge</li>
|
||||
<li>{umfang.arbeitszeiten} Arbeitszeiten</li>
|
||||
</ul>
|
||||
<FirmaLoeschenFormular firmenname={firma.name} aktion={loescheFirma.bind(null, firma.id)} />
|
||||
</>
|
||||
)}
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ 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 { validateFirmaAnlegen, validateFirmenAdmin } from "@/lib/plattform/validate";
|
||||
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);
|
||||
|
|
@ -56,6 +58,36 @@ export async function setzeFirmaAktiv(firmaId: string, aktiv: boolean): Promise<
|
|||
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") });
|
||||
|
|
|
|||
|
|
@ -12,11 +12,15 @@ import { Button } from "@/components/ui/Button";
|
|||
import { Card } from "@/components/ui/Card";
|
||||
import { FIELD_CLASSES, FormField } from "@/components/ui/FormField";
|
||||
|
||||
export default async function PlattformPage({ searchParams }: { searchParams: Promise<{ error?: string }> }) {
|
||||
export default async function PlattformPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<{ error?: string; hinweis?: string }>;
|
||||
}) {
|
||||
if (!pruefeSuperadmin(await loadProfile())) {
|
||||
redirect("/dashboard");
|
||||
}
|
||||
const { error } = await searchParams;
|
||||
const { error, hinweis } = await searchParams;
|
||||
const firmen = await listFirmen(createAdminClient());
|
||||
|
||||
return (
|
||||
|
|
@ -24,6 +28,11 @@ export default async function PlattformPage({ searchParams }: { searchParams: Pr
|
|||
<InstallHinweis />
|
||||
<h1 className="text-xl font-semibold text-asphalt-grey">Plattform-Verwaltung</h1>
|
||||
{error && <Alert>{error}</Alert>}
|
||||
{hinweis && (
|
||||
<p role="status" className="rounded-md border border-status-success bg-green-50 px-4 py-3 text-asphalt-grey">
|
||||
{hinweis}
|
||||
</p>
|
||||
)}
|
||||
|
||||
<Card>
|
||||
<h2 className="mb-4 font-semibold text-asphalt-grey">Firmen</h2>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { FIELD_CLASSES, FormField } from "@/components/ui/FormField";
|
||||
|
||||
// Rueckfrage vor dem endgueltigen Loeschen: Button erst aktiv, wenn der
|
||||
// Firmenname exakt eingetippt ist. Die Server Action prueft das erneut.
|
||||
export function FirmaLoeschenFormular({
|
||||
firmenname,
|
||||
aktion,
|
||||
}: {
|
||||
firmenname: string;
|
||||
aktion: (formData: FormData) => Promise<void>;
|
||||
}) {
|
||||
const [eingabe, setEingabe] = useState("");
|
||||
const passt = eingabe.trim() !== "" && eingabe.trim() === firmenname.trim();
|
||||
|
||||
return (
|
||||
<form action={aktion}>
|
||||
<FormField label={`Zum Bestätigen den Firmennamen eingeben: ${firmenname}`} htmlFor="loeschen-bestaetigung">
|
||||
<input
|
||||
id="loeschen-bestaetigung"
|
||||
name="bestaetigung"
|
||||
autoComplete="off"
|
||||
value={eingabe}
|
||||
onChange={(e) => setEingabe(e.target.value)}
|
||||
className={FIELD_CLASSES}
|
||||
/>
|
||||
</FormField>
|
||||
<Button type="submit" variant="destructive" disabled={!passt}>
|
||||
Endgültig löschen
|
||||
</Button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue