Files
alpenwerk-hr/db/migrations/20261123140000_standort_shop.sql
Raphael Müllner e09d94cb2f Ü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
2026-11-24 11:08:43 +01:00

140 lines
6.3 KiB
PL/PgSQL

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