fix(bookkeeping): send each missing-underlag lookup chunk once per URL (#2430)

* fix(bookkeeping): send each missing-underlag lookup chunk once per URL

The "verifikat utan underlag" filter (/bookkeeping?missingUnderlag=true)
failed with "Verifikaten kunde inte hamtas" on a self-hosted instance as
soon as the candidate set passed one chunk of 150 ids.

Why it occurred: resolveMissingUnderlagEntries issues four lookups per
chunk. Three carry the id list once; the supplier_invoices lookup
interpolated the same chunk twice into a single .or() over
registration_journal_entry_id and payment_journal_entry_id. That URL
alone crossed the 8 KB header buffer nginx/Kong ship with, so the
gateway answered 414 before PostgREST saw the request. Hosted sits at
roughly half of Cloudflare's 16 KB ceiling on the same query. The
LOOKUP_CHUNK docblock acknowledged the doubling without sizing for it.

What was removed: the runtime-built .or() string, the chunkInList
helper and its uuid guard (the .in() array filter is injection-safe on
its own). The supplier-invoice lookup is now two .in() queries, one per
FK column, merged into the same set, so every request carries the chunk
exactly once and the proxy limit stops being a dependency rather than
moving. LOOKUP_CHUNK stays 150 and its comment is now true. The
literal filter also leaves the phantom-column scanner's unresolvable
budget.

Two secondary defects from the same report: MissingUnderlagQueryError
now carries the raw driver error as `cause` and the journal-entries
route logs it, while the response keeps the Swedish text (the log used
to say only "Nagot gick fel", hiding the 414). The "Visa saknade
underlag" badge renders the total of the last successful filtered fetch
and hides on failure, instead of borrowing the list count (0 on a
failed first load, the whole ledger after a toggle).

Alternatives: halving LOOKUP_CHUNK moves the wall instead of removing
it. Pushing the list filters into the verifikat_without_documents RPC
and deleting the TS mirror leaves one predicate instead of two, but
moves search, series, date and sort into SQL; recorded in DECISIONS.md
as the intended next step for the surface owner.

Fixes #2395

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q4kpVfYooLp8CWeUguRVpQ

* fix(bookkeeping): hide the underlag badge on network failure, log the bulk route's cause

Skeptic findings on aacb17634. The badge contract is "no honest source,
no badge": the non-OK branch cleared missingCount but the network-level
catch (offline, aborted body, JSON parse rejection) did not, so a period
or series change that failed at that level kept the previous filtered
total next to the toggle. The bulk "Inget underlag kravs" route had the
same log gap as the list route: it returned the mapped text without
logging the driver error, so a gateway 414 on that path stayed
invisible.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q4kpVfYooLp8CWeUguRVpQ

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Mattsson
2026-09-08 16:35:41 +02:00
committed by GitHub
co-authored by Claude Fable 5.1
parent b2b714f829
commit 2f787a2b3c
8 changed files with 270 additions and 48 deletions
@@ -25,6 +25,17 @@ vi.mock('@/lib/auth/require-write', () => ({
requireWritePermission: vi.fn().mockResolvedValue({ ok: true }),
}))
const { mockLogError } = vi.hoisted(() => ({ mockLogError: vi.fn() }))
vi.mock('@/lib/logger', () => {
const logger = {
info: vi.fn(),
warn: vi.fn(),
error: mockLogError,
child: (): unknown => logger,
}
return { createLogger: () => logger }
})
const mockCreateJournalEntry = vi.fn()
vi.mock('@/lib/bookkeeping/engine', () => ({
createJournalEntry: (...args: unknown[]) => mockCreateJournalEntry(...args),
@@ -357,8 +368,7 @@ describe('GET /api/bookkeeping/journal-entries', () => {
})
describe('missing_underlag=true (the dashboard deep-link filter)', () => {
// Candidate ids must be UUID-shaped: the resolver interpolates them into
// the supplier-invoice .or() filter behind a UUID guard.
// UUID-shaped candidate ids, as journal_entries.id produces them.
const E1 = '11111111-1111-4111-8111-111111111111'
const E2 = '22222222-2222-4222-8222-222222222222'
const E3 = '33333333-3333-4333-8333-333333333333'
@@ -374,7 +384,8 @@ describe('GET /api/bookkeeping/journal-entries', () => {
it('returns only entries without documents, exemption-aware, with the full-set count', async () => {
enqueue({ data: [candidate(E1, 1), candidate(E2, 2), candidate(E3, 3)], error: null }) // candidates
enqueue({ data: [{ journal_entry_id: E1 }], error: null }) // E1 has a document
enqueue({ data: [], error: null }) // no SI references
enqueue({ data: [], error: null }) // no SI references (registration FK)
enqueue({ data: [], error: null }) // no SI references (payment FK)
enqueue({ data: [], error: null }) // no SI payment-row references
enqueue({ data: [{ journal_entry_id: E3 }], error: null }) // E3 exempt
enqueue({ data: [], error: null }) // no invoices pointing at the entries
@@ -414,7 +425,8 @@ describe('GET /api/bookkeeping/journal-entries', () => {
// Out of voucher order on purpose: default sort is series+number asc.
enqueue({ data: [candidate(E3, 3), candidate(E1, 1), candidate(E2, 2)], error: null })
enqueue({ data: [], error: null }) // no documents
enqueue({ data: [], error: null }) // no SI references
enqueue({ data: [], error: null }) // no SI references (registration FK)
enqueue({ data: [], error: null }) // no SI references (payment FK)
enqueue({ data: [], error: null }) // no SI payment-row references
enqueue({ data: [], error: null }) // no exemptions
enqueue({ data: [], error: null }) // no invoices pointing at the entries
@@ -452,6 +464,7 @@ describe('GET /api/bookkeeping/journal-entries', () => {
],
error: null,
})
enqueue({ data: [], error: null }) // no SI references via payment FK
enqueue({ data: [], error: null }) // no SI payment-row references
enqueue({ data: [], error: null }) // no exemptions
enqueue({ data: [], error: null }) // no invoices pointing at the entries
@@ -475,7 +488,8 @@ describe('GET /api/bookkeeping/journal-entries', () => {
// Accounted (invoice_payments row). E2: nothing points at it.
enqueue({ data: [candidate(E1, 1), candidate(E2, 2)], error: null })
enqueue({ data: [], error: null }) // no direct documents
enqueue({ data: [], error: null }) // no SI references
enqueue({ data: [], error: null }) // no SI references (registration FK)
enqueue({ data: [], error: null }) // no SI references (payment FK)
enqueue({ data: [], error: null }) // no SI payment-row references
enqueue({ data: [], error: null }) // no exemptions
enqueue({ data: [], error: null }) // no direct invoice links
@@ -501,7 +515,8 @@ describe('GET /api/bookkeeping/journal-entries', () => {
enqueue({ data: [], error: null }) // candidates by description: none
enqueue({ data: [candidate(E1, 209)], error: null }) // candidates by voucher label
enqueue({ data: [], error: null }) // no documents
enqueue({ data: [], error: null }) // no SI references
enqueue({ data: [], error: null }) // no SI references (registration FK)
enqueue({ data: [], error: null }) // no SI references (payment FK)
enqueue({ data: [], error: null }) // no SI payment-row references
enqueue({ data: [], error: null }) // no exemptions
enqueue({ data: [], error: null }) // no invoices pointing at the entries
@@ -525,6 +540,26 @@ describe('GET /api/bookkeeping/journal-entries', () => {
expect(eqCalls).toContainEqual(['voucher_number', 209])
})
it('logs the driver error and answers with the Swedish text when a lookup fails (#2395)', async () => {
enqueue({ data: [candidate(E1, 1)], error: null }) // candidates
enqueue({ data: [], error: null }) // no documents
const driverError = { message: 'Request-URI Too Large', code: '414', details: null, hint: null }
enqueue({ data: null, error: driverError }) // SI by registration FK: gateway refused the URL
const request = createMockRequest('/api/bookkeeping/journal-entries', {
searchParams: { missing_underlag: 'true', exclude_draft: 'true' },
})
const { status, body } = await parseJsonResponse<{ error: string }>(await GET(request, { params: Promise.resolve({}) }))
expect(status).toBe(500)
expect(body.error).toBe('Verifikationerna kunde inte hämtas. Försök igen.')
// The operator gets the driver error; before this the log only carried
// the user text ("Något gick fel") and the 414 was invisible.
const call = mockLogError.mock.calls.find(([msg]) => msg === 'failed to resolve missing-underlag entries')
expect(call).toBeDefined()
expect(call![2]).toEqual({ cause: driverError })
})
it('is ignored for the drafts view', async () => {
enqueue({ data: [], error: null, count: 0 })
+3 -1
View File
@@ -113,7 +113,9 @@ export const GET = withRouteContext('bookkeeping.journal_entries.list', async (r
})
} catch (err) {
if (err instanceof MissingUnderlagQueryError) {
log.error('failed to resolve missing-underlag entries', err)
// err.message is the user-facing Swedish text; the driver error is
// what an operator needs (a gateway 414 hid behind it in #2395).
log.error('failed to resolve missing-underlag entries', err, { cause: err.cause })
return NextResponse.json(
{ error: 'Verifikationerna kunde inte hämtas. Försök igen.' },
{ status: 500 }
@@ -8,6 +8,17 @@ vi.mock('@/lib/auth/require-auth', () => ({ requireAuth: vi.fn() }))
vi.mock('@/lib/company/context', () => ({ getActiveCompanyId: vi.fn() }))
vi.mock('@/lib/auth/require-write', () => ({ requireWritePermission: vi.fn() }))
const { mockLogError } = vi.hoisted(() => ({ mockLogError: vi.fn() }))
vi.mock('@/lib/logger', () => {
const logger = {
info: vi.fn(),
warn: vi.fn(),
error: mockLogError,
child: (): unknown => logger,
}
return { createLogger: () => logger }
})
import { POST } from '../route'
import { requireAuth } from '@/lib/auth/require-auth'
import { getActiveCompanyId } from '@/lib/company/context'
@@ -65,12 +76,14 @@ describe('POST /api/bookkeeping/no-doc-required/bulk-missing', () => {
})
// Queue order per candidate chunk mirrors resolveMissingUnderlagEntries:
// documents, SI references, SI payment-row references, exemptions, then the
// documents, SI references (registration FK, then payment FK), SI payment-row
// references, exemptions, then the
// customer-invoice resolver (invoices by journal_entry_id, invoice_payments).
it('dry_run counts only entries that are missing AND not exempt', async () => {
enqueue({ data: [{ id: 'a' }, { id: 'b' }, { id: 'c' }], error: null }) // candidates
enqueue({ data: [{ journal_entry_id: 'a' }], error: null }) // a has a document
enqueue({ data: [], error: null }) // no SI references with docs
enqueue({ data: [], error: null }) // no SI references with docs (registration FK)
enqueue({ data: [], error: null }) // no SI references with docs (payment FK)
enqueue({ data: [], error: null }) // no SI payment-row references
enqueue({ data: [{ journal_entry_id: 'b' }], error: null }) // b already exempt
enqueue({ data: [], error: null }) // no invoices pointing at the entries
@@ -103,6 +116,7 @@ describe('POST /api/bookkeeping/no-doc-required/bulk-missing', () => {
],
error: null,
})
enqueue({ data: [], error: null }) // no SI references via payment FK
enqueue({
data: [
{
@@ -127,7 +141,8 @@ describe('POST /api/bookkeeping/no-doc-required/bulk-missing', () => {
// c: genuinely missing
enqueue({ data: [{ id: 'a' }, { id: 'b' }, { id: 'c' }], error: null }) // candidates
enqueue({ data: [], error: null }) // no direct documents
enqueue({ data: [], error: null }) // no SI references
enqueue({ data: [], error: null }) // no SI references (registration FK)
enqueue({ data: [], error: null }) // no SI references (payment FK)
enqueue({ data: [], error: null }) // no SI payment-row references
enqueue({ data: [], error: null }) // no exemptions
enqueue({ data: [{ id: 'inv-1', journal_entry_id: 'a' }], error: null })
@@ -141,7 +156,8 @@ describe('POST /api/bookkeeping/no-doc-required/bulk-missing', () => {
it('marks the missing entries and returns the count', async () => {
enqueue({ data: [{ id: 'a' }, { id: 'b' }, { id: 'c' }], error: null }) // candidates
enqueue({ data: [], error: null }) // no documents
enqueue({ data: [], error: null }) // no SI references with docs
enqueue({ data: [], error: null }) // no SI references with docs (registration FK)
enqueue({ data: [], error: null }) // no SI references with docs (payment FK)
enqueue({ data: [], error: null }) // no SI payment-row references
enqueue({ data: [{ journal_entry_id: 'a' }], error: null }) // a already exempt
enqueue({ data: [], error: null }) // no invoices pointing at the entries
@@ -153,6 +169,20 @@ describe('POST /api/bookkeeping/no-doc-required/bulk-missing', () => {
expect(body.data.exempted).toBe(2) // b and c
})
it('logs the driver error and answers with the mapped text when a lookup fails (#2395)', async () => {
enqueue({ data: [{ id: 'a' }], error: null }) // candidates
enqueue({ data: [], error: null }) // no documents
const driverError = { message: 'Request-URI Too Large', code: '414', details: null, hint: null }
enqueue({ data: null, error: driverError }) // SI by registration FK: gateway refused the URL
const res = await POST(makeReq({ dry_run: true }), { params: Promise.resolve({}) })
const { status, body } = await parseJsonResponse<{ error: string }>(res)
expect(status).toBe(400)
expect(typeof body.error).toBe('string')
const call = mockLogError.mock.calls.find(([msg]) => msg === 'failed to resolve missing-underlag entries')
expect(call).toBeDefined()
expect(call![2]).toEqual({ cause: driverError })
})
it('short-circuits to 0 when no candidates match the filters', async () => {
enqueue({ data: [], error: null }) // no candidates
const res = await POST(makeReq({ dry_run: true }))
@@ -53,7 +53,7 @@ const BulkMissingSchema = z.object({
*/
export const POST = withRouteContext(
'journal_entry.bulk_missing_no_document_required',
async (request, { supabase, companyId, user }) => {
async (request, { supabase, companyId, user, log }) => {
const validation = await validateBody(request, BulkMissingSchema)
if (!validation.success) return validation.response
@@ -81,6 +81,8 @@ export const POST = withRouteContext(
if (err instanceof MissingUnderlagQueryError) {
// userMessage is already mapped through getErrorMessage() in the
// resolver: user-facing Swedish, never a raw driver message.
// The driver error goes to the log, same as the list route (#2395).
log.error('failed to resolve missing-underlag entries', err, { cause: err.cause })
return NextResponse.json({ error: err.userMessage }, { status: 400 })
}
throw err