forked from fahrPool/fahrPool
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:
parent
8e49394fdb
commit
e66809ffd5
|
|
@ -32,9 +32,18 @@ security definer
|
||||||
set search_path = public
|
set search_path = public
|
||||||
as $$
|
as $$
|
||||||
begin
|
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
|
if (new.start_fin is distinct from old.start_fin
|
||||||
or new.start_fin_status is distinct from old.start_fin_status)
|
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'
|
and coalesce(current_setting('app.fin_abgleich', true), '') <> 'an'
|
||||||
then
|
then
|
||||||
raise exception 'FIN-Abgleich darf nur ueber fin_bei_fahrtbeginn gesetzt werden';
|
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_fin text := upper(btrim(coalesce(p_fin, '')));
|
||||||
v_fahrzeug_id uuid;
|
v_fahrzeug_id uuid;
|
||||||
v_hinterlegt text;
|
v_hinterlegt text;
|
||||||
|
v_alt_fin text;
|
||||||
|
v_alt_status public.app_fin_abgleich;
|
||||||
v_status public.app_fin_abgleich;
|
v_status public.app_fin_abgleich;
|
||||||
begin
|
begin
|
||||||
if public.user_role() is distinct from 'fahrer'::public.app_role then
|
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).';
|
raise exception 'Die FIN muss 17 Zeichen lang sein (A–Z, 0–9, ohne I, O, Q).';
|
||||||
end if;
|
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
|
from public.auftraege a
|
||||||
where a.id = p_auftrag_id
|
where a.id = p_auftrag_id
|
||||||
and a.zugewiesen_an = auth.uid()
|
and a.zugewiesen_an = auth.uid()
|
||||||
|
|
@ -86,7 +98,10 @@ begin
|
||||||
where f.id = v_fahrzeug_id
|
where f.id = v_fahrzeug_id
|
||||||
for update;
|
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
|
if exists (select 1 from public.fahrzeuge f where f.fin = v_fin and f.id <> v_fahrzeug_id) then
|
||||||
v_status := 'abweichend';
|
v_status := 'abweichend';
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
begin;
|
begin;
|
||||||
select plan(20);
|
select plan(22);
|
||||||
|
|
||||||
insert into auth.users (id, email) values
|
insert into auth.users (id, email) values
|
||||||
('f1a00000-0000-0000-0000-000000000001', 'admin-fin@test.local'),
|
('f1a00000-0000-0000-0000-000000000001', 'admin-fin@test.local'),
|
||||||
|
|
@ -170,7 +170,21 @@ select is_empty(
|
||||||
'Fahrer kann fahrzeuge.fin nicht direkt aendern'
|
'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;
|
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 role authenticated;
|
||||||
set local request.jwt.claims to '{"sub": "f1a00000-0000-0000-0000-000000000002", "role": "authenticated"}';
|
set local request.jwt.claims to '{"sub": "f1a00000-0000-0000-0000-000000000002", "role": "authenticated"}';
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue