fix(rgi): fast download with fileVisibility gate + clear error message

Download route simplified:
1. fileVisibility check — if 404, returns "indisponibil" + eTerra URL
2. Single download pattern (the one that works)

When document not available server-side, response includes direct
eTerra URL as fallback. No more 7 pattern attempts = much faster.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-24 22:39:53 +02:00
parent 5966a11f7e
commit b0a5918bd7
2 changed files with 48 additions and 114 deletions
+10 -19
View File
@@ -295,30 +295,21 @@ function IssuedDocsPanel({
</div>
</div>
<div className="flex items-center gap-1 shrink-0">
{/* Server-side download (works when fileVisibility returns OK) */}
<Button
size="sm"
variant="outline"
className="gap-1"
onClick={() => {
// Open eTerra directly — user's browser session has download permission
// First open confirmOnView, then download
const base = "https://eterra.ancpi.ro/eterra/api";
const wid = doc.workspaceId || workspaceId;
const appId = doc.applicationId || applicationPk;
const dpk = doc.documentPk;
// Open confirm in hidden iframe, then download
const iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.src = `${base}/rgi/appdetail/issueddocs/confirmOnView/${wid}/${appId}/${dpk}`;
document.body.appendChild(iframe);
setTimeout(() => {
window.open(`${base}/rgi/appdetail/loadDocument/downloadFile/${wid}/${dpk}`, "_blank");
iframe.remove();
}, 500);
}}
asChild
>
<Download className="h-3.5 w-3.5" />
Descarca
<a
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"
>
<Download className="h-3.5 w-3.5" />
Descarca
</a>
</Button>
</div>
</div>
+38 -95
View File
@@ -4,17 +4,24 @@ import { EterraClient } from "@/modules/parcel-sync/services/eterra-client";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
/**
* GET /api/eterra/rgi/download-doc?workspaceId=127&applicationId=X&documentPk=Y&documentTypeId=Z
*
* Downloads an issued document from eTerra RGI.
* 1. fileVisibility check — if 404, document not available
* 2. loadDocument/downloadFile — actual download
*/
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";
const checkOnly = req.nextUrl.searchParams.get("check") === "1";
if (!workspaceId || !applicationId || !documentPk) {
return NextResponse.json(
{ error: "workspaceId, applicationId and documentPk are required" },
{ error: "workspaceId, applicationId and documentPk required" },
{ status: 400 },
);
}
@@ -26,108 +33,44 @@ export async function GET(req: NextRequest) {
}
const client = await EterraClient.create(username, password);
const log: { step: string; result: unknown }[] = [];
// Step 1: fileVisibility (GET)
// Step 1: fileVisibility — check if document is downloadable
if (documentTypeId) {
try {
const r = await client.rgiGet(
await client.rgiGet(
`rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentTypeId}`,
);
log.push({ step: "fileVisibility(GET,documentTypeId)", result: r });
} catch (e) {
log.push({ step: "fileVisibility(GET,documentTypeId)", result: { error: String(e instanceof Error ? e.message : e) } });
}
}
// Step 1b: fileVisibility with documentPk
try {
const r = await client.rgiGet(
`rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentPk}`,
);
log.push({ step: "fileVisibility(GET,documentPk)", result: r });
} catch (e) {
log.push({ step: "fileVisibility(GET,documentPk)", result: { error: String(e instanceof Error ? e.message : e) } });
}
// Step 2: confirmOnView (GET)
try {
const r = await client.rgiGet(
`rgi/appdetail/issueddocs/confirmOnView/${workspaceId}/${applicationId}/${documentPk}`,
);
log.push({ step: "confirmOnView(GET)", result: r });
} catch (e) {
log.push({ step: "confirmOnView(GET)", result: { error: String(e instanceof Error ? e.message : e) } });
}
// Step 2b: confirmOnView (POST)
try {
const r = await client.rgiPost(
`rgi/appdetail/issueddocs/confirmOnView/${workspaceId}/${applicationId}/${documentPk}`,
);
log.push({ step: "confirmOnView(POST)", result: r });
} catch (e) {
log.push({ step: "confirmOnView(POST)", result: { error: String(e instanceof Error ? e.message : e) } });
}
if (debug) {
// Try ALL download patterns
const patterns = [
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${applicationId}/${documentPk}`,
`rgi/appdetail/issueddocs/downloadFile/${workspaceId}/${documentPk}`,
`rgi/appdetail/issueddocs/downloadFile/${workspaceId}/${applicationId}/${documentPk}`,
`rgi/appdetail/issueddocs/download/${workspaceId}/${documentPk}`,
`rgi/appdetail/issueddocs/download/${workspaceId}/${applicationId}/${documentPk}`,
`appDetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
];
for (let i = 0; i < patterns.length; i++) {
try {
const { data, contentType, filename } = await client.rgiDownload(patterns[i]!);
log.push({
step: `download[${i}]`,
result: { success: true, contentType, filename, size: data.length, url: patterns[i] },
});
// If we found a working one, stop trying
break;
} catch (e) {
log.push({
step: `download[${i}]`,
result: { success: false, error: String(e instanceof Error ? e.message : e), url: patterns[i] },
});
}
}
return NextResponse.json({ log });
}
// Non-debug: try download patterns
const patterns = [
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${applicationId}/${documentPk}`,
`appDetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
];
for (const pattern of patterns) {
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 {
// next pattern
// fileVisibility failed — document not available for server-side download
return NextResponse.json(
{
error: "Document indisponibil pentru download server-side",
available: false,
eterraUrl: `https://eterra.ancpi.ro/eterra/api/rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
},
{ status: 403 },
);
}
}
return NextResponse.json({ error: "Download failed", log }, { status: 500 });
// Check-only mode: just verify availability
if (checkOnly) {
return NextResponse.json({ available: true });
}
// Step 2: Download
const { data, contentType, filename } = await client.rgiDownload(
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
);
return new NextResponse(new Uint8Array(data), {
status: 200,
headers: {
"Content-Type": contentType,
"Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`,
"Content-Length": String(data.length),
},
});
} catch (error) {
const message = error instanceof Error ? error.message : "Eroare server";
return NextResponse.json({ error: message }, { status: 500 });