1
0
Fork 0

feat: Outbox-Zeile beim Auftragsabschluss, Claim nach Ereignis-Art

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jonny 2026-10-11 18:18:44 +02:00
parent 2aa0f313e2
commit f795d04176
3 changed files with 173 additions and 2 deletions

View File

@ -0,0 +1,69 @@
-- Uebergabeprotokoll per E-Mail an den Ersteller des Auftrags.
-- Spec: docs/superpowers/specs/2026-10-11-protokoll-email-design.md
-- Enqueue beim Wechsel auf 'abgeschlossen'. Die E-Mail wird zum
-- Abschlusszeitpunkt im Payload festgehalten. Ohne E-Mail keine Zeile.
-- security definer: liest auth.users und schreibt in die Outbox, beides ohne
-- Rechte des ausloesenden Nutzers (Fahrer).
create function public.auftrag_protokoll_enqueue()
returns trigger
language plpgsql
security definer
set search_path = public
as $$
declare
v_email text;
begin
select u.email into v_email from auth.users u where u.id = new.erstellt_von;
if coalesce(btrim(v_email), '') = '' then
return new;
end if;
insert into public.notification_outbox (event_type, auftrag_id, payload)
values (
'protokoll_abgeschlossen',
new.id,
jsonb_build_object('auftragsnummer', new.auftragsnummer, 'empfaenger', v_email)
);
return new;
end;
$$;
create trigger auftrag_protokoll_enqueue_trg
after update on public.auftraege
for each row
when (old.status is distinct from 'abgeschlossen' and new.status = 'abgeschlossen')
execute function public.auftrag_protokoll_enqueue();
-- Claim nur fuer konfigurierte Kanaele: ist ein Kanal nicht konfiguriert,
-- bleiben seine Zeilen pending statt geclaimt und auf dead gesetzt zu werden.
-- Ersetzt claim_notification_batch(int) aus 20260903100000_notification_outbox.sql
-- (einziger Aufrufer: web/lib/notifications/dispatch.ts).
drop function public.claim_notification_batch(int);
create function public.claim_notification_batch(p_limit int, p_event_types text[])
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 event_type = any(p_event_types)
and (
(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, text[]) from public;
revoke all on function public.claim_notification_batch(int, text[]) from anon;
revoke all on function public.claim_notification_batch(int, text[]) from authenticated;
grant execute on function public.claim_notification_batch(int, text[]) to service_role;

View File

@ -91,7 +91,7 @@ set local request.jwt.claims to '{"sub": "c2222222-2222-2222-2222-222222222222",
-- Beschreibung als erwartete Fehlermeldung interpretieren. Getestet wird -- Beschreibung als erwartete Fehlermeldung interpretieren. Getestet wird
-- unveraendert: authenticated laeuft in SQLSTATE 42501 (insufficient_privilege). -- unveraendert: authenticated laeuft in SQLSTATE 42501 (insufficient_privilege).
select throws_ok( select throws_ok(
$$ select public.claim_notification_batch(5) $$, $$ select public.claim_notification_batch(5, array['auftrag_erstellt']) $$,
'42501' '42501'
); );
@ -121,7 +121,7 @@ values
now(), now() - interval '20 minutes'); now(), now() - interval '20 minutes');
select results_eq( select results_eq(
$$ select id::text from public.claim_notification_batch(10) order by id $$, $$ select id::text from public.claim_notification_batch(10, array['auftrag_erstellt']) order by id $$,
$$ values ('c5000000-0000-0000-0000-000000000001'::text), $$ values ('c5000000-0000-0000-0000-000000000001'::text),
('c5000000-0000-0000-0000-000000000004') $$, ('c5000000-0000-0000-0000-000000000004') $$,
'claim_notification_batch liefert genau die faellige pending- und die abgestandene sending-Zeile' 'claim_notification_batch liefert genau die faellige pending- und die abgestandene sending-Zeile'

View File

@ -0,0 +1,102 @@
begin;
select plan(7);
-- profiles.name ist not null und faellt sonst auf die E-Mail zurueck, daher
-- fuer den Nutzer ohne E-Mail einen Namen ueber raw_user_meta_data setzen.
insert into auth.users (id, email, raw_user_meta_data) values
('e5a00000-0000-0000-0000-000000000001', 'dispo-mail@test.local', '{}'::jsonb),
('e5a00000-0000-0000-0000-000000000002', null, '{"name": "Dispo ohne Mail"}'::jsonb);
update public.profiles set role = 'disponent' where id = 'e5a00000-0000-0000-0000-000000000001';
update public.profiles set role = 'disponent' where id = 'e5a00000-0000-0000-0000-000000000002';
insert into public.orte (id, adresse, ansprechpartner) values
('e5b00000-0000-0000-0000-000000000001', 'Hauptstr. 1, Musterstadt', 'Frau Muster'),
('e5b00000-0000-0000-0000-000000000002', 'Nebenweg 2, Musterstadt', 'Herr Beispiel');
insert into public.fahrzeuge (id, kennzeichen) values
('e5c00000-0000-0000-0000-000000000001', 'M-ML 001');
-- A: Ersteller mit E-Mail, unterwegs
-- B: Ersteller ohne E-Mail, unterwegs
-- C: Ersteller mit E-Mail, zugewiesen (wird nur storniert)
insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, start_km) values
('e5d00000-0000-0000-0000-00000000000a', 'e5b00000-0000-0000-0000-000000000001', 'e5b00000-0000-0000-0000-000000000002', 'e5c00000-0000-0000-0000-000000000001', 'e5a00000-0000-0000-0000-000000000001', 'unterwegs', 100),
('e5d00000-0000-0000-0000-00000000000b', 'e5b00000-0000-0000-0000-000000000001', 'e5b00000-0000-0000-0000-000000000002', 'e5c00000-0000-0000-0000-000000000001', 'e5a00000-0000-0000-0000-000000000002', 'unterwegs', 100),
('e5d00000-0000-0000-0000-00000000000c', 'e5b00000-0000-0000-0000-000000000001', 'e5b00000-0000-0000-0000-000000000002', 'e5c00000-0000-0000-0000-000000000001', 'e5a00000-0000-0000-0000-000000000001', 'zugewiesen', null);
-- 1) Abschluss legt genau eine Zeile an
update public.auftraege
set status = 'abgeschlossen', end_km = 150, uebergabe_bestaetigt_at = now()
where id = 'e5d00000-0000-0000-0000-00000000000a';
select is(
(select count(*)::int from public.notification_outbox
where auftrag_id = 'e5d00000-0000-0000-0000-00000000000a' and event_type = 'protokoll_abgeschlossen'),
1,
'Abschluss legt genau eine protokoll_abgeschlossen-Zeile an'
);
-- 2) Empfaenger und Auftragsnummer im Payload
select is(
(select payload->>'empfaenger' from public.notification_outbox
where auftrag_id = 'e5d00000-0000-0000-0000-00000000000a' and event_type = 'protokoll_abgeschlossen'),
'dispo-mail@test.local',
'payload.empfaenger ist die E-Mail des Erstellers'
);
select is(
(select payload->>'auftragsnummer' from public.notification_outbox
where auftrag_id = 'e5d00000-0000-0000-0000-00000000000a' and event_type = 'protokoll_abgeschlossen'),
(select auftragsnummer from public.auftraege where id = 'e5d00000-0000-0000-0000-00000000000a'),
'payload.auftragsnummer ist die Auftragsnummer'
);
-- 3) Erneutes Update eines abgeschlossenen Auftrags -> keine zweite Zeile
update public.auftraege set empfaenger_name = 'Herr Beispiel'
where id = 'e5d00000-0000-0000-0000-00000000000a';
select is(
(select count(*)::int from public.notification_outbox
where auftrag_id = 'e5d00000-0000-0000-0000-00000000000a' and event_type = 'protokoll_abgeschlossen'),
1,
'Update eines bereits abgeschlossenen Auftrags legt keine weitere Zeile an'
);
-- 4) Ersteller ohne E-Mail -> keine Zeile
update public.auftraege
set status = 'abgeschlossen', end_km = 150, uebergabe_bestaetigt_at = now()
where id = 'e5d00000-0000-0000-0000-00000000000b';
select is(
(select count(*)::int from public.notification_outbox
where auftrag_id = 'e5d00000-0000-0000-0000-00000000000b' and event_type = 'protokoll_abgeschlossen'),
0,
'Ersteller ohne E-Mail: keine Zeile'
);
-- 5) Anderer Statuswechsel (storniert) -> keine Zeile
update public.auftraege set status = 'storniert'
where id = 'e5d00000-0000-0000-0000-00000000000c';
select is(
(select count(*)::int from public.notification_outbox
where auftrag_id = 'e5d00000-0000-0000-0000-00000000000c' and event_type = 'protokoll_abgeschlossen'),
0,
'Stornierung legt keine protokoll_abgeschlossen-Zeile an'
);
-- 6) claim_notification_batch claimt nur die angefragten Ereignis-Arten
delete from public.notification_outbox;
insert into public.notification_outbox (id, event_type, status, payload, naechster_versuch_ab) values
('e5e00000-0000-0000-0000-000000000001', 'auftrag_erstellt', 'pending', '{}'::jsonb, now() - interval '1 minute'),
('e5e00000-0000-0000-0000-000000000002', 'protokoll_abgeschlossen', 'pending', '{}'::jsonb, now() - interval '1 minute');
select results_eq(
$$ select id::text from public.claim_notification_batch(10, array['protokoll_abgeschlossen']) $$,
$$ values ('e5e00000-0000-0000-0000-000000000002'::text) $$,
'claim_notification_batch claimt nur protokoll_abgeschlossen, auftrag_erstellt bleibt pending'
);
select * from finish();
rollback;