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:
+426
-390
File diff suppressed because it is too large
Load Diff
@@ -5,21 +5,33 @@ export const runtime = "nodejs";
|
|||||||
export const dynamic = "force-dynamic";
|
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.
|
* Downloads an issued document from eTerra RGI.
|
||||||
* Step 1: Calls rgi/appdetail/issueddocs/fileVisibility to check access.
|
* 3-step flow:
|
||||||
* Step 2: Downloads the file via rgi/appdetail/issueddocs/download.
|
* 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) {
|
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 docId = req.nextUrl.searchParams.get("docId");
|
const docId = req.nextUrl.searchParams.get("docId");
|
||||||
|
let fileId = req.nextUrl.searchParams.get("fileId");
|
||||||
|
|
||||||
if (!workspaceId || !applicationId || !docId) {
|
if (!workspaceId || !applicationId) {
|
||||||
return NextResponse.json(
|
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 },
|
{ status: 400 },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -27,23 +39,41 @@ export async function GET(req: NextRequest) {
|
|||||||
const username = process.env.ETERRA_USERNAME ?? "";
|
const username = process.env.ETERRA_USERNAME ?? "";
|
||||||
const password = process.env.ETERRA_PASSWORD ?? "";
|
const password = process.env.ETERRA_PASSWORD ?? "";
|
||||||
if (!username || !password) {
|
if (!username || !password) {
|
||||||
return NextResponse.json(
|
return NextResponse.json({ error: "Credentials missing" }, { status: 500 });
|
||||||
{ error: "Credentials missing" },
|
|
||||||
{ status: 500 },
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = await EterraClient.create(username, password);
|
const client = await EterraClient.create(username, password);
|
||||||
|
|
||||||
// Step 1: Check file visibility
|
// Step 1: Get fileId from visibility check (if not provided)
|
||||||
const visibilityPath = `rgi/appdetail/issueddocs/fileVisibility/${encodeURIComponent(workspaceId)}/${encodeURIComponent(applicationId)}/${encodeURIComponent(docId)}`;
|
if (!fileId && docId) {
|
||||||
const visibility = await client.rgiGet(visibilityPath);
|
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: Attempt to download the file
|
// Step 2: Confirm on view
|
||||||
try {
|
try {
|
||||||
const downloadPath = `rgi/appdetail/issueddocs/download/${encodeURIComponent(workspaceId)}/${encodeURIComponent(applicationId)}/${encodeURIComponent(docId)}`;
|
await client.rgiGet(
|
||||||
const { data, contentType, filename } =
|
`rgi/appdetail/issueddocs/confirmOnView/${workspaceId}/${applicationId}/${fileId}`,
|
||||||
await client.rgiDownload(downloadPath);
|
);
|
||||||
|
} 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), {
|
return new NextResponse(new Uint8Array(data), {
|
||||||
status: 200,
|
status: 200,
|
||||||
@@ -53,14 +83,6 @@ export async function GET(req: NextRequest) {
|
|||||||
"Content-Length": String(data.length),
|
"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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} 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 });
|
||||||
|
|||||||
Reference in New Issue
Block a user