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
parent b2b714f829
commit 2f787a2b3c
8 changed files with 270 additions and 48 deletions
+1
View File
@@ -1665,6 +1665,7 @@ One line per decision: `[YYYY-MM-DD] <decision>: <why>`. Appended by agents and
[2026-09-08] #2391 skeptic pass: orgNumberKey only strips hyphens and spaces and only unprefixes 12-digit values behind 16/18/19/20. Reason: 26 prod supplier rows hold a VAT number (orgnr + 01, prefixes 55/52/87) in org_number, and 'last 10 of any 12 digits' would have rewritten them to another company's identity; letters stay because BE0123456789 is not the Swedish 0123456789. The matcher scans live suppliers only (archived_at IS NULL), the list and v1 search compare without separators, the CSV import and the provider migration orchestrator key and write through the same rule.
[2026-09-08] correctEntry re-points the original entry's transaction_voucher_links rows to the corrected entry (lib/core/bookkeeping/storno-service.ts relinkTransactionsToEntry) instead of deleting them as issue #2364 proposed. Why: for a samlingsverifikat (bulk-book N>1) the junction is the row's only anchor, so deleting it would push rows the corrected verifikat still explains back into Att bokföra; the pointer column already follows the correction and the junction now follows it the same way, so every reader (is_transaction_booked, fetchJunctionLinkedTxIds, the bulk_book RPC) sees one live anchor. Rejected: a relink_entry_anchors RPC moving pointer and junction atomically (a migration plus pg test for a path that is already best-effort across five other statements; revisit if a partial failure ever shows up in the surfaced transactionRelinkError). Prod repair (planned, runs after merge on the founder's go; completion gets its own dated entry): the 7 stale links (3 companies) all sit on rows whose pointer names a posted entry (4 on a correction chain, 3 from a June 2026 samlingsverifikat storno that predates the junction cleanup and were re-booked 1:1); they will be re-pointed to the pointer's entry, the same rule the fix applies, rather than deleted.
[2026-09-08] delete_last_voucher returns a correction's bank anchors (transactions.journal_entry_id and transaction_voucher_links rows) to correction_of_id before the row is deleted (migration 20260908095907). Why: the #2364 skeptic showed that once the junction follows the correction, the two-step undo (delete the correction, then the storno) cascaded the links away and restored an original that explains bank rows nobody points at, so the rows surfaced as bookable again; before, the links had stayed on the original by accident. Chosen over releasing the rows (the restored original would still explain them, same trap) and over a TS pre-step in the DELETE route (not atomic with the RPC's own guards: a refused delete would leave anchors on a reversed entry). A duplicate of a link the original already holds is dropped, not re-pointed (UNIQUE (transaction_id, journal_entry_id)).
[2026-09-08] #2395 missing-underlag: split the supplier-invoice .or() into two .in() lookups instead of halving LOOKUP_CHUNK or moving the list filters into the verifikat_without_documents RPC: halving only moves the proxy-limit wall; the RPC move deletes the TS mirror (one predicate instead of two) but pulls search, series, date and sort into SQL and is the surface owner's design call. Intended next step, not taken here.
[2026-09-08] Onboarding name search picks via chip row, never the top hit blind: Typesense name ranking is fuzzy and common names ("Bygg AB", sole-trader surnames) make a blind pick a wrong company; five hits, active first, fired on Enter only to protect the 3000/mo TIC budget (issue #2418).
[2026-09-08] #2418 skeptic pass: a name-search hit's org number is derived from the Lens registrationNumber via lensRegistrationToOrgNumber (16-prefixed 12 digits and the 16-digit enskild-firma form, century plus 4-digit serial, reduce to the 10-digit key; hits that do not normalize are dropped) instead of being stored as returned. Why: the typed-orgnr path never stores Lens's number, so this was the first place a 16-digit value reached settings.org_number and createCompany refused it at the last step. Sole-trader chips name the form ("Enskild firma") instead of the number, because that number is the owner's personnummer and the repo masks those everywhere else; the number still travels in the search payload since a pick has to store it.
[2026-09-08] Medelantal anställda (Not 2, ÅRL 5:20 §) gets a whole-number override on arsredovisning_narratives (migration 20260908130127) instead of the free-text note override the support request asked for. Why: the number keeps the statutory sentence and the iXBRL MedelantaletAnstallda fact correct; free text would let a non-compliant note through and could not be tagged. One resolver (lib/salary/medelantal.ts resolveMedelantalAnstallda: override, else FTE average over employees) feeds the K2 and K3 note builders and the iXBRL input, which also reads the previous period's override so the jämförelseår column shows the same figure the previous year's document did. Rejected: rounding 0.5 up globally (silently changes every company's note and does nothing for the 148 of 195 aktiebolag with salary but no employees rows); asking the user to backdate employment_start (fixes one company, misstates the hire date).
@@ -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
+11 -2
View File
@@ -244,6 +244,11 @@ export default function JournalEntryList({
const [loadFailed, setLoadFailed] = useState(false)
const [expandedId, setExpandedId] = useState<string | null>(null)
const [count, setCount] = useState(0)
// Badge next to "Visa saknade underlag": the total of the last SUCCESSFUL
// filtered fetch, null otherwise. Borrowing `count` showed 0 on a failed
// first load and the whole ledger's total after a toggle (#2395); a badge
// with no honest source is hidden, not guessed.
const [missingCount, setMissingCount] = useState<number | null>(null)
const [page, setPage] = useState(0)
const [attachmentCounts, setAttachmentCounts] = useState<Record<string, number>>({})
// Entries a customer invoice points at (registration link or payment row):
@@ -591,6 +596,7 @@ export default function JournalEntryList({
// Surface the failure: stale rows (if any) stay on screen, the empty
// case renders the error state below, and the toast covers refetches.
setLoadFailed(true)
setMissingCount(null)
toast({ title: t('load_failed_title'), variant: 'destructive' })
setHasLoaded(true)
return
@@ -601,6 +607,7 @@ export default function JournalEntryList({
const loadedEntries = data || []
setEntries(loadedEntries)
setCount(total || 0)
setMissingCount(showMissingOnly && listMode === 'committed' ? total || 0 : null)
if (preserveSelection) {
// Reconcile with the refreshed page: rows that left it (recommitted
// elsewhere, filtered out by the new data) must leave the selection
@@ -645,6 +652,7 @@ export default function JournalEntryList({
// surfacing as a non-OK response, or the list stays dimmed forever.
if (!isCurrent()) return
setLoadFailed(true)
setMissingCount(null)
setHasLoaded(true)
toast({ title: t('load_failed_title'), variant: 'destructive' })
} finally {
@@ -1282,15 +1290,16 @@ export default function JournalEntryList({
disabled={listMode === 'drafts'}
onCheckedChange={(on) => {
setShowMissingOnly(on)
setMissingCount(null)
setPage(0)
}}
/>
<Label htmlFor="missing-attachments" className="text-sm cursor-pointer">
{t('show_missing')}
</Label>
{showMissingOnly && (
{showMissingOnly && missingCount !== null && (
<Badge variant="secondary" className="text-xs tabular-nums">
{count}
{missingCount}
</Badge>
)}
</div>
@@ -0,0 +1,137 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createQueuedMockSupabase } from '@/tests/helpers'
import type { SupabaseClient } from '@supabase/supabase-js'
import { getErrorMessage } from '@/lib/errors/get-error-message'
import {
MissingUnderlagQueryError,
resolveMissingUnderlagEntries,
} from '@/lib/bookkeeping/missing-underlag'
const { supabase, enqueue, reset, findCalls } = createQueuedMockSupabase()
const client = supabase as unknown as SupabaseClient
const E1 = '11111111-1111-4111-8111-111111111111'
const E2 = '22222222-2222-4222-8222-222222222222'
const E3 = '33333333-3333-4333-8333-333333333333'
const candidate = (id: string) => ({ id })
beforeEach(() => {
vi.clearAllMocks()
reset()
})
describe('resolveMissingUnderlagEntries: supplier-invoice reference lookup (#2395)', () => {
it('sends the candidate chunk once per URL: two .in() lookups, never one .or() over both FKs', async () => {
enqueue({ data: [candidate(E1), candidate(E2), candidate(E3)], error: null }) // candidates
enqueue({ data: [], error: null }) // no direct documents
enqueue({ data: [], error: null }) // SI by registration FK
enqueue({ data: [], error: null }) // SI by payment FK
enqueue({ data: [], error: null }) // no SI payment rows
enqueue({ data: [], error: null }) // no exemptions
enqueue({ data: [], error: null }) // no customer invoices pointing at the entries
enqueue({ data: [], error: null }) // no invoice payment rows
const missing = await resolveMissingUnderlagEntries(client, 'company-1', {}, { idOnly: true })
expect(missing.map((e) => e.id)).toEqual([E1, E2, E3])
// The doubled .or() is what pushed one lookup over an 8 KB proxy header
// buffer (414 on self-hosted Kong) while the single-list lookups passed.
expect(findCalls('supplier_invoices', 'or')).toHaveLength(0)
expect(findCalls('supplier_invoices', 'in')).toEqual([
['registration_journal_entry_id', [E1, E2, E3]],
['payment_journal_entry_id', [E1, E2, E3]],
])
// Both lookups stay scoped to the company and to invoices with a document.
expect(findCalls('supplier_invoices', 'eq')).toEqual([
['company_id', 'company-1'],
['company_id', 'company-1'],
])
expect(findCalls('supplier_invoices', 'not')).toEqual([
['document_id', 'is', null],
['document_id', 'is', null],
])
})
it('treats an anchored reference from either lookup as underlag, unanchored from neither', async () => {
enqueue({ data: [candidate(E1), candidate(E2), candidate(E3)], error: null })
enqueue({ data: [], error: null }) // no direct documents
enqueue({
data: [
{
registration_journal_entry_id: E1,
payment_journal_entry_id: null,
document: { journal_entry_id: E1 }, // anchored
},
],
error: null,
})
enqueue({
data: [
{
registration_journal_entry_id: null,
payment_journal_entry_id: E2,
document: { journal_entry_id: E2 }, // anchored
},
{
registration_journal_entry_id: null,
payment_journal_entry_id: E3,
document: { journal_entry_id: null }, // unanchored: deletable, not underlag
},
],
error: null,
})
enqueue({ data: [], error: null }) // no SI payment rows
enqueue({ data: [], error: null }) // no exemptions
enqueue({ data: [], error: null }) // no customer invoices pointing at the entries
enqueue({ data: [], error: null }) // no invoice payment rows
const missing = await resolveMissingUnderlagEntries(client, 'company-1', {}, { idOnly: true })
expect(missing.map((e) => e.id)).toEqual([E3])
})
it('chunks the candidate list at 150 ids and issues both supplier-invoice lookups per chunk', async () => {
const ids = Array.from(
{ length: 151 },
(_, i) => `${String(i).padStart(8, '0')}-0000-4000-8000-000000000000`,
)
enqueue({ data: ids.map(candidate), error: null })
for (let chunk = 0; chunk < 2; chunk++) {
enqueue({ data: [], error: null }) // documents
enqueue({ data: [], error: null }) // SI by registration FK
enqueue({ data: [], error: null }) // SI by payment FK
enqueue({ data: [], error: null }) // SI payment rows
enqueue({ data: [], error: null }) // exemptions
enqueue({ data: [], error: null }) // customer invoices
enqueue({ data: [], error: null }) // invoice payment rows
}
const missing = await resolveMissingUnderlagEntries(client, 'company-1', {}, { idOnly: true })
expect(missing).toHaveLength(151)
const inCalls = findCalls('supplier_invoices', 'in')
expect(inCalls.map(([column, list]) => [column, (list as string[]).length])).toEqual([
['registration_journal_entry_id', 150],
['payment_journal_entry_id', 150],
['registration_journal_entry_id', 1],
['payment_journal_entry_id', 1],
])
expect(findCalls('supplier_invoices', 'or')).toHaveLength(0)
})
it('carries the driver error as cause alongside the user-facing Swedish text', async () => {
enqueue({ data: [candidate(E1)], error: null })
enqueue({ data: [], error: null }) // documents
const driverError = { message: 'Request-URI Too Large', code: '414', details: null, hint: null }
enqueue({ data: null, error: driverError }) // SI by registration FK fails
const thrown = await resolveMissingUnderlagEntries(client, 'company-1').catch((e) => e)
expect(thrown).toBeInstanceOf(MissingUnderlagQueryError)
// The raw driver error survives for the server log, unmapped.
expect(thrown.cause).toBe(driverError)
// What the user sees went through the shared mapper, same as before.
expect(thrown.userMessage).toBe(getErrorMessage(driverError))
expect(thrown.message).toBe(thrown.userMessage)
})
})
+40 -34
View File
@@ -1,5 +1,4 @@
import type { SupabaseClient } from '@supabase/supabase-js'
import { z } from 'zod'
import type { PostgrestError, SupabaseClient } from '@supabase/supabase-js'
import { fetchAllRows } from '@/lib/supabase/fetch-all'
import { NEEDS_DOC_SOURCE_TYPES } from '@/lib/worklist/categories'
import { escapeLikePattern } from '@/lib/invoices/duplicate-payment-guard'
@@ -45,23 +44,26 @@ export interface MissingUnderlagEntry {
/**
* Sub-query failure. `userMessage` is already mapped through getErrorMessage()
* (user-facing Swedish), never a raw driver message.
* (user-facing Swedish), never a raw driver message. `cause` is the raw
* PostgREST error for the server log: the mapped text alone hid a gateway 414
* behind "Något gick fel" for a whole evening of proxy-log reading (#2395).
*/
export class MissingUnderlagQueryError extends Error {
constructor(public readonly userMessage: string) {
constructor(
public readonly userMessage: string,
public readonly cause: PostgrestError | unknown,
) {
super(userMessage)
}
}
// Journal-entry ids are interpolated into the supplier-invoice .or() filter
// string below, so they must be UUIDs. They originate from journal_entries.id
// (DB-sourced, never request input), but this guard keeps the injection-safety
// contract identical to /api/documents/counts.
const uuidSchema = z.string().uuid()
// 150 keeps the embedded id lists well under PostgREST's URL-length limit:
// the supplier-invoice .or() below repeats the chunk twice (registration +
// payment FK), so a larger chunk would risk truncating the GET filter.
/**
* Ids per PostgREST .in() filter. Ids travel in the GET query string; 150
* UUIDs is about 5.6 KB, under the 8 KB header buffer that nginx/Kong ship
* with and that self-hosted Supabase inherits. Every lookup below carries the
* chunk exactly ONCE: a filter that repeats it (one .or() over two FK columns)
* doubles the URL and is answered 414 before PostgREST ever sees it (#2395).
*/
const LOOKUP_CHUNK = 150
/**
@@ -169,20 +171,14 @@ export async function resolveMissingUnderlagEntries(
const exempt = new Set<string>()
for (let i = 0; i < candidateIds.length; i += LOOKUP_CHUNK) {
const chunk = candidateIds.slice(i, i + LOOKUP_CHUNK)
// Only UUIDs reach the interpolated .or() string (the .in() array filters
// are already injection-safe); mirrors the guard in documents/counts.
const chunkInList = `(${chunk.filter((id) => uuidSchema.safeParse(id).success).join(',')})`
const [docRes, siRefRes, sipRefRes, exemptRes] = await Promise.all([
supabase
.from('document_attachments')
.select('journal_entry_id')
.eq('company_id', companyId)
.eq('is_current_version', true)
.in('journal_entry_id', chunk),
// BFL 5 kap 7 § hänvisning: an entry referenced by a supplier invoice
// whose source document is retained AND anchored to a journal entry
// is NOT missing underlag (only anchored docs sit behind the WORM
// deletion guards). Mirrors the verifikat_without_documents RPC.
// BFL 5 kap 7 § hänvisning: an entry referenced by a supplier invoice
// whose source document is retained AND anchored to a journal entry
// is NOT missing underlag (only anchored docs sit behind the WORM
// deletion guards). Mirrors the verifikat_without_documents RPC.
// One query per FK column, never one .or() over both: the chunk must
// appear once per URL (see LOOKUP_CHUNK), and a literal .in() keeps the
// filter resolvable for tests/schema/no-phantom-columns.test.ts.
const supplierInvoiceRefs = () =>
supabase
.from('supplier_invoices')
.select(
@@ -190,9 +186,15 @@ export async function resolveMissingUnderlagEntries(
)
.eq('company_id', companyId)
.not('document_id', 'is', null)
.or(
`registration_journal_entry_id.in.${chunkInList},payment_journal_entry_id.in.${chunkInList}`,
),
const [docRes, siRegRes, siPayRes, sipRefRes, exemptRes] = await Promise.all([
supabase
.from('document_attachments')
.select('journal_entry_id')
.eq('company_id', companyId)
.eq('is_current_version', true)
.in('journal_entry_id', chunk),
supplierInvoiceRefs().in('registration_journal_entry_id', chunk),
supplierInvoiceRefs().in('payment_journal_entry_id', chunk),
supabase
.from('supplier_invoice_payments')
.select(
@@ -206,13 +208,17 @@ export async function resolveMissingUnderlagEntries(
.eq('company_id', companyId)
.in('journal_entry_id', chunk),
])
for (const res of [docRes, siRefRes, sipRefRes, exemptRes]) {
if (res.error) throw new MissingUnderlagQueryError(getUserErrorMessage(res.error))
for (const res of [docRes, siRegRes, siPayRes, sipRefRes, exemptRes]) {
if (res.error) {
throw new MissingUnderlagQueryError(getUserErrorMessage(res.error), res.error)
}
}
for (const r of (docRes.data ?? []) as { journal_entry_id: string }[]) {
withDoc.add(r.journal_entry_id)
}
for (const r of (siRefRes.data ?? []) as unknown as {
// Both lookups return the same row shape; an invoice matched by both
// columns lands twice, harmlessly, in the set.
for (const r of [...(siRegRes.data ?? []), ...(siPayRes.data ?? [])] as unknown as {
registration_journal_entry_id: string | null
payment_journal_entry_id: string | null
document: { journal_entry_id: string | null } | null
@@ -243,7 +249,7 @@ export async function resolveMissingUnderlagEntries(
try {
invoiceRefs = await getInvoiceReferencesForJournalEntries(supabase, companyId, chunk)
} catch (err) {
throw new MissingUnderlagQueryError(getUserErrorMessage(err))
throw new MissingUnderlagQueryError(getUserErrorMessage(err), err)
}
for (const journalEntryId of invoiceRefs.keys()) withDoc.add(journalEntryId)
}