* feat(dimensions): substrate — SIE-native registry + dimensions JSONB on journal lines (PR1)
Implements phase 1 of dev_docs/dimensions_implementation_plan.md:
- New company-native registry tables: dimensions (= SIE #DIM/#UNDERDIM,
seeded is_system 1=Kostnadsställe / 6=Projekt via ensure_company_dimensions,
nullable bare firm_id) and dimension_values (= #OBJEKT), full RLS incl.
DELETE, audit + updated_at triggers, guard triggers (system dims undeletable,
sie_dim_no immutable, values referenced by posted lines archive-not-delete).
- journal_entry_lines.dimensions jsonb NOT NULL DEFAULT '{}' as the single
source of truth ({sie_dim_no: object_code}), CHECK object-typed, GIN
(jsonb_path_ops) + partial expression indexes on dims 1/6. Inherits posted-
line immutability from the existing trigger with zero new triggers.
- Backfill: representation copy of legacy cost_center/project text into the
JSONB map (trigger-disabled, schema_sync precedent); legacy cost_centers/
projects registry rows copied into dimension_values; inactive placeholder
values for orphaned free-text codes.
- Dual-write: engine buildLineInserts + storno/correction/date-move now derive
cost_center/project mirrors from the map via lib/bookkeeping/dimension-resolver.ts
(normalizeLineDimensions / lineDimensionColumns); reversal copies dims.
- CreateJournalEntryLineInput + shared Zod line schema gain a dimensions bag
(cost_center/project stay as deprecated aliases); pending-ops voucher lines
coerce it.
- CI ratchet: direct-jel-insert check in no-new-antipatterns.mjs — inserts into
journal_entry_lines outside sanctioned writers fail CI.
- pg-real suite: registry RLS/guards/retention, ensure_company_dimensions
tenant guard, dims frozen on posted lines, CHECK enforcement (13 tests).
Non-breaking: companies without dimensions see zero change; no UI yet.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* fix(dimensions): address review findings — canonical keys, boundary-validated staged bags, migration guidance
- normalizeLineDimensions canonicalizes numeric keys ('01' -> '1') so
leading-zero keys can't split values or miss the cost_center/project mirrors
(PR Agent finding).
- New coerceDimensionsBag() in dimension-resolver is the single boundary
validator for untyped staged payloads, enforcing the same constraints as the
Zod line schema (string-only values, 1-40 chars, no SIE-framing chars,
canonical keys). pending-operations normalizeVoucherLines now uses it —
staged payloads can no longer bypass API-layer validation via numeric
coercion (compliance-swarm V2.2/V1.2.5/PI1.1, Swedish review finding 4).
- Migration backfill comment now spells out the exact conditions under which
the trigger-disable pattern is defensible (BFL 5:5 / BFNAR 2013:2) and what
a future reviewer must verify before reusing it (Swedish review finding 2).
- 10 new resolver tests incl. reversal-parity (empty bag + aliases ==
alias-only) proving the reverseEntry and storno paths normalize identically
(PR Agent finding 1).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* fix(dimensions): round-2 review — shared Zod schema, transactional backfill, empty-string guard
- DimensionsBagSchema now lives in dimension-resolver as the single source of
truth; CreateJournalEntryLineSchema and coerceDimensionsBag both delegate to
it, so the API layer and the staged pending-operations path provably cannot
drift (compliance-swarm V2.2). coerceDimensionsBag switches to whole-bag
semantics: any invalid entry rejects the bag, exactly like the API schema.
- Migration backfill now runs DISABLE TRIGGER / UPDATE / ENABLE TRIGGER inside
one transaction — the ACCESS EXCLUSIVE lock from ALTER TABLE holds until
COMMIT, so no concurrent writer can slip an unguarded line write into the
window during a live apply (compliance-swarm V1.2, Swedish review finding 1).
- NULLIF guard: empty-string legacy mirrors can no longer mint {"n":""}
entries the resolver would interpret as "cleared" (PR Agent round-2 edge).
- COMMENT ON dimensions.resets_annually documenting the SIE4 #IB/#OIB
semantics the PR2+ export path must honour (Swedish review finding 2).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
135 lines
5.0 KiB
TypeScript
135 lines
5.0 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import {
|
|
normalizeLineDimensions,
|
|
lineDimensionColumns,
|
|
coerceDimensionsBag,
|
|
DIM_COST_CENTER,
|
|
DIM_PROJECT,
|
|
} from '@/lib/bookkeeping/dimension-resolver'
|
|
|
|
describe('normalizeLineDimensions', () => {
|
|
it('returns empty map for a line with no dimension data', () => {
|
|
expect(normalizeLineDimensions({})).toEqual({})
|
|
expect(normalizeLineDimensions({ cost_center: null, project: null })).toEqual({})
|
|
})
|
|
|
|
it('maps the deprecated aliases to SIE keys 1 and 6', () => {
|
|
expect(normalizeLineDimensions({ cost_center: 'KS01', project: 'P001' })).toEqual({
|
|
'1': 'KS01',
|
|
'6': 'P001',
|
|
})
|
|
})
|
|
|
|
it('passes an explicit bag through', () => {
|
|
expect(normalizeLineDimensions({ dimensions: { '1': 'KS01', '7': 'ANST-4' } })).toEqual({
|
|
'1': 'KS01',
|
|
'7': 'ANST-4',
|
|
})
|
|
})
|
|
|
|
it('lets the explicit bag win over aliases per key', () => {
|
|
expect(
|
|
normalizeLineDimensions({
|
|
dimensions: { '1': 'KS-BAG' },
|
|
cost_center: 'KS-ALIAS',
|
|
project: 'P-ALIAS',
|
|
})
|
|
).toEqual({ '1': 'KS-BAG', '6': 'P-ALIAS' })
|
|
})
|
|
|
|
it('treats an explicit empty string in the bag as clearing that dimension', () => {
|
|
expect(
|
|
normalizeLineDimensions({ dimensions: { '1': '' }, cost_center: 'KS-ALIAS' })
|
|
).toEqual({})
|
|
})
|
|
|
|
it('trims whitespace and drops blank values', () => {
|
|
expect(
|
|
normalizeLineDimensions({ dimensions: { '6': ' P001 ' }, cost_center: ' ' })
|
|
).toEqual({ '6': 'P001' })
|
|
})
|
|
|
|
it('drops non-numeric and zero/negative keys', () => {
|
|
expect(
|
|
normalizeLineDimensions({
|
|
dimensions: { projekt: 'X', '0': 'Y', '6': 'P001' } as Record<string, string>,
|
|
})
|
|
).toEqual({ '6': 'P001' })
|
|
})
|
|
|
|
it("canonicalizes leading-zero keys ('01' -> '1') so mirrors are derived", () => {
|
|
const dims = normalizeLineDimensions({ dimensions: { '01': 'KS01', '06': 'P001' } })
|
|
expect(dims).toEqual({ '1': 'KS01', '6': 'P001' })
|
|
expect(lineDimensionColumns(dims)).toEqual({ cost_center: 'KS01', project: 'P001' })
|
|
})
|
|
|
|
it("clearing via a leading-zero key ('01': '') also clears the alias-filled '1'", () => {
|
|
expect(
|
|
normalizeLineDimensions({ dimensions: { '01': '' }, cost_center: 'KS-ALIAS' })
|
|
).toEqual({})
|
|
})
|
|
|
|
it('reversal parity: empty bag + populated aliases equals alias-only input', () => {
|
|
// reverseEntry passes {dimensions: {}, cost_center, project} for legacy
|
|
// rows; storno passes the row directly — both must normalize identically.
|
|
expect(
|
|
normalizeLineDimensions({ dimensions: {}, cost_center: 'KS01', project: 'P001' })
|
|
).toEqual(normalizeLineDimensions({ cost_center: 'KS01', project: 'P001' }))
|
|
})
|
|
})
|
|
|
|
describe('coerceDimensionsBag (boundary validator for staged payloads)', () => {
|
|
it('returns undefined for non-objects', () => {
|
|
expect(coerceDimensionsBag(undefined)).toBeUndefined()
|
|
expect(coerceDimensionsBag(null)).toBeUndefined()
|
|
expect(coerceDimensionsBag('P001')).toBeUndefined()
|
|
expect(coerceDimensionsBag(['6', 'P001'])).toBeUndefined()
|
|
})
|
|
|
|
it('accepts a valid bag and normalizes values', () => {
|
|
expect(coerceDimensionsBag({ '6': ' P001 ', '1': 'KS01' })).toEqual({
|
|
'6': 'P001',
|
|
'1': 'KS01',
|
|
})
|
|
})
|
|
|
|
it('rejects the WHOLE bag on any invalid entry — same as the API schema', () => {
|
|
// Numeric value (no silent coercion), invalid key, leading-zero key:
|
|
// exactly what CreateJournalEntryLineSchema would reject.
|
|
expect(coerceDimensionsBag({ '6': 42 })).toBeUndefined()
|
|
expect(coerceDimensionsBag({ '6': 42, '1': 'KS01' })).toBeUndefined()
|
|
expect(coerceDimensionsBag({ projekt: 'P001', '6': 'P001' })).toBeUndefined()
|
|
expect(coerceDimensionsBag({ '06': 'P001' })).toBeUndefined()
|
|
})
|
|
|
|
it('enforces the same length/charset constraints as the Zod line schema', () => {
|
|
expect(coerceDimensionsBag({ '6': 'x'.repeat(41) })).toBeUndefined()
|
|
expect(coerceDimensionsBag({ '6': 'P"1' })).toBeUndefined()
|
|
expect(coerceDimensionsBag({ '6': 'P{1}' })).toBeUndefined()
|
|
expect(coerceDimensionsBag({ '6': 'x'.repeat(40) })).toEqual({ '6': 'x'.repeat(40) })
|
|
})
|
|
|
|
it('returns undefined for an empty or whitespace-only bag', () => {
|
|
expect(coerceDimensionsBag({})).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('lineDimensionColumns', () => {
|
|
it('derives both mirrors from the map', () => {
|
|
expect(lineDimensionColumns({ [DIM_COST_CENTER]: 'KS01', [DIM_PROJECT]: 'P001' })).toEqual({
|
|
cost_center: 'KS01',
|
|
project: 'P001',
|
|
})
|
|
})
|
|
|
|
it('returns nulls for missing keys', () => {
|
|
expect(lineDimensionColumns({})).toEqual({ cost_center: null, project: null })
|
|
expect(lineDimensionColumns({ '7': 'ANST-4' })).toEqual({ cost_center: null, project: null })
|
|
})
|
|
|
|
it('round-trips with normalizeLineDimensions (mirror consistency)', () => {
|
|
const dims = normalizeLineDimensions({ cost_center: 'KS01', dimensions: { '6': 'P001' } })
|
|
expect(lineDimensionColumns(dims)).toEqual({ cost_center: 'KS01', project: 'P001' })
|
|
})
|
|
})
|