Files
alpenwerk-hr/db/migrations/20261201120000_fl_ohne_stufe_8.sql
Andrei Laas 9ce6375b33
All checks were successful
CI / Lint, Typen, Tests, Build (push) Successful in 11m56s
CI / Migrationen auf leerer Datenbank (push) Successful in 10m27s
CI / Ausrollen (push) Successful in 7s
FL statt VL, Stufen 1 bis 7
Die Angabe heisst ueberall "FL" (Einstellung, Wiedereintritt, Daten aendern,
Akte, Zusammenfassung, Export, Austauschdatei). Gespeichert bleibt sie in
vl, in Historie und Feldkarte als "VL"; anzeige() zeigt dort "FL".

Migration 20261201120000: chk_vl auf 1 bis 7. Bestand mit 8 wird geleert
und je Person im Protokoll vermerkt ("FL geleert"). Geplante Aenderungen
auf 8 verlieren diese Angabe in Vormerkung und Historienzeile; bleibt
nichts uebrig, entfaellt der Vorgang samt Zeile. Jeweils Protokolleintrag.

Geprueft: lint, tsc, types:check, npm test (1123), waechter; Migrationen auf
leerer Datenbank zweimal (Nichts anzuwenden), rauchtest.sql,
vertragsangaben.sql (FL 8 abgewiesen, Wiedereintritt mit FL 6); Bestand
mit FL 8, geplanter Aenderung FL 8 plus Bonus und geplanter Aenderung nur
FL 8: geleert, Bonus bleibt vorgemerkt, Vorgang entfaellt.
2026-12-01 12:12:59 +01:00

79 lines
3.6 KiB
SQL

-- FL: Stufen 1 bis 7
--
-- Die Angabe heisst fachlich „FL" und kennt die Stufen 1 bis 7. Gespeichert
-- bleibt sie in der Spalte vl, und in der Historie und der Feldkarte heisst
-- das Feld weiter „VL": daran hängen Berichtigen und Zurücksetzen bestehender
-- Einträge. Die Oberfläche zeigt „FL" (lib/anzeige.ts).
--
-- Bestand mit Stufe 8: welche Stufe stattdessen gilt, geht aus den Daten
-- nicht hervor. Die Angabe wird geleert und je Person im Protokoll vermerkt;
-- ein geratener Wert sähe aus wie ein erfasster. Geplante Änderungen auf
-- Stufe 8 verlieren diese eine Angabe, in der Vormerkung und in ihrer
-- Historienzeile; der Rest des Vorgangs bleibt. Bleibt nichts übrig, entfällt
-- der Vorgang samt Zeile, wie beim Abbrechen in der Historie.
do $geplant$
declare
v_plan record;
v_payload jsonb;
begin
for v_plan in
select * from pending_org_changes
where status = 'pending' and payload->'contract'->>'vl' = '8'
loop
v_payload := jsonb_set(v_plan.payload, '{contract}', (v_plan.payload->'contract') - 'vl');
insert into audit_log (actor_user_id, actor_name, action, target_label, target_employee_id, details)
select null, 'Wartung', 'Geplante Änderung angepasst', e.first_name || ' ' || e.last_name, e.id,
'Zum ' || v_plan.effective_date || ' war FL 8 vorgemerkt. Die Stufe 8 gibt es nicht mehr; '
|| 'diese Angabe entfällt aus dem Vorgang.'
from employees e where e.id = v_plan.employee_id;
if coalesce(v_payload->'person', '{}'::jsonb) = '{}'::jsonb
and coalesce(v_payload->'contract', '{}'::jsonb) = '{}'::jsonb
and coalesce(v_payload->'role', '{}'::jsonb) = '{}'::jsonb then
update pending_org_changes set status = 'cancelled' where id = v_plan.id;
delete from employee_history where pending_id = v_plan.id;
else
update pending_org_changes set payload = v_payload where id = v_plan.id;
-- In der Beschreibung steht die Liste der Felder, jedes gefolgt von
-- einem Komma („Geänderte Felder: VL, Bonusanspruch, wirksam ab …").
update employee_history h
set changes = (select coalesce(jsonb_agg(a), '[]'::jsonb)
from jsonb_array_elements(h.changes) a
where a->>'feld' <> 'VL'),
description = replace(h.description, 'VL, ', '')
where h.pending_id = v_plan.id;
end if;
end loop;
end
$geplant$;
insert into audit_log (actor_user_id, actor_name, action, target_label, target_employee_id, details)
select null, 'Wartung', 'FL geleert', e.first_name || ' ' || e.last_name, e.id,
'Die Stufe 8 gibt es nicht mehr; FL war 8 und ist jetzt leer. Bitte die zutreffende Stufe (1 bis 7) nachtragen.'
from employees e
where e.vl = 8;
update employees set vl = null where vl = 8;
alter table employees drop constraint if exists chk_vl;
alter table employees add constraint chk_vl check (vl is null or vl between 1 and 7);
comment on column employees.vl is 'FL, Stufe 1 bis 7; leer heisst nicht erfasst. In Historie und Feldkarte als „VL" geführt.';
do $pruefung$
begin
if exists (select 1 from employees where vl not between 1 and 7) then
raise exception 'Es gibt noch FL-Werte ausserhalb von 1 bis 7.';
end if;
if exists (select 1 from pending_org_changes where status = 'pending' and payload->'contract'->>'vl' = '8') then
raise exception 'Es gibt noch geplante Änderungen auf FL 8.';
end if;
if pg_get_constraintdef((select oid from pg_constraint where conname = 'chk_vl' and conrelid = 'public.employees'::regclass))
not like '%vl >= 1%vl <= 7%' then
raise exception 'chk_vl erlaubt nicht genau 1 bis 7.';
end if;
end
$pruefung$;