diff --git a/src/app/(modules)/rgi-test/page.tsx b/src/app/(modules)/rgi-test/page.tsx index 7173a63..92a012b 100644 --- a/src/app/(modules)/rgi-test/page.tsx +++ b/src/app/(modules)/rgi-test/page.tsx @@ -295,30 +295,21 @@ function IssuedDocsPanel({
+ {/* Server-side download (works when fileVisibility returns OK) */}
diff --git a/src/app/api/eterra/rgi/download-doc/route.ts b/src/app/api/eterra/rgi/download-doc/route.ts index 1632bc5..6352e90 100644 --- a/src/app/api/eterra/rgi/download-doc/route.ts +++ b/src/app/api/eterra/rgi/download-doc/route.ts @@ -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 });