Files
accounted/lib/bookkeeping/__tests__/engine.pg.test.ts
T
Mattsson cd344b6dbb fix(db): enforce balance check on directly inserted posted journal entries (v2) (#1439)
* fix(db): enforce balance check on directly inserted posted journal entries

check_balance_on_post only fires on the draft-to-posted UPDATE transition,
so any code path that INSERTs a row with status 'posted' directly skipped
balance validation entirely. The invariant sum(debit) = sum(credit) on
every posted entry was DB-enforced only for the engine's commit lifecycle.

Add check_balance_on_posted_insert, a deferred constraint trigger on
AFTER INSERT WHEN (NEW.status = 'posted') reusing the existing
check_journal_entry_balance() function, which already handles the
journal_entries INSERT context via NEW.id/NEW.status. Deferred semantics
let an atomic transaction insert header and lines together; zero-line and
unbalanced posted inserts are rejected at constraint evaluation. All
existing checks stay intact; this only adds coverage.

The one first-party posted-INSERT path outside an RPC, the sandbox seed,
now books through the bookkeeping engine (createJournalEntry) instead of
raw inserts. SIE import already inserts header and lines in a single
transaction via its structured RPC and passes unchanged.

pg tests cover the new path (zero-line rejected, unbalanced rejected at
SET CONSTRAINTS IMMEDIATE, balanced same-transaction insert accepted) and
existing posted-entry fixtures move to a transactional
insertPostedJournalEntry helper so they stay valid setup.

Fixes #327

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

* fix(tests): insert list-filters pg fixtures in one transaction

The list-filters suite (landed via a sibling merge) inserted posted
headers with getPool().query, where each query autocommits: the deferred
check_balance_on_posted_insert constraint fired at the header's own
commit with zero lines and correctly rejected the fixture. Header and
balanced lines now share one BEGIN/COMMIT so the constraint evaluates
the complete entry, mirroring the insertPostedJournalEntry helper.

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

* fix(seed): insert journal headers as drafts, post after lines land

check_balance_on_posted_insert (renamed to apply-time version
20260806130000) rejects a posted header whose transaction has no lines.
PostgREST autocommits each request, so every seed path that inserted
posted headers first would die with "has zero total": the sandbox seed
(ledger history, invoice vouchers, salary vouchers), seed-demo-account
and seed-export-data. All now insert draft headers, insert lines, then
flip to posted so check_balance_on_post validates the finished
verifikat. The sandbox seed keeps its documented no-events design.

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

* fix(db): preserve a preset committed_at on draft-to-posted transition

set_committed_at() stamped now() unconditionally, so the seed flows that
post backdated drafts lost their historical booking timestamps and every
demo verifikat read as booked today (CodeRabbit finding on PR 1439).
Stamp only when committed_at is NULL: the engine path (drafts carry no
committed_at) behaves exactly as before and a posted entry still always
has a committed_at; an explicitly supplied value now survives posting.

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

* fix(db): preserve preset committed_at only for trusted roles

The IS NULL guard alone (20260806150000, never shipped; replaced by
20260806160000) let any RLS-permitted member backdate committed_at
through PostgREST by presetting it on a draft and posting, which the
Swedish accounting review flagged: committed_at is what the BFL 5 kap
timeliness checks and behandlingshistorik treat as the genuine
transition time. Preset values now survive posting only for
service_role/postgres/supabase_admin; authenticated and anon writers
always get the now() stamp. Consequence: the sandbox seed (runs as the
requesting user) gets committed_at = posting time, accepted and
documented in the route; the demo scripts run as service_role and keep
their backdated history. pg tests cover all four paths, with the upper
timestamp bound CodeRabbit asked for.

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

* fix(db): restore superseded migration so the preview tracker stays consistent

The preview branch had already applied 20260806150000 when the previous
commit deleted the file, orphaning the preview's migration tracker
("Remote migration versions not found in local migrations directory").
Restored with a header explaining it is superseded in the same deploy by
20260806160000, so the unguarded semantics are never live on their own.

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

* fix(db): decide committed_at trust by JWT claims, not current_user

The Swedish review found the current_user guard bypassable:
commit_journal_entry is SECURITY DEFINER and granted to authenticated,
so inside it current_user is the function owner and a member could
preset a backdated committed_at on a direct-inserted draft and launder
it through the RPC. The guard now reads the JWT claims role (same
primitive as the RPC's own tenant guard): preset values survive only
for service_role or claim-less backend connections; authenticated and
anon callers are always stamped now(), on both the direct UPDATE and
the RPC path (new pg test). Both migration files now carry the
identical final body so no unguarded intermediate exists as a
standalone applyable unit. Behandlingshistorik logging of trusted
overrides is follow-up #1444.

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

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-06 23:00:05 +02:00

239 lines
9.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { getPool } from '@/tests/pg/setup'
import {
insertBalancedLines,
insertDraftJournalEntry,
seedCompany,
} from '@/tests/pg/fixtures'
describe('engine.pg: triggers & RPCs that mocks cannot catch', () => {
it('rejects a directly inserted posted journal entry with no lines', async () => {
const { userId, companyId, fiscalPeriodId } = await seedCompany()
await expect(
getPool().query(
`INSERT INTO public.journal_entries
(user_id, company_id, fiscal_period_id, voucher_number, voucher_series,
entry_date, description, source_type, status)
VALUES ($1, $2, $3, 1, 'A', '2026-06-01', 'Direct posted insert', 'manual', 'posted')`,
[userId, companyId, fiscalPeriodId],
),
).rejects.toThrow(/has zero total/i)
})
it('rejects an unbalanced directly inserted posted journal entry at constraint time', async () => {
const { userId, companyId, fiscalPeriodId } = await seedCompany()
const client = await getPool().connect()
try {
await client.query('BEGIN')
const inserted = await client.query<{ id: string }>(
`INSERT INTO public.journal_entries
(user_id, company_id, fiscal_period_id, voucher_number, voucher_series,
entry_date, description, source_type, status)
VALUES ($1, $2, $3, 1, 'A', '2026-06-01', 'Direct posted insert', 'manual', 'posted')
RETURNING id`,
[userId, companyId, fiscalPeriodId],
)
await client.query(
`INSERT INTO public.journal_entry_lines
(journal_entry_id, account_number, debit_amount, credit_amount)
VALUES ($1, '1930', 100, 0)`,
[inserted.rows[0]!.id],
)
await expect(
client.query('SET CONSTRAINTS check_balance_on_posted_insert IMMEDIATE'),
).rejects.toThrow(/not balanced/i)
} finally {
await client.query('ROLLBACK').catch(() => {})
client.release()
}
})
it('allows balanced lines to follow a posted header in the same transaction', async () => {
const { userId, companyId, fiscalPeriodId } = await seedCompany()
const client = await getPool().connect()
try {
await client.query('BEGIN')
const inserted = await client.query<{ id: string }>(
`INSERT INTO public.journal_entries
(user_id, company_id, fiscal_period_id, voucher_number, voucher_series,
entry_date, description, source_type, status)
VALUES ($1, $2, $3, 1, 'A', '2026-06-01', 'Direct posted insert', 'manual', 'posted')
RETURNING id`,
[userId, companyId, fiscalPeriodId],
)
await client.query(
`INSERT INTO public.journal_entry_lines
(journal_entry_id, account_number, debit_amount, credit_amount)
VALUES ($1, '1930', 100, 0),
($1, '3001', 0, 100)`,
[inserted.rows[0]!.id],
)
await client.query('SET CONSTRAINTS check_balance_on_posted_insert IMMEDIATE')
const persisted = await client.query<{ status: string }>(
`SELECT status FROM public.journal_entries WHERE id = $1`,
[inserted.rows[0]!.id],
)
expect(persisted.rows[0]!.status).toBe('posted')
} finally {
await client.query('ROLLBACK').catch(() => {})
client.release()
}
})
it('rejects INSERT into journal_entries when the fiscal period is closed', async () => {
const { userId, companyId, fiscalPeriodId } = await seedCompany({ isClosed: true })
await expect(
insertDraftJournalEntry({ userId, companyId, fiscalPeriodId }),
).rejects.toThrow(/locked\/closed fiscal period/i)
})
it('commit_journal_entry assigns sequential voucher numbers under concurrency', async () => {
const { userId, companyId, fiscalPeriodId } = await seedCompany()
const entryA = await insertDraftJournalEntry({ userId, companyId, fiscalPeriodId })
const entryB = await insertDraftJournalEntry({ userId, companyId, fiscalPeriodId })
await insertBalancedLines(entryA)
await insertBalancedLines(entryB)
// Two dedicated clients so the row-level lock on voucher_sequences is
// actually exercised: not just a single connection serialising calls.
const clientA = await getPool().connect()
const clientB = await getPool().connect()
try {
const [resA, resB] = await Promise.all([
clientA.query<{ voucher_number: number }>(
`SELECT voucher_number FROM public.commit_journal_entry($1::uuid, $2::uuid)`,
[companyId, entryA],
),
clientB.query<{ voucher_number: number }>(
`SELECT voucher_number FROM public.commit_journal_entry($1::uuid, $2::uuid)`,
[companyId, entryB],
),
])
const numbers = [resA.rows[0]!.voucher_number, resB.rows[0]!.voucher_number].sort(
(a, b) => a - b,
)
expect(numbers).toEqual([1, 2])
} finally {
clientA.release()
clientB.release()
}
})
it('rejects UPDATE to a posted journal entry (committed immutability)', async () => {
const { userId, companyId, fiscalPeriodId } = await seedCompany()
// Bypass commit_journal_entry with the direct-posted fixture. It inserts
// balanced lines in the same transaction so the deferred insert balance
// trigger accepts the setup before immutability is exercised below.
const entryId = await insertDraftJournalEntry({
userId,
companyId,
fiscalPeriodId,
status: 'posted',
voucherNumber: 1,
})
await expect(
getPool().query(
`UPDATE public.journal_entries SET description = 'tampered' WHERE id = $1`,
[entryId],
),
).rejects.toThrow(/Cannot modify a posted journal entry/i)
})
it('next_voucher_number falls back to the company owner when auth.uid() is NULL', async () => {
// The superuser pg connection has no Supabase JWT, so auth.uid() IS NULL:
// exactly the service-role shape (repair scripts, cron) that used to fail
// the voucher_sequences user_id NOT NULL check before ON CONFLICT could
// arbitrate (commit_journal_entry got the fallback in 20260421170500;
// next_voucher_number (the storno/correction path) did not until
// 20260623130000).
const { userId, companyId, fiscalPeriodId } = await seedCompany()
const first = await getPool().query<{ n: number }>(
`SELECT public.next_voucher_number($1::uuid, $2::uuid) AS n`,
[companyId, fiscalPeriodId],
)
const second = await getPool().query<{ n: number }>(
`SELECT public.next_voucher_number($1::uuid, $2::uuid) AS n`,
[companyId, fiscalPeriodId],
)
expect(first.rows[0]!.n).toBe(1)
expect(second.rows[0]!.n).toBe(2)
// Attribution on the sequence row falls back to companies.created_by.
const seq = await getPool().query<{ user_id: string }>(
`SELECT user_id FROM public.voucher_sequences
WHERE company_id = $1::uuid AND fiscal_period_id = $2::uuid AND voucher_series = 'A'`,
[companyId, fiscalPeriodId],
)
expect(seq.rows[0]!.user_id).toBe(userId)
})
// reverseEntry() clears the period's IB link when it stornos an
// opening_balance entry. enforce_opening_balance_immutability dictates the
// shape of that write, and only a real Postgres can prove the ordering: a
// mocked client accepts the single-statement version that the trigger
// rejects, which is how a "fixed" storno can still leave the period pinned
// to a cancelled IB (blocking year-end forever).
it('enforce_opening_balance_immutability forces a two-step IB unlink', async () => {
const { userId, companyId, fiscalPeriodId } = await seedCompany()
const ibEntryId = await insertDraftJournalEntry({
userId,
companyId,
fiscalPeriodId,
status: 'posted',
voucherNumber: 1,
})
// Linking is legal: the trigger only guards the pointer once it is set.
await getPool().query(
`UPDATE public.fiscal_periods
SET opening_balance_entry_id = $2::uuid, opening_balances_set = true
WHERE id = $1::uuid`,
[fiscalPeriodId, ibEntryId],
)
// Clearing both columns at once still reads OLD.opening_balances_set =
// true, so the trigger rejects it. This is the write reverseEntry must
// never emit.
await expect(
getPool().query(
`UPDATE public.fiscal_periods
SET opening_balance_entry_id = NULL, opening_balances_set = false
WHERE id = $1::uuid`,
[fiscalPeriodId],
),
).rejects.toThrow(/opening balances are immutable once set/i)
// Flag first, pointer second: the order reverseEntry uses.
await getPool().query(
`UPDATE public.fiscal_periods SET opening_balances_set = false WHERE id = $1::uuid`,
[fiscalPeriodId],
)
await getPool().query(
`UPDATE public.fiscal_periods SET opening_balance_entry_id = NULL WHERE id = $1::uuid`,
[fiscalPeriodId],
)
const period = await getPool().query<{
opening_balance_entry_id: string | null
opening_balances_set: boolean
}>(
`SELECT opening_balance_entry_id, opening_balances_set
FROM public.fiscal_periods WHERE id = $1::uuid`,
[fiscalPeriodId],
)
expect(period.rows[0]!.opening_balance_entry_id).toBeNull()
expect(period.rows[0]!.opening_balances_set).toBe(false)
})
})