Eine Planstelle kann zu einem Tag enden, bis zu dem ihre Inhaberin versetzt oder befoerdert ist, auch wenn der Vorgang noch vorgemerkt ist: der Nachtlauf schliesst die Besetzung an diesem Tag. Ein vorgemerkter Austritt befristet die Besetzung schon beim Erfassen und war bereits erlaubt. Damit die Person danach nicht ueber das Ende hinaus sitzen bleibt, laesst sich eine solche Vormerkung nicht mehr abbrechen oder hinter das Ende verschieben (Trigger auf pending_org_changes, gilt fuer jeden Weg). Geprueft: Migrationen auf frischer DB, zweiter Lauf leer; Rauchtest 35/35; lint, tsc, types:check, 1010 Tests, Waechter.
123 lines
5.7 KiB
PL/PgSQL
123 lines
5.7 KiB
PL/PgSQL
-- Planstellenende und vorgemerkte Versetzung
|
|
--
|
|
-- Eine Planstelle kann enden, wenn ihre Besetzung bis dahin endet. Das tut
|
|
-- sie auch, wenn die Person zu einem Tag bis zum Ende versetzt oder befördert
|
|
-- ist und dieser Vorgang noch vorgemerkt ist: der Nachtlauf schliesst die
|
|
-- Besetzung an diesem Tag. Ein vorgemerkter Austritt braucht keine eigene
|
|
-- Regel; er befristet die Besetzung schon beim Erfassen.
|
|
--
|
|
-- Umgekehrt darf die Vormerkung dann nicht mehr wegfallen oder hinter das
|
|
-- Ende rutschen. Sonst bliebe die Person über das Ende der Planstelle hinaus
|
|
-- auf ihr sitzen, ohne dass es jemand bemerkt. Die Regel steht in einem
|
|
-- Trigger auf pending_org_changes und gilt damit für jeden Weg, der eine
|
|
-- Vormerkung abbricht oder verschiebt.
|
|
|
|
-- ── Planstelle beenden ──────────────────────────────────────────────
|
|
do $migration$
|
|
declare
|
|
v_anker text := $a$ if exists (select 1 from position_assignments
|
|
where position_id = v_id and (valid_to is null or valid_to > v_valid_to)) then$a$;
|
|
v_neu text := $a$ -- Eine vorgemerkte Versetzung oder Beförderung weg von hier beendet
|
|
-- die Besetzung zu ihrem Tag.
|
|
if exists (select 1 from position_assignments a
|
|
where a.position_id = v_id and (a.valid_to is null or a.valid_to > v_valid_to)
|
|
and not exists (select 1 from pending_org_changes o
|
|
where o.employee_id = a.employee_id
|
|
and o.status = 'pending'
|
|
and o.change_type in ('transfer', 'promotion')
|
|
and o.effective_date >= a.valid_from
|
|
and o.effective_date <= v_valid_to
|
|
and nullif(o.payload->>'target_position_id', '')::uuid <> v_id)) then$a$;
|
|
v_def text;
|
|
v_treffer integer;
|
|
begin
|
|
select pg_get_functiondef(p.oid) into v_def
|
|
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
|
where n.nspname = 'public' and p.prokind = 'f' and p.proname = 'update_position';
|
|
if v_def is null then
|
|
raise exception 'update_position ist nicht vorhanden.';
|
|
end if;
|
|
if position(v_neu in v_def) = 0 then
|
|
v_treffer := (length(v_def) - length(replace(v_def, v_anker, ''))) / length(v_anker);
|
|
if v_treffer <> 1 then
|
|
raise exception 'update_position: der Anker kommt % mal vor, erwartet genau einmal.', v_treffer;
|
|
end if;
|
|
execute replace(v_def, v_anker, v_neu);
|
|
end if;
|
|
end
|
|
$migration$;
|
|
|
|
-- ── Vormerkung abbrechen oder verschieben ───────────────────────────
|
|
create or replace function public.fn_vormerkung_vor_planstellenende()
|
|
returns trigger
|
|
language plpgsql
|
|
set search_path = public, pg_temp
|
|
as $$
|
|
declare
|
|
v_stelle record;
|
|
begin
|
|
if old.status <> 'pending' or old.change_type not in ('transfer', 'promotion') or new.status = 'applied' then
|
|
return new;
|
|
end if;
|
|
if new.status = 'pending' and new.effective_date <= old.effective_date then
|
|
return new;
|
|
end if;
|
|
|
|
-- Die Planstelle, die die Person mit dieser Vormerkung verlässt und die
|
|
-- endet, während die Besetzung noch läuft.
|
|
select p.position_number, p.valid_to into v_stelle
|
|
from position_assignments a
|
|
join om_positions p on p.id = a.position_id
|
|
where a.employee_id = old.employee_id
|
|
and p.valid_to is not null
|
|
and (a.valid_to is null or a.valid_to > p.valid_to)
|
|
and old.effective_date <= p.valid_to
|
|
and p.id is distinct from nullif(old.payload->>'target_position_id', '')::uuid
|
|
limit 1;
|
|
if not found then
|
|
return new;
|
|
end if;
|
|
if new.status = 'pending' and new.effective_date <= v_stelle.valid_to then
|
|
return new;
|
|
end if;
|
|
-- Eine zweite Vormerkung bis zum Ende genügt ebenso.
|
|
if exists (select 1 from pending_org_changes o
|
|
where o.employee_id = old.employee_id
|
|
and o.id <> old.id
|
|
and o.status = 'pending'
|
|
and o.change_type in ('transfer', 'promotion')
|
|
and o.effective_date <= v_stelle.valid_to) then
|
|
return new;
|
|
end if;
|
|
|
|
raise exception 'Die Planstelle % endet am %. Die vorgemerkte Versetzung oder Beförderung lässt sich nicht abbrechen oder dahinter verschieben, solange die Planstelle endet.',
|
|
v_stelle.position_number, to_char(v_stelle.valid_to, 'DD.MM.YYYY');
|
|
end;
|
|
$$;
|
|
|
|
drop trigger if exists trg_vormerkung_vor_planstellenende on pending_org_changes;
|
|
create trigger trg_vormerkung_vor_planstellenende
|
|
before update on pending_org_changes
|
|
for each row execute function fn_vormerkung_vor_planstellenende();
|
|
|
|
do $pruefung$
|
|
declare
|
|
v_def text;
|
|
begin
|
|
select pg_get_functiondef('public.update_position'::regproc) into v_def;
|
|
if (length(v_def) - length(replace(v_def, 'nullif(o.payload->>''target_position_id'', '''')::uuid <> v_id', '')))
|
|
/ length('nullif(o.payload->>''target_position_id'', '''')::uuid <> v_id') <> 1 then
|
|
raise exception 'update_position rechnet die vorgemerkte Versetzung nicht genau einmal mit.';
|
|
end if;
|
|
if position('Für diese Planstelle ist zum % eine Versetzung oder Beförderung vorgemerkt' in v_def) = 0 then
|
|
raise exception 'update_position prüft keine Vormerkung auf die Planstelle mehr.';
|
|
end if;
|
|
if not exists (select 1 from pg_trigger where tgname = 'trg_vormerkung_vor_planstellenende' and not tgisinternal) then
|
|
raise exception 'Der Trigger trg_vormerkung_vor_planstellenende fehlt.';
|
|
end if;
|
|
if exists (select 1 from pg_proc where proname = 'fn_vormerkung_vor_planstellenende' and prosecdef) then
|
|
raise exception 'fn_vormerkung_vor_planstellenende darf nicht SECURITY DEFINER sein.';
|
|
end if;
|
|
end
|
|
$pruefung$;
|