fix(rgi): user-friendly page + 3-step download flow

Rewrote RGI test page:
- Clean card-based UI with status icons (green=solved, amber=pending)
- Click row to expand and see issued documents
- Each document has a direct "Descarca" download button
- Filter toggle "Doar solutionate cu termen viitor"
- No more raw JSON tables

Download route now follows eTerra's 3-step flow:
1. fileVisibility — check access, get fileId
2. confirmOnView — confirm document view
3. loadDocument/downloadFile — actual file download

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-24 21:18:32 +02:00
parent aa11ca389e
commit 64f10a63ff
2 changed files with 504 additions and 446 deletions
File diff suppressed because it is too large Load Diff
+57 -35
View File
@@ -5,21 +5,33 @@ export const runtime = "nodejs";
export const dynamic = "force-dynamic";
/**
* GET /api/eterra/rgi/download-doc?workspaceId=...&applicationId=...&docId=...
* GET /api/eterra/rgi/download-doc?workspaceId=127&applicationId=X&docId=Y&fileId=Z
*
* Check file visibility and download an issued document from RGI.
* Step 1: Calls rgi/appdetail/issueddocs/fileVisibility to check access.
* Step 2: Downloads the file via rgi/appdetail/issueddocs/download.
* Downloads an issued document from eTerra RGI.
* 3-step flow:
* 1. fileVisibility/{wid}/{appId}/{docId} — check access, get fileId
* 2. confirmOnView/{wid}/{appId}/{fileId} — confirm view
* 3. loadDocument/downloadFile/{wid}/{fileId} — actual download
*
* If fileId is provided, skips step 1.
*/
export async function GET(req: NextRequest) {
try {
const workspaceId = req.nextUrl.searchParams.get("workspaceId");
const applicationId = req.nextUrl.searchParams.get("applicationId");
const docId = req.nextUrl.searchParams.get("docId");
let fileId = req.nextUrl.searchParams.get("fileId");
if (!workspaceId || !applicationId || !docId) {
if (!workspaceId || !applicationId) {
return NextResponse.json(
{ error: "workspaceId, applicationId and docId are required" },
{ error: "workspaceId and applicationId are required" },
{ status: 400 },
);
}
if (!docId && !fileId) {
return NextResponse.json(
{ error: "docId or fileId is required" },
{ status: 400 },
);
}
@@ -27,40 +39,50 @@ export async function GET(req: NextRequest) {
const username = process.env.ETERRA_USERNAME ?? "";
const password = process.env.ETERRA_PASSWORD ?? "";
if (!username || !password) {
return NextResponse.json(
{ error: "Credentials missing" },
{ status: 500 },
);
return NextResponse.json({ error: "Credentials missing" }, { status: 500 });
}
const client = await EterraClient.create(username, password);
// Step 1: Check file visibility
const visibilityPath = `rgi/appdetail/issueddocs/fileVisibility/${encodeURIComponent(workspaceId)}/${encodeURIComponent(applicationId)}/${encodeURIComponent(docId)}`;
const visibility = await client.rgiGet(visibilityPath);
// Step 2: Attempt to download the file
try {
const downloadPath = `rgi/appdetail/issueddocs/download/${encodeURIComponent(workspaceId)}/${encodeURIComponent(applicationId)}/${encodeURIComponent(docId)}`;
const { data, contentType, filename } =
await client.rgiDownload(downloadPath);
return new NextResponse(new Uint8Array(data), {
status: 200,
headers: {
"Content-Type": contentType,
"Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`,
"Content-Length": String(data.length),
},
});
} catch {
// Download failed — return the visibility response instead so the caller
// can inspect what eTerra reported (may contain a URL or error details).
return NextResponse.json({
visibility,
downloadFailed: true,
});
// Step 1: Get fileId from visibility check (if not provided)
if (!fileId && docId) {
const visibility = await client.rgiGet(
`rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${docId}`,
);
// Extract fileId from visibility response
if (visibility && typeof visibility === "object") {
const v = visibility as Record<string, unknown>;
fileId = String(
v.fileId ?? v.id ?? v.pk ?? v.documentFileId ?? "",
);
}
if (!fileId) {
return NextResponse.json({ error: "fileId not found in visibility response", visibility }, { status: 404 });
}
}
// Step 2: Confirm on view
try {
await client.rgiGet(
`rgi/appdetail/issueddocs/confirmOnView/${workspaceId}/${applicationId}/${fileId}`,
);
} catch {
// Non-critical — continue to download
}
// Step 3: Download
const { data, contentType, filename } = await client.rgiDownload(
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${fileId}`,
);
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 });