Übergabestand 2026-11-24

Alpenwerk HR 0.2.2

- C-Level als Auswahl aus der fachlichen Liste (C1 … VD) statt Ja/Nein; überall als Code angezeigt
- Standorte mit Unterstandorten: bei „Shop“ folgt die Auswahl des einzelnen Shops
- Angehörige und Notfallkontakte im Import: Verhältnis aus der Liste, bei „Sonstige“ mit eigenem Text
- Wochenstunden mit zwei Nachkommastellen (z. B. 19,25)
- Langzeitabwesenheit: „Endet am“ statt Rückkehr; eine anschliessende Abwesenheit lässt sich nur nach der Wochenhilfe (Mutterschutz) planen
- Notizen mit Fälligkeit erscheinen erst ab diesem Tag in der Glocke
- Testinstallationen: mit TESTSYSTEM=1 in app.env trägt jede Seite den Schriftzug „Testsystem“ und einen roten Rahmen
- Next.js 16.4.6
This commit is contained in:
2026-11-24 11:08:43 +01:00
parent 322d0f4407
commit e09d94cb2f
72 changed files with 1451 additions and 332 deletions

View File

@@ -0,0 +1,148 @@
-- Wochenstunden mit zwei Nachkommastellen
--
-- Teilzeit wird in Viertelstunden vereinbart: 19,25 Stunden sind eine
-- übliche Angabe. Eine Spalte mit einer Nachkommastelle rundet sie beim
-- Speichern stillschweigend auf 19,3, und in den Stammdaten steht dann ein
-- Wert, den niemand vereinbart hat. numeric(4,2) reicht bis 99,99; mehr als 38,5
-- lässt chk_weekly_hours ohnehin nicht zu.
--
-- ── Die IT-Sicht steht der Typänderung im Weg ─────────────────────────────
--
-- it.mitarbeiter liest die Spalte, und PostgreSQL ändert den Typ einer
-- Spalte nicht, solange eine Sicht sie verwendet. Die Sicht wird deshalb aus
-- ihrer eigenen, gespeicherten Definition neu angelegt — nicht aus einer
-- Abschrift, die hier veralten könnte —, samt Kommentar und Leserechten.
--
-- ── Verglichen wird ohne angehängte Nullen ────────────────────────────────
--
-- change_employee_data und record_karenz_return schreiben eine Änderung der
-- Wochenstunden nur, wenn sich der Text unterscheidet. Mit zwei
-- Nachkommastellen lautete der gespeicherte Wert „38.50", die Eingabe „38.5"
-- — eine Änderung über nichts in Historie und Bericht. trim_scale nimmt auf
-- beiden Seiten die angehängten Nullen weg; so steht in der Historie „19.25"
-- und „38.5", in derselben Schreibweise wie die vorhandenen Einträge.
do $migration$
declare
v_sicht text;
v_kommentar text;
v_recht record;
v_rechte text[] := '{}';
v_def text;
v_anzahl int;
v_alt text;
v_neu text;
begin
-- 1. Sicht sichern, entfernen, Typ ändern, Sicht wiederherstellen.
select pg_get_viewdef('it.mitarbeiter'::regclass, true),
obj_description('it.mitarbeiter'::regclass, 'pg_class')
into v_sicht, v_kommentar;
if v_sicht is null then raise exception 'it.mitarbeiter ist nicht vorhanden.'; end if;
for v_recht in
select case when a.grantee = 0 then 'public' else quote_ident(r.rolname) end as wem,
a.privilege_type as was
from pg_class c
cross join lateral aclexplode(c.relacl) a
left join pg_roles r on r.oid = a.grantee
where c.oid = 'it.mitarbeiter'::regclass
and a.grantee <> c.relowner
loop
v_rechte := v_rechte || format('grant %s on it.mitarbeiter to %s', v_recht.was, v_recht.wem);
end loop;
drop view it.mitarbeiter;
alter table employees alter column weekly_hours type numeric(4,2);
execute 'create view it.mitarbeiter as ' || v_sicht;
if v_kommentar is not null then
execute format('comment on view it.mitarbeiter is %L', v_kommentar);
end if;
for i in 1 .. coalesce(array_length(v_rechte, 1), 0) loop
execute v_rechte[i];
end loop;
-- 2. change_employee_data: Vergleich ohne angehängte Nullen.
v_alt := $anker$v_old.weekly_hours::text, (nullif(v_contract->>'weekly_hours','')::numeric(4,1))::text$anker$;
v_neu := $anker$trim_scale(v_old.weekly_hours)::text, trim_scale(nullif(v_contract->>'weekly_hours','')::numeric(4,2))::text$anker$;
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.proname = 'change_employee_data' and p.prokind = 'f';
if v_def is null then raise exception 'change_employee_data ist nicht vorhanden.'; end if;
v_anzahl := (length(v_def) - length(replace(v_def, v_alt, ''))) / length(v_alt);
if v_anzahl <> 1 then
raise exception 'change_employee_data: Anker der Wochenstunden kommt % mal vor, erwartet einmal.', v_anzahl;
end if;
execute replace(v_def, v_alt, v_neu);
-- 3. record_karenz_return: dasselbe bei der Rückkehr mit anderen Stunden.
v_alt := $anker$v_old.weekly_hours::text, v_weekly_hours::text)$anker$;
v_neu := $anker$trim_scale(v_old.weekly_hours)::text, trim_scale(v_weekly_hours::numeric(4,2))::text)$anker$;
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.proname = 'record_karenz_return' and p.prokind = 'f';
if v_def is null then raise exception 'record_karenz_return ist nicht vorhanden.'; end if;
v_anzahl := (length(v_def) - length(replace(v_def, v_alt, ''))) / length(v_alt);
if v_anzahl <> 1 then
raise exception 'record_karenz_return: Anker der Wochenstunden kommt % mal vor, erwartet einmal.', v_anzahl;
end if;
execute replace(v_def, v_alt, v_neu);
end
$migration$;
-- Selbstprüfung: das Neue und das, was bei der Neuerzeugung verlorengehen kann.
do $$
declare
v_def text;
begin
if (select format_type(a.atttypid, a.atttypmod)
from pg_attribute a
where a.attrelid = 'public.employees'::regclass and a.attname = 'weekly_hours') <> 'numeric(4,2)' then
raise exception 'employees.weekly_hours ist nicht numeric(4,2).';
end if;
if (select format_type(a.atttypid, a.atttypmod)
from pg_attribute a
where a.attrelid = 'it.mitarbeiter'::regclass and a.attname = 'weekly_hours') <> 'numeric(4,2)' then
raise exception 'it.mitarbeiter zeigt die Wochenstunden nicht mit zwei Nachkommastellen.';
end if;
if not has_table_privilege('it_lesen', 'it.mitarbeiter', 'select') then
raise exception 'it_lesen kann it.mitarbeiter nicht mehr lesen.';
end if;
if obj_description('it.mitarbeiter'::regclass, 'pg_class') is null then
raise exception 'it.mitarbeiter hat den Kommentar verloren.';
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.proname = 'change_employee_data' and p.prokind = 'f';
if v_def not like '%trim_scale(nullif(v_contract->>''weekly_hours'','''')::numeric(4,2))::text%' then
raise exception 'change_employee_data vergleicht die Wochenstunden nicht ohne angehängte Nullen.';
end if;
if v_def like '%numeric(4,1)%' then
raise exception 'change_employee_data rundet noch auf eine Nachkommastelle.';
end if;
if v_def not like '%require_hr_admin()%' then
raise exception 'change_employee_data prüft die Rechte nicht.';
end if;
if v_def not like '%search_path%' then
raise exception 'change_employee_data hat keinen festen search_path.';
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.proname = 'record_karenz_return' and p.prokind = 'f';
if v_def not like '%trim_scale(v_weekly_hours::numeric(4,2))::text%' then
raise exception 'record_karenz_return vergleicht die Wochenstunden nicht ohne angehängte Nullen.';
end if;
if v_def not like '%require_hr_admin()%' then
raise exception 'record_karenz_return prüft die Rechte nicht.';
end if;
if v_def not like '%search_path%' then
raise exception 'record_karenz_return hat keinen festen search_path.';
end if;
if (19.25)::numeric(4,2) <> 19.25 then
raise exception 'numeric(4,2) hält 19,25 nicht.';
end if;
end
$$;

View File

@@ -0,0 +1,250 @@
-- C-Level als Auswahl aus einer fachlichen Liste
--
-- C-Level ist die Führungs- bzw. Funktionsstufe einer Person, als Code:
-- C1 Bereichsleitung, C11 Mitarbeiter gfg., C2 Abteilungsleitung, C3 Experte,
-- C4 Teamleitung, C8 Lehrling, C9 Mitarbeiter, MG Manager, P Prokurist,
-- VD Vorstand. Jede Person hat eine Stufe; ein Ja/Nein sagt darüber nichts.
--
-- Neue Spalte `c_level`, freiwillig (leer = nicht erfasst), mit CHECK statt
-- Aufzählungstyp: eine Liste, die sich ändern kann, ist so eine Migration
-- mit einer Zeile — wie Mitarbeiterart und Personenkreis.
--
-- ── Was mit is_c_level geschieht ──────────────────────────────────────────
--
-- Die Spalte bleibt, die Oberfläche schreibt sie nicht mehr. Bestehende
-- Historieneinträge „C-Level: false → true" beziehen sich auf sie; damit
-- sie sich weiter berichtigen und löschen lassen, führt die Feldkarte sie
-- unter „C-Level (Ja/Nein)", und die vorhandenen Einträge tragen diese
-- Bezeichnung. „C-Level" bezeichnet ab hier die neue Spalte.
--
-- Die IT-Sicht zeigt statt des Ja/Nein den Code: ein Ja/Nein, das niemand
-- mehr pflegt, wäre dort eine falsche Auskunft.
--
-- Fünf Funktionen werden aus ihrer gespeicherten Definition an genau den
-- Stellen ergänzt, an denen is_c_level steht; jeder Anker muss genau einmal
-- vorkommen.
alter table employees add column if not exists c_level text;
do $$
begin
if not exists (select 1 from pg_constraint where conname = 'chk_c_level' and conrelid = 'public.employees'::regclass) then
alter table employees add constraint chk_c_level
check (c_level in ('C1', 'C11', 'C2', 'C3', 'C4', 'C8', 'C9', 'MG', 'P', 'VD'));
end if;
end
$$;
comment on column employees.c_level is
'Führungs-/Funktionsstufe als Code (C1 … VD); leer = nicht erfasst. is_c_level ist abgelöst und wird nicht mehr geschrieben.';
do $migration$
declare
v_def text;
v_anzahl int;
-- Ersetzt in der gespeicherten Definition von p_funktion jeden Anker durch
-- seinen Ersatz und legt die Funktion neu an. Jeder Anker muss genau einmal
-- vorkommen; sonst bricht die Migration ab, bevor etwas geändert ist.
v_funktionen text[] := array['hire_employee', 'rehire_employee', 'change_employee_data',
'apply_due_pending_changes', 'app_feld_karte'];
v_anker jsonb := jsonb_build_object(
'hire_employee', jsonb_build_array(
jsonb_build_array(
$a$is_betriebsrat, has_dienstwagen, is_laterale_fuehrung, is_c_level,$a$,
$a$is_betriebsrat, has_dienstwagen, is_laterale_fuehrung, is_c_level, c_level,$a$),
jsonb_build_array(
$a$coalesce((payload->>'is_c_level')::boolean, false),$a$,
$a$coalesce((payload->>'is_c_level')::boolean, false),
nullif(payload->>'c_level', ''),$a$)),
'rehire_employee', jsonb_build_array(
jsonb_build_array(
$a$is_c_level = coalesce((payload->>'is_c_level')::boolean, is_c_level),$a$,
$a$is_c_level = coalesce((payload->>'is_c_level')::boolean, is_c_level),
c_level = case when payload ? 'c_level' then nullif(payload->>'c_level', '') else c_level end,$a$)),
'change_employee_data', jsonb_build_array(
jsonb_build_array(
$a$is_c_level = coalesce((v_role->>'is_c_level')::boolean, is_c_level),$a$,
$a$is_c_level = coalesce((v_role->>'is_c_level')::boolean, is_c_level),
c_level = case when v_role ? 'c_level' then nullif(v_role->>'c_level', '') else c_level end,$a$),
jsonb_build_array(
$a$app_aenderung(v_contract_changes, 'C-Level', v_old.is_c_level::text, v_role->>'is_c_level'); end if;$a$,
$a$app_aenderung(v_contract_changes, 'C-Level (Ja/Nein)', v_old.is_c_level::text, v_role->>'is_c_level'); end if;
if v_role ? 'c_level' then v_contract_changes := app_aenderung(v_contract_changes, 'C-Level', v_old.c_level, nullif(v_role->>'c_level', '')); end if;$a$)),
'apply_due_pending_changes', jsonb_build_array(
jsonb_build_array(
$a$is_c_level = coalesce((v_role->>'is_c_level')::boolean, is_c_level),$a$,
$a$is_c_level = coalesce((v_role->>'is_c_level')::boolean, is_c_level),
c_level = case when v_role ? 'c_level' then nullif(v_role->>'c_level', '') else c_level end,$a$)),
'app_feld_karte', jsonb_build_array(
jsonb_build_array(
$a$'C-Level', jsonb_build_array('is_c_level', 'boolean', 'role')$a$,
$a$'C-Level (Ja/Nein)', jsonb_build_array('is_c_level', 'boolean', 'role')$a$),
jsonb_build_array(
$a$jsonb_build_array('arbeitszeitmodell', 'arbeitszeit', 'role')$a$,
$a$jsonb_build_array('arbeitszeitmodell', 'arbeitszeit', 'role'),
'C-Level', jsonb_build_array('c_level', 'text', 'role')$a$))
);
v_name text;
v_paar jsonb;
v_alt text;
begin
foreach v_name in array v_funktionen 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.proname = v_name and p.prokind = 'f';
if v_def is null then raise exception '% ist nicht vorhanden.', v_name; end if;
for v_paar in select * from jsonb_array_elements(v_anker -> v_name) loop
v_alt := v_paar ->> 0;
v_anzahl := (length(v_def) - length(replace(v_def, v_alt, ''))) / length(v_alt);
if v_anzahl <> 1 then
raise exception '%: Anker „%" kommt % mal vor, erwartet einmal.', v_name, v_alt, v_anzahl;
end if;
v_def := replace(v_def, v_alt, v_paar ->> 1);
end loop;
execute v_def;
end loop;
end
$migration$;
-- Vorhandene Historieneinträge zum Ja/Nein tragen dessen neue Bezeichnung.
-- Nur die Bezeichnung im Änderungsdetail ändert sich, nicht Datum, Werte
-- oder Reihenfolge. Die Prüfung „kein Vorgang ab Austritt" gilt neuen
-- Vorgängen; sie bleibt für diese eine Umbenennung aus.
alter table employee_history disable trigger trg_kein_vorgang_ab_austritt;
update employee_history h
set changes = (
select jsonb_agg(case when e->>'feld' = 'C-Level'
then jsonb_set(e, '{feld}', to_jsonb('C-Level (Ja/Nein)'::text))
else e end
order by i)
from jsonb_array_elements(h.changes) with ordinality t(e, i))
where jsonb_typeof(h.changes) = 'array'
and h.changes @> '[{"feld": "C-Level"}]';
alter table employee_history enable trigger trg_kein_vorgang_ab_austritt;
-- IT-Sicht: der Code statt des abgelösten Ja/Nein. Neu angelegt aus der
-- gespeicherten Definition, samt Kommentar und Leserechten.
do $sicht$
declare
v_sicht text;
v_kommentar text;
v_recht record;
v_rechte text[] := '{}';
v_anzahl int;
begin
select pg_get_viewdef('it.mitarbeiter'::regclass, true),
obj_description('it.mitarbeiter'::regclass, 'pg_class')
into v_sicht, v_kommentar;
if v_sicht is null then raise exception 'it.mitarbeiter ist nicht vorhanden.'; end if;
v_anzahl := (length(v_sicht) - length(replace(v_sicht, 'e.is_c_level,', ''))) / length('e.is_c_level,');
if v_anzahl <> 1 then
raise exception 'it.mitarbeiter: e.is_c_level kommt % mal vor, erwartet einmal.', v_anzahl;
end if;
for v_recht in
select case when a.grantee = 0 then 'public' else quote_ident(r.rolname) end as wem,
a.privilege_type as was
from pg_class c
cross join lateral aclexplode(c.relacl) a
left join pg_roles r on r.oid = a.grantee
where c.oid = 'it.mitarbeiter'::regclass
and a.grantee <> c.relowner
loop
v_rechte := v_rechte || format('grant %s on it.mitarbeiter to %s', v_recht.was, v_recht.wem);
end loop;
drop view it.mitarbeiter;
execute 'create view it.mitarbeiter as ' || replace(v_sicht, 'e.is_c_level,', 'e.c_level,');
if v_kommentar is not null then
execute format('comment on view it.mitarbeiter is %L', v_kommentar);
end if;
for i in 1 .. coalesce(array_length(v_rechte, 1), 0) loop
execute v_rechte[i];
end loop;
end
$sicht$;
-- ── Selbstprüfung ─────────────────────────────────────────────────────────
do $$
declare
v_def text;
v_name text;
begin
if not exists (select 1 from pg_constraint where conname = 'chk_c_level' and conrelid = 'public.employees'::regclass) then
raise exception 'chk_c_level fehlt.';
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.proname = 'hire_employee' and p.prokind = 'f';
if v_def not like '%is_c_level, c_level,%' or v_def not like '%nullif(payload->>''c_level'', ''''),%' then
raise exception 'hire_employee schreibt c_level nicht.';
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.proname = 'rehire_employee' and p.prokind = 'f';
if v_def not like '%c_level = case when payload ? ''c_level''%' then
raise exception 'rehire_employee schreibt c_level nicht.';
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.proname = 'change_employee_data' and p.prokind = 'f';
if v_def not like '%c_level = case when v_role ? ''c_level''%' then
raise exception 'change_employee_data schreibt c_level nicht.';
end if;
if v_def not like '%''C-Level'', v_old.c_level%' or v_def not like '%''C-Level (Ja/Nein)'', v_old.is_c_level%' then
raise exception 'change_employee_data protokolliert C-Level nicht wie vorgesehen.';
end if;
-- Was bei einer Neuerzeugung dieser Funktion verlorengehen kann.
if v_def not like '%trim_scale(nullif(v_contract->>''weekly_hours'','''')::numeric(4,2))::text%' then
raise exception 'change_employee_data hat den Vergleich der Wochenstunden verloren.';
end if;
if v_def not like '%cornerstone_id%' then
raise exception 'change_employee_data hat die Cornerstone-ID verloren.';
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.proname = 'apply_due_pending_changes' and p.prokind = 'f';
if v_def not like '%c_level = case when v_role ? ''c_level''%' then
raise exception 'apply_due_pending_changes schreibt c_level nicht.';
end if;
if app_feld_karte() -> 'C-Level' ->> 0 is distinct from 'c_level'
or app_feld_karte() -> 'C-Level (Ja/Nein)' ->> 0 is distinct from 'is_c_level' then
raise exception 'app_feld_karte führt C-Level nicht wie vorgesehen.';
end if;
foreach v_name in array array['hire_employee', 'rehire_employee', 'change_employee_data', 'apply_due_pending_changes'] 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.proname = v_name and p.prokind = 'f';
if v_name <> 'apply_due_pending_changes' and v_def not like '%require_hr_admin()%' then
raise exception '% prüft die Rechte nicht.', v_name;
end if;
if v_def not like '%search_path%' then
raise exception '% hat keinen festen search_path.', v_name;
end if;
end loop;
if exists (select 1 from employee_history
where jsonb_typeof(changes) = 'array' and changes @> '[{"feld": "C-Level"}]') then
raise exception 'Es gibt noch Historieneinträge mit der alten Bezeichnung „C-Level".';
end if;
if not exists (select 1 from pg_trigger where tgname = 'trg_kein_vorgang_ab_austritt' and tgenabled = 'O') then
raise exception 'trg_kein_vorgang_ab_austritt ist nicht wieder eingeschaltet.';
end if;
if not exists (select 1 from information_schema.columns
where table_schema = 'it' and table_name = 'mitarbeiter' and column_name = 'c_level') then
raise exception 'it.mitarbeiter zeigt c_level nicht.';
end if;
if not has_table_privilege('it_lesen', 'it.mitarbeiter', 'select') then
raise exception 'it_lesen kann it.mitarbeiter nicht mehr lesen.';
end if;
end
$$;

View File

@@ -0,0 +1,139 @@
-- Standorte mit Unterstandorten: „Shop" und die einzelnen Shops
--
-- Ein Standort kann Unterstandorte haben. Eine Person sitzt immer auf einem
-- Standort ohne Unterstandorte: „Shop" allein sagt nicht, wo jemand
-- arbeitet. Die Oberfläche bietet zuerst die Standorte ohne übergeordneten
-- an und bei einem mit Unterstandorten eine zweite Auswahl.
--
-- Der Name eines Unterstandorts nennt den übergeordneten mit („Shop W17"):
-- Liste, Berichte, Exporte und IT-Sicht lesen nur den Namen und bleiben so
-- ohne weitere Verknüpfung eindeutig.
--
-- Eine Ebene: ein Unterstandort hat keine eigenen. Zwei Trigger halten das
-- fest, auch für den Import und den Nachtlauf, die an der Oberfläche
-- vorbeigehen.
alter table locations add column if not exists parent_id uuid references locations(id);
comment on column locations.parent_id is
'Übergeordneter Standort (etwa „Shop"); leer = Standort der obersten Ebene. Personen sitzen nur auf Standorten ohne Unterstandorte.';
alter table locations drop constraint if exists chk_location_nicht_eigener_parent;
alter table locations add constraint chk_location_nicht_eigener_parent check (parent_id is null or parent_id <> id);
create index if not exists idx_locations_parent on locations (parent_id) where parent_id is not null;
-- Sitzt schon jemand auf einem Standort „Shop", fehlt die Angabe, auf welchem.
-- Raten wäre schlechter als anhalten.
do $pruefung$
begin
if exists (select 1 from employees e join locations l on l.id = e.location_id where l.name = 'Shop') then
raise exception 'Auf dem Standort „Shop" sitzen Personen. Sie brauchen zuerst einen anderen Standort; danach die Migration erneut ausführen.';
end if;
end
$pruefung$;
-- ── Regeln ──────────────────────────────────────────────────────────────
create or replace function public.standort_ebene_pruefen()
returns trigger
language plpgsql
set search_path = public, pg_temp
as $$
begin
if new.parent_id is null then
return new;
end if;
if exists (select 1 from locations where id = new.parent_id and parent_id is not null) then
raise exception 'Ein Unterstandort kann keine eigenen Unterstandorte haben.';
end if;
if exists (select 1 from locations where parent_id = new.id) then
raise exception 'Ein Standort mit Unterstandorten kann nicht selbst unter einem anderen stehen.';
end if;
if exists (select 1 from employees where location_id = new.parent_id) then
raise exception 'Auf dem Standort „%" sitzen Personen; er kann keine Unterstandorte bekommen.',
(select name from locations where id = new.parent_id);
end if;
return new;
end;
$$;
drop trigger if exists trg_standort_ebene on locations;
create trigger trg_standort_ebene
before insert or update of parent_id on locations
for each row execute function public.standort_ebene_pruefen();
create or replace function public.standort_ohne_unterstandorte()
returns trigger
language plpgsql
set search_path = public, pg_temp
as $$
begin
if new.location_id is not null and exists (select 1 from locations where parent_id = new.location_id) then
raise exception 'Der Standort „%" fasst mehrere zusammen. Bitte einen davon auswählen.',
(select name from locations where id = new.location_id);
end if;
return new;
end;
$$;
drop trigger if exists trg_standort_ohne_unterstandorte on employees;
create trigger trg_standort_ohne_unterstandorte
before insert or update of location_id on employees
for each row execute function public.standort_ohne_unterstandorte();
-- ── Shop und die einzelnen Shops ────────────────────────────────────────
insert into locations (name, country) values ('Shop', 'Österreich') on conflict (name) do nothing;
insert into locations (name, country, parent_id)
select v.name, 'Österreich', (select id from locations where name = 'Shop')
from (values ('Shop W17'), ('Shop Wodo'), ('Shop Parndorf'), ('Shop Donaupark'), ('Shop W1'), ('Shop Wien Hauptbahnhof')) v(name)
on conflict (name) do nothing;
-- Bestanden einzelne Shops schon als eigene Standorte, kommen sie unter „Shop".
update locations
set parent_id = (select id from locations where name = 'Shop')
where name in ('Shop W17', 'Shop Wodo', 'Shop Parndorf', 'Shop Donaupark', 'Shop W1', 'Shop Wien Hauptbahnhof')
and parent_id is null;
-- ── Selbstprüfung ───────────────────────────────────────────────────────
do $migration$
declare
v_shop uuid;
begin
select id into v_shop from locations where name = 'Shop';
if v_shop is null then
raise exception 'Der Standort „Shop" fehlt.';
end if;
if (select parent_id from locations where id = v_shop) is not null then
raise exception '„Shop" steht unter einem anderen Standort.';
end if;
if (select count(*) from locations where parent_id = v_shop) <> 6 then
raise exception 'Unter „Shop" stehen nicht genau die sechs Shops.';
end if;
if exists (
select 1 from (values ('Shop W17'), ('Shop Wodo'), ('Shop Parndorf'), ('Shop Donaupark'), ('Shop W1'), ('Shop Wien Hauptbahnhof')) v(name)
where not exists (select 1 from locations l where l.name = v.name and l.parent_id = v_shop)
) then
raise exception 'Ein Shop fehlt oder steht nicht unter „Shop".';
end if;
if exists (select 1 from employees e join locations l on l.parent_id = e.location_id) then
raise exception 'Eine Person sitzt auf einem Standort mit Unterstandorten.';
end if;
if (select count(*) from pg_trigger
where not tgisinternal and tgenabled = 'O'
and tgname in ('trg_standort_ebene', 'trg_standort_ohne_unterstandorte')) <> 2 then
raise exception 'Die Trigger zu den Unterstandorten fehlen.';
end if;
if exists (
select 1 from pg_proc p join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
and p.proname in ('standort_ebene_pruefen', 'standort_ohne_unterstandorte')
and (p.prosecdef or not ('search_path=public, pg_temp' = any (coalesce(p.proconfig, '{}'))))
) then
raise exception 'Die Triggerfunktionen laufen nicht unter den Rechten des Aufrufers oder ohne search_path.';
end if;
end
$migration$;

View File

@@ -60,7 +60,7 @@ declare
v_psnr text;
begin
select id into v_wurzel from org_units where parent_id is null;
select id into v_ort from locations order by name limit 1;
select l.id into v_ort from locations l where not exists (select 1 from locations u where u.parent_id = l.id) order by l.name limit 1;
select coalesce(max(personnel_number), 0) + 1 into v_nr from employees;
if v_wurzel is null or v_ort is null then
raise exception 'Kein Bestand: es fehlt die oberste Einheit oder ein Standort.';

View File

@@ -62,7 +62,7 @@ begin
raise exception 'Voraussetzung fehlt: zwei aktive und ein abgeschaltetes HR-Konto.';
end if;
select id into v_wurzel from org_units where parent_id is null;
select id into v_ort from locations order by name limit 1;
select l.id into v_ort from locations l where not exists (select 1 from locations u where u.parent_id = l.id) order by l.name limit 1;
select coalesce(max(personnel_number), 0) + 1 into v_nr from employees;
v_einheit := create_org_unit(jsonb_build_object(
@@ -253,7 +253,7 @@ declare
v_e employees%rowtype;
v_a uuid := (select id from employees where last_name = 'Test-A' and first_name = 'Vertrag');
v_p3 uuid := (select p.id from om_positions p join jobs j on j.id = p.job_id where j.title = 'Vertragstest Drei');
v_ort uuid := (select id from locations order by name limit 1);
v_ort uuid := (select l.id from locations l where not exists (select 1 from locations u where u.parent_id = l.id) order by l.name limit 1);
begin
perform apply_due_pending_changes();
select * into v_e from employees where id = v_b;