1
0
Fork 0

fix: FIN-Abgleich idempotent, Pruefdaten beim Zurueckgeben loeschen, NULL-Rolle abfangen

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jonny 2026-10-09 11:21:23 +02:00
parent 8e49394fdb
commit e66809ffd5
2 changed files with 33 additions and 4 deletions

View File

@ -32,9 +32,18 @@ security definer
set search_path = public
as $$
begin
-- Gibt ein Fahrer den Auftrag zurueck (-> offen), duerfen die Pruefdaten
-- nicht auf den naechsten Fahrer vererbt werden. Gilt fuer alle Rollen.
if new.status = 'offen'::public.app_auftrag_status
and old.status is distinct from 'offen'::public.app_auftrag_status then
new.start_fin := null;
new.start_fin_status := null;
return new;
end if;
if (new.start_fin is distinct from old.start_fin
or new.start_fin_status is distinct from old.start_fin_status)
and public.user_role() not in ('admin', 'disponent')
and coalesce(public.user_role()::text, '') not in ('admin', 'disponent')
and coalesce(current_setting('app.fin_abgleich', true), '') <> 'an'
then
raise exception 'FIN-Abgleich darf nur ueber fin_bei_fahrtbeginn gesetzt werden';
@ -60,6 +69,8 @@ declare
v_fin text := upper(btrim(coalesce(p_fin, '')));
v_fahrzeug_id uuid;
v_hinterlegt text;
v_alt_fin text;
v_alt_status public.app_fin_abgleich;
v_status public.app_fin_abgleich;
begin
if public.user_role() is distinct from 'fahrer'::public.app_role then
@ -70,7 +81,8 @@ begin
raise exception 'Die FIN muss 17 Zeichen lang sein (A–Z, 0–9, ohne I, O, Q).';
end if;
select a.fahrzeug_id into v_fahrzeug_id
select a.fahrzeug_id, a.start_fin, a.start_fin_status
into v_fahrzeug_id, v_alt_fin, v_alt_status
from public.auftraege a
where a.id = p_auftrag_id
and a.zugewiesen_an = auth.uid()
@ -86,7 +98,10 @@ begin
where f.id = v_fahrzeug_id
for update;
if v_hinterlegt is null then
if v_alt_status = 'nachgetragen' and v_alt_fin = v_fin then
-- Wiederholter Aufruf mit derselben FIN: Ergebnis bleibt 'nachgetragen'.
v_status := 'nachgetragen';
elsif v_hinterlegt is null then
if exists (select 1 from public.fahrzeuge f where f.fin = v_fin and f.id <> v_fahrzeug_id) then
v_status := 'abweichend';
else

View File

@ -1,5 +1,5 @@
begin;
select plan(20);
select plan(22);
insert into auth.users (id, email) values
('f1a00000-0000-0000-0000-000000000001', 'admin-fin@test.local'),
@ -170,7 +170,21 @@ select is_empty(
'Fahrer kann fahrzeuge.fin nicht direkt aendern'
);
-- 21) zweiter Aufruf mit gleicher FIN bleibt nachgetragen
select is(
public.fin_bei_fahrtbeginn('f1d00000-0000-0000-0000-000000000001', 'WVWZZZ1KZAW000001'),
'nachgetragen'::public.app_fin_abgleich,
'Wiederholter Abgleich mit gleicher FIN bleibt nachgetragen'
);
-- 22) Auftrag zurueck auf offen loescht die Pruefdaten
update public.auftraege set status = 'offen', zugewiesen_an = null
where id = 'f1d00000-0000-0000-0000-000000000003';
reset role;
select ok(
(select start_fin_status is null and start_fin is null from public.auftraege where id = 'f1d00000-0000-0000-0000-000000000003'),
'Zurueck auf offen setzt FIN-Abgleich zurueck'
);
set local role authenticated;
set local request.jwt.claims to '{"sub": "f1a00000-0000-0000-0000-000000000002", "role": "authenticated"}';