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$;