Mehrere Notfallkontakte, Planstelle nicht vor ihrer Einheit, Anstehend fuehrt in die Historie, Uebernahme, Meine Notizen
All checks were successful
CI / Lint, Typen, Tests, Build (push) Successful in 11m47s
CI / Migrationen auf leerer Datenbank (push) Successful in 10m19s
CI / Ausrollen (push) Successful in 7s

- Notfallkontakte: eigene Tabelle employee_emergency_contacts (Migration
  20261019160000), beliebig viele je Person, angelegt, geaendert und
  entfernt wie Angehoerige (add_/update_/delete_emergency_contact, mit
  Historie und Protokoll). Bestand uebernommen. Verhaeltnis-Liste wie bei
  den Angehoerigen (Ehepartner, Lebenspartner, Kind, Sonstige mit Freitext).
  Neueinstellung, Wiedereintritt, Import und Mitarbeiterexport schreiben
  und lesen die Tabelle.
- Planstelle beginnt nicht vor und nicht nach dem Bestehen ihrer
  Organisationseinheit: Ausloeser an om_positions (Migration
  20261019150000), gilt fuer Anlegen, Aendern und Klonen.
- Anstehend auf der Uebersicht: Klick oeffnet die Akte im Reiter Historie,
  bei einer Wiedervorlage im Reiter HR-Notizen (?tab=).
- Uebernahme: Vertrag zeigt Besetzungsart und "Uebernahme Ja/Nein" aus der
  Historie; die Kachel Eintritte (YTD) zaehlt Uebernahmen mit.
- Glocke: "Meine Notizen" schaltet bei hinzugewaehlten Kollegen auf nur
  die eigenen Notizen um.

Rauchtest Schritt 18.

Geprueft: lint, tsc, types:check, 921 Tests, Waechter
This commit is contained in:
2026-10-19 17:17:02 +02:00
parent b14d3f9aad
commit d25778d2fa
24 changed files with 642 additions and 220 deletions

View File

@@ -0,0 +1,63 @@
-- Eine Planstelle beginnt nicht vor ihrer Organisationseinheit.
--
-- Eine Planstelle gehört zu einer Einheit; ohne die Einheit hat sie keinen
-- Ort im Organigramm und keine Berichtslinie. Liesse sich eine Planstelle ab
-- 01.10. an eine Einheit hängen, die erst ab 01.11. besteht, zeigte jeder
-- Stichtag dazwischen eine Stelle ohne Einheit — im Organigramm, in den
-- Berichten und in der Berichtslinie ihrer Besetzung.
--
-- Die Prüfung sitzt als Auslöser an der Tabelle und nicht in
-- create_position, update_position und clone_position: drei Wege führen
-- hinein, und eine Bedingung, die an einem fehlt, ist keine. Geprüft wird nur
-- beim Anlegen und wenn sich Einheit oder Zeitraum ändern; Planstellen, die
-- schon so stehen, bleiben, bis jemand sie anfasst.
create or replace function public.fn_planstelle_in_einheit()
returns trigger
language plpgsql
set search_path = public, pg_temp
as $$
declare
v_einheit org_units%rowtype;
begin
if tg_op = 'UPDATE'
and new.org_unit_id is not distinct from old.org_unit_id
and new.valid_from is not distinct from old.valid_from
and new.valid_to is not distinct from old.valid_to then
return new;
end if;
select * into v_einheit from org_units where id = new.org_unit_id;
if not found then
return new;
end if;
if new.valid_from < v_einheit.valid_from then
raise exception 'Die Organisationseinheit „%" besteht erst ab %. Die Planstelle kann nicht früher beginnen.',
v_einheit.name, to_char(v_einheit.valid_from, 'DD.MM.YYYY');
end if;
if v_einheit.valid_to is not null and new.valid_from >= v_einheit.valid_to then
raise exception 'Die Organisationseinheit „%" besteht nur bis %. Die Planstelle kann nicht danach beginnen.',
v_einheit.name, to_char(v_einheit.valid_to - 1, 'DD.MM.YYYY');
end if;
return new;
end;
$$;
grant execute on function public.fn_planstelle_in_einheit() to alpenwerk_app;
drop trigger if exists trg_planstelle_in_einheit on om_positions;
create trigger trg_planstelle_in_einheit
before insert or update on om_positions
for each row execute function fn_planstelle_in_einheit();
-- ── Selbstprüfung ─────────────────────────────────────────────────────────
do $pruefung$
begin
if not exists (select 1 from pg_trigger
where tgname = 'trg_planstelle_in_einheit'
and tgrelid = 'public.om_positions'::regclass) then
raise exception 'Der Auslöser an om_positions fehlt.';
end if;
end
$pruefung$;

