Data model - employee_assignments records org placement over time (valid_from/valid_to), written by a trigger on `employees` rather than inside each RPC: ~70 `update employees` statements spread over fifteen migrations mean per-call bookkeeping would miss paths today and again with every future RPC. A partial unique index enforces the one-open-interval invariant the trigger relies on when closing the current row. - The Organigramm gains a Stichtag (default today). Membership comes from entry/exit/karenz, past placement from the new history, future placement projected from pending_org_changes. Placements predating the migration are backfilled with today's values and flagged as such in the UI, since employee_history only ever stored free text and cannot be reconstructed. Correctness - Reports and exports silently truncated at PostgREST's 1000-row cap (db.max_rows); employee_history is already past it at ~800 staff. Every whole-table read now pages explicitly. - XLSX date cells were a day early: ExcelJS converts a Date to an Excel serial straight off getTime(), so a Date built at local midnight lands on the previous day's serial in any positive-offset zone. - Date handling is pinned to Europe/Vienna throughout, and date-only strings are formatted without a Date round-trip. The dashboard's YTD window was built by round-tripping a local Date through toISOString(), which shifted it a day early and dropped 31 December entirely. - Export routes parsed measure/group/split/eventType with unchecked `as` casts, so an unknown value reached column headers as `undefined` and the Content-Disposition filename. Parsed against the label maps now, with the filename slugged as a backstop. - toXlsx keyed columns by header text, silently dropping the second of any two columns sharing a name — split columns take their header from data. - The org chart tree walks had no cycle guard; nothing in the schema forbids a manager_id cycle, and one would hang the tab rather than misreport. - The login page reflected ?error= verbatim, letting anyone put arbitrary text on the real sign-in screen; messages are looked up by code now. - React Flow needs elementsSelectable on, or it sets pointer-events:none on the whole node and the expand control stops responding. UI - Mobile: the shell was unusable below lg — a fixed 236px margin pushed content off-screen with no mobile navigation at all. The sidebar is now a drawer, dvh replaces vh, safe-area insets are honoured, inputs are 16px so iOS stops zooming on focus, and form grids stack. - Org chart nodes redesigned: per-kind accent stripes and icons, vacant roles called out, expand control moved to the bottom edge carrying the child count. - Pagination is windowed; it previously rendered one link per page (54 for the employee list, unbounded for the audit log). - Positions page reduced to open positions with a single "Besetzen" action. - The employee Organisation tab links into the org chart focused on that person, reusing the chart's existing search-match highlighting. Also included, uncommitted until now - Dependants, HR notes, academic titles, split address fields, position validity and role/employment fields, with their migrations and UI. - Docker/compose deployment setup, data-model and security-review docs.
121 lines
6.3 KiB
PL/PgSQL
121 lines
6.3 KiB
PL/PgSQL
-- Org-assignment history, so the Organigramm can be shown as of any date.
|
|
--
|
|
-- Until now `employees` carried only the *current* placement (manager_id,
|
|
-- team_id, division_id, job_title, is_lead, org_level) and every mutation
|
|
-- overwrote it in place. employee_history recorded that something happened,
|
|
-- but only as free text — no old/new values — so a past reporting line was
|
|
-- gone for good. Forward-looking dates already worked (pending_org_changes
|
|
-- holds not-yet-due changes structurally); it was the past that could not be
|
|
-- reconstructed. This migration adds the missing timeline.
|
|
--
|
|
-- Captured by a trigger rather than by editing the mutating RPCs: there are
|
|
-- ~70 `update employees` statements spread over fifteen migrations (several
|
|
-- of which redefine the same function repeatedly), so per-RPC bookkeeping
|
|
-- would miss paths today and again with every future RPC. A trigger on the
|
|
-- table catches all of them, including ones not written yet.
|
|
|
|
create table employee_assignments (
|
|
id uuid primary key default gen_random_uuid(),
|
|
employee_id uuid not null references employees(id) on delete cascade,
|
|
manager_id uuid references employees(id) on delete set null,
|
|
team_id uuid references teams(id),
|
|
division_id uuid not null references divisions(id),
|
|
job_title text not null,
|
|
is_lead boolean not null default false,
|
|
org_level int not null,
|
|
valid_from date not null,
|
|
-- Exclusive upper bound; null means "still in force". Note this tracks
|
|
-- *placement*, not employment: the row of someone who has left stays open,
|
|
-- because whether they were employed on a given date is derived separately
|
|
-- from entry/exit/karenz dates. Keeping the two apart is what lets a
|
|
-- rehire reuse the same open row instead of needing it reopened.
|
|
valid_to date,
|
|
created_at timestamptz not null default now(),
|
|
constraint chk_assignment_range check (valid_to is null or valid_to > valid_from)
|
|
);
|
|
|
|
create index on employee_assignments (employee_id, valid_from desc);
|
|
create index on employee_assignments (valid_from);
|
|
-- At most one open interval per employee — the invariant the trigger relies
|
|
-- on when it looks up "the current row" to close.
|
|
create unique index employee_assignments_one_open on employee_assignments (employee_id) where valid_to is null;
|
|
|
|
alter table employee_assignments enable row level security;
|
|
create policy "employee_assignments_hr_read" on employee_assignments for select using (is_hr_user());
|
|
|
|
-- ── Backfill ───────────────────────────────────────────────────────
|
|
-- One open interval per employee, starting at their entry date, holding
|
|
-- today's placement. Changes made *before* this migration are not
|
|
-- recoverable — employee_history never stored structured old values — so a
|
|
-- Stichtag before today's date shows the placement as it stands now for
|
|
-- anyone whose assignment predates this table. Everything from here on is
|
|
-- exact.
|
|
insert into employee_assignments (employee_id, manager_id, team_id, division_id, job_title, is_lead, org_level, valid_from)
|
|
select id, manager_id, team_id, division_id, job_title, is_lead, org_level, entry_date
|
|
from employees;
|
|
|
|
-- ── Trigger ────────────────────────────────────────────────────────
|
|
-- The date a change takes effect is normally the day it is written: an RPC
|
|
-- with a future effective date does not touch `employees` at all (it queues
|
|
-- into pending_org_changes, and apply_due_pending_changes writes it on the
|
|
-- due date), and one dated today writes immediately. A *backdated* change is
|
|
-- the exception — it writes immediately although it should take effect
|
|
-- earlier — so callers may set `app.effective_date` for the transaction to
|
|
-- say so explicitly; unset, it falls back to the current date.
|
|
create or replace function fn_track_employee_assignment()
|
|
returns trigger
|
|
language plpgsql
|
|
security definer
|
|
set search_path = public
|
|
as $$
|
|
declare
|
|
v_date date := coalesce(nullif(current_setting('app.effective_date', true), '')::date, current_date);
|
|
v_open_from date;
|
|
begin
|
|
if tg_op = 'INSERT' then
|
|
insert into employee_assignments (employee_id, manager_id, team_id, division_id, job_title, is_lead, org_level, valid_from)
|
|
values (new.id, new.manager_id, new.team_id, new.division_id, new.job_title, new.is_lead, new.org_level,
|
|
coalesce(new.entry_date, v_date));
|
|
return new;
|
|
end if;
|
|
|
|
if new.manager_id is not distinct from old.manager_id
|
|
and new.team_id is not distinct from old.team_id
|
|
and new.division_id is not distinct from old.division_id
|
|
and new.job_title is not distinct from old.job_title
|
|
and new.is_lead is not distinct from old.is_lead
|
|
and new.org_level is not distinct from old.org_level then
|
|
return new;
|
|
end if;
|
|
|
|
select valid_from into v_open_from
|
|
from employee_assignments where employee_id = new.id and valid_to is null;
|
|
|
|
if v_open_from is null then
|
|
insert into employee_assignments (employee_id, manager_id, team_id, division_id, job_title, is_lead, org_level, valid_from)
|
|
values (new.id, new.manager_id, new.team_id, new.division_id, new.job_title, new.is_lead, new.org_level, v_date);
|
|
elsif v_date <= v_open_from then
|
|
-- Two changes on the same day (or a backdate landing inside the open
|
|
-- interval): overwrite in place, so no zero-length or inverted interval
|
|
-- is ever stored.
|
|
update employee_assignments
|
|
set manager_id = new.manager_id, team_id = new.team_id, division_id = new.division_id,
|
|
job_title = new.job_title, is_lead = new.is_lead, org_level = new.org_level
|
|
where employee_id = new.id and valid_to is null;
|
|
else
|
|
update employee_assignments set valid_to = v_date where employee_id = new.id and valid_to is null;
|
|
insert into employee_assignments (employee_id, manager_id, team_id, division_id, job_title, is_lead, org_level, valid_from)
|
|
values (new.id, new.manager_id, new.team_id, new.division_id, new.job_title, new.is_lead, new.org_level, v_date);
|
|
end if;
|
|
|
|
return new;
|
|
end;
|
|
$$;
|
|
|
|
drop trigger if exists trg_track_employee_assignment on employees;
|
|
create trigger trg_track_employee_assignment
|
|
after insert or update on employees
|
|
for each row execute function fn_track_employee_assignment();
|
|
|
|
grant all on table employee_assignments to anon, authenticated, service_role;
|