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>
<Button size="sm" variant="outline" className="shrink-0 gap-1" asChild>
<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"
rel="noopener noreferrer"
>
+88 -23
View File
@@ -5,18 +5,23 @@ export const runtime = "nodejs";
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.
* 2-step flow:
* 1. confirmOnView/{wid}/{appId}/{documentPk}
* 2. loadDocument/downloadFile/{wid}/{documentPk}
* Flow:
* 1. fileVisibility/{wid}/{appId}/{documentTypeId} — get file info
* 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) {
try {
const workspaceId = req.nextUrl.searchParams.get("workspaceId");
const applicationId = req.nextUrl.searchParams.get("applicationId");
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) {
return NextResponse.json(
@@ -32,29 +37,89 @@ export async function GET(req: NextRequest) {
}
const client = await EterraClient.create(username, password);
const results: Record<string, unknown> = {};
// Step 1: Confirm on view
try {
await client.rgiGet(
`rgi/appdetail/issueddocs/confirmOnView/${workspaceId}/${applicationId}/${documentPk}`,
);
} catch {
// Non-critical — continue to download
// Step 1: fileVisibility (if documentTypeId provided)
if (documentTypeId) {
try {
const vis = await client.rgiGet(
`rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentTypeId}`,
);
results.fileVisibility = vis;
} catch (e) {
results.fileVisibilityError = e instanceof Error ? e.message : String(e);
}
}
// Step 2: Download file
const { data, contentType, filename } = await client.rgiDownload(
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
);
// Step 2: confirmOnView
try {
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), {
status: 200,
headers: {
"Content-Type": contentType,
"Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`,
"Content-Length": String(data.length),
},
});
if (debug) {
// Try multiple download URL patterns to find the right one
const patterns = [
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
`rgi/appdetail/issueddocs/downloadFile/${workspaceId}/${applicationId}/${documentPk}`,
`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) {
const message = error instanceof Error ? error.message : "Eroare server";
return NextResponse.json({ error: message }, { status: 500 });