* feat(dimensions): PR5 SIE round-trip — lossless dimension import, registry upsert, undo lockstep
SIE import previously parsed and silently DISCARDED all dimension data
(object lists at sie-parser.ts:651-654, #DIM/#OBJEKT in the ignore list at
:689). Import is now lossless — the dimensions plan PR5 milestone.
Parser: #TRANS object lists ({1 "KS01" 6 "P001"}) land on the line as an
SIE-dim-no → code map (canonical numeric keys, quoted codes, malformed
pairs warn); #DIM/#UNDERDIM/#OBJEKT parse into registry records. OIB/OUB
stay ignored (dimension reporting is P&L-only in v1).
Importer (lib/import/sie-dimensions.ts): upserts missing dimensions/
dimension_values rows — never renames existing ones (ON CONFLICT DO
NOTHING); undeclared reserved numbers synthesize their SIE-standard names
(mirroring the export's orphan synthesis); codes violating the registry
CHECK are skipped with a warning but survive verbatim on lines (documented
legacy-free-text exception). Bulk voucher insert now writes the dimensions
jsonb + cost_center/project mirrors via the sanctioned dual-write helpers
(no trigger suppression needed — the immutability trigger guards
UPDATE/DELETE, not INSERT). Import auto-enables dimensions_enabled with a
result-card notice (pre-authorized by the column comment). arcim-migration
provider syncs inherit all of it via the shared parser/importer.
Undo lockstep (migration 20260702154500): created_by_import_id provenance
on both registry tables (ON DELETE SET NULL); undo_sie_import deletes the
values/dimensions the undone import introduced when no remaining
posted/reversed line references them — user-created rows and rows other
bookkeeping references are untouched. The registry guard triggers act as
backstop. replace_sie_import deliberately skips the lockstep (re-import
re-upserts the same codes). Six pg-real tests cover the lockstep.
Round-trip pinned by test: parse → import state → export → parse preserves
declarations (#UNDERDIM parent links included), values, and per-line object
lists — including synthesis of referenced-but-undeclared values.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* fix(dimensions): restate function-local statement_timeout on undo_sie_import
CREATE OR REPLACE resets proconfig, so the 290s timeout from 20260629160100
was silently dropped — regressing service-client bulk deletes to the
authenticator role's 8s limit. Caught by sie-import.replace.pg.test.ts in CI.
Full pg-real suite green (483/483, TZ=UTC).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* fix(dimensions): surface OIB/OUB drops and dimension presence as parse-level info (#866 review)
Dropping object-balance records must never be silent — one info issue counts
the skipped #OIB/#OUB rows (object-level balances are P&L-out-of-scope in
v1), and a second announces dimension data before the user executes the
import (the preview step renders parse issues), so the auto-enable notice is
no longer purely post-hoc.
Triage notes for the remaining findings: the RPC's opening SELECT is the
company-ownership check the swarm asked for; registry writes are RLS-bound;
line-verbatim codes are the documented legacy-free-text exception; export
emits no #KSUMMA so there is nothing to recompute; SIE dims 3–5 are
"reserved for future use" with no standard names, so generic synthesis is
spec-correct; ON DELETE SET NULL is deliberate — provenance is operational
metadata for undo, not räkenskapsinformation (the guarded journal lines
are), and RESTRICT would block legitimate post-retention housekeeping.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
233 lines
9.1 KiB
TypeScript
233 lines
9.1 KiB
TypeScript
import { randomUUID } from 'node:crypto'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { getPool } from '@/tests/pg/setup'
|
|
import { seedCompany, insertDraftJournalEntry } from '@/tests/pg/fixtures'
|
|
|
|
// Migration 20260702154500_dimension_import_provenance_undo_lockstep.sql:
|
|
// SIE import creates dimensions/dimension_values rows carrying
|
|
// created_by_import_id; undo_sie_import deletes the rows the undone import
|
|
// introduced — but ONLY when no remaining posted/reversed line references
|
|
// them, and NEVER user-created rows (created_by_import_id IS NULL).
|
|
//
|
|
// The registry guard triggers (enforce_dimension_registry_guards /
|
|
// enforce_dimension_value_retention) fire on these deletes as a backstop, so
|
|
// these tests also prove the lockstep deletes are trigger-compatible.
|
|
|
|
async function insertCompletedImport(params: {
|
|
companyId: string
|
|
userId: string
|
|
fiscalPeriodId: string
|
|
}): Promise<string> {
|
|
const id = randomUUID()
|
|
await getPool().query(
|
|
`INSERT INTO public.sie_imports
|
|
(id, user_id, company_id, filename, file_hash, sie_type,
|
|
fiscal_year_start, fiscal_year_end, accounts_count, transactions_count,
|
|
status, fiscal_period_id, imported_at)
|
|
VALUES ($1, $2, $3, 'undo-dims-test.se', $4, 4,
|
|
'2026-01-01', '2026-12-31', 0, 1,
|
|
'completed', $5, now())`,
|
|
[id, params.userId, params.companyId, `hash-${id}`, params.fiscalPeriodId],
|
|
)
|
|
return id
|
|
}
|
|
|
|
/** Posted entry with dimension-tagged balanced lines. */
|
|
async function insertPostedTaggedEntry(params: {
|
|
companyId: string
|
|
userId: string
|
|
fiscalPeriodId: string
|
|
sourceType: string
|
|
voucherNumber: number
|
|
dimensions: Record<string, string>
|
|
}): Promise<string> {
|
|
const jeId = await insertDraftJournalEntry({
|
|
userId: params.userId,
|
|
companyId: params.companyId,
|
|
fiscalPeriodId: params.fiscalPeriodId,
|
|
sourceType: params.sourceType,
|
|
status: 'draft',
|
|
voucherNumber: params.voucherNumber,
|
|
})
|
|
await getPool().query(
|
|
`INSERT INTO public.journal_entry_lines
|
|
(journal_entry_id, account_number, debit_amount, credit_amount, dimensions)
|
|
VALUES ($1, '5010', 1000, 0, $2::jsonb),
|
|
($1, '1930', 0, 1000, '{}'::jsonb)`,
|
|
[jeId, JSON.stringify(params.dimensions)],
|
|
)
|
|
await getPool().query(
|
|
`UPDATE public.journal_entries SET status = 'posted' WHERE id = $1`,
|
|
[jeId],
|
|
)
|
|
return jeId
|
|
}
|
|
|
|
async function insertDimension(params: {
|
|
companyId: string
|
|
sieDimNo: number
|
|
name: string
|
|
importId?: string | null
|
|
isSystem?: boolean
|
|
}): Promise<string> {
|
|
const { rows } = await getPool().query<{ id: string }>(
|
|
`INSERT INTO public.dimensions
|
|
(company_id, sie_dim_no, name, resets_annually, is_system, created_by_import_id)
|
|
VALUES ($1, $2, $3, true, $4, $5)
|
|
RETURNING id`,
|
|
[params.companyId, params.sieDimNo, params.name, params.isSystem ?? false, params.importId ?? null],
|
|
)
|
|
return rows[0].id
|
|
}
|
|
|
|
async function insertDimensionValue(params: {
|
|
companyId: string
|
|
dimensionId: string
|
|
code: string
|
|
importId?: string | null
|
|
}): Promise<string> {
|
|
const { rows } = await getPool().query<{ id: string }>(
|
|
`INSERT INTO public.dimension_values
|
|
(company_id, dimension_id, code, name, created_by_import_id)
|
|
VALUES ($1, $2, $3, $3, $4)
|
|
RETURNING id`,
|
|
[params.companyId, params.dimensionId, params.code, params.importId ?? null],
|
|
)
|
|
return rows[0].id
|
|
}
|
|
|
|
async function callUndo(companyId: string, importId: string, actor: string) {
|
|
return getPool().query<{ deleted: number }>(
|
|
`SELECT public.undo_sie_import($1::uuid, $2::uuid, $3::uuid) AS deleted`,
|
|
[companyId, importId, actor],
|
|
)
|
|
}
|
|
|
|
async function countRows(table: string, id: string): Promise<number> {
|
|
const { rows } = await getPool().query(
|
|
`SELECT 1 FROM public.${table} WHERE id = $1`,
|
|
[id],
|
|
)
|
|
return rows.length
|
|
}
|
|
|
|
describe('undo_sie_import: dimension registry lockstep', () => {
|
|
it('deletes import-created values whose references vanish with the import', async () => {
|
|
const { companyId, userId, fiscalPeriodId } = await seedCompany()
|
|
const importId = await insertCompletedImport({ companyId, userId, fiscalPeriodId })
|
|
|
|
const dimId = await insertDimension({ companyId, sieDimNo: 6, name: 'Projekt' })
|
|
const valueId = await insertDimensionValue({ companyId, dimensionId: dimId, code: 'P001', importId })
|
|
await insertPostedTaggedEntry({
|
|
companyId, userId, fiscalPeriodId,
|
|
sourceType: 'import', voucherNumber: 1,
|
|
dimensions: { '6': 'P001' },
|
|
})
|
|
|
|
const res = await callUndo(companyId, importId, userId)
|
|
expect(res.rows[0].deleted).toBe(1)
|
|
|
|
// The import's value is gone; the dimension itself (not import-created)
|
|
// survives.
|
|
expect(await countRows('dimension_values', valueId)).toBe(0)
|
|
expect(await countRows('dimensions', dimId)).toBe(1)
|
|
})
|
|
|
|
it('keeps import-created values that other posted bookkeeping still references', async () => {
|
|
const { companyId, userId, fiscalPeriodId } = await seedCompany()
|
|
const importId = await insertCompletedImport({ companyId, userId, fiscalPeriodId })
|
|
|
|
const dimId = await insertDimension({ companyId, sieDimNo: 6, name: 'Projekt' })
|
|
const valueId = await insertDimensionValue({ companyId, dimensionId: dimId, code: 'P002', importId })
|
|
await insertPostedTaggedEntry({
|
|
companyId, userId, fiscalPeriodId,
|
|
sourceType: 'import', voucherNumber: 1,
|
|
dimensions: { '6': 'P002' },
|
|
})
|
|
// A MANUAL posted entry tagged with the same code — survives the undo and
|
|
// must keep its registry row (retention trigger would block the delete;
|
|
// the lockstep's own WHERE avoids even attempting it).
|
|
await insertPostedTaggedEntry({
|
|
companyId, userId, fiscalPeriodId,
|
|
sourceType: 'manual', voucherNumber: 2,
|
|
dimensions: { '6': 'P002' },
|
|
})
|
|
|
|
const res = await callUndo(companyId, importId, userId)
|
|
expect(res.rows[0].deleted).toBe(1) // only the import entry
|
|
|
|
expect(await countRows('dimension_values', valueId)).toBe(1)
|
|
})
|
|
|
|
it('never touches user-created values (no provenance)', async () => {
|
|
const { companyId, userId, fiscalPeriodId } = await seedCompany()
|
|
const importId = await insertCompletedImport({ companyId, userId, fiscalPeriodId })
|
|
|
|
const dimId = await insertDimension({ companyId, sieDimNo: 6, name: 'Projekt' })
|
|
const userValueId = await insertDimensionValue({ companyId, dimensionId: dimId, code: 'EGEN', importId: null })
|
|
await insertPostedTaggedEntry({
|
|
companyId, userId, fiscalPeriodId,
|
|
sourceType: 'import', voucherNumber: 1,
|
|
dimensions: { '6': 'EGEN' },
|
|
})
|
|
|
|
await callUndo(companyId, importId, userId)
|
|
|
|
// Unreferenced now, but user-created → stays.
|
|
expect(await countRows('dimension_values', userValueId)).toBe(1)
|
|
})
|
|
|
|
it('deletes an import-created custom dimension once empty and unreferenced', async () => {
|
|
const { companyId, userId, fiscalPeriodId } = await seedCompany()
|
|
const importId = await insertCompletedImport({ companyId, userId, fiscalPeriodId })
|
|
|
|
const dimId = await insertDimension({ companyId, sieDimNo: 7, name: 'Anställd', importId })
|
|
const valueId = await insertDimensionValue({ companyId, dimensionId: dimId, code: 'ANNA', importId })
|
|
await insertPostedTaggedEntry({
|
|
companyId, userId, fiscalPeriodId,
|
|
sourceType: 'import', voucherNumber: 1,
|
|
dimensions: { '7': 'ANNA' },
|
|
})
|
|
|
|
await callUndo(companyId, importId, userId)
|
|
|
|
expect(await countRows('dimension_values', valueId)).toBe(0)
|
|
expect(await countRows('dimensions', dimId)).toBe(0)
|
|
})
|
|
|
|
it('keeps an import-created dimension that still has user-created values', async () => {
|
|
const { companyId, userId, fiscalPeriodId } = await seedCompany()
|
|
const importId = await insertCompletedImport({ companyId, userId, fiscalPeriodId })
|
|
|
|
const dimId = await insertDimension({ companyId, sieDimNo: 8, name: 'Kund', importId })
|
|
const userValueId = await insertDimensionValue({ companyId, dimensionId: dimId, code: 'KUND1', importId: null })
|
|
await insertPostedTaggedEntry({
|
|
companyId, userId, fiscalPeriodId,
|
|
sourceType: 'import', voucherNumber: 1,
|
|
dimensions: { '8': 'KUND1' },
|
|
})
|
|
|
|
await callUndo(companyId, importId, userId)
|
|
|
|
// The user's value anchors the dimension.
|
|
expect(await countRows('dimension_values', userValueId)).toBe(1)
|
|
expect(await countRows('dimensions', dimId)).toBe(1)
|
|
})
|
|
|
|
it('deleting an old sie_imports row nulls provenance instead of cascading', async () => {
|
|
const { companyId, userId, fiscalPeriodId } = await seedCompany()
|
|
const importId = await insertCompletedImport({ companyId, userId, fiscalPeriodId })
|
|
const dimId = await insertDimension({ companyId, sieDimNo: 6, name: 'Projekt' })
|
|
const valueId = await insertDimensionValue({ companyId, dimensionId: dimId, code: 'P009', importId })
|
|
|
|
await getPool().query(`DELETE FROM public.sie_imports WHERE id = $1`, [importId])
|
|
|
|
const { rows } = await getPool().query<{ created_by_import_id: string | null }>(
|
|
`SELECT created_by_import_id FROM public.dimension_values WHERE id = $1`,
|
|
[valueId],
|
|
)
|
|
expect(rows).toHaveLength(1)
|
|
expect(rows[0].created_by_import_id).toBeNull()
|
|
})
|
|
})
|