Files
alpenwerk-hr/db/migrations/20261022110000_standort_aenderbar.sql
Andrei Laas c6f617093f
All checks were successful
CI / Lint, Typen, Tests, Build (push) Successful in 12m1s
CI / Migrationen auf leerer Datenbank (push) Successful in 10m40s
CI / Ausrollen (push) Successful in 6s
Versetzung mit Standortwechsel; Standort aenderbar ueber change_employee_data
- Versetzen: Feld "Standort", vorbelegt mit dem bisherigen. Ein anderer
  Standort wechselt zum selben Tag, in derselben Transaktion wie die
  Versetzung (sofort oder vorgemerkt).
- change_employee_data, Nachtlauf und Feldkarte kennen den Standort
  (Gruppe role): Historienzeile "Standort", berichtigbar und
  zuruecknehmbar. In der Historie steht die Kennung, angezeigt wird der
  Name (StandorteContext).

Rauchtest: Schritt 30.

Geprueft: lint, tsc, types:check, 949 Tests, Uebergabe-Waechter
2026-10-22 14:50:19 +02:00

85 lines
4.5 KiB
SQL

-- Der Standort einer Person lässt sich ändern.
--
-- Fachlich vorgegeben: mit einer Versetzung kann auch der Standort wechseln.
-- Geändert wird er über change_employee_data (Gruppe `role`), mit Stichtag:
-- sofort wirksam oder vorgemerkt wie jede andere Vertragsänderung, mit Zeile
-- in der Historie, berichtigbar und zurücknehmbar. Gespeichert wird in der
-- Historie die Kennung des Standorts; die Oberfläche zeigt den Namen.
--
-- Ein leer geschickter Standort ändert nichts — die Spalte ist Pflicht.
--
-- Ergänzt wird an der laufenden Definition, an den Zeilen der Lehrzeit
-- (20261021100000), an genau einem Anker je Stelle.
do $migration$
declare
v_ziele text[][] := array[
array['change_employee_data',
$a$if v_role ? 'lehrzeit_bis' then v_contract_changes := app_aenderung(v_contract_changes, 'Lehrzeit bis', v_old.lehrzeit_bis::text, (nullif(v_role->>'lehrzeit_bis', '')::date)::text); end if;$a$,
$a$if v_role ? 'lehrzeit_bis' then v_contract_changes := app_aenderung(v_contract_changes, 'Lehrzeit bis', v_old.lehrzeit_bis::text, (nullif(v_role->>'lehrzeit_bis', '')::date)::text); end if;
if nullif(v_role->>'location_id', '') is not null then v_contract_changes := app_aenderung(v_contract_changes, 'Standort', v_old.location_id::text, (v_role->>'location_id')::uuid::text); end if;$a$],
array['change_employee_data',
$a$lehrzeit_bis = case when v_role ? 'lehrzeit_bis' then nullif(v_role->>'lehrzeit_bis', '')::date else lehrzeit_bis end,$a$,
$a$lehrzeit_bis = case when v_role ? 'lehrzeit_bis' then nullif(v_role->>'lehrzeit_bis', '')::date else lehrzeit_bis end,
location_id = coalesce(nullif(v_role->>'location_id', '')::uuid, location_id),$a$],
array['apply_due_pending_changes',
$a$lehrzeit_bis = case when v_role ? 'lehrzeit_bis' then nullif(v_role->>'lehrzeit_bis', '')::date else lehrzeit_bis end,$a$,
$a$lehrzeit_bis = case when v_role ? 'lehrzeit_bis' then nullif(v_role->>'lehrzeit_bis', '')::date else lehrzeit_bis end,
location_id = coalesce(nullif(v_role->>'location_id', '')::uuid, location_id),$a$],
array['app_feld_karte',
$a$'Lehrzeit bis', jsonb_build_array('lehrzeit_bis', 'date', 'role'),$a$,
$a$'Lehrzeit bis', jsonb_build_array('lehrzeit_bis', 'date', 'role'),
'Standort', jsonb_build_array('location_id', 'uuid', 'role'),$a$]
];
v_fn text;
v_def text;
v_treffer integer;
i integer;
begin
foreach v_fn in array array['change_employee_data', 'apply_due_pending_changes', 'app_feld_karte'] loop
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 = v_fn;
if v_def is null then
raise exception '% ist nicht vorhanden.', v_fn;
end if;
if position('location_id = coalesce(nullif(v_role' in v_def) > 0 or position('''Standort''' in v_def) > 0 then
raise notice '% kennt den Standort bereits — nichts zu tun.', v_fn;
continue;
end if;
for i in 1 .. array_length(v_ziele, 1) loop
continue when v_ziele[i][1] <> v_fn;
v_treffer := (length(v_def) - length(replace(v_def, v_ziele[i][2], ''))) / length(v_ziele[i][2]);
if v_treffer <> 1 then
raise exception '%: Anker % kommt % mal vor, erwartet genau einmal.', v_fn, i, v_treffer;
end if;
v_def := replace(v_def, v_ziele[i][2], v_ziele[i][3]);
end loop;
execute v_def;
end loop;
end
$migration$;
do $pruefung$
declare
v_def text;
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 = 'change_employee_data';
if position('''Standort''' in v_def) = 0 or position('location_id = coalesce(nullif(v_role' in v_def) = 0
or position('lehrzeit_von' in v_def) = 0 or position('require_hr_admin()' in v_def) = 0
or position('v_pending_vertrag' in v_def) = 0 then
raise exception 'change_employee_data ist nicht wie erwartet angepasst.';
end if;
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 = 'apply_due_pending_changes';
if position('location_id = coalesce(nullif(v_role' in v_def) = 0 then
raise exception 'Der Nachtlauf setzt den Standort nicht.';
end if;
if not (app_feld_karte() ? 'Standort' and app_feld_karte() ? 'Lehrzeit bis') then
raise exception 'Die Feldkarte kennt den Standort nicht.';
end if;
end
$pruefung$;