La sección Presupuesto (PDF) usaba lead.pdfUrl, que nunca se rellena en el MVP, así que siempre mostraba "Aún no generado". Ahora apunta a la ruta on-demand /panel/[id]/presupuesto cuando existe desglose, con un parámetro ?download=1 que fuerza Content-Disposition: attachment. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import { notFound } from 'next/navigation';
|
|
import { renderToBuffer } from '@react-pdf/renderer';
|
|
import { getLead } from '@/db/queries';
|
|
import { getTenantPerfil } from '@/db/tenant-queries';
|
|
import { TIPO_LABEL } from '@/lib/funnel';
|
|
import { PresupuestoDoc } from '@/lib/pdf/PresupuestoDoc';
|
|
import type { BudgetResult } from '@/budget/types';
|
|
|
|
export const runtime = 'nodejs';
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export async function GET(
|
|
req: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params;
|
|
const data = await getLead(id);
|
|
if (!data) notFound();
|
|
|
|
const descargar = new URL(req.url).searchParams.get('download') === '1';
|
|
|
|
const { lead } = data;
|
|
const empresa = await getTenantPerfil();
|
|
|
|
const snapshot = lead.desgloseSnapshot as { result: BudgetResult } | null;
|
|
const desglose = snapshot?.result ?? null;
|
|
|
|
const buffer = await renderToBuffer(
|
|
PresupuestoDoc({
|
|
empresa,
|
|
cliente: { nombre: lead.nombre, telefono: lead.telefono, provincia: lead.provincia },
|
|
reforma: {
|
|
tipoLabel: lead.tipoReforma ? TIPO_LABEL[lead.tipoReforma] : 'Reforma',
|
|
fecha: lead.createdAt,
|
|
},
|
|
desglose,
|
|
})
|
|
);
|
|
|
|
const slug = lead.nombre.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '');
|
|
return new Response(new Uint8Array(buffer), {
|
|
headers: {
|
|
'Content-Type': 'application/pdf',
|
|
'Content-Disposition': `${descargar ? 'attachment' : 'inline'}; filename="presupuesto-${slug || lead.id}.pdf"`,
|
|
'Cache-Control': 'no-store',
|
|
},
|
|
});
|
|
}
|