import { NextRequest, NextResponse } from 'next/server'; import { db } from '@/lib/db'; import { markdownToHtml, fillEmployeeName, fillPosition, fillStartDate, fillSalary } from '@/lib/api-helpers/pdf'; export const dynamic = 'force-dynamic'; /** Parse and validate request params. Returns null if invalid (with response). */ function parseRequest(req: NextRequest) { const { searchParams } = new URL(req.url); const id = searchParams.get('id'); if (!id) return { error: NextResponse.json({ error: 'id required' }, { status: 400 }) }; const templateId = Number.parseInt(id, 10); if (Number.isNaN(templateId)) return { error: NextResponse.json({ error: 'Invalid id' }, { status: 400 }) }; return { templateId, employeeName: searchParams.get('employee') || '', position: searchParams.get('position') || '', startDate: searchParams.get('startDate') || '', salary: searchParams.get('salary') || '', }; } /** Build the full HTML page with styles. */ function buildFullHtml(html: string, template: { title: string; templateCode: string; charsCount: number }): string { return `