View File

@@ -0,0 +1,177 @@
-- Mehrere Notfallkontakte je Person, wie bei den Angehörigen.
--
-- Fachlich vorgegeben: so viele Notfallkontakte, wie es braucht, angelegt
-- und geändert wie Angehörige. Drei Spalten an `employees` fassen genau
-- einen; deshalb eine eigene Tabelle mit einer Zeile je Kontakt.
--
-- Die vorhandenen Kontakte werden übernommen. Die drei Spalten
-- emergency_contact_name, _phone, _relation bleiben an `employees` stehen,
-- weil ältere Einträge in Historie und Protokoll sie nennen; Oberfläche,
-- Import und Export lesen und schreiben die Kontakte ab hier in dieser
-- Tabelle.
--
-- Ein Notfallkontakt hat keinen Stichtag: eine Nummer, die im Ernstfall
-- gewählt wird, gilt ab sofort. In der Historie steht der Vorgang zum
-- heutigen Tag, vor dem Eintritt zum Eintrittstag (app_stichtag_vor_eintritt).
create table if not exists employee_emergency_contacts (
id uuid primary key default gen_random_uuid(),
employee_id uuid not null references employees(id) on delete cascade,
name text not null check (btrim(name) <> ''),
phone text not null check (btrim(phone) <> ''),
relation text,
created_at timestamptz not null default now()
);
create index if not exists employee_emergency_contacts_employee_idx on employee_emergency_contacts (employee_id);
alter table employee_emergency_contacts enable row level security;
drop policy if exists employee_emergency_contacts_hr_all on employee_emergency_contacts;
create policy employee_emergency_contacts_hr_all on employee_emergency_contacts
for all using ((select is_hr_user())) with check ((select is_hr_user()));
grant select, insert, update, delete on employee_emergency_contacts to alpenwerk_app;
-- Bestand übernehmen. chk_emergency_contact verlangt Name und Nummer
-- gemeinsam; steht trotzdem ein Name ohne Nummer da, gilt „nicht erfasst".
insert into employee_emergency_contacts (employee_id, name, phone, relation)
select e.id, btrim(e.emergency_contact_name), coalesce(nullif(btrim(e.emergency_contact_phone), ''), 'nicht erfasst'),
nullif(btrim(e.emergency_contact_relation), '')
from employees e
where nullif(btrim(e.emergency_contact_name), '') is not null
and not exists (select 1 from employee_emergency_contacts k where k.employee_id = e.id);
-- ── Hinzufügen, ändern, entfernen ─────────────────────────────────────────
create or replace function public.add_emergency_contact(payload jsonb)
returns uuid
language plpgsql
set search_path = public, pg_temp
as $$
declare
v_employee_id uuid := (payload->>'employee_id')::uuid;
v_name text := nullif(btrim(payload->>'name'), '');
v_phone text := nullif(btrim(payload->>'phone'), '');
v_relation text := nullif(btrim(payload->>'relation'), '');
v_person text;
v_id uuid;
begin
perform require_hr_admin();
select first_name || ' ' || last_name into v_person from employees where id = v_employee_id;
if v_person is null then
raise exception 'Die Person existiert nicht.';
end if;
if v_name is null or v_phone is null then
raise exception 'Name und Telefonnummer sind nötig.';
end if;
insert into employee_emergency_contacts (employee_id, name, phone, relation)
values (v_employee_id, v_name, v_phone, v_relation)
returning id into v_id;
insert into employee_history (employee_id, event_date, event_type, description)
values (v_employee_id, app_stichtag_vor_eintritt(v_employee_id, current_date), 'Stammdatenänderung',
'Notfallkontakt hinzugefügt: ' || v_name || coalesce(' (' || v_relation || ')', ''));
insert into audit_log (actor_user_id, actor_name, action, target_label, target_employee_id, details)
values (app_current_user_id(), current_actor_name(), 'Notfallkontakt hinzugefügt', v_person, v_employee_id,
v_name || coalesce(' (' || v_relation || ')', '') || ', ' || v_phone);
return v_id;
end;
$$;
create or replace function public.update_emergency_contact(payload jsonb)
returns void
language plpgsql
set search_path = public, pg_temp
as $$
declare
v_alt employee_emergency_contacts%rowtype;
v_name text := nullif(btrim(payload->>'name'), '');
v_phone text := nullif(btrim(payload->>'phone'), '');
v_relation text := nullif(btrim(payload->>'relation'), '');
v_person text;
begin
perform require_hr_admin();
select * into v_alt from employee_emergency_contacts where id = (payload->>'id')::uuid for update;
if not found then
raise exception 'Notfallkontakt nicht gefunden.';
end if;
if v_name is null or v_phone is null then
raise exception 'Name und Telefonnummer sind nötig.';
end if;
select first_name || ' ' || last_name into v_person from employees where id = v_alt.employee_id;
update employee_emergency_contacts set name = v_name, phone = v_phone, relation = v_relation where id = v_alt.id;
insert into employee_history (employee_id, event_date, event_type, description)
values (v_alt.employee_id, app_stichtag_vor_eintritt(v_alt.employee_id, current_date), 'Stammdatenänderung',
'Notfallkontakt geändert: ' || v_alt.name || ' → ' || v_name || coalesce(' (' || v_relation || ')', ''));
insert into audit_log (actor_user_id, actor_name, action, target_label, target_employee_id, details, changes)
values (app_current_user_id(), current_actor_name(), 'Notfallkontakt geändert', v_person, v_alt.employee_id,
v_alt.name || ' → ' || v_name,
app_aenderung(app_aenderung(app_aenderung('[]'::jsonb,
'Name', v_alt.name, v_name),
'Telefon', v_alt.phone, v_phone),
'Verhältnis', v_alt.relation, v_relation));
end;
$$;
create or replace function public.delete_emergency_contact(payload jsonb)
returns void
language plpgsql
set search_path = public, pg_temp
as $$
declare
v_alt employee_emergency_contacts%rowtype;
v_person text;
begin
perform require_hr_admin();
select * into v_alt from employee_emergency_contacts where id = (payload->>'id')::uuid;
if not found then
raise exception 'Notfallkontakt nicht gefunden.';
end if;
select first_name || ' ' || last_name into v_person from employees where id = v_alt.employee_id;
delete from employee_emergency_contacts where id = v_alt.id;
insert into employee_history (employee_id, event_date, event_type, description)
values (v_alt.employee_id, app_stichtag_vor_eintritt(v_alt.employee_id, current_date), 'Stammdatenänderung',
'Notfallkontakt entfernt: ' || v_alt.name || coalesce(' (' || v_alt.relation || ')', ''));
insert into audit_log (actor_user_id, actor_name, action, target_label, target_employee_id, details)
values (app_current_user_id(), current_actor_name(), 'Notfallkontakt entfernt', v_person, v_alt.employee_id,
v_alt.name || coalesce(' (' || v_alt.relation || ')', '') || ', ' || v_alt.phone);
end;
$$;
grant execute on function public.add_emergency_contact(jsonb) to alpenwerk_app;
grant execute on function public.update_emergency_contact(jsonb) to alpenwerk_app;
grant execute on function public.delete_emergency_contact(jsonb) to alpenwerk_app;
-- ── Selbstprüfung ─────────────────────────────────────────────────────────
do $pruefung$
declare
v_fn text;
v_def text;
v_bestand integer;
v_uebernommen integer;
begin
if not (select relrowsecurity from pg_class where oid = 'public.employee_emergency_contacts'::regclass) then
raise exception 'employee_emergency_contacts ist ohne Zeilenschutz.';
end if;
foreach v_fn in array array['add_emergency_contact', 'update_emergency_contact', 'delete_emergency_contact'] 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 position('require_hr_admin()' in v_def) = 0 or position('insert into audit_log' in v_def) = 0 then
raise exception '% prüft die Rechte nicht oder schreibt kein Protokoll.', v_fn;
end if;
end loop;
-- Jeder vorhandene Kontakt ist übernommen.
select count(*) into v_bestand from employees where nullif(btrim(emergency_contact_name), '') is not null;
select count(distinct employee_id) into v_uebernommen from employee_emergency_contacts;
if v_uebernommen < v_bestand then
raise exception 'Von % Notfallkontakten sind nur % übernommen.', v_bestand, v_uebernommen;
end if;
end
$pruefung$;

