1
0
Fork 0

feat: add auftraege table with status enum and RLS

This commit is contained in:
Jonny 2026-07-05 11:08:47 +02:00
parent 0850f0ccfc
commit 8ed34cfa96
1 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,49 @@
create type public.app_auftrag_status as enum (
'offen', 'zugewiesen', 'unterwegs', 'abgeschlossen', 'storniert'
);
create table public.auftraege (
id uuid primary key default gen_random_uuid(),
abholort_id uuid not null references public.orte (id),
zielort_id uuid not null references public.orte (id),
fahrzeug_id uuid not null references public.fahrzeuge (id),
status public.app_auftrag_status not null default 'offen',
zugewiesen_an uuid references public.profiles (id),
erstellt_von uuid not null references public.profiles (id),
start_zeit timestamptz,
start_km integer,
end_km integer,
uebergabe_bestaetigt_at timestamptz,
vorgaenger_auftrag_id uuid references public.auftraege (id),
created_at timestamptz not null default now()
);
alter table public.auftraege enable row level security;
grant select, insert, update on public.auftraege to authenticated;
create policy "Sichtbarkeit von Auftraegen nach Rolle"
on public.auftraege for select
using (
public.user_role() in ('admin', 'disponent', 'readonly')
or zugewiesen_an = auth.uid()
or status = 'offen'
);
create policy "Disponent und Admin legen Auftraege an"
on public.auftraege for insert
with check (public.user_role() in ('admin', 'disponent'));
create policy "Disponent und Admin bearbeiten alle Auftraege"
on public.auftraege for update
using (public.user_role() in ('admin', 'disponent'))
with check (public.user_role() in ('admin', 'disponent'));
-- Fahrer duerfen eine offene Zeile uebernehmen (USING erlaubt Zugriff auf
-- offene Zeilen, WITH CHECK erzwingt danach, dass zugewiesen_an entweder
-- der eigene Nutzer ist oder wieder auf null gesetzt wird -- letzteres
-- deckt den Übergang "zugewiesen -> offen" (Auftrag zuruecklegen) ab).
create policy "Fahrer uebernimmt offene oder bearbeitet eigene Auftraege"
on public.auftraege for update
using (zugewiesen_an = auth.uid() or status = 'offen')
with check (zugewiesen_an = auth.uid() or zugewiesen_an is null);