Files
accounted/app/api/transactions/bulk-book/route.ts
T
Jakob WennbergandClaude Opus 4.7 4da87e5e4c feat(transactions): bulk-book + is-booked predicate (#606)
* feat(transactions): bulk-book + is-booked predicate

Closes the second of the two multi-tx ↔ multi-voucher flows from the
original plan. Where PR #603's match_batch_allocate took 1 tx and
spread it across N invoices (samlingsbetalning), this PR takes N bank
transactions on the same day and rolls them up into ONE combined
verifikat (samlingsverifikation per BFL 5 kap 6§ st 3) — the kiosk
masshantering pattern the user explicitly asked for.

## Backend (Phase 3b)

- **PL/pgSQL RPC** bulk_book_transactions: two branches, both atomic.

  1. Link to existing posted verifikat (p_existing_journal_entry_id):
     no new JE. Validates the JE's 19xx net equals sum(tx.amount),
     inserts N transaction_voucher_links rows, and for N=1 also sets
     transactions.journal_entry_id (1:1 reader-path back-compat).

  2. Create new combined verifikat (p_new_entry with pre-computed
     balanced lines): the route's applyTemplate() has already done
     ratio + VAT expansion per the chosen mode. The RPC validates the
     lines balance and the 1930 net matches sum(tx.amount), then
     commits via commit_journal_entry.

  Same security pattern as match_batch_allocate: company-member check
  via auth.uid(), SELECT … FOR UPDATE on each tx in id order,
  deterministic fiscal-period resolution (ORDER BY period_start DESC).

- **Endpoint** POST /api/transactions/bulk-book — fetches template via
  RLS, expands per mode (one_line_per_tx | sum_per_account) using
  lib/bookkeeping/template-library.applyTemplate, passes the resulting
  lines to the RPC. On success emits one transaction.reconciled event
  per tx.

- **22 new BULK_BOOK_* error codes** (sv + en) covering all guard
  paths.

## UI (Phase 5b)

- **BulkBookDialog** — template picker + mode toggle (segmented
  control: en rad per transaktion / summera per konto) + live preview
  table with balance + bank-leg invariant indicators. Confirm only
  enabled when both pass.

- **Multi-select inbox** — sticky action bar gains a "Bokför i klump"
  button gated by same-date + same-direction across selected txs.
  Tooltip explains the disabled state.

## Phase 6: is-booked predicate

New lib/transactions/is-booked.ts. After multi-allocation and bulk-
book, tx.journal_entry_id can be NULL even though the tx is anchored
(via invoice_payments / supplier_invoice_payments /
transaction_voucher_links). The helper checks all three storage
locations so future readers don't falsely show multi-anchored txs as
"unbooked". Companion getPrimaryJournalEntryId() resolves the best
JE link to surface in UI. SQL mirror is_transaction_booked() exists
from the PR #602 foundation migration.

Existing readers (TransactionHistoryList, TransactionInboxCard) are
not yet refactored to use the helper — that's a follow-up that
touches per-tx JE links across multiple call sites. The helper is
documented + tested so subsequent refactors are mechanical.

## Tests

- tests/pg/bulk-book-transactions.pg.test.ts — 8 pg-real scenarios
  (happy path create-new with 3 txs, happy path link-existing, date
  mismatch, direction mismatch, amount mismatch, unbalanced lines,
  unauthorized).
- app/api/transactions/bulk-book/__tests__/route.test.ts — 5 unit
  tests (schema XOR, link path, create-new with template fetch +
  applyTemplate, structured-error mapping).
- lib/transactions/__tests__/is-booked.test.ts — 11 cases covering
  all three storage locations + primary-JE resolution.

26 unit tests pass on touched paths. RPC migration applied to remote
via Supabase MCP.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(bulk-book): PR #606 review round 1 + CI fixes

Closes the build failure and the two real Greptile findings.

## CI

- **core-only + Vercel build fail**: I used useMemo for
  selectedTransactions and bulkBookEligible on the transactions page
  without importing it. TypeScript build (`next build`) caught it
  with "Cannot find name 'useMemo'". Fixed the import.

## Review findings

- **(P1) Currency mismatch returned BULK_BOOK_DIRECTION_MISMATCH**
  whose user-facing message blames direction. Mixed SEK + EUR batches
  would show "All transactions must be the same direction" which is
  factually wrong. Introduced dedicated BULK_BOOK_MIXED_CURRENCY code
  (sv + en) explaining the actual constraint, and switched the route
  to use it.

- **(P1) Branch B (create-new) N=1 missed
  reconciliation_method='manual'**. Branch A's N=1 UPDATE sets it
  alongside journal_entry_id; Branch B's didn't, leaving the
  reconciliation_method NULL even though the single tx was reconciled
  via the same flow. Downstream readers (reconciliation reports,
  status indicators) would treat the two N=1 paths differently. New
  follow-up migration patches Branch B's final UPDATE.

RPC patch applied to remote via Supabase MCP. 26 unit tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-29 14:55:24 +02:00

255 lines
8.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { validateBody } from '@/lib/api/validate'
import { BulkBookSchema } from '@/lib/api/schemas'
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
import { applyTemplate } from '@/lib/bookkeeping/template-library'
import { eventBus } from '@/lib/events/bus'
import { ensureInitialized } from '@/lib/init'
import type { BookingTemplateLibraryLine, Transaction } from '@/types'
ensureInitialized()
interface RpcOk {
ok: true
mode: 'link_existing' | 'create_new'
journal_entry_id: string
voucher_series: string | null
voucher_number: number | null
linked_tx_count: number
tx_sum: number
}
interface RpcErr {
ok: false
code: string
details?: Record<string, unknown>
}
interface ComputedLine {
account_number: string
debit_amount: number
credit_amount: number
currency: string
line_description?: string
sort_order?: number
}
function round2(n: number): number {
return Math.round(n * 100) / 100
}
/**
* POST /api/transactions/bulk-book
*
* Bulk-book N bank transactions on the same date into one combined
* verifikat (samlingsverifikation per BFL 5 kap 6§). Two flows:
*
* 1. Link to existing voucher — { tx_ids, existing_journal_entry_id }.
* No new JE; the RPC just inserts N transaction_voucher_links rows.
*
* 2. Create new from template — { tx_ids, template_id, mode,
* entry_description }. The route fetches the template, expands it
* per the chosen mode, and passes the resulting balanced lines to
* the RPC. The RPC then commits the verifikat atomically.
*
* `applyTemplate` lives in TS (ratio / VAT math); the RPC stays focused
* on locking, balance, and link insertion.
*/
export const POST = withRouteContext(
'transaction.bulk_book',
async (request, ctx) => {
const { user, supabase, companyId, log, requestId } = ctx
const validation = await validateBody(request, BulkBookSchema, {
log,
operation: 'transaction.bulk_book',
})
if (!validation.success) return validation.response
const body = validation.data
const opLog = log.child({ txCount: body.tx_ids.length })
// Branch 2 needs the template + tx amounts; branch 1 hands off to the
// RPC directly with a null new_entry.
let newEntryPayload: { description: string; lines: ComputedLine[] } | null = null
if (body.template_id && body.mode && body.entry_description) {
// Fetch the template. RLS scopes to user's companies + system templates,
// so we don't need a company_id filter here.
const { data: template, error: templateError } = await supabase
.from('booking_template_library')
.select('id, name, lines, is_active')
.eq('id', body.template_id)
.single()
if (templateError || !template) {
return errorResponseFromCode('BULK_BOOK_TEMPLATE_NOT_FOUND', opLog, { requestId })
}
if (!template.is_active) {
return errorResponseFromCode('BULK_BOOK_TEMPLATE_NOT_FOUND', opLog, {
requestId,
details: { reason: 'template_inactive' },
})
}
const templateLines = (template.lines ?? []) as BookingTemplateLibraryLine[]
// Need each tx's amount + currency to expand per mode. The RPC also
// re-validates (date, direction, not-already-booked) but we need the
// amount sum to drive the template expansion.
const { data: txs, error: txError } = await supabase
.from('transactions')
.select('id, amount, currency, description, date')
.in('id', body.tx_ids)
.eq('company_id', companyId)
if (txError || !txs || txs.length === 0) {
return errorResponseFromCode('BULK_BOOK_TXS_NOT_FOUND', opLog, { requestId })
}
if (txs.length !== body.tx_ids.length) {
return errorResponseFromCode('BULK_BOOK_TXS_NOT_FOUND', opLog, {
requestId,
details: { expected: body.tx_ids.length, found: txs.length },
})
}
const txTyped = txs as Pick<Transaction, 'id' | 'amount' | 'currency' | 'description' | 'date'>[]
// Same-currency invariant for v1. Mixed-currency batches would need
// FX conversion per tx; out of scope. Use the dedicated
// BULK_BOOK_MIXED_CURRENCY code so the toast doesn't blame direction
// (PR #606 review fix).
const currencies = new Set(txTyped.map((t) => t.currency))
if (currencies.size > 1) {
return errorResponseFromCode('BULK_BOOK_MIXED_CURRENCY', opLog, {
requestId,
details: { currencies: Array.from(currencies) },
})
}
const currency = txTyped[0]!.currency
const txAbsAmounts = txTyped.map((t) => Math.abs(t.amount))
const totalAbs = round2(txAbsAmounts.reduce((s, a) => s + a, 0))
const lines: ComputedLine[] = []
let sortOrder = 0
if (body.mode === 'sum_per_account') {
// One application of the template at the summed amount → one line
// per template line. Compact verifikat; per-tx detail recoverable
// via transaction_voucher_links.
const applied = applyTemplate(templateLines, totalAbs)
for (const formLine of applied) {
const debit = parseFloat(formLine.debit_amount || '0') || 0
const credit = parseFloat(formLine.credit_amount || '0') || 0
if (debit === 0 && credit === 0) continue
lines.push({
account_number: formLine.account_number,
debit_amount: round2(debit),
credit_amount: round2(credit),
currency,
line_description: formLine.line_description || undefined,
sort_order: sortOrder++,
})
}
} else {
// one_line_per_tx — apply template per tx, prefix description with
// a short tx reference so the verifikat preserves per-row audit
// detail (BFL 5 kap 7§ motpart identification).
for (const tx of txTyped) {
const applied = applyTemplate(templateLines, Math.abs(tx.amount))
for (const formLine of applied) {
const debit = parseFloat(formLine.debit_amount || '0') || 0
const credit = parseFloat(formLine.credit_amount || '0') || 0
if (debit === 0 && credit === 0) continue
const txTag = (tx.description || '').slice(0, 40).trim()
lines.push({
account_number: formLine.account_number,
debit_amount: round2(debit),
credit_amount: round2(credit),
currency,
line_description: txTag
? `${formLine.line_description ?? ''} – ${txTag}`.trim()
: formLine.line_description || undefined,
sort_order: sortOrder++,
})
}
}
}
newEntryPayload = {
description: body.entry_description,
lines,
}
}
const { data, error } = await supabase.rpc('bulk_book_transactions', {
p_tx_ids: body.tx_ids,
p_existing_journal_entry_id: body.existing_journal_entry_id ?? null,
p_new_entry: newEntryPayload,
p_user_id: user.id,
p_company_id: companyId,
})
if (error) {
opLog.error('bulk_book_transactions RPC error', error)
return errorResponseFromCode('BULK_BOOK_RPC_FAILED', opLog, {
requestId,
details: { message: error.message },
})
}
const result = data as RpcOk | RpcErr | null
if (!result || !result.ok) {
const code = (result as RpcErr | null)?.code ?? 'BULK_BOOK_RPC_FAILED'
const details = (result as RpcErr | null)?.details
return errorResponseFromCode(code, opLog, { requestId, details })
}
// Emit one transaction.reconciled event per tx so existing subscribers
// (reminder cancellation, automation, processing-history) keep working.
// Best-effort; a failure here does not roll back the booking.
const { data: linkedTxs } = await supabase
.from('transactions')
.select('*')
.in('id', body.tx_ids)
.eq('company_id', companyId)
if (linkedTxs) {
for (const tx of linkedTxs as Transaction[]) {
try {
await eventBus.emit({
type: 'transaction.reconciled',
payload: {
transaction: tx,
journalEntryId: result.journal_entry_id,
method: 'manual',
userId: user.id,
companyId,
},
})
} catch (err) {
opLog.warn('bulk_book transaction.reconciled emission failed', {
err,
txId: tx.id,
journalEntryId: result.journal_entry_id,
})
}
}
}
return NextResponse.json({
data: {
mode: result.mode,
journal_entry_id: result.journal_entry_id,
voucher_series: result.voucher_series,
voucher_number: result.voucher_number,
linked_tx_count: result.linked_tx_count,
tx_sum: result.tx_sum,
},
})
},
{ requireWrite: true },
)