fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s

Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
This commit is contained in:
2026-11-10 09:37:22 +01:00
parent 077251ac94
commit e7f0070e84
6135 changed files with 1718997 additions and 4 deletions
@@ -0,0 +1,180 @@
/**
* Tests for POST /api/import/documents/preview: the read-only match plan that
* pairs underlag filenames with SIE-migrated verifikat before anything is
* uploaded or linked.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { NextResponse } from 'next/server'
import { createMockRequest, parseJsonResponse } from '@/tests/helpers'
import type { UnderlagPlan } from '@/lib/documents/underlag-import'
const PERIOD_OPEN = '33333333-3333-4333-8333-333333333333'
const PERIODS = [
{
id: PERIOD_OPEN,
period_start: '2024-01-01',
period_end: '2024-12-31',
is_closed: false,
locked_at: null,
},
]
let vouchers: Record<string, unknown>[] = []
/** The row the fiscal-period ownership lookup finds, or null for "not ours". */
let ownedPeriod: Record<string, unknown> | null = null
/** Shape-keyed double: the plan reads vouchers and periods concurrently. */
const supabase = {
from(table: string) {
let filteredByNumber = false
let single = false
const result = () => {
if (table === 'fiscal_periods') {
return single
? { data: ownedPeriod, error: null }
: { data: PERIODS, error: null, count: PERIODS.length }
}
return {
data: filteredByNumber ? vouchers : [],
error: null,
count: vouchers.length,
}
}
const chain: Record<string, unknown> = {}
for (const method of ['select', 'eq', 'not', 'order', 'limit', 'single']) {
chain[method] = () => chain
}
chain.maybeSingle = () => {
single = true
return chain
}
chain.in = () => {
filteredByNumber = true
return chain
}
chain.range = () => Promise.resolve(result())
chain.then = (onFulfilled: (value: unknown) => unknown) =>
Promise.resolve(result()).then(onFulfilled)
return chain
},
}
const requireAuthMock = vi.fn()
vi.mock('@/lib/auth/require-auth', () => ({
requireAuth: (...args: unknown[]) => requireAuthMock(...args),
}))
// Guideline mock: no real Supabase client may ever be constructed in a route
// test, even though this suite injects its double through requireAuth.
vi.mock('@/lib/supabase/server', () => ({
createClient: () => Promise.resolve(supabase),
}))
vi.mock('@/lib/company/context', () => ({
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
}))
vi.mock('@/lib/init', () => ({ ensureInitialized: vi.fn() }))
import { POST } from '../route'
const emptyParams = { params: Promise.resolve({}) }
function makeRequest(body: Record<string, unknown>) {
return createMockRequest('/api/import/documents/preview', {
method: 'POST',
body: { fiscal_period_id: PERIOD_OPEN, ...body },
})
}
describe('POST /api/import/documents/preview', () => {
beforeEach(() => {
vi.clearAllMocks()
vouchers = [
{
id: 'je-1',
fiscal_period_id: PERIOD_OPEN,
entry_date: '2024-03-14',
description: 'Inköp kontorsmaterial',
voucher_series: 'A',
voucher_number: 47,
source_voucher_series: 'A',
source_voucher_number: 31,
},
]
ownedPeriod = { id: PERIOD_OPEN }
requireAuthMock.mockResolvedValue({ user: { id: 'user-1' }, supabase })
})
it('returns 401 when unauthenticated', async () => {
requireAuthMock.mockResolvedValue({
user: null,
supabase,
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
})
const res = await POST(makeRequest({ file_names: ['A31.pdf'] }), emptyParams)
expect(res.status).toBe(401)
})
it('returns 400 when file_names is missing or empty', async () => {
expect((await POST(makeRequest({}), emptyParams)).status).toBe(400)
expect((await POST(makeRequest({ file_names: [] }), emptyParams)).status).toBe(400)
})
it('returns 400 without a fiscal year: a filename alone cannot identify a verifikat', async () => {
const noYear = createMockRequest('/api/import/documents/preview', {
method: 'POST',
body: { file_names: ['A31.pdf'] },
})
expect((await POST(noYear, emptyParams)).status).toBe(400)
const badYear = await POST(
makeRequest({ file_names: ['A31.pdf'], fiscal_period_id: 'not-a-uuid' }),
emptyParams,
)
expect(badYear.status).toBe(400)
})
it("returns 404 for a fiscal year that is not this company's", async () => {
ownedPeriod = null
const res = await POST(makeRequest({ file_names: ['A31.pdf'] }), emptyParams)
expect(res.status).toBe(404)
})
it('returns 400 when the batch exceeds the 2000-file cap', async () => {
const oversized = Array.from({ length: 2001 }, (_, i) => `A${i + 1}.pdf`)
const res = await POST(makeRequest({ file_names: oversized }), emptyParams)
expect(res.status).toBe(400)
})
it('returns the match plan for the submitted filenames (happy path)', async () => {
const res = await POST(
makeRequest({ file_names: ['A31_8c2db060.pdf', 'kvitto.pdf'] }),
emptyParams,
)
const { status, body } = await parseJsonResponse<{ data: UnderlagPlan }>(res)
expect(status).toBe(200)
expect(body.data.rows).toHaveLength(2)
expect(body.data.rows[0]).toMatchObject({ status: 'matched', journal_entry_id: 'je-1' })
expect(body.data.rows[1]).toMatchObject({ status: 'unparsed', journal_entry_id: null })
expect(body.data.summary).toMatchObject({ total: 2, matched: 1, unparsed: 1 })
})
it('never returns a target the filename does not name', async () => {
const res = await POST(makeRequest({ file_names: ['A99.pdf'] }), emptyParams)
const { body } = await parseJsonResponse<{ data: UnderlagPlan }>(res)
expect(body.data.rows[0].status).toBe('no_match')
expect(body.data.rows[0].journal_entry_id).toBeNull()
})
})
+54
View File
@@ -0,0 +1,54 @@
import { NextResponse } from 'next/server'
import { ensureInitialized } from '@/lib/init'
import { withRouteContext } from '@/lib/api/with-route-context'
import { validateBody } from '@/lib/api/validate'
import { UnderlagImportPreviewSchema } from '@/lib/api/schemas'
import { buildUnderlagPlan } from '@/lib/documents/underlag-import'
import { errorResponse, errorResponseFromCode } from '@/lib/errors/get-structured-error'
ensureInitialized()
/**
* POST /api/import/documents/preview: plan which verifikat each underlag file
* belongs to, from the voucher reference in its filename.
*
* Read-only by construction. Only the NAMES are sent, so a user can review the
* whole plan (including the misses) before a single byte is uploaded, and
* before a single irreversible link is written.
*
* Scoped to the fiscal year the caller declares: `A31` identifies a verifikat
* only inside a year, because source systems restart numbering annually.
*/
export const POST = withRouteContext('import.documents.preview', async (request, ctx) => {
const { supabase, companyId, log, requestId } = ctx
const validation = await validateBody(request, UnderlagImportPreviewSchema)
if (!validation.success) return validation.response
const { file_names: fileNames, fiscal_period_id: fiscalPeriodId } = validation.data
// The period must belong to this company: buildUnderlagPlan filters entries
// by it, and an unowned id would silently produce an all-misses plan rather
// than an honest error.
const { data: period, error: periodError } = await supabase
.from('fiscal_periods')
.select('id')
.eq('id', fiscalPeriodId)
.eq('company_id', companyId!)
.maybeSingle()
if (periodError) {
log.error('underlag import preview period lookup failed', periodError)
return errorResponse(periodError, log, { requestId })
}
if (!period) {
return errorResponseFromCode('FISCAL_PERIOD_NOT_FOUND', log, { requestId })
}
try {
const plan = await buildUnderlagPlan(supabase, companyId!, fileNames, fiscalPeriodId)
return NextResponse.json({ data: plan })
} catch (err) {
log.error('underlag import preview failed', err as Error, { fileCount: fileNames.length })
return errorResponse(err, log, { requestId })
}
})