View File

@@ -46,6 +46,7 @@ declare
v_einheit uuid;
v_p1 uuid; v_p2 uuid; v_p3 uuid; v_p4 uuid; v_p5 uuid; v_p6 uuid; v_p7 uuid; v_p8 uuid; v_p9 uuid;
v_a uuid; v_b uuid; v_c uuid; v_d uuid; v_e uuid;
v_spaet uuid;
v_notiz uuid;
v_ort uuid;
v_nr int;
@@ -76,14 +77,14 @@ begin
'job_title', 'Rauchtest Drei', 'is_chief', false, 'valid_from', (current_date - 30)::text));
v_p4 := create_position(jsonb_build_object('org_unit_id', v_einheit,
'job_title', 'Rauchtest Vier', 'is_chief', false, 'valid_from', (current_date - 30)::text));
raise notice '1/17 Einheit und vier Planstellen angelegt';
raise notice '1/18 Einheit und vier Planstellen angelegt';
-- ── Einstellung ─────────────────────────────────────────────────
v_a := hire_employee(jsonb_build_object(
'personnel_number', v_nr, 'first_name', 'Rauch', 'last_name', 'Test-A',
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
'position_id', v_p1, 'entry_date', current_date::text));
raise notice '2/17 Einstellung auf Planstelle 1';
raise notice '2/18 Einstellung auf Planstelle 1';
-- ── Versetzung am selben Tag (20260929180000) ───────────────────
-- Die Besetzung begann heute; sie auf heute zu schliessen wäre ein
@@ -96,7 +97,7 @@ begin
if v_stelle is distinct from v_p2 then
raise exception 'Nach der Versetzung sitzt die Person nicht auf Planstelle 2.';
end if;
raise notice '3/17 Versetzung am selben Tag';
raise notice '3/18 Versetzung am selben Tag';
-- ── Beförderung mit Planstellenwechsel, ebenfalls heute ─────────
perform promote_employee(jsonb_build_object(
@@ -121,7 +122,7 @@ begin
if v_titel <> 'Rauchtest Leitung' then
raise exception 'Die Zielplanstelle heisst nach der Beförderung „%", erwartet „Rauchtest Leitung".', v_titel;
end if;
raise notice '4/17 Beförderung am selben Tag: Hay-Grade und Planstellenbezeichnung';
raise notice '4/18 Beförderung am selben Tag: Hay-Grade und Planstellenbezeichnung';
-- Dieselbe Planstelle, nur ein neuer Name.
perform promote_employee(jsonb_build_object(
@@ -139,7 +140,7 @@ begin
if not exists (select 1 from jobs where title = 'Rauchtest Leitung') then
raise exception 'Der alte Katalogeintrag wurde umbenannt statt die Planstelle umgehängt.';
end if;
raise notice '4b/17 Beförderung ohne Stellenwechsel benennt dieselbe Planstelle um';
raise notice '4b/18 Beförderung ohne Stellenwechsel benennt dieselbe Planstelle um';
-- ── Vorgemerkte Planstelle (20260929200000) ─────────────────────
-- Eine Versetzung in die Zukunft belegt die Zielstelle, obwohl dort noch
@@ -170,13 +171,13 @@ begin
if v_meldung not like '%bereits eine Versetzung vorgemerkt%' then
raise exception 'Erwartet wurde die Meldung zur Vormerkung, kam aber: %', v_meldung;
end if;
raise notice '5/17 Vorgemerkte Planstelle wird abgewiesen';
raise notice '5/18 Vorgemerkte Planstelle wird abgewiesen';
-- Die eigene Vormerkung darf nicht im Weg stehen: dieselbe Versetzung noch
-- einmal zu erfassen ist eine Berichtigung.
perform transfer_employee(jsonb_build_object(
'employee_id', v_a, 'target_position_id', v_p4, 'effective_date', (current_date + 20)::text));
raise notice '6/17 Die eigene Vormerkung steht nicht im Weg';
raise notice '6/18 Die eigene Vormerkung steht nicht im Weg';
-- ── Austritt am selben Tag (20260929220000) ─────────────────────
perform terminate_employee(jsonb_build_object(
@@ -188,7 +189,7 @@ begin
if v_offen <> 0 then
raise exception 'Nach dem Austritt ist die Planstelle nicht frei.';
end if;
raise notice '7/17 Austritt am selben Tag, Planstelle frei';
raise notice '7/18 Austritt am selben Tag, Planstelle frei';
-- ── Wiedereintritt (20260929160000) ─────────────────────────────
perform rehire_employee(jsonb_build_object(
@@ -205,7 +206,7 @@ begin
if v_stelle is distinct from v_p1 then
raise exception 'Nach dem Wiedereintritt sitzt die Person nicht auf Planstelle 1.';
end if;
raise notice '8/17 Wiedereintritt';
raise notice '8/18 Wiedereintritt';
-- ── Rückdatierte Versetzung (20261013140000) ────────────────────
--
@@ -240,7 +241,7 @@ begin
if v_stelle is distinct from v_p2 then
raise exception 'Nach der rückdatierten Versetzung sitzt die Person nicht auf Planstelle 2.';
end if;
raise notice '9/17 Rückdatierte Versetzung lässt keine zweite Besetzung stehen';
raise notice '9/18 Rückdatierte Versetzung lässt keine zweite Besetzung stehen';
-- ── Versetzung bei datiertem Austritt (20261013140000) ──────────
--
@@ -274,7 +275,7 @@ begin
if v_offen <> 1 then
raise exception 'Die neue Besetzung endet nicht am Austrittstag.';
end if;
raise notice '10/17 Versetzung bei datiertem Austritt: kein Überlappen, kein Überleben';
raise notice '10/18 Versetzung bei datiertem Austritt: kein Überlappen, kein Überleben';
-- ── Änderung vor dem Eintritt (20261018100000) ──────────────────
--
@@ -313,7 +314,7 @@ begin
and event_date = current_date + 40) then
raise exception 'Mit dem Eintritt ist die Änderung davor nicht mitgewandert.';
end if;
raise notice '11/17 Änderung vor dem Eintritt wirkt sofort und wandert mit dem Eintritt';
raise notice '11/18 Änderung vor dem Eintritt wirkt sofort und wandert mit dem Eintritt';
-- ── Abwesenheit mit geplanter Rückkehr löschen (20261019100000) ─
--
@@ -347,7 +348,7 @@ begin
if v_status <> 'Aktiv' then
raise exception 'Nach dem Löschen der Abwesenheit steht der Status auf %, erwartet Aktiv.', v_status;
end if;
raise notice '12/17 Abwesenheit mit geplanter Rückkehr lässt sich löschen';
raise notice '12/18 Abwesenheit mit geplanter Rückkehr lässt sich löschen';
-- ── Zweite Versetzung zum selben Tag (20261019110000) ───────────
--
@@ -387,7 +388,7 @@ begin
if exists (select 1 from pending_org_changes where employee_id = v_d and status = 'pending') then
raise exception 'Nach dem Abbrechen ist die Versetzung weiter vorgemerkt.';
end if;
raise notice '13/17 Zweite Versetzung zum selben Tag abgewiesen, die erste lässt sich abbrechen';
raise notice '13/18 Zweite Versetzung zum selben Tag abgewiesen, die erste lässt sich abbrechen';
-- ── Notiz löschen (20261019130000) ──────────────────────────────
-- Die eigene lässt sich löschen; das Protokoll behält den Text.
@@ -400,7 +401,7 @@ begin
if not exists (select 1 from audit_log where action = 'HR-Notiz gelöscht' and details like '%Rauchtest-Notiz%') then
raise exception 'Das Löschen der Notiz steht nicht im Protokoll.';
end if;
raise notice '14/17 Eigene Notiz lässt sich löschen, das Protokoll behält sie';
raise notice '14/18 Eigene Notiz lässt sich löschen, das Protokoll behält sie';
-- ── Austritt zurücknehmen (20261019140000) ──────────────────────
-- v_b hat seit Schritt 10 einen datierten Austritt; die Besetzung auf
@@ -415,7 +416,7 @@ begin
if exists (select 1 from employee_history where employee_id = v_b and event_type = 'Austritt') then
raise exception 'Nach der Rücknahme steht der Austritt noch in der Historie.';
end if;
raise notice '15/17 Austritt zurückgenommen, Besetzung läuft wieder';
raise notice '15/18 Austritt zurückgenommen, Besetzung läuft wieder';
-- ── Letzte Versetzung zurücknehmen (20261019140000) ─────────────
v_p8 := create_position(jsonb_build_object('org_unit_id', v_einheit,
@@ -438,7 +439,7 @@ begin
if exists (select 1 from position_assignments where position_id = v_p9) then
raise exception 'Nach der Rücknahme steht auf der Zielplanstelle noch eine Besetzung.';
end if;
raise notice '16/17 Letzte Versetzung zurückgenommen, vorherige Planstelle wieder besetzt';
raise notice '16/18 Letzte Versetzung zurückgenommen, vorherige Planstelle wieder besetzt';
-- ── Planstelle beim Eintritt berichtigen (20261019140000) ───────
-- v_c (Schritt 11) ist noch nicht eingetreten und nie versetzt worden.
@@ -452,7 +453,27 @@ begin
if exists (select 1 from employee_history where employee_id = v_c and event_type = 'Versetzung') then
raise exception 'Die Berichtigung hat eine Versetzung hinterlassen.';
end if;
raise notice '17/17 Planstelle beim Eintritt berichtigt, ohne Versetzung';
raise notice '17/18 Planstelle beim Eintritt berichtigt, ohne Versetzung';
-- ── Planstelle nicht vor ihrer Einheit (20261019150000) ─────────
v_spaet := create_org_unit(jsonb_build_object(
'parent_id', v_wurzel, 'org_number', 'RAUCHTEST-SPAET', 'name', 'Rauchtest später',
'unit_type', 'Team', 'valid_from', (current_date + 30)::text));
v_ging := false;
begin
perform create_position(jsonb_build_object('org_unit_id', v_spaet,
'job_title', 'Rauchtest zu früh', 'is_chief', false, 'valid_from', current_date::text));
v_ging := true;
exception
when others then v_meldung := SQLERRM;
end;
if v_ging then
raise exception 'Eine Planstelle liess sich vor ihrer Einheit anlegen.';
end if;
if v_meldung not like '%besteht erst ab%' then
raise exception 'Erwartet wurde die Meldung zur Einheit, kam aber: %', v_meldung;
end if;
raise notice '18/18 Planstelle vor ihrer Einheit wird abgewiesen';
raise notice '';
raise notice 'Rauchtest bestanden. Alles Angelegte wird gleich verworfen.';
@@ -464,6 +485,6 @@ rollback;
-- Beweis, dass nichts geblieben ist. Läuft ausserhalb der verworfenen
-- Transaktion und muss überall 0 liefern.
select
(select count(*) from org_units where org_number = 'RAUCHTEST') as einheiten,
(select count(*) from org_units where org_number like 'RAUCHTEST%') as einheiten,
(select count(*) from employees where last_name like 'Test-%' and first_name = 'Rauch') as personen,
(select count(*) from jobs where title like 'Rauchtest %') as taetigkeiten;