Files
accounted/supabase/migrations/20260703200000_account_dimension_rules.sql
T
Jakob Wennberg 764348e99c feat(dimensions): PR10 advanced — custom dimensions, hierarchy, account rules, commit enforcement (#886)
* feat(dimensions): PR10 advanced — custom dimensions, hierarchy, account rules, commit enforcement

The final rung of the dimensions ladder
(dev_docs/dimensions_implementation_plan.md §7 row 10):

- custom dimensions: POST /api/dimensions creates registry dims (next free
  SIE number >= 20 when omitted; explicit numbers allowed — SIE import
  already mints reserved ones); register gets a 'Ny dimension' dialog with
  a quiet Avancerat disclosure for the #UNDERDIM parent; GET now carries
  parent_sie_dim_no (the column + SIE round-trip existed since PR1/PR5 —
  this exposes it)
- account_dimension_rules (migration 20260703120000): one rule per
  (account, dimension) — required / default / fixed, per-rule is_active,
  company-scoped RLS, composite FK to the registry, value-presence CHECK
- enforcement, opt-in BY CONSTRUCTION (zero rules = engine byte-identical;
  deliberately NO settings toggle — a rule that exists but is ignored is
  worse than either extreme): default/fixed apply onto line bags at draft
  creation (fixed overwrites, default fills); required asserts at
  commitEntry with a Swedish MANDATORY_DIMENSION_MISSING naming every
  account + dimension; the bulk-book route runs the same policy before its
  RPC; storno/correction paths never pass through commitEntry so history
  always reverses regardless of policy; rule fetches fail open incl.
  thrown exceptions
- chart of accounts: per-account Dimensionsregler section in
  EditAccountDialog (Krävs/Förval/Låst, value picker, pause switch),
  gated on the existing dimensions toggle, quiet when empty
- pickers: LineDimensionFields is registry-driven (one combobox per active
  dimension, cached fetch, hardcoded 1/6 fallback) — every existing mount
  lights up custom dims with zero changes
- agent briefing: per-dimension required_on_accounts/default_on_accounts
  so agents self-correct instead of bouncing off the policy error
- rules CRUD API with existence/active/company validation and qualified
  DTO ids; firm_id FK deferred until the firms table lands (per plan)

39 new tests (pure-fn rules, engine enforcement, both new API surfaces,
pg-real RLS/CHECK/cascade suite); full suite 6,791 green; migration
replayed on a fresh container.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: renumber migration to 20260703200000 — version collision with prod

The concurrent session shipped pending_operations_add_link_document_to_voucher
as 20260703120000 today; the Supabase preview branch (cloned from prod)
rejected the duplicate version key.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: review round — auto-pick retry on collision, fail-open warnings, query schema

- POST /api/dimensions retries once past a concurrent number claim when the
  number was auto-picked (explicit choices still 409)
- every fail-open skip of the dimension-rules policy now logs a structured
  warning (engine draft/commit paths + bulk-book) — deliberate fail-open,
  but observable
- GET /api/dimensions/rules validates its query through
  ListDimensionRulesQuerySchema instead of an inline regex

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 16:50:28 +02:00

84 lines
4.2 KiB
SQL

-- Dimensions PR10 (advanced): per-account dimension policy.
--
-- account_dimension_rules — one rule per (account, dimension):
-- 'required' the account cannot be POSTED without a value for the
-- dimension (enforced TS-side at commitEntry + the bulk-book
-- route pre-check; drafts may be incomplete)
-- 'default' the value is pre-applied to the line's bag at draft
-- creation when the key is absent (user-overridable)
-- 'fixed' the value is ALWAYS applied at draft creation (overwrites
-- whatever the caller sent for that key)
--
-- Opt-in by construction: zero rows (every company's default) = the engine
-- behaves exactly as before. There is deliberately NO settings toggle for
-- enforcement — a rule that exists but is silently ignored is worse than
-- either extreme; pausing one rule is what is_active is for.
--
-- Shape follows the dimensions registry (20260702084500): company_id-native,
-- no user_id (rules are company policy, not personal data), RLS via
-- user_company_ids(). value_id's dimension/company consistency is validated
-- at the API layer (and re-checked by engine-side registry validation at
-- booking); a composite FK is deliberately skipped — dimension_values has no
-- (id, dimension_id) unique pair and adding one for this is not worth the
-- churn.
--
-- pg-test: tests/pg/account-dimension-rules.pg.test.ts (RLS + CHECKs +
-- cascade behavior).
CREATE TABLE public.account_dimension_rules (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
company_id uuid NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE,
-- Exact BAS account; ranges can layer on later without schema change.
account_number text NOT NULL CHECK (account_number ~ '^[0-9]{4}$'),
dimension_id uuid NOT NULL,
rule_type text NOT NULL CHECK (rule_type IN ('required', 'default', 'fixed')),
-- required → no value; default/fixed → the value to apply.
value_id uuid REFERENCES public.dimension_values(id) ON DELETE CASCADE,
is_active boolean NOT NULL DEFAULT true,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
-- Composite FK: the dimension must belong to the same company (the
-- registry's UNIQUE (id, company_id) exists exactly for this pattern).
FOREIGN KEY (dimension_id, company_id)
REFERENCES public.dimensions(id, company_id) ON DELETE CASCADE,
-- One rule per (account, dimension) — 'required'+'default' combos et al.
-- are a later refinement; three clean types for v1.
UNIQUE (company_id, account_number, dimension_id),
CONSTRAINT adr_value_presence CHECK (
(rule_type = 'required' AND value_id IS NULL)
OR (rule_type IN ('default', 'fixed') AND value_id IS NOT NULL)
)
);
ALTER TABLE public.account_dimension_rules ENABLE ROW LEVEL SECURITY;
CREATE POLICY "view own-company account_dimension_rules"
ON public.account_dimension_rules FOR SELECT
USING (company_id IN (SELECT user_company_ids()));
CREATE POLICY "insert own-company account_dimension_rules"
ON public.account_dimension_rules FOR INSERT
WITH CHECK (company_id IN (SELECT user_company_ids()));
CREATE POLICY "update own-company account_dimension_rules"
ON public.account_dimension_rules FOR UPDATE
USING (company_id IN (SELECT user_company_ids()));
CREATE POLICY "delete own-company account_dimension_rules"
ON public.account_dimension_rules FOR DELETE
USING (company_id IN (SELECT user_company_ids()));
CREATE INDEX idx_adr_company_account
ON public.account_dimension_rules (company_id, account_number);
CREATE TRIGGER set_updated_at_account_dimension_rules
BEFORE UPDATE ON public.account_dimension_rules
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
CREATE TRIGGER audit_account_dimension_rules
AFTER INSERT OR UPDATE OR DELETE ON public.account_dimension_rules
FOR EACH ROW EXECUTE FUNCTION public.write_audit_log();
COMMENT ON TABLE public.account_dimension_rules IS
'Per-account dimension policy (dimensions PR10): required blocks posting without a value (TS-side, commitEntry), default pre-fills, fixed always applies. Zero rows = no behavior change.';
NOTIFY pgrst, 'reload schema';