1
0
Fork 0
fahrPool/supabase/migrations/20261106090000_firma_loesch...

97 lines
4.3 KiB
PL/PgSQL

-- Firma endgueltig loeschen (Superadmin). Spec: docs/superpowers/specs/2026-11-06-firma-loeschen-design.md
--
-- Storage-Dateien loescht vorher die Server Action ueber die Storage-API
-- (storage.objects ist per Trigger protect_objects_delete gegen direktes
-- SQL-Loeschen geschuetzt). Diese Funktion entfernt alle Datenbankdaten der
-- Firma atomar -- inklusive Logins (auth.users) und Audit-Log -- und
-- hinterlaesst genau einen Audit-Eintrag "firma/geloescht".
-- Audit-Log bleibt unveraenderlich. Einzige Ausnahme: DELETE von Zeilen genau
-- der Firma, die firma_endgueltig_loeschen() in dieser Transaktion loescht
-- (transaktionslokale Einstellung app.firma_loeschen). authenticated und
-- service_role haben weiterhin kein DELETE-Grant auf audit_log; die Ausnahme
-- ist also nur aus security-definer-Code des Eigentuemers erreichbar.
create or replace function public.audit_log_unveraenderlich()
returns trigger
language plpgsql
set search_path = public
as $$
begin
if tg_op = 'DELETE'
and old.firma_id is not null
and current_setting('app.firma_loeschen', true) = old.firma_id::text then
return old;
end if;
raise exception 'Audit-Log-Eintraege sind unveraenderlich' using errcode = '42501';
end;
$$;
create or replace function public.firma_endgueltig_loeschen(p_firma_id uuid, p_akteur_id uuid)
returns jsonb
language plpgsql
security definer
set search_path = public
as $$
declare
v_name text;
v_aktiv boolean;
v_zaehler jsonb;
begin
select name, aktiv into v_name, v_aktiv from public.firmen where id = p_firma_id for update;
if not found then
raise exception 'Firma nicht gefunden';
end if;
if v_aktiv then
raise exception 'Firma ist noch aktiv';
end if;
v_zaehler := jsonb_build_object(
'nutzer', (select count(*) from public.profiles where firma_id = p_firma_id),
'auftraege', (select count(*) from public.auftraege where firma_id = p_firma_id),
'fotos', (select count(*) from public.auftrag_fotos where firma_id = p_firma_id),
'orte', (select count(*) from public.orte where firma_id = p_firma_id),
'fahrzeuge', (select count(*) from public.fahrzeuge where firma_id = p_firma_id),
'arbeitszeiten', (select count(*) from public.arbeitszeiten where firma_id = p_firma_id)
);
perform set_config('app.firma_loeschen', p_firma_id::text, true);
-- Reihenfolge folgt den Fremdschluesseln (alle ohne Cascade ausser
-- auftrag_fotos->auftraege, nutzer_details->profiles, profiles->auth.users,
-- firmen_smtp->firmen).
delete from public.auftrag_fotos where firma_id = p_firma_id;
delete from public.notification_outbox where firma_id = p_firma_id;
update public.auftraege set vorgaenger_auftrag_id = null
where firma_id = p_firma_id and vorgaenger_auftrag_id is not null;
delete from public.auftraege where firma_id = p_firma_id;
delete from public.arbeitszeiten where firma_id = p_firma_id;
delete from public.orte where firma_id = p_firma_id;
delete from public.fahrzeuge where firma_id = p_firma_id;
delete from public.auftragsnummer_zaehler where firma_id = p_firma_id;
delete from public.nutzer_details where firma_id = p_firma_id;
-- Firmenfremde Verweise auf Nutzer dieser Firma leeren (per RLS nicht
-- ausgeschlossen, z.B. orte.created_by); sonst blockiert der FK das Loeschen.
update public.orte set created_by = null
where created_by in (select id from public.profiles where firma_id = p_firma_id);
update public.auftrag_fotos set uploaded_by = null
where uploaded_by in (select id from public.profiles where firma_id = p_firma_id);
update public.arbeitszeiten set korrigiert_von = null
where korrigiert_von in (select id from public.profiles where firma_id = p_firma_id);
delete from auth.users where id in (select id from public.profiles where firma_id = p_firma_id);
delete from public.firmen where id = p_firma_id;
-- Inklusive der Eintraege, die die Audit-Trigger beim Loeschen eben erzeugt haben.
delete from public.audit_log where firma_id = p_firma_id;
perform public.audit_schreiben(
p_firma_id, p_akteur_id, 'firma', 'geloescht', p_firma_id::text, null,
jsonb_build_object('name', v_name) || v_zaehler
);
return v_zaehler;
end;
$$;
revoke execute on function public.firma_endgueltig_loeschen(uuid, uuid) from public, anon, authenticated;
grant execute on function public.firma_endgueltig_loeschen(uuid, uuid) to service_role;