1
0
Fork 0
fahrPool/supabase/tests/protokoll_email_test.sql

103 lines
4.9 KiB
PL/PgSQL

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;