Files
alpenwerk-hr/supabase/migrations/20260714120050_pending_org_changes.sql
Maximilian Stubhan 901c5c426e Consolidation pass: HR-only access, effective-dated mutations, data integrity guards, test suite
Reworks the app from a two-role (hr_admin/manager) model to a single
HR-only role gated by profiles.is_active, fixes transfer/promote/karenz/
reorg RPCs to actually defer future-dated changes via a new
pending_org_changes table instead of writing them immediately (applied
by a daily Vercel Cron route), makes reorg undo append-only instead of
deleting history, adds Karenz-return and history-date integrity guards,
deprecates the salary column, and adds explicit schema grants + perf
indexes needed to run against a fresh (non-hosted) Postgres instance.

Adds vitest unit + integration test suites (the latter against a real
local Supabase instance) covering all of the above, plus lint/typecheck/
build wiring (`npm run check`).
2026-07-14 20:32:20 +02:00

39 lines
2.0 KiB
SQL

-- Deferred/effective-dated changes (spec §3.3)
--
-- Finding from the consolidation review: transfer_employee, promote_employee,
-- start_karenz, change_employee_data, and apply_reorg all accepted a
-- "Wirksam ab" / effective date, but only ever used it as metadata for the
-- employee_history/audit_log rows — the actual `update employees` always
-- ran immediately regardless of that date. A transfer or promotion dated
-- months in the future silently overwrote the *current* live record today.
-- This table backs the fix: when an effective date is in the future, the
-- mutating RPC stores the intended change here instead of writing it to
-- `employees` right away; a scheduled job (apply_due_pending_changes(),
-- see the following migration, invoked by a Vercel Cron route handler)
-- applies it once its date arrives. The employee_history/audit_log rows are
-- written immediately either way (dated with the effective date), which is
-- what already drives the "zukünftig" badge in the Historie tab.
create table pending_org_changes (
id uuid primary key default gen_random_uuid(),
employee_id uuid not null references employees(id) on delete cascade,
change_type text not null check (change_type in (
'transfer', 'promotion', 'karenz_start', 'karenz_return', 'contract_change', 'reorg'
)),
effective_date date not null,
payload jsonb not null,
reorg_scenario_id uuid references reorg_scenarios(id) on delete cascade,
status text not null default 'pending' check (status in ('pending', 'applied', 'cancelled')),
created_by uuid references auth.users(id),
created_at timestamptz not null default now(),
applied_at timestamptz
);
create index on pending_org_changes (employee_id);
create index on pending_org_changes (status, effective_date);
create index on pending_org_changes (reorg_scenario_id);
alter table pending_org_changes enable row level security;
create policy "pending_org_changes_hr_all" on pending_org_changes for all
using (is_hr_user()) with check (is_hr_user());