* feat(reconciliation): close the bank-feed loop on voucher links and re-tag mis-typed opening balances
Two related fixes to bank reconciliation correctness:
1. Auto-reconcile on voucher link. Linking an invoice or supplier invoice to
an existing voucher previously advanced only the invoice — the bank
transaction that paid it kept sitting in the Transactions inbox with a null
journal_entry_id. linkInvoiceToVoucher / linkSupplierInvoiceToVoucher now
call autoReconcileTransactionForLinkedVoucher (lib/reconciliation), which
links the bank transaction to the same verifikat when exactly one unbooked
line matches it. Best-effort and post-commit: a failure here never fails the
link. The result surfaces reconciledTransactionId; the inbox row leaves the
list and the UI shows link_success_tx_reconciled.
2. Re-tag mis-typed opening balances. getReconciliationStatus and the GL-line
matching RPCs identify a cash account's ingående balans solely by
journal_entries.source_type='opening_balance'. Companies migrated from other
systems often booked the bank IB as an ordinary voucher (source_type
'import' or 'manual'), so it was never excluded and surfaced as a phantom
reconciliation difference equal to the opening balance. Adds:
- migration mark_entry_as_opening_balance: a GUC-gated carve-out in the
immutability trigger plus a SECURITY DEFINER RPC that validates the entry
(balance-sheet lines only, dated on a fiscal-period boundary), flips the
source_type, and writes an audit row — no blanket data sweep.
- POST /api/reconciliation/bank/mark-opening-balance + MarkOpeningBalanceSchema.
- BankReconciliationView action to trigger it from the IB diff.
The gnubok_create_voucher executor now accepts a typed is_opening_balance flag
and derives source_type='opening_balance' only after validating class 1/2 lines
on the period start, so new IBs land correctly typed.
Covered by lib/reconciliation auto-reconcile tests, voucher-executors tests,
and a mark-entry-as-opening-balance pg-real test.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* chore: rebrand gnubok → Accounted and prune swarm agent skills
Product rebrand and skills housekeeping. No runtime behaviour change.
Rebrand: replace user-visible "gnubok" with "Accounted" across docs, READMEs,
in-code comments, doc-site content, MCP skill/resource prose, and the
gnubok-mcp package description. The MCP resource URI scheme is moved gnubok://
→ Accounted:// consistently across resource registrations, the event-type
comment, and the resource/skill tests. Deliberately preserved as stable
identifiers (NOT rebranded): the gnubok-company-id cookie, gnubok_sk_ / gnubok_inv_
token prefixes, the gnubok-mcp npm bridge name, and the AGI <gem:Programnamn>
value (kept 'gnubok' per its source comment — it is the software identifier sent
to Skatteverket and must not churn across visual rebrands).
Skills: remove the 27 swarm-* agent SKILL.md atoms (no longer used; already
absent from the agent_atom_registry in prod), refresh the remaining skill docs,
add the .claude/rules/ path-scoped rule set, and regenerate the
seed_agent_atom_bodies migration + .skill-body-manifest.json via
`npm run skills:generate` so the DB-backed skill bodies match the trimmed set.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
266 lines
11 KiB
TypeScript
266 lines
11 KiB
TypeScript
import { randomUUID } from 'node:crypto'
|
|
import { describe, expect, it } from 'vitest'
|
|
import {
|
|
insertAuthUser,
|
|
insertCompany,
|
|
insertCompanyMember,
|
|
insertFiscalPeriod,
|
|
} from '@/tests/pg/fixtures'
|
|
import { getClient, getPool, withUserContext } from '@/tests/pg/setup'
|
|
|
|
/**
|
|
* Covers 20260613120000_mark_entry_as_opening_balance:
|
|
* - the enforce_journal_entry_immutability carve-out (GUC-gated, source_type
|
|
* manual/import -> opening_balance only), and
|
|
* - the mark_entry_as_opening_balance() SECURITY DEFINER RPC and its guards.
|
|
*
|
|
* Why this matters: bank reconciliation excludes an opening balance from the
|
|
* period movement only when source_type='opening_balance'. A migrated IB booked
|
|
* as an ordinary voucher (manual/import) is otherwise immutable, so this is the
|
|
* only sanctioned path to re-tag it. The carve-out must NOT open any other edit.
|
|
*/
|
|
|
|
// Posted entry with explicit source_type and lines, created via draft so the
|
|
// balance + line-immutability triggers are satisfied. Default lines are a
|
|
// realistic bank-account IB (1930 debit / 2099 equity credit).
|
|
async function insertPostedEntry(params: {
|
|
userId: string
|
|
companyId: string
|
|
fiscalPeriodId: string
|
|
voucherNumber: number
|
|
sourceType?: string
|
|
lines?: Array<{ account: string; debit: number; credit: number }>
|
|
}): Promise<string> {
|
|
const id = randomUUID()
|
|
await getPool().query(
|
|
`INSERT INTO public.journal_entries
|
|
(id, user_id, company_id, fiscal_period_id, voucher_number, voucher_series,
|
|
entry_date, description, source_type, status)
|
|
VALUES ($1, $2, $3, $4, $5, 'A', '2026-01-01', 'Ingående balanser 2026', $6, 'draft')`,
|
|
[id, params.userId, params.companyId, params.fiscalPeriodId, params.voucherNumber, params.sourceType ?? 'manual'],
|
|
)
|
|
const lines = params.lines ?? [
|
|
{ account: '1930', debit: 5000, credit: 0 },
|
|
{ account: '2099', debit: 0, credit: 5000 },
|
|
]
|
|
for (const l of lines) {
|
|
await getPool().query(
|
|
`INSERT INTO public.journal_entry_lines
|
|
(journal_entry_id, account_number, debit_amount, credit_amount)
|
|
VALUES ($1, $2, $3, $4)`,
|
|
[id, l.account, l.debit, l.credit],
|
|
)
|
|
}
|
|
await getPool().query(`UPDATE public.journal_entries SET status = 'posted' WHERE id = $1`, [id])
|
|
return id
|
|
}
|
|
|
|
describe('mark_entry_as_opening_balance RPC', () => {
|
|
it('re-tags a manual bank-account IB to opening_balance and writes an audit row', async () => {
|
|
const userId = await insertAuthUser()
|
|
const companyId = await insertCompany({ createdBy: userId })
|
|
await insertCompanyMember({ companyId, userId, role: 'owner' })
|
|
const fiscalPeriodId = await insertFiscalPeriod({ userId, companyId })
|
|
const entryId = await insertPostedEntry({ userId, companyId, fiscalPeriodId, voucherNumber: 1 })
|
|
|
|
// withUserContext rolls back, so observe the RPC's effects inside the tx.
|
|
await withUserContext(userId, async (client) => {
|
|
const r = await client.query<{ mark_entry_as_opening_balance: { retagged: boolean; previous_source_type: string } }>(
|
|
`SELECT mark_entry_as_opening_balance($1, $2)`,
|
|
[companyId, entryId],
|
|
)
|
|
const result = r.rows[0]!.mark_entry_as_opening_balance
|
|
expect(result.retagged).toBe(true)
|
|
expect(result.previous_source_type).toBe('manual')
|
|
|
|
const after = await client.query<{ source_type: string; status: string }>(
|
|
`SELECT source_type, status FROM public.journal_entries WHERE id = $1`,
|
|
[entryId],
|
|
)
|
|
expect(after.rows[0]!.source_type).toBe('opening_balance')
|
|
expect(after.rows[0]!.status).toBe('posted')
|
|
|
|
const audit = await client.query<{ count: string }>(
|
|
`SELECT COUNT(*)::text AS count FROM public.audit_log
|
|
WHERE table_name = 'journal_entries' AND record_id = $1
|
|
AND description LIKE '%-> opening_balance%'`,
|
|
[entryId],
|
|
)
|
|
expect(Number(audit.rows[0]!.count)).toBeGreaterThanOrEqual(1)
|
|
})
|
|
})
|
|
|
|
it('re-tags an import-sourced IB as well', async () => {
|
|
const userId = await insertAuthUser()
|
|
const companyId = await insertCompany({ createdBy: userId })
|
|
await insertCompanyMember({ companyId, userId, role: 'admin' })
|
|
const fiscalPeriodId = await insertFiscalPeriod({ userId, companyId })
|
|
const entryId = await insertPostedEntry({
|
|
userId, companyId, fiscalPeriodId, voucherNumber: 1, sourceType: 'import',
|
|
})
|
|
|
|
await withUserContext(userId, async (client) => {
|
|
await client.query(`SELECT mark_entry_as_opening_balance($1, $2)`, [companyId, entryId])
|
|
const after = await client.query<{ source_type: string }>(
|
|
`SELECT source_type FROM public.journal_entries WHERE id = $1`,
|
|
[entryId],
|
|
)
|
|
expect(after.rows[0]!.source_type).toBe('opening_balance')
|
|
})
|
|
})
|
|
|
|
it('refuses callers who are not owner/admin', async () => {
|
|
const userId = await insertAuthUser()
|
|
const companyId = await insertCompany({ createdBy: userId })
|
|
await insertCompanyMember({ companyId, userId, role: 'viewer' })
|
|
const fiscalPeriodId = await insertFiscalPeriod({ userId, companyId })
|
|
const entryId = await insertPostedEntry({ userId, companyId, fiscalPeriodId, voucherNumber: 1 })
|
|
|
|
await withUserContext(userId, async (client) => {
|
|
await expect(
|
|
client.query(`SELECT mark_entry_as_opening_balance($1, $2)`, [companyId, entryId]),
|
|
).rejects.toThrow(/owners and admins/i)
|
|
})
|
|
})
|
|
|
|
it('refuses an entry that does not touch a bank/cash account', async () => {
|
|
const userId = await insertAuthUser()
|
|
const companyId = await insertCompany({ createdBy: userId })
|
|
await insertCompanyMember({ companyId, userId, role: 'owner' })
|
|
const fiscalPeriodId = await insertFiscalPeriod({ userId, companyId })
|
|
// 1510 receivable / 2440 payable — balance-sheet only, no 19xx bank line.
|
|
const entryId = await insertPostedEntry({
|
|
userId, companyId, fiscalPeriodId, voucherNumber: 1,
|
|
lines: [
|
|
{ account: '1510', debit: 5000, credit: 0 },
|
|
{ account: '2440', debit: 0, credit: 5000 },
|
|
],
|
|
})
|
|
|
|
await withUserContext(userId, async (client) => {
|
|
await expect(
|
|
client.query(`SELECT mark_entry_as_opening_balance($1, $2)`, [companyId, entryId]),
|
|
).rejects.toThrow(/bank\/cash account/i)
|
|
})
|
|
})
|
|
|
|
it('refuses an entry whose source_type is not manual/import', async () => {
|
|
const userId = await insertAuthUser()
|
|
const companyId = await insertCompany({ createdBy: userId })
|
|
await insertCompanyMember({ companyId, userId, role: 'owner' })
|
|
const fiscalPeriodId = await insertFiscalPeriod({ userId, companyId })
|
|
const entryId = await insertPostedEntry({
|
|
userId, companyId, fiscalPeriodId, voucherNumber: 1, sourceType: 'bank_transaction',
|
|
})
|
|
|
|
await withUserContext(userId, async (client) => {
|
|
await expect(
|
|
client.query(`SELECT mark_entry_as_opening_balance($1, $2)`, [companyId, entryId]),
|
|
).rejects.toThrow(/manual\/import/i)
|
|
})
|
|
})
|
|
|
|
it('refuses re-tagging in a locked fiscal period', async () => {
|
|
const userId = await insertAuthUser()
|
|
const companyId = await insertCompany({ createdBy: userId })
|
|
await insertCompanyMember({ companyId, userId, role: 'owner' })
|
|
const fiscalPeriodId = await insertFiscalPeriod({ userId, companyId })
|
|
const entryId = await insertPostedEntry({ userId, companyId, fiscalPeriodId, voucherNumber: 1 })
|
|
await getPool().query(`UPDATE public.fiscal_periods SET locked_at = now() WHERE id = $1`, [fiscalPeriodId])
|
|
|
|
await withUserContext(userId, async (client) => {
|
|
await expect(
|
|
client.query(`SELECT mark_entry_as_opening_balance($1, $2)`, [companyId, entryId]),
|
|
).rejects.toThrow(/locked fiscal period/i)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('enforce_journal_entry_immutability — source_type retag carve-out', () => {
|
|
it('blocks a bare source_type UPDATE when the bypass flag is NOT set', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedOwner()
|
|
const entryId = await insertPostedEntry({ userId, companyId, fiscalPeriodId, voucherNumber: 1 })
|
|
|
|
await expect(
|
|
getPool().query(
|
|
`UPDATE public.journal_entries SET source_type = 'opening_balance' WHERE id = $1`,
|
|
[entryId],
|
|
),
|
|
).rejects.toThrow(/Committed entries are immutable/i)
|
|
})
|
|
|
|
it('allows a source_type-only retag when the bypass flag IS set (in one tx)', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedOwner()
|
|
const entryId = await insertPostedEntry({ userId, companyId, fiscalPeriodId, voucherNumber: 1 })
|
|
|
|
const client = await getClient()
|
|
try {
|
|
await client.query('BEGIN')
|
|
await client.query(`SELECT set_config('gnubok.allow_source_type_retag', 'true', true)`)
|
|
await client.query(
|
|
`UPDATE public.journal_entries SET source_type = 'opening_balance' WHERE id = $1`,
|
|
[entryId],
|
|
)
|
|
const after = await client.query<{ source_type: string }>(
|
|
`SELECT source_type FROM public.journal_entries WHERE id = $1`,
|
|
[entryId],
|
|
)
|
|
expect(after.rows[0]!.source_type).toBe('opening_balance')
|
|
await client.query('ROLLBACK')
|
|
} finally {
|
|
client.release()
|
|
}
|
|
})
|
|
|
|
it('blocks even with the flag when another field also changes', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedOwner()
|
|
const entryId = await insertPostedEntry({ userId, companyId, fiscalPeriodId, voucherNumber: 1 })
|
|
|
|
const client = await getClient()
|
|
try {
|
|
await client.query('BEGIN')
|
|
await client.query(`SELECT set_config('gnubok.allow_source_type_retag', 'true', true)`)
|
|
await expect(
|
|
client.query(
|
|
`UPDATE public.journal_entries
|
|
SET source_type = 'opening_balance', description = 'tampered'
|
|
WHERE id = $1`,
|
|
[entryId],
|
|
),
|
|
).rejects.toThrow(/Committed entries are immutable/i)
|
|
await client.query('ROLLBACK')
|
|
} finally {
|
|
client.release()
|
|
}
|
|
})
|
|
|
|
it('blocks even with the flag when the target source_type is not opening_balance', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedOwner()
|
|
const entryId = await insertPostedEntry({ userId, companyId, fiscalPeriodId, voucherNumber: 1 })
|
|
|
|
const client = await getClient()
|
|
try {
|
|
await client.query('BEGIN')
|
|
await client.query(`SELECT set_config('gnubok.allow_source_type_retag', 'true', true)`)
|
|
await expect(
|
|
client.query(
|
|
`UPDATE public.journal_entries SET source_type = 'system' WHERE id = $1`,
|
|
[entryId],
|
|
),
|
|
).rejects.toThrow(/Committed entries are immutable/i)
|
|
await client.query('ROLLBACK')
|
|
} finally {
|
|
client.release()
|
|
}
|
|
})
|
|
})
|
|
|
|
// Local owner seed (company + owner membership + open period).
|
|
async function seedOwner(): Promise<{ userId: string; companyId: string; fiscalPeriodId: string }> {
|
|
const userId = await insertAuthUser()
|
|
const companyId = await insertCompany({ createdBy: userId })
|
|
await insertCompanyMember({ companyId, userId, role: 'owner' })
|
|
const fiscalPeriodId = await insertFiscalPeriod({ userId, companyId })
|
|
return { userId, companyId, fiscalPeriodId }
|
|
}
|