* 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>
663 lines
21 KiB
TypeScript
663 lines
21 KiB
TypeScript
import { randomUUID } from 'node:crypto'
|
|
import { describe, expect, it } from 'vitest'
|
|
import type { PoolClient } from 'pg'
|
|
import { getClient, getPool } from '@/tests/pg/setup'
|
|
import {
|
|
insertAuthUser,
|
|
insertCompanyMember,
|
|
seedCompany,
|
|
} from '@/tests/pg/fixtures'
|
|
|
|
type ReplacementResult = {
|
|
new_entry_id: string
|
|
storno_entry_id: string
|
|
new_voucher_number: number
|
|
storno_voucher_number: number
|
|
}
|
|
|
|
type SeededOpeningBalance = {
|
|
oldEntryId: string
|
|
lines: Array<Record<string, unknown>>
|
|
}
|
|
|
|
async function seedOpeningBalance(params: {
|
|
userId: string
|
|
companyId: string
|
|
fiscalPeriodId: string
|
|
debit?: number
|
|
link?: boolean
|
|
}): Promise<SeededOpeningBalance> {
|
|
const amount = params.debit ?? 100
|
|
await getPool().query(
|
|
`INSERT INTO public.chart_of_accounts
|
|
(user_id, company_id, account_number, account_name, account_class,
|
|
account_type, normal_balance, is_active)
|
|
VALUES
|
|
($1, $2, '1930', 'Bankkonto', 1, 'asset', 'debit', true),
|
|
($1, $2, '2010', 'Eget kapital', 2, 'equity', 'credit', true)
|
|
ON CONFLICT (company_id, account_number) DO NOTHING`,
|
|
[params.userId, params.companyId],
|
|
)
|
|
const accounts = await getPool().query<{ id: string; account_number: string }>(
|
|
`SELECT id, account_number
|
|
FROM public.chart_of_accounts
|
|
WHERE company_id = $1
|
|
AND account_number = ANY($2::text[])
|
|
ORDER BY account_number`,
|
|
[params.companyId, ['1930', '2010']],
|
|
)
|
|
const accountIds = new Map(accounts.rows.map((account) => [account.account_number, account.id]))
|
|
|
|
expect(accountIds.get('1930')).toBeTruthy()
|
|
expect(accountIds.get('2010')).toBeTruthy()
|
|
|
|
const oldEntryId = randomUUID()
|
|
const client = await getClient()
|
|
try {
|
|
await client.query('BEGIN')
|
|
const voucher = await client.query<{ next_number: number }>(
|
|
`SELECT COALESCE(max(voucher_number), 0)::int + 1 AS next_number
|
|
FROM public.journal_entries
|
|
WHERE company_id = $1
|
|
AND fiscal_period_id = $2
|
|
AND voucher_series = 'A'`,
|
|
[params.companyId, params.fiscalPeriodId],
|
|
)
|
|
await client.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',
|
|
'Old opening balance', 'opening_balance', 'posted')`,
|
|
[
|
|
oldEntryId,
|
|
params.userId,
|
|
params.companyId,
|
|
params.fiscalPeriodId,
|
|
voucher.rows[0]!.next_number,
|
|
],
|
|
)
|
|
await client.query(
|
|
`INSERT INTO public.journal_entry_lines
|
|
(journal_entry_id, account_number, account_id, debit_amount,
|
|
credit_amount, currency, dimensions, sort_order)
|
|
VALUES
|
|
($1, '1930', $2, $4, 0, 'SEK', '{}'::jsonb, 0),
|
|
($1, '2010', $3, 0, $4, 'SEK', '{}'::jsonb, 1)`,
|
|
[oldEntryId, accountIds.get('1930'), accountIds.get('2010'), amount],
|
|
)
|
|
await client.query(
|
|
`INSERT INTO public.voucher_sequences
|
|
(company_id, user_id, fiscal_period_id, voucher_series, last_number)
|
|
VALUES ($1, $2, $3, 'A', $4)
|
|
ON CONFLICT (company_id, fiscal_period_id, voucher_series)
|
|
DO UPDATE SET last_number = GREATEST(public.voucher_sequences.last_number, $4)`,
|
|
[
|
|
params.companyId,
|
|
params.userId,
|
|
params.fiscalPeriodId,
|
|
voucher.rows[0]!.next_number,
|
|
],
|
|
)
|
|
await client.query('COMMIT')
|
|
} catch (error) {
|
|
await client.query('ROLLBACK').catch(() => {})
|
|
throw error
|
|
} finally {
|
|
client.release()
|
|
}
|
|
if (params.link !== false) {
|
|
await getPool().query(
|
|
`UPDATE public.fiscal_periods
|
|
SET opening_balance_entry_id = $1,
|
|
opening_balances_set = true
|
|
WHERE id = $2`,
|
|
[oldEntryId, params.fiscalPeriodId],
|
|
)
|
|
}
|
|
|
|
return {
|
|
oldEntryId,
|
|
lines: [
|
|
{
|
|
account_number: '1930',
|
|
account_id: accountIds.get('1930'),
|
|
debit_amount: 150,
|
|
credit_amount: 0,
|
|
currency: 'SEK',
|
|
amount_in_currency: null,
|
|
exchange_rate: null,
|
|
line_description: 'IB 1930',
|
|
tax_code: null,
|
|
dimensions: {},
|
|
sort_order: 0,
|
|
},
|
|
{
|
|
account_number: '2010',
|
|
account_id: accountIds.get('2010'),
|
|
debit_amount: 0,
|
|
credit_amount: 150,
|
|
currency: 'SEK',
|
|
amount_in_currency: null,
|
|
exchange_rate: null,
|
|
line_description: 'IB 2010',
|
|
tax_code: null,
|
|
dimensions: {},
|
|
sort_order: 1,
|
|
},
|
|
],
|
|
}
|
|
}
|
|
|
|
async function setRole(
|
|
client: PoolClient,
|
|
role: 'authenticated' | 'service_role',
|
|
userId?: string,
|
|
): Promise<void> {
|
|
const claims = userId
|
|
? { sub: userId, role }
|
|
: { role }
|
|
await client.query(`SELECT set_config('request.jwt.claims', $1, true)`, [JSON.stringify(claims)])
|
|
await client.query(`SELECT set_config('request.jwt.claim.role', $1, true)`, [role])
|
|
await client.query(`SELECT set_config('request.jwt.claim.sub', $1, true)`, [userId ?? ''])
|
|
const roleStatements = {
|
|
authenticated: 'SET LOCAL ROLE authenticated',
|
|
service_role: 'SET LOCAL ROLE service_role',
|
|
} as const
|
|
await client.query(roleStatements[role])
|
|
}
|
|
|
|
async function runAs<T>(
|
|
role: 'authenticated' | 'service_role',
|
|
userId: string | undefined,
|
|
operation: (client: PoolClient) => Promise<T>,
|
|
): Promise<T> {
|
|
const client = await getClient()
|
|
try {
|
|
await client.query('BEGIN')
|
|
await setRole(client, role, userId)
|
|
const result = await operation(client)
|
|
await client.query('COMMIT')
|
|
return result
|
|
} catch (error) {
|
|
await client.query('ROLLBACK').catch(() => {})
|
|
throw error
|
|
} finally {
|
|
client.release()
|
|
}
|
|
}
|
|
|
|
async function callReplacement(
|
|
client: PoolClient,
|
|
params: {
|
|
companyId: string
|
|
fiscalPeriodId: string
|
|
expectedOldEntryId: string
|
|
userId: string
|
|
lines: Array<Record<string, unknown>>
|
|
},
|
|
): Promise<ReplacementResult> {
|
|
const result = await client.query<ReplacementResult>(
|
|
`SELECT * FROM public.commit_opening_balance_replacement(
|
|
$1::uuid, $2::uuid, $3::uuid, $4::uuid, '2026-01-01'::date,
|
|
'Replacement opening balance', 'A', $5::jsonb, NULL, NULL
|
|
)`,
|
|
[
|
|
params.companyId,
|
|
params.fiscalPeriodId,
|
|
params.expectedOldEntryId,
|
|
params.userId,
|
|
JSON.stringify(params.lines),
|
|
],
|
|
)
|
|
return result.rows[0]!
|
|
}
|
|
|
|
async function expectUnchanged(params: {
|
|
companyId: string
|
|
fiscalPeriodId: string
|
|
oldEntryId: string
|
|
entryCount: number
|
|
lastNumber: number
|
|
}): Promise<void> {
|
|
const state = await getPool().query<{
|
|
opening_balance_entry_id: string | null
|
|
opening_balances_set: boolean
|
|
old_status: string
|
|
entry_count: number
|
|
last_number: number
|
|
}>(
|
|
`SELECT fp.opening_balance_entry_id,
|
|
fp.opening_balances_set,
|
|
old.status AS old_status,
|
|
(SELECT count(*)::int
|
|
FROM public.journal_entries je
|
|
WHERE je.company_id = $1) AS entry_count,
|
|
sequence.last_number
|
|
FROM public.fiscal_periods fp
|
|
JOIN public.journal_entries old ON old.id = $2
|
|
JOIN public.voucher_sequences sequence
|
|
ON sequence.company_id = $1
|
|
AND sequence.fiscal_period_id = fp.id
|
|
AND sequence.voucher_series = 'A'
|
|
WHERE fp.id = $3`,
|
|
[params.companyId, params.oldEntryId, params.fiscalPeriodId],
|
|
)
|
|
expect(state.rows[0]).toEqual({
|
|
opening_balance_entry_id: params.oldEntryId,
|
|
opening_balances_set: true,
|
|
old_status: 'posted',
|
|
entry_count: params.entryCount,
|
|
last_number: params.lastNumber,
|
|
})
|
|
}
|
|
|
|
describe('commit_opening_balance_replacement', () => {
|
|
it('atomically replaces the IB for a member without a duplicate balance', async () => {
|
|
const { userId: ownerId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const memberId = await insertAuthUser()
|
|
await insertCompanyMember({ companyId, userId: memberId, role: 'member' })
|
|
const seeded = await seedOpeningBalance({
|
|
userId: ownerId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
})
|
|
|
|
const outcome = await runAs('authenticated', memberId, (client) => callReplacement(client, {
|
|
companyId,
|
|
fiscalPeriodId,
|
|
expectedOldEntryId: seeded.oldEntryId,
|
|
userId: memberId,
|
|
lines: seeded.lines,
|
|
}))
|
|
|
|
const state = await getPool().query<{
|
|
id: string
|
|
status: string
|
|
source_type: string
|
|
reverses_id: string | null
|
|
reversed_by_id: string | null
|
|
user_id: string
|
|
}>(
|
|
`SELECT id, status, source_type, reverses_id, reversed_by_id, user_id
|
|
FROM public.journal_entries
|
|
WHERE id = ANY($1::uuid[])`,
|
|
[[seeded.oldEntryId, outcome.new_entry_id, outcome.storno_entry_id]],
|
|
)
|
|
expect(state.rows).toEqual(expect.arrayContaining([
|
|
expect.objectContaining({
|
|
id: seeded.oldEntryId,
|
|
status: 'reversed',
|
|
reversed_by_id: outcome.storno_entry_id,
|
|
}),
|
|
expect.objectContaining({
|
|
id: outcome.new_entry_id,
|
|
status: 'posted',
|
|
source_type: 'opening_balance',
|
|
user_id: memberId,
|
|
}),
|
|
expect.objectContaining({
|
|
id: outcome.storno_entry_id,
|
|
status: 'posted',
|
|
source_type: 'storno',
|
|
reverses_id: seeded.oldEntryId,
|
|
user_id: memberId,
|
|
}),
|
|
]))
|
|
|
|
const period = await getPool().query<{ opening_balance_entry_id: string }>(
|
|
`SELECT opening_balance_entry_id
|
|
FROM public.fiscal_periods
|
|
WHERE id = $1`,
|
|
[fiscalPeriodId],
|
|
)
|
|
expect(period.rows[0]!.opening_balance_entry_id).toBe(outcome.new_entry_id)
|
|
|
|
const net = await getPool().query<{ account_number: string; amount: number }>(
|
|
`SELECT line.account_number,
|
|
sum(line.debit_amount - line.credit_amount)::float8 AS amount
|
|
FROM public.journal_entry_lines line
|
|
JOIN public.journal_entries entry ON entry.id = line.journal_entry_id
|
|
WHERE entry.id = ANY($1::uuid[])
|
|
AND entry.status IN ('posted', 'reversed')
|
|
GROUP BY line.account_number
|
|
ORDER BY line.account_number`,
|
|
[[seeded.oldEntryId, outcome.new_entry_id, outcome.storno_entry_id]],
|
|
)
|
|
expect(net.rows).toEqual([
|
|
{ account_number: '1930', amount: 150 },
|
|
{ account_number: '2010', amount: -150 },
|
|
])
|
|
})
|
|
|
|
it('supports the service-role SIE path with a scoped member actor', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const seeded = await seedOpeningBalance({ userId, companyId, fiscalPeriodId })
|
|
|
|
const outcome = await runAs('service_role', undefined, (client) => callReplacement(client, {
|
|
companyId,
|
|
fiscalPeriodId,
|
|
expectedOldEntryId: seeded.oldEntryId,
|
|
userId,
|
|
lines: seeded.lines,
|
|
}))
|
|
|
|
expect(outcome.new_entry_id).toBeTruthy()
|
|
expect(outcome.storno_entry_id).toBeTruthy()
|
|
expect(outcome.new_voucher_number).toBe(2)
|
|
expect(outcome.storno_voucher_number).toBe(3)
|
|
})
|
|
|
|
it('rejects a viewer before writing any replacement entries', async () => {
|
|
const { userId: ownerId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const viewerId = await insertAuthUser()
|
|
await insertCompanyMember({ companyId, userId: viewerId, role: 'viewer' })
|
|
const seeded = await seedOpeningBalance({
|
|
userId: ownerId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
})
|
|
const before = await getPool().query<{ count: number }>(
|
|
`SELECT count(*)::int AS count FROM public.journal_entries WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
|
|
await expect(runAs('authenticated', viewerId, (client) => callReplacement(client, {
|
|
companyId,
|
|
fiscalPeriodId,
|
|
expectedOldEntryId: seeded.oldEntryId,
|
|
userId: viewerId,
|
|
lines: seeded.lines,
|
|
}))).rejects.toMatchObject({ code: '42501' })
|
|
|
|
await expectUnchanged({
|
|
companyId,
|
|
fiscalPeriodId,
|
|
oldEntryId: seeded.oldEntryId,
|
|
entryCount: before.rows[0]!.count,
|
|
lastNumber: 1,
|
|
})
|
|
})
|
|
|
|
it('rejects authenticated and service-role writes to an archived company', async () => {
|
|
const { userId: ownerId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const memberId = await insertAuthUser()
|
|
await insertCompanyMember({ companyId, userId: memberId, role: 'member' })
|
|
const seeded = await seedOpeningBalance({
|
|
userId: ownerId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
})
|
|
await getPool().query(
|
|
`UPDATE public.companies SET archived_at = now() WHERE id = $1`,
|
|
[companyId],
|
|
)
|
|
const before = await getPool().query<{ count: number }>(
|
|
`SELECT count(*)::int AS count FROM public.journal_entries WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
|
|
const callers: Array<{
|
|
role: 'authenticated' | 'service_role'
|
|
jwtUserId: string | undefined
|
|
}> = [
|
|
{ role: 'authenticated', jwtUserId: memberId },
|
|
{ role: 'service_role', jwtUserId: undefined },
|
|
]
|
|
|
|
for (const caller of callers) {
|
|
await expect(runAs(caller.role, caller.jwtUserId, (client) => callReplacement(client, {
|
|
companyId,
|
|
fiscalPeriodId,
|
|
expectedOldEntryId: seeded.oldEntryId,
|
|
userId: memberId,
|
|
lines: seeded.lines,
|
|
}))).rejects.toMatchObject({ code: '42501' })
|
|
|
|
await expectUnchanged({
|
|
companyId,
|
|
fiscalPeriodId,
|
|
oldEntryId: seeded.oldEntryId,
|
|
entryCount: before.rows[0]!.count,
|
|
lastNumber: 1,
|
|
})
|
|
}
|
|
})
|
|
|
|
it('rejects a locked period before writing any replacement entries', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const seeded = await seedOpeningBalance({ userId, companyId, fiscalPeriodId })
|
|
await getPool().query(
|
|
`UPDATE public.fiscal_periods SET locked_at = now() WHERE id = $1`,
|
|
[fiscalPeriodId],
|
|
)
|
|
const before = await getPool().query<{ count: number }>(
|
|
`SELECT count(*)::int AS count FROM public.journal_entries WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
|
|
await expect(runAs('authenticated', userId, (client) => callReplacement(client, {
|
|
companyId,
|
|
fiscalPeriodId,
|
|
expectedOldEntryId: seeded.oldEntryId,
|
|
userId,
|
|
lines: seeded.lines,
|
|
}))).rejects.toThrow(/locked\/closed fiscal period/)
|
|
|
|
await getPool().query(
|
|
`UPDATE public.fiscal_periods SET locked_at = NULL WHERE id = $1`,
|
|
[fiscalPeriodId],
|
|
)
|
|
await expectUnchanged({
|
|
companyId,
|
|
fiscalPeriodId,
|
|
oldEntryId: seeded.oldEntryId,
|
|
entryCount: before.rows[0]!.count,
|
|
lastNumber: 1,
|
|
})
|
|
})
|
|
|
|
it('rejects a company lock date before writing any replacement entries', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const seeded = await seedOpeningBalance({ userId, companyId, fiscalPeriodId })
|
|
await getPool().query(
|
|
`INSERT INTO public.company_settings
|
|
(user_id, company_id, bookkeeping_locked_through)
|
|
VALUES ($1, $2, '2026-01-01')
|
|
ON CONFLICT (company_id)
|
|
DO UPDATE SET bookkeeping_locked_through = EXCLUDED.bookkeeping_locked_through`,
|
|
[userId, companyId],
|
|
)
|
|
const before = await getPool().query<{ count: number }>(
|
|
`SELECT count(*)::int AS count FROM public.journal_entries WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
|
|
await expect(runAs('authenticated', userId, (client) => callReplacement(client, {
|
|
companyId,
|
|
fiscalPeriodId,
|
|
expectedOldEntryId: seeded.oldEntryId,
|
|
userId,
|
|
lines: seeded.lines,
|
|
}))).rejects.toThrow(/Bookkeeping is locked through/)
|
|
|
|
await getPool().query(
|
|
`UPDATE public.company_settings
|
|
SET bookkeeping_locked_through = NULL
|
|
WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
await expectUnchanged({
|
|
companyId,
|
|
fiscalPeriodId,
|
|
oldEntryId: seeded.oldEntryId,
|
|
entryCount: before.rows[0]!.count,
|
|
lastNumber: 1,
|
|
})
|
|
})
|
|
|
|
it('uses compare-and-swap protection when the period pointer changed', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const seeded = await seedOpeningBalance({ userId, companyId, fiscalPeriodId })
|
|
const competing = await seedOpeningBalance({
|
|
userId,
|
|
companyId,
|
|
fiscalPeriodId,
|
|
debit: 125,
|
|
link: false,
|
|
})
|
|
await getPool().query(
|
|
`UPDATE public.fiscal_periods
|
|
SET opening_balances_set = false
|
|
WHERE id = $1`,
|
|
[fiscalPeriodId],
|
|
)
|
|
await getPool().query(
|
|
`UPDATE public.fiscal_periods
|
|
SET opening_balance_entry_id = $1,
|
|
opening_balances_set = true
|
|
WHERE id = $2`,
|
|
[competing.oldEntryId, fiscalPeriodId],
|
|
)
|
|
const before = await getPool().query<{ count: number }>(
|
|
`SELECT count(*)::int AS count FROM public.journal_entries WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
|
|
await expect(runAs('authenticated', userId, (client) => callReplacement(client, {
|
|
companyId,
|
|
fiscalPeriodId,
|
|
expectedOldEntryId: seeded.oldEntryId,
|
|
userId,
|
|
lines: seeded.lines,
|
|
}))).rejects.toMatchObject({ code: '40001' })
|
|
|
|
const state = await getPool().query<{
|
|
opening_balance_entry_id: string
|
|
original_status: string
|
|
entry_count: number
|
|
}>(
|
|
`SELECT fp.opening_balance_entry_id,
|
|
original.status AS original_status,
|
|
(SELECT count(*)::int FROM public.journal_entries WHERE company_id = $1) AS entry_count
|
|
FROM public.fiscal_periods fp
|
|
JOIN public.journal_entries original ON original.id = $2
|
|
WHERE fp.id = $3`,
|
|
[companyId, seeded.oldEntryId, fiscalPeriodId],
|
|
)
|
|
expect(state.rows[0]).toEqual({
|
|
opening_balance_entry_id: competing.oldEntryId,
|
|
original_status: 'posted',
|
|
entry_count: before.rows[0]!.count,
|
|
})
|
|
})
|
|
|
|
it('rolls back vouchers, storno, pointer, status, and sequence on a late failure', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const seeded = await seedOpeningBalance({ userId, companyId, fiscalPeriodId })
|
|
const before = await getPool().query<{ count: number }>(
|
|
`SELECT count(*)::int AS count FROM public.journal_entries WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
const client = await getClient()
|
|
|
|
try {
|
|
await client.query('BEGIN')
|
|
await client.query(`
|
|
CREATE FUNCTION public.test_fail_atomic_ib_pointer_swap()
|
|
RETURNS trigger
|
|
LANGUAGE plpgsql
|
|
AS $trigger$
|
|
BEGIN
|
|
IF NEW.opening_balance_entry_id IS DISTINCT FROM OLD.opening_balance_entry_id THEN
|
|
RAISE EXCEPTION 'forced late pointer failure';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$trigger$
|
|
`)
|
|
await client.query(`
|
|
CREATE TRIGGER test_fail_atomic_ib_pointer_swap
|
|
BEFORE UPDATE ON public.fiscal_periods
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION public.test_fail_atomic_ib_pointer_swap()
|
|
`)
|
|
await setRole(client, 'authenticated', userId)
|
|
await client.query('SAVEPOINT before_replacement')
|
|
|
|
await expect(callReplacement(client, {
|
|
companyId,
|
|
fiscalPeriodId,
|
|
expectedOldEntryId: seeded.oldEntryId,
|
|
userId,
|
|
lines: seeded.lines,
|
|
})).rejects.toThrow(/forced late pointer failure/)
|
|
|
|
await client.query('ROLLBACK TO SAVEPOINT before_replacement')
|
|
await client.query('RESET ROLE')
|
|
|
|
const state = await client.query<{
|
|
opening_balance_entry_id: string
|
|
opening_balances_set: boolean
|
|
old_status: string
|
|
entry_count: number
|
|
last_number: number
|
|
}>(
|
|
`SELECT fp.opening_balance_entry_id,
|
|
fp.opening_balances_set,
|
|
old.status AS old_status,
|
|
(SELECT count(*)::int FROM public.journal_entries WHERE company_id = $1) AS entry_count,
|
|
sequence.last_number
|
|
FROM public.fiscal_periods fp
|
|
JOIN public.journal_entries old ON old.id = $2
|
|
JOIN public.voucher_sequences sequence
|
|
ON sequence.company_id = $1
|
|
AND sequence.fiscal_period_id = fp.id
|
|
AND sequence.voucher_series = 'A'
|
|
WHERE fp.id = $3`,
|
|
[companyId, seeded.oldEntryId, fiscalPeriodId],
|
|
)
|
|
expect(state.rows[0]).toEqual({
|
|
opening_balance_entry_id: seeded.oldEntryId,
|
|
opening_balances_set: true,
|
|
old_status: 'posted',
|
|
entry_count: before.rows[0]!.count,
|
|
last_number: 1,
|
|
})
|
|
} finally {
|
|
await client.query('ROLLBACK').catch(() => {})
|
|
client.release()
|
|
}
|
|
})
|
|
|
|
it('grants execution only to authenticated and service-role callers', async () => {
|
|
const privileges = await getPool().query<{
|
|
authenticated_can: boolean
|
|
service_role_can: boolean
|
|
anon_can: boolean
|
|
}>(
|
|
`SELECT
|
|
has_function_privilege(
|
|
'authenticated',
|
|
'public.commit_opening_balance_replacement(uuid,uuid,uuid,uuid,date,text,text,jsonb,text,text)',
|
|
'EXECUTE'
|
|
) AS authenticated_can,
|
|
has_function_privilege(
|
|
'service_role',
|
|
'public.commit_opening_balance_replacement(uuid,uuid,uuid,uuid,date,text,text,jsonb,text,text)',
|
|
'EXECUTE'
|
|
) AS service_role_can,
|
|
has_function_privilege(
|
|
'anon',
|
|
'public.commit_opening_balance_replacement(uuid,uuid,uuid,uuid,date,text,text,jsonb,text,text)',
|
|
'EXECUTE'
|
|
) AS anon_can`,
|
|
)
|
|
expect(privileges.rows[0]).toEqual({
|
|
authenticated_can: true,
|
|
service_role_can: true,
|
|
anon_can: false,
|
|
})
|
|
})
|
|
})
|