1
0
Fork 0
fahrPool/supabase/migrations/20260818120000_auftrag_foto...

57 lines
2.1 KiB
PL/PgSQL

-- Remove photo-count requirement from check_auftrag_status_transition trigger.
-- Photos are now optional when starting a trip (status zugewiesen -> unterwegs).
-- All other checks (column lockdown, allowed transitions, cancel-only-by-admin/disponent,
-- start_km requirement, end_km/uebergabe_bestaetigt_at requirements) are preserved.
create or replace function public.check_auftrag_status_transition()
returns trigger
language plpgsql
security definer
set search_path = public
as $$
begin
if public.user_role() not in ('admin', 'disponent') then
if new.abholort_id is distinct from old.abholort_id
or new.zielort_id is distinct from old.zielort_id
or new.fahrzeug_id is distinct from old.fahrzeug_id
or new.erstellt_von is distinct from old.erstellt_von
or new.vorgaenger_auftrag_id is distinct from old.vorgaenger_auftrag_id
then
raise exception 'Nur Disponent/Admin duerfen Abholort, Zielort, Fahrzeug, Ersteller oder Vorgaenger-Auftrag aendern';
end if;
end if;
if old.status = new.status then
return new;
end if;
if not (
(old.status = 'offen' and new.status in ('zugewiesen', 'storniert'))
or (old.status = 'zugewiesen' and new.status in ('unterwegs', 'offen', 'storniert'))
or (old.status = 'unterwegs' and new.status in ('abgeschlossen', 'storniert'))
) then
raise exception 'Ungueltiger Statuswechsel: % -> %', old.status, new.status;
end if;
if new.status = 'storniert' and public.user_role() not in ('admin', 'disponent') then
raise exception 'Nur Disponent/Admin duerfen einen Auftrag stornieren';
end if;
if new.status = 'unterwegs' then
if new.start_km is null then
raise exception 'start_km muss gesetzt sein, bevor der Status auf unterwegs wechselt';
end if;
end if;
if new.status = 'abgeschlossen' then
if new.end_km is null then
raise exception 'end_km muss gesetzt sein, bevor der Status auf abgeschlossen wechselt';
end if;
if new.uebergabe_bestaetigt_at is null then
raise exception 'uebergabe_bestaetigt_at muss gesetzt sein, bevor der Status auf abgeschlossen wechselt';
end if;
end if;
return new;
end;
$$;