- 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
178 lines
8.3 KiB
PL/PgSQL
178 lines
8.3 KiB
PL/PgSQL
-- 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$;
|