837f354d81
* fix(sie): add ?encoding=cp437 option for legacy bookkeeping software SIE spec mandates CP437 (#FORMAT PC8) but accounted generates UTF-8. Most modern cloud tools (Fortnox, Bokio) accept UTF-8 fine, so UTF-8 remains the default. Pass ?encoding=cp437 to get a properly encoded CP437 binary with #FORMAT PC8 in the header, required by desktop software such as Visma Administration and BL Administration. Removes the spurious #FORMAT PC8 tag from the default UTF-8 output since declaring CP437 while serving UTF-8 caused mojibake on import. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Jonas Flodén <jonas@floden.nu> * fix(sie): wrap Uint8Array in Buffer.from so NextResponse accepts it Uint8Array is not directly assignable to BodyInit in the Next.js NextResponse constructor — wrapping with Buffer.from() satisfies the type without changing the byte content. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Jonas Flodén <jonas@floden.nu> --------- Signed-off-by: Jonas Flodén <jonas@floden.nu>
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { generateSIEExport, encodeSIEToCP437 } from '@/lib/reports/sie-export'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
|
|
export const GET = withRouteContext(
|
|
'report.sie_export',
|
|
async (request, ctx) => {
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const periodId = searchParams.get('period_id')
|
|
const excludeClosing = searchParams.get('exclude_closing') === 'true'
|
|
const useCP437 = searchParams.get('encoding') === 'cp437'
|
|
|
|
if (!periodId) {
|
|
return errorResponseFromCode('REPORT_PERIOD_REQUIRED', log, { requestId })
|
|
}
|
|
|
|
const opLog = log.child({ periodId })
|
|
|
|
const { data: company } = await supabase
|
|
.from('company_settings')
|
|
.select('company_name, org_number')
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!company) {
|
|
return errorResponseFromCode('SIE_EXPORT_COMPANY_NOT_FOUND', opLog, { requestId })
|
|
}
|
|
|
|
try {
|
|
const sieContent = await generateSIEExport(supabase, companyId!, {
|
|
fiscal_period_id: periodId,
|
|
company_name: company.company_name || 'Unknown',
|
|
org_number: company.org_number,
|
|
exclude_year_end_closing: excludeClosing,
|
|
emit_format_pc8: useCP437,
|
|
})
|
|
|
|
const body = useCP437 ? Buffer.from(encodeSIEToCP437(sieContent)) : sieContent
|
|
const contentType = useCP437 ? 'application/octet-stream' : 'text/plain; charset=utf-8'
|
|
|
|
return new NextResponse(body, {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': contentType,
|
|
'Content-Disposition': `attachment; filename="export_${periodId}.se"`,
|
|
'X-Request-Id': requestId,
|
|
},
|
|
})
|
|
} catch (err) {
|
|
opLog.error('sie export generation failed', err as Error)
|
|
return errorResponseFromCode('SIE_EXPORT_FAILED', opLog, {
|
|
requestId,
|
|
details: { reason: err instanceof Error ? err.message : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
)
|