chore(report): remove stale GET_debug helper that broke Next 14 type checks
This commit is contained in:
parent
87a51fbce7
commit
6101ac550d
1 changed files with 105 additions and 0 deletions
105
Workflow/app/api/report/[type]/[id]/pdf/route.ts
Normal file
105
Workflow/app/api/report/[type]/[id]/pdf/route.ts
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
// Fetches the entity, builds the minimax-pdf content blocks,
|
||||
// renders to a branded PDF via the patched minimax-pdf skill, streams the
|
||||
// PDF back.
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { promises as fs } from "fs";
|
||||
import os from "os";
|
||||
import path from "path";
|
||||
import {
|
||||
fetchRecord,
|
||||
fetchJunctionEntriesForCompany,
|
||||
fetchJunctionEntriesForDirector,
|
||||
} from "@/lib/pb";
|
||||
import {
|
||||
buildReportBlocks,
|
||||
reportFilename,
|
||||
reportTitle,
|
||||
type ReportData,
|
||||
} from "@/lib/report";
|
||||
import { renderPdf } from "@/lib/pdf-render";
|
||||
import type { YsterbaardCompany, YsterbaardDirector } from "@/lib/types";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: { type: string; id: string } }
|
||||
) {
|
||||
const { type, id } = params;
|
||||
|
||||
if (type !== "company" && type !== "director") {
|
||||
return NextResponse.json(
|
||||
{ error: "type must be 'company' or 'director'" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
let reportData: ReportData;
|
||||
|
||||
if (type === "company") {
|
||||
const company = await fetchRecord<YsterbaardCompany>(
|
||||
"ysterbaard_companies", id, "groups"
|
||||
);
|
||||
if (!company) {
|
||||
return NextResponse.json({ error: "Company not found" }, { status: 404 });
|
||||
}
|
||||
const junctions = await fetchJunctionEntriesForCompany(id);
|
||||
const linkedDirectors = junctions.map((j) => ({
|
||||
director: (j.expand as Record<string, unknown>)?.director as YsterbaardDirector,
|
||||
director_type: (j as { director_type?: string }).director_type,
|
||||
}));
|
||||
reportData = { type: "company", company, linkedDirectors };
|
||||
} else {
|
||||
const director = await fetchRecord<YsterbaardDirector>("ysterbaard_directors", id);
|
||||
if (!director) {
|
||||
return NextResponse.json({ error: "Director not found" }, { status: 404 });
|
||||
}
|
||||
const junctions = await fetchJunctionEntriesForDirector(id);
|
||||
const linkedCompanies = junctions.map((j) => ({
|
||||
company: (j.expand as Record<string, unknown>)?.company as YsterbaardCompany,
|
||||
director_type: (j as { director_type?: string }).director_type,
|
||||
}));
|
||||
reportData = { type: "director", director, linkedCompanies };
|
||||
}
|
||||
|
||||
const blocks = buildReportBlocks(reportData);
|
||||
const filename = reportFilename(reportData);
|
||||
|
||||
// Write the rendered PDF to a temp path (minimax-pdf needs a file path,
|
||||
// not a stream). Then read it back into a Buffer for the HTTP response.
|
||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "wf-report-resp-"));
|
||||
const outPath = path.join(tmpDir, filename);
|
||||
|
||||
try {
|
||||
const pdfBuffer = await renderPdf({
|
||||
title: reportTitle(reportData),
|
||||
author: "Ysterbaard Corporate Services",
|
||||
date: new Date().toLocaleDateString("en-ZA", {
|
||||
day: "2-digit", month: "long", year: "numeric",
|
||||
}),
|
||||
accent: "#1C3A5E", // Deep navy — legal/compliance.
|
||||
type: "report",
|
||||
blocks,
|
||||
outPath,
|
||||
});
|
||||
|
||||
return new NextResponse(pdfBuffer, {
|
||||
headers: {
|
||||
"Content-Type": "application/pdf",
|
||||
"Content-Disposition": `attachment; filename="${filename.replace(/\.pdf$/, "")}.pdf"`,
|
||||
"Content-Length": String(pdfBuffer.length),
|
||||
"Cache-Control": "no-store",
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
await fs.unlink(outPath).catch(() => {});
|
||||
await fs.rmdir(tmpDir).catch(() => {});
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
console.error("Report PDF error:", msg);
|
||||
return NextResponse.json({ error: msg }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue