feat(dimensions): PR2 registry — CRUD API, register UI, settings toggle, SIE export on the new registry (#858)

* feat(dimensions): PR2 registry — CRUD API, register UI, settings toggle, SIE export on the new registry

Phase 2 of dev_docs/dimensions_implementation_plan.md. Companies with
dimensions_enabled=false (default) see zero change.

API:
- Dashboard CRUD: GET /api/dimensions (lazy-seeds system dims 1/6 via the
  ensure_company_dimensions RPC), PATCH /api/dimensions/[id] (is_system
  rename blocked), POST/PATCH/DELETE values (code immutable after creation;
  strict Fortnox code format ^[A-Za-z0-9ÅÄÖåäö_+\-]{1,20}$ at the API layer;
  retention-trigger deletes surface the Swedish "arkivera istället" message
  as 409 DIMENSION_VALUE_REFERENCED).
- POST /api/dimensions/import-existing — scans journal_entry_lines.dimensions
  for unregistered codes and mints inactive placeholder registry rows.
- v1 public API: GET dimensions + POST values (Idempotency-Key, dry-run),
  registered in the OpenAPI spec (102→104 endpoints).
- dimensions_enabled boolean on company_settings (new migration,
  UI-visibility only, never correctness-bearing) exposed through the
  existing settings read/update path.

SIE export (lib/reports/sie-export.ts):
- Reads the new dimensions/dimension_values registry; legacy
  cost_centers/projects tables now have zero readers (drop migration next).
- Fixes the latent Visma-rejection bug: #OBJEKT now declared for INACTIVE
  values referenced by lines.
- Generic-N: #DIM/#UNDERDIM loop sorted by sie_dim_no; #TRANS object lists
  serialize from the line JSONB map (sorted, '01'→'1' collapse); orphan
  codes/dims synthesize declarations from the SIE reserved-number seed —
  every referenced (dim, code) pair is guaranteed declared.

UI:
- /dimensions register (Register-recipe): tabs per dimension, search,
  sortable table, value dialog (code immutable on edit, projekt dates on
  dim 6), archive-not-delete affordances.
- DimensionCombobox shipped (mounts in the tagging PR).
- Settings toggle "Aktivera kostnadsställen & projekt" — toggle-on runs the
  import-existing scan and links to the register.
- Nav row in redovisning, rendered only when dimensions_enabled (same
  mechanism as pays_salaries).
- dimensions.* i18n namespace (51 keys, sv/en parity).
- Sandbox seed: demo dims + values, revenue line tagged {"1":"BUTIK","6":"P001"}.

Verified: 6328/6328 unit tests, guard + coverage gate green, tsc parity with
main (210=210), production build passes.

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

* fix(dimensions): PR2 review round — atomic archived-create, UNDERDIM ordering, import robustness, date semantics

- POST values accepts is_active so "create as archived" is atomic; the UI's
  fragile create-then-PATCH fallback is deleted (PR Agent finding 1).
- DimensionCombobox blur revert reads the committed value/values through refs
  so a selection landing inside the 150ms window always wins (finding 2).
- import-existing sanitizes candidate codes like the PR1 backfill and upserts
  with ignoreDuplicates — one bad/duplicate code can no longer abort the
  batch; created counted from returned rows (finding 3).
- SIE export emits all root #DIM before any #UNDERDIM so a parent always
  precedes a lower-numbered child (SIE4 declaration order — Swedish review);
  synthesized placeholder declarations now log one structured warning
  (BFNAR 2013:2 behandlingshistorik) + defence-in-depth comment.
- Value dates rejected (400 DIMENSION_VALUE_DATES_NOT_ALLOWED) when the
  parent dimension is flow-period (resets_annually=true); explicit null
  still clears (Swedish review).
- Sandbox seed logs seeded dimension codes; GET /api/dimensions documents
  the deliberate absence of dimensions_enabled gating (UI-visibility flag,
  not a security boundary — compliance-swarm V8.2.1 rejected by design).

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

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-07-02 12:26:42 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 8cc2efb083
commit 8bb49c07a2
35 changed files with 3981 additions and 100 deletions
+70
View File
@@ -2462,6 +2462,75 @@ const ASSETS: Record<string, StructuredErrorEntry> = {
},
}
// Dimensions registry (kostnadsställe/projekt) — dev_docs/dimensions_implementation_plan.md §6
const DIMENSION: Record<string, StructuredErrorEntry> = {
DIMENSION_NOT_FOUND: {
httpStatus: 404,
message_sv: 'Dimensionen kunde inte hittas.',
message_en: 'Dimension not found.',
},
DIMENSION_SYSTEM_RENAME: {
httpStatus: 400,
message_sv: 'Systemdimensioner kan inte döpas om.',
message_en: 'System dimensions (kostnadsställe/projekt) cannot be renamed.',
},
DIMENSION_UPDATE_FAILED: {
httpStatus: 500,
message_sv: 'Dimensionen kunde inte uppdateras.',
message_en: 'Failed to update dimension.',
},
DIMENSION_VALUE_NOT_FOUND: {
httpStatus: 404,
message_sv: 'Dimensionsvärdet kunde inte hittas.',
message_en: 'Dimension value not found.',
},
DIMENSION_VALUE_DUPLICATE_CODE: {
httpStatus: 409,
message_sv: 'Ett värde med samma kod finns redan i dimensionen.',
message_en: 'A value with that code already exists in the dimension.',
},
DIMENSION_VALUE_DATES_NOT_ALLOWED: {
httpStatus: 400,
message_sv: 'Datum kan bara sättas på ackumulerande dimensioner (t.ex. projekt).',
message_en:
'Start/end dates can only be set on accumulating dimensions (e.g. projects) — this dimension resets annually.',
},
DIMENSION_VALUE_CREATE_FAILED: {
httpStatus: 500,
message_sv: 'Dimensionsvärdet kunde inte skapas.',
message_en: 'Failed to create dimension value.',
},
DIMENSION_VALUE_UPDATE_FAILED: {
httpStatus: 500,
message_sv: 'Dimensionsvärdet kunde inte uppdateras.',
message_en: 'Failed to update dimension value.',
},
// The DB retention trigger (enforce_dimension_value_retention) raises when a
// code is referenced by posted/reversed lines. Routes surface the trigger's
// own Swedish message via `messageSv` so the code + kod appear in the toast.
DIMENSION_VALUE_REFERENCED: {
httpStatus: 409,
message_sv:
'Värdet används på bokförda verifikat och kan inte tas bort — arkivera det istället.',
message_en:
'The value is referenced by posted vouchers and cannot be deleted — archive (inactivate) it instead.',
remediation: {
description:
'Archive the value instead: PATCH the dimension value with { "is_active": false }. Codes referenced by posted lines are retained for the BFL 7-year period.',
},
},
DIMENSION_VALUE_DELETE_FAILED: {
httpStatus: 500,
message_sv: 'Dimensionsvärdet kunde inte tas bort.',
message_en: 'Failed to delete dimension value.',
},
DIMENSION_IMPORT_FAILED: {
httpStatus: 500,
message_sv: 'Import av befintliga dimensionskoder misslyckades.',
message_en: 'Failed to import existing dimension codes from journal lines.',
},
}
// ─────────────────────────────────────────────────────────────────
// Combined registry
// ─────────────────────────────────────────────────────────────────
@@ -2505,6 +2574,7 @@ const REGISTRY: Record<string, StructuredErrorEntry> = {
...SKATTEVERKET,
...BOLAGSVERKET,
...ASSETS,
...DIMENSION,
}
export function getErrorEntry(code: string): StructuredErrorEntry | undefined {