* refactor: optimize page loading and data fetching * fix: resolve recurring production runtime errors * feat: add MCP company and customer updates * fix: handle year-end tax adjustments * feat: harden annual report compliance * fix: expand invoice logo and font support * fix: sanitize API route error responses * fix: sanitize user-facing error messages * feat: persist onboarding and tax assessment notices * fix: reduce cloud backup audit churn * feat: refine invoice editor layout * fix: show saved tax adjustments in INK2 * fix: complete annual report API mappings * docs: record operational safeguards and decisions * fix: harden annual report review findings * fix: adjust column span for description based on VAT registration * New css class name
86 lines
3.1 KiB
TypeScript
86 lines
3.1 KiB
TypeScript
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { NextResponse } from 'next/server'
|
|
import { renderToBuffer } from '@react-pdf/renderer'
|
|
import { generateResultatrapport } from '@/lib/reports/resultatrapport'
|
|
import { ResultatrapportPDF } from '@/lib/reports/operational-report-pdf-template'
|
|
import { parseReportDateRange } from '@/lib/reports/date-range'
|
|
import type { CompanySettings } from '@/types'
|
|
import { parseDimensionFilterParams, dimensionFilterDisclosure, dimensionFilterFileSuffix } from '@/lib/reports/dimension-filter'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
export const GET = withRouteContext('report.resultatrapport.pdf', async (request, { supabase, companyId }) => {
|
|
const { searchParams } = new URL(request.url)
|
|
const periodId = searchParams.get('period_id')
|
|
|
|
if (!periodId) {
|
|
return NextResponse.json({ error: 'period_id is required' }, { status: 400 })
|
|
}
|
|
|
|
const [{ data: period }, { data: companyRow }] = await Promise.all([
|
|
supabase
|
|
.from('fiscal_periods')
|
|
.select('period_start, period_end')
|
|
.eq('id', periodId)
|
|
.eq('company_id', companyId)
|
|
.single(),
|
|
supabase
|
|
.from('company_settings')
|
|
.select('*')
|
|
.eq('company_id', companyId)
|
|
.single(),
|
|
])
|
|
|
|
if (!companyRow) {
|
|
return NextResponse.json({ error: 'Företagsinställningar saknas' }, { status: 404 })
|
|
}
|
|
// An identifiable period is part of räkenskapsinformation (BFL 7 kap). Refuse
|
|
// to render a PDF that can't be archived with the period it refers to.
|
|
if (!period) {
|
|
return NextResponse.json(
|
|
{ error: 'Räkenskapsperioden kunde inte läsas. Välj en befintlig period innan du genererar PDF.' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const parsedRange = parseReportDateRange(searchParams, period)
|
|
if (!parsedRange.ok) {
|
|
return NextResponse.json({ error: parsedRange.error }, { status: 400 })
|
|
}
|
|
|
|
const dimFilter = parseDimensionFilterParams(searchParams)
|
|
if (!dimFilter.ok) {
|
|
return NextResponse.json({ error: dimFilter.error }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
const report = await generateResultatrapport(supabase, companyId, periodId, {
|
|
...parsedRange.range,
|
|
dimensions: dimFilter.dimensions,
|
|
})
|
|
|
|
const pdfBuffer = await renderToBuffer(
|
|
ResultatrapportPDF({
|
|
report,
|
|
company: companyRow as CompanySettings,
|
|
generatedAt: new Date().toISOString(),
|
|
// Partial-view disclosure in the document header (BFNAR 2013:2).
|
|
filterNote: dimensionFilterDisclosure(dimFilter.dimensions) ?? undefined,
|
|
})
|
|
)
|
|
|
|
const filename = `resultatrapport${dimensionFilterFileSuffix(dimFilter.dimensions)}-${report.period.start}--${report.period.end}.pdf`
|
|
|
|
return new Response(new Uint8Array(pdfBuffer), {
|
|
headers: {
|
|
'Content-Type': 'application/pdf',
|
|
'Content-Disposition': `attachment; filename="${filename}"`,
|
|
},
|
|
})
|
|
} catch (err) {
|
|
return NextResponse.json(
|
|
{ error: err instanceof Error ? getUserErrorMessage(err) : 'Kunde inte generera resultatrapport' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
})
|