Files
accounted/tests/pg/get_account_gl_lines_for_matching.pg.test.ts
T
MattssonandClaude Fable 5 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

272 lines
12 KiB
TypeScript

/**
* pg-real test for get_account_gl_lines_for_matching
* (20260610120000_gl_lines_for_matching.sql, link-count semantics reworked in
* 20260723160000_gl_lines_matching_account_scoped_count.sql).
*
* This RPC backs the N:1 "lägga på flera" feature: it mirrors get_unlinked_gl_lines
* but can ALSO surface already-matched vouchers (so a second/third bank
* transaction can be attached to one verifikat), each carrying how many
* transactions already point at it.
*
* Since 20260723090000 the link count is scoped to the requested settlement
* account: a transaction provably on ANOTHER cash account does not mark the
* voucher as matched for p_account_number. This surfaces the unsettled second
* leg of an own-account transfer by default (issue #1026) while transactions
* with no resolvable cash account keep counting for every account.
* (The companion mark_entry_as_opening_balance guard from the same migration
* is covered in mark-entry-as-opening-balance.pg.test.ts.)
*/
import { describe, it, expect } from 'vitest'
import { getPool } from './setup'
import {
insertAuthUser,
insertCashAccount,
insertCompany,
insertFiscalPeriod,
insertPostedJournalEntry as insertAtomicPostedJournalEntry,
insertTransaction,
} from './fixtures'
async function insertPostedJournalEntry(params: {
userId: string
companyId: string
fiscalPeriodId: string
entryDate: string
sourceType: 'opening_balance' | 'manual' | 'bank_transaction' | 'import' | 'storno' | 'correction'
voucherNumber: number
amount?: number
/** Line rows to book; defaults to the classic 1930 debit / 2091 credit pair. */
lines?: Array<{ account: string; debit: number; credit: number }>
}): Promise<string> {
const amount = params.amount ?? 1000
const lines = params.lines ?? [
{ account: '1930', debit: amount, credit: 0 },
{ account: '2091', debit: 0, credit: amount },
]
return insertAtomicPostedJournalEntry({
userId: params.userId,
companyId: params.companyId,
fiscalPeriodId: params.fiscalPeriodId,
voucherNumber: params.voucherNumber,
entryDate: params.entryDate,
description: `Test ${params.sourceType}`,
sourceType: params.sourceType,
lines: lines.map((line) => ({
accountNumber: line.account,
debitAmount: line.debit,
creditAmount: line.credit,
})),
})
}
describe('get_account_gl_lines_for_matching RPC: N:1 candidates', () => {
it('returns already-matched vouchers (with link count) only when p_include_matched is true', async () => {
const userId = await insertAuthUser()
const companyId = await insertCompany({ createdBy: userId })
const fiscalPeriodId = await insertFiscalPeriod({
userId, companyId, periodStart: '2026-01-01', periodEnd: '2026-12-31',
})
// One unmatched voucher, one voucher already settled by TWO transactions
// (the salary-run-paid-in-two-transfers shape).
const unmatchedEntry = await insertPostedJournalEntry({
userId, companyId, fiscalPeriodId,
entryDate: '2026-03-15', sourceType: 'bank_transaction', voucherNumber: 1, amount: 1500,
})
const matchedEntry = await insertPostedJournalEntry({
userId, companyId, fiscalPeriodId,
entryDate: '2026-03-20', sourceType: 'manual', voucherNumber: 2, amount: 30000,
})
await insertTransaction({ companyId, userId, currency: 'SEK', journalEntryId: matchedEntry })
await insertTransaction({ companyId, userId, currency: 'SEK', journalEntryId: matchedEntry })
// Default (p_include_matched=false): parity with get_unlinked_gl_lines: only
// the unmatched voucher, count 0.
const { rows: unmatchedOnly } = await getPool().query(
`SELECT journal_entry_id, linked_transaction_count
FROM public.get_account_gl_lines_for_matching(p_company_id => $1)`,
[companyId],
)
const unmatchedIds = new Set(unmatchedOnly.map((r) => r.journal_entry_id))
expect(unmatchedIds.has(unmatchedEntry)).toBe(true)
expect(unmatchedIds.has(matchedEntry)).toBe(false)
expect(unmatchedOnly.find((r) => r.journal_entry_id === unmatchedEntry).linked_transaction_count).toBe(0)
// p_include_matched=true: the matched voucher appears too, reporting both links.
const { rows: withMatched } = await getPool().query(
`SELECT journal_entry_id, linked_transaction_count
FROM public.get_account_gl_lines_for_matching(p_company_id => $1, p_include_matched => true)`,
[companyId],
)
const byId = new Map(withMatched.map((r) => [r.journal_entry_id, r.linked_transaction_count]))
expect(byId.get(unmatchedEntry)).toBe(0)
expect(byId.get(matchedEntry)).toBe(2)
})
it('still excludes opening_balance / storno / correction even with p_include_matched', async () => {
const userId = await insertAuthUser()
const companyId = await insertCompany({ createdBy: userId })
const fiscalPeriodId = await insertFiscalPeriod({
userId, companyId, periodStart: '2026-01-01', periodEnd: '2026-12-31',
})
// These book-only / IB vouchers have no bank-feed counterpart and can never
// be a match target: the include_matched opt-in must not resurrect them.
await insertPostedJournalEntry({
userId, companyId, fiscalPeriodId,
entryDate: '2026-01-01', sourceType: 'opening_balance', voucherNumber: 1, amount: 50000,
})
await insertPostedJournalEntry({
userId, companyId, fiscalPeriodId,
entryDate: '2026-05-02', sourceType: 'storno', voucherNumber: 2, amount: 25000,
})
await insertPostedJournalEntry({
userId, companyId, fiscalPeriodId,
entryDate: '2026-05-02', sourceType: 'correction', voucherNumber: 3, amount: 25000,
})
const bankEntry = await insertPostedJournalEntry({
userId, companyId, fiscalPeriodId,
entryDate: '2026-05-03', sourceType: 'bank_transaction', voucherNumber: 4, amount: 1500,
})
const { rows } = await getPool().query(
`SELECT journal_entry_id, source_type
FROM public.get_account_gl_lines_for_matching(p_company_id => $1, p_include_matched => true)`,
[companyId],
)
const returnedIds = new Set(rows.map((r) => r.journal_entry_id))
expect(returnedIds.has(bankEntry)).toBe(true)
expect(rows.find((r) => r.source_type === 'opening_balance')).toBeUndefined()
expect(rows.find((r) => r.source_type === 'storno')).toBeUndefined()
expect(rows.find((r) => r.source_type === 'correction')).toBeUndefined()
})
})
describe('get_account_gl_lines_for_matching RPC: account-scoped link count (#1026)', () => {
it('surfaces the unsettled leg of an own-account transfer by default', async () => {
const userId = await insertAuthUser()
const companyId = await insertCompany({ createdBy: userId })
const fiscalPeriodId = await insertFiscalPeriod({
userId, companyId, periodStart: '2026-01-01', periodEnd: '2026-12-31',
})
await insertCashAccount({ companyId, ledgerAccount: '1930' })
const acc1940 = await insertCashAccount({ companyId, ledgerAccount: '1940' })
// Own-account transfer: one voucher, debit 1930 / credit 1940. The outgoing
// leg (a transaction on the 1940 account) is already matched to it.
const transferEntry = await insertPostedJournalEntry({
userId, companyId, fiscalPeriodId,
entryDate: '2026-06-26', sourceType: 'manual', voucherNumber: 1,
lines: [
{ account: '1930', debit: 2344.16, credit: 0 },
{ account: '1940', debit: 0, credit: 2344.16 },
],
})
await insertTransaction({
companyId, userId, amount: -2344.16, date: '2026-06-26',
journalEntryId: transferEntry, cashAccountId: acc1940,
})
// From 1930's perspective the voucher is unmatched: it must appear in the
// DEFAULT list (no toggle) with a zero link count, so ranking/auto-select
// treat it as a normal candidate.
const { rows: on1930 } = await getPool().query(
`SELECT journal_entry_id, linked_transaction_count
FROM public.get_account_gl_lines_for_matching(p_company_id => $1, p_account_number => '1930')`,
[companyId],
)
const row1930 = on1930.find((r) => r.journal_entry_id === transferEntry)
expect(row1930).toBeDefined()
expect(row1930.linked_transaction_count).toBe(0)
// From 1940's perspective it IS settled: hidden by default, visible with
// the opt-in and carrying the link.
const { rows: on1940Default } = await getPool().query(
`SELECT journal_entry_id
FROM public.get_account_gl_lines_for_matching(p_company_id => $1, p_account_number => '1940')`,
[companyId],
)
expect(on1940Default.find((r) => r.journal_entry_id === transferEntry)).toBeUndefined()
const { rows: on1940Matched } = await getPool().query(
`SELECT journal_entry_id, linked_transaction_count
FROM public.get_account_gl_lines_for_matching(
p_company_id => $1, p_account_number => '1940', p_include_matched => true)`,
[companyId],
)
expect(on1940Matched.find((r) => r.journal_entry_id === transferEntry).linked_transaction_count).toBe(1)
})
it('keeps same-account N:1 vouchers behind the include_matched opt-in', async () => {
const userId = await insertAuthUser()
const companyId = await insertCompany({ createdBy: userId })
const fiscalPeriodId = await insertFiscalPeriod({
userId, companyId, periodStart: '2026-01-01', periodEnd: '2026-12-31',
})
const acc1930 = await insertCashAccount({ companyId, ledgerAccount: '1930' })
// A salary-run shape: one voucher on 1930, partially settled by a first
// transfer FROM THE SAME account. The second instalment must still require
// the deliberate opt-in; account scoping must not open the N:1 floodgate.
const salaryEntry = await insertPostedJournalEntry({
userId, companyId, fiscalPeriodId,
entryDate: '2026-06-25', sourceType: 'manual', voucherNumber: 1, amount: 30000,
})
await insertTransaction({
companyId, userId, amount: -10000, date: '2026-06-25',
journalEntryId: salaryEntry, cashAccountId: acc1930,
})
const { rows: byDefault } = await getPool().query(
`SELECT journal_entry_id
FROM public.get_account_gl_lines_for_matching(p_company_id => $1, p_account_number => '1930')`,
[companyId],
)
expect(byDefault.find((r) => r.journal_entry_id === salaryEntry)).toBeUndefined()
const { rows: withMatched } = await getPool().query(
`SELECT journal_entry_id, linked_transaction_count
FROM public.get_account_gl_lines_for_matching(
p_company_id => $1, p_account_number => '1930', p_include_matched => true)`,
[companyId],
)
expect(withMatched.find((r) => r.journal_entry_id === salaryEntry).linked_transaction_count).toBe(1)
})
it('treats transactions without a resolvable cash account as settling every account', async () => {
const userId = await insertAuthUser()
const companyId = await insertCompany({ createdBy: userId })
const fiscalPeriodId = await insertFiscalPeriod({
userId, companyId, periodStart: '2026-01-01', periodEnd: '2026-12-31',
})
// Legacy shape: the linked transaction carries no cash_account_id, so it
// could belong to any account. The voucher must stay hidden by default
// (conservative: pre-account-scoping behavior).
const legacyEntry = await insertPostedJournalEntry({
userId, companyId, fiscalPeriodId,
entryDate: '2026-06-20', sourceType: 'bank_transaction', voucherNumber: 1, amount: 500,
})
await insertTransaction({
companyId, userId, amount: 500, date: '2026-06-20',
journalEntryId: legacyEntry, cashAccountId: null,
})
const { rows: byDefault } = await getPool().query(
`SELECT journal_entry_id
FROM public.get_account_gl_lines_for_matching(p_company_id => $1, p_account_number => '1930')`,
[companyId],
)
expect(byDefault.find((r) => r.journal_entry_id === legacyEntry)).toBeUndefined()
const { rows: withMatched } = await getPool().query(
`SELECT journal_entry_id, linked_transaction_count
FROM public.get_account_gl_lines_for_matching(
p_company_id => $1, p_account_number => '1930', p_include_matched => true)`,
[companyId],
)
expect(withMatched.find((r) => r.journal_entry_id === legacyEntry).linked_transaction_count).toBe(1)
})
})