fix(rgi): diagnostic download route — tries multiple URL patterns

Download route now:
- Calls fileVisibility with documentTypeId (if provided)
- Calls confirmOnView with documentPk
- Tries 3 different download URL patterns until one works
- Add &debug=1 to see diagnostic results instead of downloading
- Page now passes documentTypeId in download link

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-24 22:13:34 +02:00
parent 7a28d3ad33
commit d780c3c973
2 changed files with 89 additions and 24 deletions
+1 -1
View File
@@ -296,7 +296,7 @@ function IssuedDocsPanel({
</div> </div>
<Button size="sm" variant="outline" className="shrink-0 gap-1" asChild> <Button size="sm" variant="outline" className="shrink-0 gap-1" asChild>
<a <a
href={`/api/eterra/rgi/download-doc?workspaceId=${doc.workspaceId || workspaceId}&applicationId=${doc.applicationId || applicationPk}&documentPk=${doc.documentPk}`} href={`/api/eterra/rgi/download-doc?workspaceId=${doc.workspaceId || workspaceId}&applicationId=${doc.applicationId || applicationPk}&documentPk=${doc.documentPk}&documentTypeId=${doc.documentTypeId}`}
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
> >
+88 -23
View File
@@ -5,18 +5,23 @@ export const runtime = "nodejs";
export const dynamic = "force-dynamic"; export const dynamic = "force-dynamic";
/** /**
* GET /api/eterra/rgi/download-doc?workspaceId=127&applicationId=X&documentPk=Y * GET /api/eterra/rgi/download-doc?workspaceId=127&applicationId=X&documentPk=Y&documentTypeId=Z
* *
* Downloads an issued document from eTerra RGI. * Downloads an issued document from eTerra RGI.
* 2-step flow: * Flow:
* 1. confirmOnView/{wid}/{appId}/{documentPk} * 1. fileVisibility/{wid}/{appId}/{documentTypeId} — get file info
* 2. loadDocument/downloadFile/{wid}/{documentPk} * 2. confirmOnView/{wid}/{appId}/{documentPk}
* 3. loadDocument/downloadFile/{wid}/{documentPk}
*
* Add &debug=1 to see diagnostic info instead of downloading.
*/ */
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
try { try {
const workspaceId = req.nextUrl.searchParams.get("workspaceId"); const workspaceId = req.nextUrl.searchParams.get("workspaceId");
const applicationId = req.nextUrl.searchParams.get("applicationId"); const applicationId = req.nextUrl.searchParams.get("applicationId");
const documentPk = req.nextUrl.searchParams.get("documentPk"); const documentPk = req.nextUrl.searchParams.get("documentPk");
const documentTypeId = req.nextUrl.searchParams.get("documentTypeId");
const debug = req.nextUrl.searchParams.get("debug") === "1";
if (!workspaceId || !applicationId || !documentPk) { if (!workspaceId || !applicationId || !documentPk) {
return NextResponse.json( return NextResponse.json(
@@ -32,29 +37,89 @@ export async function GET(req: NextRequest) {
} }
const client = await EterraClient.create(username, password); const client = await EterraClient.create(username, password);
const results: Record<string, unknown> = {};
// Step 1: Confirm on view // Step 1: fileVisibility (if documentTypeId provided)
try { if (documentTypeId) {
await client.rgiGet( try {
`rgi/appdetail/issueddocs/confirmOnView/${workspaceId}/${applicationId}/${documentPk}`, const vis = await client.rgiGet(
); `rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentTypeId}`,
} catch { );
// Non-critical — continue to download results.fileVisibility = vis;
} catch (e) {
results.fileVisibilityError = e instanceof Error ? e.message : String(e);
}
} }
// Step 2: Download file // Step 2: confirmOnView
const { data, contentType, filename } = await client.rgiDownload( try {
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`, const confirm = await client.rgiGet(
); `rgi/appdetail/issueddocs/confirmOnView/${workspaceId}/${applicationId}/${documentPk}`,
);
results.confirmOnView = confirm;
} catch (e) {
results.confirmOnViewError = e instanceof Error ? e.message : String(e);
}
return new NextResponse(new Uint8Array(data), { if (debug) {
status: 200, // Try multiple download URL patterns to find the right one
headers: { const patterns = [
"Content-Type": contentType, `rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
"Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`, `rgi/appdetail/issueddocs/downloadFile/${workspaceId}/${applicationId}/${documentPk}`,
"Content-Length": String(data.length), `rgi/appdetail/issueddocs/download/${workspaceId}/${applicationId}/${documentPk}`,
}, ];
});
for (const pattern of patterns) {
try {
const { data, contentType, filename } = await client.rgiDownload(pattern);
results[`download_${pattern.split('/').slice(-1)[0]}`] = {
success: true,
contentType,
filename,
size: data.length,
pattern,
};
} catch (e) {
results[`download_${pattern.split('/').slice(-1)[0]}`] = {
success: false,
error: e instanceof Error ? e.message : String(e),
pattern,
};
}
}
return NextResponse.json(results, { status: 200 });
}
// Step 3: Download — try multiple patterns
const downloadPatterns = [
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
`rgi/appdetail/issueddocs/downloadFile/${workspaceId}/${applicationId}/${documentPk}`,
`rgi/appdetail/issueddocs/download/${workspaceId}/${applicationId}/${documentPk}`,
];
for (const pattern of downloadPatterns) {
try {
const { data, contentType, filename } = await client.rgiDownload(pattern);
if (data.length > 0) {
return new NextResponse(new Uint8Array(data), {
status: 200,
headers: {
"Content-Type": contentType,
"Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`,
"Content-Length": String(data.length),
},
});
}
} catch {
// Try next pattern
}
}
return NextResponse.json(
{ error: "Download failed — all URL patterns returned errors", diagnostics: results },
{ status: 500 },
);
} catch (error) { } catch (error) {
const message = error instanceof Error ? error.message : "Eroare server"; const message = error instanceof Error ? error.message : "Eroare server";
return NextResponse.json({ error: message }, { status: 500 }); return NextResponse.json({ error: message }, { status: 500 });