forked from fahrPool/fahrPool
fix: remove photo-count check from DB trigger, photos now optional at Fahrtbeginn
- Add migration 20260818120000_auftrag_fotos_optional_trigger.sql that replaces check_auftrag_status_transition() function, removing ONLY the photo-count check while preserving all other validation (start_km, end_km, uebergabe_bestaetigt_at, status transitions, column lockdown, role-based restrictions) - Update pgTAP test: plan(13) -> plan(12), replace photo-mandatory test with test verifying transition succeeds without photos - Completes DB-level optional photos feature (app-level optional already done in Task 1) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HCHRPWuV3bd37NJhW2NK6P
This commit is contained in:
parent
df79d9324e
commit
9f410cbe80
|
|
@ -0,0 +1,56 @@
|
|||
-- Remove photo-count requirement from check_auftrag_status_transition trigger.
|
||||
-- Photos are now optional when starting a trip (status zugewiesen -> unterwegs).
|
||||
-- All other checks (column lockdown, allowed transitions, cancel-only-by-admin/disponent,
|
||||
-- start_km requirement, end_km/uebergabe_bestaetigt_at requirements) are preserved.
|
||||
create or replace function public.check_auftrag_status_transition()
|
||||
returns trigger
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = public
|
||||
as $$
|
||||
begin
|
||||
if public.user_role() not in ('admin', 'disponent') then
|
||||
if new.abholort_id is distinct from old.abholort_id
|
||||
or new.zielort_id is distinct from old.zielort_id
|
||||
or new.fahrzeug_id is distinct from old.fahrzeug_id
|
||||
or new.erstellt_von is distinct from old.erstellt_von
|
||||
or new.vorgaenger_auftrag_id is distinct from old.vorgaenger_auftrag_id
|
||||
then
|
||||
raise exception 'Nur Disponent/Admin duerfen Abholort, Zielort, Fahrzeug, Ersteller oder Vorgaenger-Auftrag aendern';
|
||||
end if;
|
||||
end if;
|
||||
|
||||
if old.status = new.status then
|
||||
return new;
|
||||
end if;
|
||||
|
||||
if not (
|
||||
(old.status = 'offen' and new.status in ('zugewiesen', 'storniert'))
|
||||
or (old.status = 'zugewiesen' and new.status in ('unterwegs', 'offen', 'storniert'))
|
||||
or (old.status = 'unterwegs' and new.status in ('abgeschlossen', 'storniert'))
|
||||
) then
|
||||
raise exception 'Ungueltiger Statuswechsel: % -> %', old.status, new.status;
|
||||
end if;
|
||||
|
||||
if new.status = 'storniert' and public.user_role() not in ('admin', 'disponent') then
|
||||
raise exception 'Nur Disponent/Admin duerfen einen Auftrag stornieren';
|
||||
end if;
|
||||
|
||||
if new.status = 'unterwegs' then
|
||||
if new.start_km is null then
|
||||
raise exception 'start_km muss gesetzt sein, bevor der Status auf unterwegs wechselt';
|
||||
end if;
|
||||
end if;
|
||||
|
||||
if new.status = 'abgeschlossen' then
|
||||
if new.end_km is null then
|
||||
raise exception 'end_km muss gesetzt sein, bevor der Status auf abgeschlossen wechselt';
|
||||
end if;
|
||||
if new.uebergabe_bestaetigt_at is null then
|
||||
raise exception 'uebergabe_bestaetigt_at muss gesetzt sein, bevor der Status auf abgeschlossen wechselt';
|
||||
end if;
|
||||
end if;
|
||||
|
||||
return new;
|
||||
end;
|
||||
$$;
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
begin;
|
||||
select plan(13);
|
||||
select plan(12);
|
||||
|
||||
insert into auth.users (id, email) values
|
||||
('a1111111-1111-1111-1111-111111111111', 'admin@auftraege.test.local'),
|
||||
|
|
@ -129,34 +129,12 @@ select lives_ok(
|
|||
'start_km setzen ohne Statuswechsel ist erlaubt'
|
||||
);
|
||||
|
||||
select throws_ok(
|
||||
$$
|
||||
update public.auftraege set status = 'unterwegs'
|
||||
where id = '10000000-0000-0000-0000-000000000002'
|
||||
$$,
|
||||
'P0001',
|
||||
'Alle 8 Pflichtfotos muessen vorhanden sein, bevor der Status auf unterwegs wechselt',
|
||||
'Uebergang zugewiesen -> unterwegs ohne alle 8 Pflichtfotos wird vom Trigger abgelehnt'
|
||||
);
|
||||
|
||||
reset role;
|
||||
|
||||
insert into public.auftrag_fotos (auftrag_id, position, storage_path, uploaded_by)
|
||||
select '10000000-0000-0000-0000-000000000002', position, '10000000-0000-0000-0000-000000000002/' || position || '.jpg', 'f3333333-3333-3333-3333-333333333333'
|
||||
from unnest(array[
|
||||
'front', 'seitlich_rechts', 'rechte_seite', 'seitlich_rechts_heck',
|
||||
'heck', 'seitlich_links_heck', 'linke_seite', 'seitlich_vorne_links'
|
||||
]) as position;
|
||||
|
||||
set local role authenticated;
|
||||
set local request.jwt.claims to '{"sub": "f3333333-3333-3333-3333-333333333333", "role": "authenticated"}';
|
||||
|
||||
select lives_ok(
|
||||
$$
|
||||
update public.auftraege set status = 'unterwegs'
|
||||
where id = '10000000-0000-0000-0000-000000000002'
|
||||
$$,
|
||||
'Uebergang zugewiesen -> unterwegs mit start_km und allen 8 Pflichtfotos gelingt'
|
||||
'Uebergang zugewiesen -> unterwegs mit start_km (ohne Fotos) gelingt'
|
||||
);
|
||||
|
||||
select throws_ok(
|
||||
|
|
|
|||
Loading…
Reference in New Issue