-- Transaktionale Outbox fuer ausgehende Benachrichtigungen. Vorerst nur das -- Ereignis 'auftrag_erstellt' (WhatsApp an eine feste Sammel-Nummer), aber -- event_type/payload sind generisch fuer spaetere Ereignisse. Geschrieben wird -- ausschliesslich vom SECURITY DEFINER-Enqueue-Trigger und vom -- Service-Role-Client der Dispatch-Route; RLS gibt nur admin Leserechte. create table public.notification_outbox ( id uuid primary key default gen_random_uuid(), event_type text not null, auftrag_id uuid references public.auftraege(id) on delete set null, payload jsonb not null, status text not null default 'pending' check (status in ('pending', 'sending', 'sent', 'failed', 'dead')), versuche int not null default 0, max_versuche int not null default 5, naechster_versuch_ab timestamptz not null default now(), letzter_fehler text, wa_message_id text, created_at timestamptz not null default now(), updated_at timestamptz not null default now() ); create index notification_outbox_faellig_idx on public.notification_outbox (status, naechster_versuch_ab); alter table public.notification_outbox enable row level security; -- Nur Einsicht fuer admin (Studio / spaeteres Admin-View). Kein -- INSERT/UPDATE/DELETE fuer authenticated: der Enqueue-Trigger laeuft als -- SECURITY DEFINER, die Dispatch-Route ueber den Service-Role-Key -- beide -- umgehen RLS. create policy "Admin liest notification_outbox" on public.notification_outbox for select using (public.user_role() = 'admin'); -- Enqueue: bei jedem INSERT in auftraege genau eine pending-Zeile. Laeuft als -- AFTER INSERT, auftragsnummer ist zu diesem Zeitpunkt vom BEFORE-Trigger -- auftragsnummer_generieren bereits gefuellt. create function public.auftrag_notification_enqueue() returns trigger language plpgsql security definer set search_path = public as $$ begin insert into public.notification_outbox (event_type, auftrag_id, payload) select 'auftrag_erstellt', new.id, jsonb_build_object( 'auftragsnummer', new.auftragsnummer, 'abholort_adresse', abhol.adresse, 'zielort_adresse', ziel.adresse, 'kennzeichen', fz.kennzeichen) from public.orte abhol join public.orte ziel on ziel.id = new.zielort_id join public.fahrzeuge fz on fz.id = new.fahrzeug_id where abhol.id = new.abholort_id; return new; end; $$; create trigger auftrag_notification_enqueue_trg after insert on public.auftraege for each row execute function public.auftrag_notification_enqueue(); -- Atomare Batch-Uebernahme fuer den Dispatcher: selektiert faellige Zeilen -- (pending/failed und Zeitpunkt erreicht, oder in 'sending' haengengeblieben -- seit >15 min) und setzt sie in einem Rutsch auf 'sending'. FOR UPDATE SKIP -- LOCKED verhindert Doppel-Uebernahme bei ueberlappenden Laeufen. create function public.claim_notification_batch(p_limit int) returns setof public.notification_outbox language sql security definer set search_path = public as $$ update public.notification_outbox o set status = 'sending', updated_at = now() where o.id in ( select id from public.notification_outbox where (status in ('pending', 'failed') and naechster_versuch_ab <= now()) or (status = 'sending' and updated_at < now() - interval '15 minutes') order by created_at limit p_limit for update skip locked ) returning o.*; $$; revoke all on function public.claim_notification_batch(int) from public; revoke all on function public.claim_notification_batch(int) from anon; revoke all on function public.claim_notification_batch(int) from authenticated; -- Nur die Dispatch-Route (Service-Role-Client) darf den Batch uebernehmen. grant execute on function public.claim_notification_batch(int) to service_role;