Übergabestand 2026-10-14
Alpenwerk HR — HR-Stammdatenverwaltung Mitarbeiter:innen, Organisationsstruktur, Planstellen, Eintritt, Austritt, Versetzung, Beförderung, Karenz, On- und Offboarding, Berichte, Exporte, Massenimport und ein lückenloser Nachweis jeder Änderung. Next.js 16, React 19 und TypeScript; PostgreSQL über Kysely; Anmeldung ausschliesslich über Microsoft Entra ID. Die Fachlogik liegt in der Datenbank, die Zugriffsschranke ist Row Level Security. Betrieb auf Windows Server: siehe deploy/windows/README.md. Sicherheitskonzept: docs/sicherheitskonzept.md.
This commit is contained in:
290
db/tests/rauchtest.sql
Normal file
290
db/tests/rauchtest.sql
Normal file
@@ -0,0 +1,290 @@
|
||||
-- Rauchtest: der Lebenszyklus, einmal wirklich ausgeführt
|
||||
--
|
||||
-- psql --dbname=<MIGRATE_DATABASE_URL> -f db/tests/rauchtest.sql
|
||||
--
|
||||
-- ═══ Warum es das gibt ════════════════════════════════════════════
|
||||
--
|
||||
-- Ein Fehler, der erst beim Aufruf einer Funktion entsteht — etwa ein
|
||||
-- fehlender Umwandlungsschritt beim Status in rehire_employee —, fällt keiner
|
||||
-- anderen Prüfung der Anwendung auf: die Selbstprüfungen der Migrationen
|
||||
-- lesen den **Text** der Funktionen, die Unit-Tests laufen ohne Datenbank,
|
||||
-- und die Integrationstests haben keinen Bestand, gegen den sie liefen.
|
||||
--
|
||||
-- Diese Datei schliesst genau diese Lücke: sie **ruft** die Funktionen auf,
|
||||
-- gegen die echte Datenbank, in der Reihenfolge des Lebenszyklus.
|
||||
--
|
||||
-- ═══ Zwei Eigenschaften, ohne die es gefährlich wäre ══════════════
|
||||
--
|
||||
-- 1. **Es wird zurückgerollt.** Alles läuft in einer Transaktion, die am Ende
|
||||
-- verworfen wird. Nach dem Lauf steht kein Testmensch, keine Testplanstelle
|
||||
-- und keine Testeinheit in der Datenbank, und keine reale Person bleibt
|
||||
-- auf einer Testplanstelle zurück.
|
||||
--
|
||||
-- 2. **Es läuft als Anwendungsrolle.** psql verbindet sich als Superuser, und
|
||||
-- der geht an jeder Zeilenschutz-Regel und an require_hr_admin() vorbei.
|
||||
-- Ein Lauf als postgres würde beweisen, dass die Funktionen für niemanden
|
||||
-- funktionieren, der sie benutzt.
|
||||
--
|
||||
-- Fehlschläge brechen mit einer Meldung ab, die den Schritt nennt. Kommt der
|
||||
-- Lauf bis „Rauchtest bestanden" durch, ist der Lebenszyklus gegangen worden.
|
||||
|
||||
\set ON_ERROR_STOP on
|
||||
|
||||
begin;
|
||||
|
||||
select set_config(
|
||||
'app.user_id',
|
||||
(select p.id::text from profiles p where p.role = 'hr' and p.is_active limit 1),
|
||||
true
|
||||
) as angemeldet_als;
|
||||
|
||||
set local role alpenwerk_app;
|
||||
|
||||
do $rauchtest$
|
||||
declare
|
||||
v_wurzel uuid;
|
||||
v_einheit uuid;
|
||||
v_p1 uuid; v_p2 uuid; v_p3 uuid; v_p4 uuid;
|
||||
v_a uuid; v_b uuid;
|
||||
v_ort uuid;
|
||||
v_nr int;
|
||||
v_status text;
|
||||
v_stelle uuid;
|
||||
v_offen int;
|
||||
v_meldung text;
|
||||
v_ging boolean;
|
||||
v_titel 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 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.';
|
||||
end if;
|
||||
|
||||
-- ── Spielwiese ──────────────────────────────────────────────────
|
||||
v_einheit := create_org_unit(jsonb_build_object(
|
||||
'parent_id', v_wurzel, 'org_number', 'RAUCHTEST', 'name', 'Rauchtest',
|
||||
'unit_type', 'Team', 'valid_from', (current_date - 30)::text));
|
||||
|
||||
v_p1 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
'job_title', 'Rauchtest Eins', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
||||
v_p2 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
'job_title', 'Rauchtest Zwei', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
||||
v_p3 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
'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/10 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/10 Einstellung auf Planstelle 1';
|
||||
|
||||
-- ── Versetzung am selben Tag (20260929180000) ───────────────────
|
||||
-- Die Besetzung begann heute; sie auf heute zu schliessen wäre ein
|
||||
-- Intervall von null Tagen, das chk_assignment_range abweisen würde.
|
||||
perform transfer_employee(jsonb_build_object(
|
||||
'employee_id', v_a, 'target_position_id', v_p2, 'effective_date', current_date::text));
|
||||
|
||||
select position_id into v_stelle from position_assignments
|
||||
where employee_id = v_a and valid_to is null;
|
||||
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/10 Versetzung am selben Tag';
|
||||
|
||||
-- ── Beförderung mit Planstellenwechsel, ebenfalls heute ─────────
|
||||
perform promote_employee(jsonb_build_object(
|
||||
'employee_id', v_a, 'effective_date', current_date::text,
|
||||
'new_title', 'Rauchtest Leitung', 'new_paygrade', 'HG15',
|
||||
'target_position_id', v_p3));
|
||||
|
||||
select position_id into v_stelle from position_assignments
|
||||
where employee_id = v_a and valid_to is null;
|
||||
if v_stelle is distinct from v_p3 then
|
||||
raise exception 'Nach der Beförderung sitzt die Person nicht auf Planstelle 3.';
|
||||
end if;
|
||||
if (select paygrade::text from employees where id = v_a) <> 'HG15' then
|
||||
raise exception 'Die Beförderung hat den Hay-Grade nicht gesetzt.';
|
||||
end if;
|
||||
|
||||
-- Die Bezeichnung gehört zur Planstelle: dort lesen sie Akte, Organigramm
|
||||
-- und die Exporte. Stünde der neue Titel allein an der Person, wäre er
|
||||
-- nirgends zu sehen.
|
||||
select j.title into v_titel
|
||||
from om_positions p join jobs j on j.id = p.job_id where p.id = v_p3;
|
||||
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/10 Beförderung am selben Tag: Hay-Grade und Planstellenbezeichnung';
|
||||
|
||||
-- Dieselbe Planstelle, nur ein neuer Name.
|
||||
perform promote_employee(jsonb_build_object(
|
||||
'employee_id', v_a, 'effective_date', current_date::text,
|
||||
'new_title', 'Rauchtest Leitung Senior', 'new_paygrade', 'HG16'));
|
||||
|
||||
select j.title into v_titel
|
||||
from om_positions p join jobs j on j.id = p.job_id where p.id = v_p3;
|
||||
if v_titel <> 'Rauchtest Leitung Senior' then
|
||||
raise exception 'Ohne Stellenwechsel heisst die Planstelle „%", erwartet „Rauchtest Leitung Senior".', v_titel;
|
||||
end if;
|
||||
|
||||
-- Der Katalogeintrag der alten Bezeichnung bleibt stehen: an ihm können
|
||||
-- andere Planstellen hängen. Umbenannt wird die Stelle, nicht der Katalog.
|
||||
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/10 Beförderung ohne Stellenwechsel benennt dieselbe Planstelle um';
|
||||
|
||||
-- ── Vorgemerkte Planstelle (20260929200000) ─────────────────────
|
||||
-- Eine Versetzung in die Zukunft belegt die Zielstelle, obwohl dort noch
|
||||
-- keine Besetzung steht. Wer sie vorher besetzt, bringt den Nachtlauf zum
|
||||
-- Stehen — deshalb wird das abgewiesen.
|
||||
perform transfer_employee(jsonb_build_object(
|
||||
'employee_id', v_a, 'target_position_id', v_p4, 'effective_date', (current_date + 10)::text));
|
||||
|
||||
-- Der Fehlschlag wird **ausserhalb** des Blocks ausgewertet. Ein `raise`
|
||||
-- innerhalb landet im eigenen exception-Zweig — und enthält seine Meldung
|
||||
-- das Wort „vorgemerkte", bestünde der Schritt auch dann, wenn die
|
||||
-- Migration gar nicht angewendet ist. Ein Test, der sich selbst
|
||||
-- durchwinkt, ist schlimmer als keiner.
|
||||
v_ging := false;
|
||||
begin
|
||||
perform hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 1, 'first_name', 'Rauch', 'last_name', 'Test-B',
|
||||
'gender', 'm', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
||||
'position_id', v_p4, 'entry_date', current_date::text));
|
||||
v_ging := true;
|
||||
exception
|
||||
when others then v_meldung := SQLERRM;
|
||||
end;
|
||||
|
||||
if v_ging then
|
||||
raise exception 'Die vorgemerkte Planstelle liess sich besetzen — die Pruefung greift nicht.';
|
||||
end if;
|
||||
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/10 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/10 Die eigene Vormerkung steht nicht im Weg';
|
||||
|
||||
-- ── Austritt am selben Tag (20260929220000) ─────────────────────
|
||||
perform terminate_employee(jsonb_build_object(
|
||||
'employee_id', v_a, 'exit_date', current_date::text,
|
||||
'exit_reason', 'Einvernehmliche Auflösung', 'austrittsart', 'freiwillig'));
|
||||
|
||||
select count(*) into v_offen from position_assignments
|
||||
where employee_id = v_a and valid_to is null;
|
||||
if v_offen <> 0 then
|
||||
raise exception 'Nach dem Austritt ist die Planstelle nicht frei.';
|
||||
end if;
|
||||
raise notice '7/10 Austritt am selben Tag, Planstelle frei';
|
||||
|
||||
-- ── Wiedereintritt (20260929160000) ─────────────────────────────
|
||||
perform rehire_employee(jsonb_build_object(
|
||||
'employee_id', v_a, 'rehire_date', current_date::text, 'position_id', v_p1,
|
||||
'first_name', 'Rauch', 'last_name', 'Test-A', 'gender', 'w',
|
||||
'birth_date', '1990-01-01', 'nationality', 'Österreich', 'location_id', v_ort));
|
||||
|
||||
select status::text into v_status from employees where id = v_a;
|
||||
if v_status <> 'Aktiv' then
|
||||
raise exception 'Nach dem Wiedereintritt steht der Status auf %, erwartet Aktiv.', v_status;
|
||||
end if;
|
||||
select position_id into v_stelle from position_assignments
|
||||
where employee_id = v_a and valid_to is null;
|
||||
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/10 Wiedereintritt';
|
||||
|
||||
-- ── Rückdatierte Versetzung (20261013140000) ────────────────────
|
||||
--
|
||||
-- Eine Person darf nie zweimal auf derselben Planstelle sitzen. Hergang:
|
||||
-- eine Versetzung auf heute schliesst die laufende Besetzung auf heute;
|
||||
-- eine zweite, auf einen vergangenen Tag datierte Versetzung darf danach
|
||||
-- nicht nur aufräumen, was **offen** ist — sonst bleibt die eben
|
||||
-- geschlossene Zeile stehen und überlappt mit der neuen.
|
||||
--
|
||||
-- Hier gebraucht wird jemand, dessen Besetzung in der Vergangenheit
|
||||
-- beginnt — bei einer heute begonnenen greift schon das delete, und der
|
||||
-- Fehler zeigte sich nicht.
|
||||
v_b := hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 2, 'first_name', 'Rauch', 'last_name', 'Test-C',
|
||||
'gender', 'm', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
||||
'position_id', v_p2, 'entry_date', (current_date - 10)::text));
|
||||
|
||||
perform transfer_employee(jsonb_build_object(
|
||||
'employee_id', v_b, 'target_position_id', v_p3, 'effective_date', current_date::text));
|
||||
|
||||
-- Zurück auf die alte Stelle, datiert auf den Eintrittstag — die
|
||||
-- Berichtigung einer versehentlich erfassten Versetzung.
|
||||
perform transfer_employee(jsonb_build_object(
|
||||
'employee_id', v_b, 'target_position_id', v_p2, 'effective_date', (current_date - 10)::text));
|
||||
|
||||
select count(*) into v_offen from position_assignments where employee_id = v_b;
|
||||
if v_offen <> 1 then
|
||||
raise exception 'Nach der rückdatierten Versetzung stehen % Besetzungen, erwartet genau eine.', v_offen;
|
||||
end if;
|
||||
select position_id into v_stelle from position_assignments
|
||||
where employee_id = v_b and valid_to is null;
|
||||
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/10 Rückdatierte Versetzung lässt keine zweite Besetzung stehen';
|
||||
|
||||
-- ── Versetzung bei datiertem Austritt (20261013140000) ──────────
|
||||
--
|
||||
-- Die zweite Hälfte derselben Regel. Ein Austritt in der Zukunft
|
||||
-- befristet die Besetzung auf den Austrittstag. Suchte eine spätere
|
||||
-- Versetzung nur nach „valid_to is null", träfe sie nichts und fügte
|
||||
-- trotzdem ein — zwei gleichzeitige Besetzungen. Und die neue liefe
|
||||
-- unbefristet weiter, obwohl die Person das Haus verlässt.
|
||||
perform terminate_employee(jsonb_build_object(
|
||||
'employee_id', v_b, 'exit_date', (current_date + 20)::text,
|
||||
'exit_reason', 'Einvernehmliche Auflösung', 'austrittsart', 'freiwillig'));
|
||||
|
||||
perform transfer_employee(jsonb_build_object(
|
||||
'employee_id', v_b, 'target_position_id', v_p3, 'effective_date', current_date::text));
|
||||
|
||||
select count(*) into v_offen from position_assignments where employee_id = v_b;
|
||||
if v_offen <> 2 then
|
||||
raise exception 'Nach der Versetzung stehen % Besetzungen, erwartet zwei aufeinanderfolgende.', v_offen;
|
||||
end if;
|
||||
|
||||
-- Keine offene Besetzung mehr: die neue endet mit der Person.
|
||||
select count(*) into v_offen from position_assignments
|
||||
where employee_id = v_b and valid_to is null;
|
||||
if v_offen <> 0 then
|
||||
raise exception 'Die neue Besetzung läuft unbefristet weiter, obwohl ein Austritt datiert ist.';
|
||||
end if;
|
||||
|
||||
select count(*) into v_offen from position_assignments
|
||||
where employee_id = v_b and position_id = v_p3
|
||||
and valid_from = current_date and valid_to = current_date + 20;
|
||||
if v_offen <> 1 then
|
||||
raise exception 'Die neue Besetzung endet nicht am Austrittstag.';
|
||||
end if;
|
||||
raise notice '10/10 Versetzung bei datiertem Austritt: kein Überlappen, kein Überleben';
|
||||
|
||||
raise notice '';
|
||||
raise notice 'Rauchtest bestanden. Alles Angelegte wird gleich verworfen.';
|
||||
end
|
||||
$rauchtest$;
|
||||
|
||||
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 employees where last_name like 'Test-%' and first_name = 'Rauch') as personen,
|
||||
(select count(*) from jobs where title like 'Rauchtest %') as taetigkeiten;
|
||||
284
db/tests/vertragsangaben.sql
Normal file
284
db/tests/vertragsangaben.sql
Normal file
@@ -0,0 +1,284 @@
|
||||
-- Rauchtest: VL, Bonusanspruch, zuständige HR, erlernter Beruf und die Kollektivverträge
|
||||
--
|
||||
-- psql -f db/tests/vertragsangaben.sql
|
||||
--
|
||||
-- Dieselben zwei Eigenschaften wie db/tests/rauchtest.sql: alles läuft in
|
||||
-- einer Transaktion, die am Ende verworfen wird, und es läuft als
|
||||
-- Anwendungsrolle — ein Lauf als Superuser ginge an require_hr_admin() und
|
||||
-- am Zeilenschutz vorbei und bewiese nichts.
|
||||
--
|
||||
-- Die Migrationen 20261007090000 und 20261007100000 prüfen sich selbst, aber
|
||||
-- nur am **Text** der Funktionen. Hier werden sie aufgerufen: Einstellung,
|
||||
-- „Daten ändern" sofort und datiert, der Nachtlauf, Löschen und Berichtigen
|
||||
-- in der Historie, Austritt und Wiedereintritt.
|
||||
--
|
||||
-- Voraussetzung: zwei aktive HR-Konten und ein abgeschaltetes, ein Standort.
|
||||
|
||||
\set ON_ERROR_STOP on
|
||||
|
||||
begin;
|
||||
|
||||
select set_config(
|
||||
'app.user_id',
|
||||
(select p.id::text from profiles p where p.role = 'hr' and p.is_active order by p.id limit 1),
|
||||
true
|
||||
) as angemeldet_als;
|
||||
|
||||
-- Die Kennungen der Konten für den Test, vor dem Rollenwechsel gelesen.
|
||||
create temp table hr_konten on commit drop as
|
||||
select
|
||||
(select id from profiles where role = 'hr' and is_active order by id limit 1) as hr1,
|
||||
(select id from profiles where role = 'hr' and is_active order by id offset 1 limit 1) as hr2,
|
||||
(select id from profiles where role = 'hr' and not is_active order by id limit 1) as hr_aus;
|
||||
grant select on hr_konten to alpenwerk_app;
|
||||
|
||||
set local role alpenwerk_app;
|
||||
|
||||
do $test$
|
||||
declare
|
||||
v_hr1 uuid; v_hr2 uuid; v_hr_aus uuid;
|
||||
v_wurzel uuid; v_einheit uuid; v_ort uuid;
|
||||
v_p1 uuid; v_p2 uuid; v_p3 uuid;
|
||||
v_nr int;
|
||||
v_a uuid; v_b uuid;
|
||||
v_e employees%rowtype;
|
||||
v_hist uuid;
|
||||
v_changes jsonb;
|
||||
v_meldung text;
|
||||
v_ging boolean;
|
||||
begin
|
||||
select hr1, hr2, hr_aus into v_hr1, v_hr2, v_hr_aus from hr_konten;
|
||||
if v_hr1 is null or v_hr2 is null or v_hr_aus is null then
|
||||
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 coalesce(max(personnel_number), 0) + 1 into v_nr from employees;
|
||||
|
||||
v_einheit := create_org_unit(jsonb_build_object(
|
||||
'parent_id', v_wurzel, 'org_number', 'VERTRAGSTEST', 'name', 'Vertragstest',
|
||||
'unit_type', 'Team', 'valid_from', (current_date - 30)::text));
|
||||
v_p1 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
'job_title', 'Vertragstest Eins', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
||||
v_p2 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
'job_title', 'Vertragstest Zwei', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
||||
v_p3 := create_position(jsonb_build_object('org_unit_id', v_einheit,
|
||||
'job_title', 'Vertragstest Drei', 'is_chief', false, 'valid_from', (current_date - 30)::text));
|
||||
|
||||
-- ── 1. Einstellung mit allen Vertragsangaben ───────────────────
|
||||
v_a := hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr, 'first_name', 'Vertrag', 'last_name', 'Test-A',
|
||||
'gender', 'w', 'birth_date', '1990-01-01', 'location_id', v_ort,
|
||||
'position_id', v_p1, 'entry_date', (current_date - 10)::text,
|
||||
'worker_type', 'Arbeiter:in', 'collective_agreement', 'Süßware Arbeiter',
|
||||
'vl', 3, 'has_bonusanspruch', true, 'bonusanspruch_art', 'Shop',
|
||||
'zustaendige_hr_id', v_hr1, 'erlernter_beruf', ' Konditorin '));
|
||||
select * into v_e from employees where id = v_a;
|
||||
if v_e.erlernter_beruf is distinct from 'Konditorin' then
|
||||
raise exception '1: Erlernter Beruf ist „%", erwartet getrimmt „Konditorin".', v_e.erlernter_beruf;
|
||||
end if;
|
||||
if v_e.vl <> 3 or not v_e.has_bonusanspruch or v_e.bonusanspruch_art <> 'Shop'
|
||||
or v_e.zustaendige_hr_id <> v_hr1 or v_e.collective_agreement::text <> 'Süßware Arbeiter' then
|
||||
raise exception '1: Einstellung hat VL/Bonus/HR/KV nicht geschrieben: % % % % %',
|
||||
v_e.vl, v_e.has_bonusanspruch, v_e.bonusanspruch_art, v_e.zustaendige_hr_id, v_e.collective_agreement;
|
||||
end if;
|
||||
raise notice '1/9 Einstellung mit VL 3, Bonus Shop, zuständiger HR, Süßware Arbeiter';
|
||||
|
||||
-- ── 2. Einstellung ohne Angaben: die Vorgaben ──────────────────
|
||||
v_b := hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 1, 'first_name', 'Vertrag', 'last_name', 'Test-B',
|
||||
'gender', 'm', 'birth_date', '1985-05-05', 'location_id', v_ort,
|
||||
'position_id', v_p2, 'entry_date', current_date::text, 'worker_type', 'Angestellte:r'));
|
||||
select * into v_e from employees where id = v_b;
|
||||
if v_e.vl is not null or v_e.has_bonusanspruch or v_e.bonusanspruch_art is not null or v_e.zustaendige_hr_id is not null then
|
||||
raise exception '2: Ohne Angaben müssen VL, Bonus und HR leer bzw. Nein sein.';
|
||||
end if;
|
||||
if v_e.collective_agreement::text <> 'Süßware Angestellter' then
|
||||
raise exception '2: Vorgabe-Kollektivvertrag für Angestellte ist „%", erwartet „Süßware Angestellter".', v_e.collective_agreement;
|
||||
end if;
|
||||
raise notice '2/9 Vorgaben: VL leer, Bonus Nein, HR leer, KV nach Beschäftigtengruppe';
|
||||
|
||||
-- ── 3. Abweisungen mit lesbarer Meldung ────────────────────────
|
||||
begin
|
||||
perform hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 2, 'first_name', 'X', 'last_name', 'Y', 'gender', 'm',
|
||||
'birth_date', '1980-01-01', 'location_id', v_ort, 'position_id', v_p3,
|
||||
'entry_date', current_date::text, 'has_bonusanspruch', true));
|
||||
v_ging := true;
|
||||
exception when others then v_ging := false; v_meldung := sqlerrm;
|
||||
end;
|
||||
if v_ging or v_meldung not like '%Bonusregelung%' then
|
||||
raise exception '3: Bonus Ja ohne Regelung wurde nicht verständlich abgewiesen: %', v_meldung;
|
||||
end if;
|
||||
|
||||
begin
|
||||
perform hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 2, 'first_name', 'X', 'last_name', 'Y', 'gender', 'm',
|
||||
'birth_date', '1980-01-01', 'location_id', v_ort, 'position_id', v_p3,
|
||||
'entry_date', current_date::text, 'zustaendige_hr_id', v_hr_aus));
|
||||
v_ging := true;
|
||||
exception when others then v_ging := false; v_meldung := sqlerrm;
|
||||
end;
|
||||
if v_ging or v_meldung not like '%zuständige HR%' then
|
||||
raise exception '3: Abgeschaltetes HR-Konto wurde nicht abgewiesen: %', v_meldung;
|
||||
end if;
|
||||
|
||||
begin
|
||||
perform hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 2, 'first_name', 'X', 'last_name', 'Y', 'gender', 'm',
|
||||
'birth_date', '1980-01-01', 'location_id', v_ort, 'position_id', v_p3,
|
||||
'entry_date', current_date::text, 'collective_agreement', 'Süßwaren'));
|
||||
v_ging := true;
|
||||
exception when others then v_ging := false;
|
||||
end;
|
||||
if v_ging then
|
||||
raise exception '3: Der alte Kollektivvertrag „Süßwaren" wurde angenommen.';
|
||||
end if;
|
||||
|
||||
begin
|
||||
perform hire_employee(jsonb_build_object(
|
||||
'personnel_number', v_nr + 2, 'first_name', 'X', 'last_name', 'Y', 'gender', 'm',
|
||||
'birth_date', '1980-01-01', 'location_id', v_ort, 'position_id', v_p3,
|
||||
'entry_date', current_date::text, 'vl', 9));
|
||||
v_ging := true;
|
||||
exception when others then v_ging := false;
|
||||
end;
|
||||
if v_ging then
|
||||
raise exception '3: VL 9 wurde angenommen.';
|
||||
end if;
|
||||
raise notice '3/9 Abgewiesen: Bonus ohne Regelung, abgeschaltete HR, „Süßwaren", VL 9';
|
||||
|
||||
-- ── 4. Daten ändern, sofort ────────────────────────────────────
|
||||
perform change_employee_data(jsonb_build_object(
|
||||
'employee_id', v_a, 'effective_date', current_date::text,
|
||||
'person', jsonb_build_object('zustaendige_hr_id', v_hr2, 'erlernter_beruf', ' '),
|
||||
'contract', jsonb_build_object('vl', '5', 'has_bonusanspruch', false, 'bonusanspruch_art', ''),
|
||||
'role', jsonb_build_object('collective_agreement', 'Handel')));
|
||||
select * into v_e from employees where id = v_a;
|
||||
if v_e.vl <> 5 or v_e.has_bonusanspruch or v_e.bonusanspruch_art is not null
|
||||
or v_e.zustaendige_hr_id <> v_hr2 or v_e.collective_agreement::text <> 'Handel' then
|
||||
raise exception '4: Daten ändern hat nicht geschrieben: % % % % %',
|
||||
v_e.vl, v_e.has_bonusanspruch, v_e.bonusanspruch_art, v_e.zustaendige_hr_id, v_e.collective_agreement;
|
||||
end if;
|
||||
|
||||
-- Die Historie nennt jede Änderung mit vorher und nachher.
|
||||
select id, changes into v_hist, v_changes from employee_history
|
||||
where employee_id = v_a and event_type = 'Vertragsänderung' order by created_at desc limit 1;
|
||||
if not (v_changes @> '[{"feld":"VL","vorher":"3","nachher":"5"}]'
|
||||
and v_changes @> '[{"feld":"Bonusanspruch","vorher":"true","nachher":"false"}]'
|
||||
and v_changes @> '[{"feld":"Bonusregelung","vorher":"Shop","nachher":null}]') then
|
||||
raise exception '4: Historie der Vertragsänderung unvollständig: %', v_changes;
|
||||
end if;
|
||||
if not exists (select 1 from employee_history where employee_id = v_a and event_type = 'Stammdatenänderung'
|
||||
and changes @> jsonb_build_array(jsonb_build_object('feld', 'Zuständige HR', 'vorher', v_hr1::text, 'nachher', v_hr2::text))) then
|
||||
raise exception '4: Die Historie nennt den Wechsel der zuständigen HR nicht.';
|
||||
end if;
|
||||
if v_e.erlernter_beruf is not null then
|
||||
raise exception '4: Ein Leerzeichen als erlernter Beruf wurde nicht zu leer.';
|
||||
end if;
|
||||
if not exists (select 1 from employee_history where employee_id = v_a and event_type = 'Stammdatenänderung'
|
||||
and changes @> '[{"feld":"Erlernter Beruf","vorher":"Konditorin","nachher":null}]') then
|
||||
raise exception '4: Die Historie nennt das Leeren des erlernten Berufs nicht.';
|
||||
end if;
|
||||
raise notice '4/9 Daten ändern sofort: VL 3→5, Bonus Ja→Nein, HR gewechselt, KV Handel — alles in der Historie';
|
||||
|
||||
-- ── 5. Eintrag löschen und zurücksetzen ───────────────────────
|
||||
perform delete_history_entry(jsonb_build_object('history_id', v_hist));
|
||||
select * into v_e from employees where id = v_a;
|
||||
if v_e.vl <> 3 or not v_e.has_bonusanspruch or v_e.bonusanspruch_art <> 'Shop' then
|
||||
raise exception '5: Zurücksetzen hat VL/Bonus nicht wiederhergestellt: % % %', v_e.vl, v_e.has_bonusanspruch, v_e.bonusanspruch_art;
|
||||
end if;
|
||||
raise notice '5/9 Löschen und zurücksetzen: wieder VL 3, Bonus Ja · Shop';
|
||||
|
||||
-- ── 6. Bonus Ja ohne Regelung über Daten ändern ───────────────
|
||||
perform change_employee_data(jsonb_build_object(
|
||||
'employee_id', v_b, 'effective_date', current_date::text,
|
||||
'person', '{}'::jsonb, 'contract', jsonb_build_object('vl', '1'), 'role', '{}'::jsonb));
|
||||
begin
|
||||
perform change_employee_data(jsonb_build_object(
|
||||
'employee_id', v_b, 'effective_date', current_date::text,
|
||||
'person', '{}'::jsonb, 'contract', jsonb_build_object('has_bonusanspruch', true, 'bonusanspruch_art', ''), 'role', '{}'::jsonb));
|
||||
v_ging := true;
|
||||
exception when others then v_ging := false; v_meldung := sqlerrm;
|
||||
end;
|
||||
if v_ging or v_meldung not like '%Bonusregelung%' then
|
||||
raise exception '6: Daten ändern nahm Bonus Ja ohne Regelung an: %', v_meldung;
|
||||
end if;
|
||||
raise notice '6/9 Daten ändern weist Bonus Ja ohne Regelung verständlich ab';
|
||||
|
||||
-- ── 7. Berichtigen (smallint aus der Feldkarte) ───────────────
|
||||
select id into v_hist from employee_history
|
||||
where employee_id = v_b and event_type = 'Vertragsänderung' order by created_at desc limit 1;
|
||||
perform update_history_entry(jsonb_build_object('history_id', v_hist,
|
||||
'werte', jsonb_build_array(jsonb_build_object('feld', 'VL', 'nachher', '2'))));
|
||||
if (select vl from employees where id = v_b) <> 2 then
|
||||
raise exception '7: Berichtigen hat VL nicht auf 2 gesetzt.';
|
||||
end if;
|
||||
raise notice '7/9 Berichtigen in der Historie: VL 1 → 2';
|
||||
|
||||
-- ── 8. Datiert ändern und der Nachtlauf ───────────────────────
|
||||
perform change_employee_data(jsonb_build_object(
|
||||
'employee_id', v_b, 'effective_date', (current_date + 5)::text,
|
||||
'person', jsonb_build_object('zustaendige_hr_id', v_hr1),
|
||||
'contract', jsonb_build_object('vl', '7', 'has_bonusanspruch', true, 'bonusanspruch_art', 'Außendienst'),
|
||||
'role', '{}'::jsonb));
|
||||
select * into v_e from employees where id = v_b;
|
||||
if v_e.vl <> 2 or v_e.has_bonusanspruch or v_e.zustaendige_hr_id is not null then
|
||||
raise exception '8: Eine datierte Änderung wurde sofort geschrieben.';
|
||||
end if;
|
||||
end
|
||||
$test$;
|
||||
|
||||
-- Den Stichtag der vorgemerkten Änderung auf heute ziehen — als Eigentümer,
|
||||
-- die Anwendungsrolle darf pending_org_changes nicht so ändern.
|
||||
reset role;
|
||||
update pending_org_changes set effective_date = current_date
|
||||
where status = 'pending' and employee_id = (select id from employees where last_name = 'Test-B' and first_name = 'Vertrag');
|
||||
set local role alpenwerk_app;
|
||||
|
||||
do $test2$
|
||||
declare
|
||||
v_b uuid := (select id from employees where last_name = 'Test-B' and first_name = 'Vertrag');
|
||||
v_hr1 uuid := (select hr1 from hr_konten);
|
||||
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);
|
||||
begin
|
||||
perform apply_due_pending_changes();
|
||||
select * into v_e from employees where id = v_b;
|
||||
if v_e.vl <> 7 or not v_e.has_bonusanspruch or v_e.bonusanspruch_art <> 'Außendienst' or v_e.zustaendige_hr_id <> v_hr1 then
|
||||
raise exception '8: Der Nachtlauf hat VL/Bonus/HR nicht angewendet: % % % %',
|
||||
v_e.vl, v_e.has_bonusanspruch, v_e.bonusanspruch_art, v_e.zustaendige_hr_id;
|
||||
end if;
|
||||
raise notice '8/9 Datierte Änderung: erst nichts, nach dem Nachtlauf VL 7, Bonus Außendienst, HR gesetzt';
|
||||
|
||||
-- ── 9. Austritt und Wiedereintritt ────────────────────────────
|
||||
perform terminate_employee(jsonb_build_object(
|
||||
'employee_id', v_a, 'exit_date', current_date::text,
|
||||
'exit_reason', 'Einvernehmliche Auflösung', 'austrittsart', 'freiwillig'));
|
||||
perform rehire_employee(jsonb_build_object(
|
||||
'employee_id', v_a, 'rehire_date', current_date::text, 'position_id', v_p3,
|
||||
'first_name', 'Vertrag', 'last_name', 'Test-A', 'gender', 'w',
|
||||
'birth_date', '1990-01-01', 'nationality', 'Österreich', 'location_id', v_ort,
|
||||
'vl', 8, 'has_bonusanspruch', false, 'zustaendige_hr_id', v_hr1,
|
||||
'collective_agreement', 'Süßware Angestellter', 'erlernter_beruf', 'Bäckerin'));
|
||||
select * into v_e from employees where id = v_a;
|
||||
if v_e.vl <> 8 or v_e.has_bonusanspruch or v_e.bonusanspruch_art is not null
|
||||
or v_e.zustaendige_hr_id <> v_hr1 or v_e.collective_agreement::text <> 'Süßware Angestellter'
|
||||
or v_e.erlernter_beruf is distinct from 'Bäckerin' then
|
||||
raise exception '9: Wiedereintritt hat VL/Bonus/HR/KV nicht übernommen: % % % % %',
|
||||
v_e.vl, v_e.has_bonusanspruch, v_e.bonusanspruch_art, v_e.zustaendige_hr_id, v_e.collective_agreement;
|
||||
end if;
|
||||
raise notice '9/9 Wiedereintritt übernimmt VL 8, Bonus Nein, HR und Süßware Angestellter';
|
||||
|
||||
raise notice 'Vertragsangaben-Rauchtest bestanden. Alles Angelegte wird gleich verworfen.';
|
||||
end
|
||||
$test2$;
|
||||
|
||||
rollback;
|
||||
|
||||
-- Gegenprobe: nichts ist geblieben.
|
||||
select
|
||||
(select count(*) from org_units where org_number = 'VERTRAGSTEST') as einheiten,
|
||||
(select count(*) from employees where first_name = 'Vertrag') as personen;
|
||||
Reference in New Issue
Block a user