diff --git a/src/app/api/eterra/rgi/download-doc/route.ts b/src/app/api/eterra/rgi/download-doc/route.ts index 6352e90..7179e98 100644 --- a/src/app/api/eterra/rgi/download-doc/route.ts +++ b/src/app/api/eterra/rgi/download-doc/route.ts @@ -8,8 +8,9 @@ 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 + * Tries server-side download first. If that fails (some documents are + * restricted to the current actor), returns a direct eTerra URL that + * works in the user's browser session. */ export async function GET(req: NextRequest) { try { @@ -17,7 +18,6 @@ export async function GET(req: NextRequest) { const applicationId = req.nextUrl.searchParams.get("applicationId"); const documentPk = req.nextUrl.searchParams.get("documentPk"); const documentTypeId = req.nextUrl.searchParams.get("documentTypeId"); - const checkOnly = req.nextUrl.searchParams.get("check") === "1"; if (!workspaceId || !applicationId || !documentPk) { return NextResponse.json( @@ -34,43 +34,46 @@ export async function GET(req: NextRequest) { const client = await EterraClient.create(username, password); - // Step 1: fileVisibility — check if document is downloadable + // Try fileVisibility first (fast check) + let available = false; if (documentTypeId) { try { - await client.rgiGet( + const vis = await client.rgiGet( `rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentTypeId}`, ); + if (vis && typeof vis === "object" && (vis as Record).msg === "OK") { + available = true; + } } catch { - // 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 }, - ); + // Not available server-side } } - // Check-only mode: just verify availability - if (checkOnly) { - return NextResponse.json({ available: true }); + // If fileVisibility passed, try download + if (available) { + try { + const { data, contentType, filename } = await client.rgiDownload( + `rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`, + ); + 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 { + // Fall through to redirect + } } - // 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), - }, - }); + // Server-side download not available — redirect to eTerra direct URL + // User's browser session (if logged into eTerra) can download it + const eterraUrl = `https://eterra.ancpi.ro/eterra/api/rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`; + return NextResponse.redirect(eterraUrl, 302); } catch (error) { const message = error instanceof Error ? error.message : "Eroare server"; return NextResponse.json({ error: message }, { status: 500 }); diff --git a/src/modules/parcel-sync/services/eterra-client.ts b/src/modules/parcel-sync/services/eterra-client.ts index 5206aec..beb9f9c 100644 --- a/src/modules/parcel-sync/services/eterra-client.ts +++ b/src/modules/parcel-sync/services/eterra-client.ts @@ -217,6 +217,14 @@ export class EterraClient { const cookies = await this.jar.getCookies(LOGIN_URL); if (!cookies.some((c) => c.key === "JSESSIONID")) throw new Error("Login failed / session not set"); + + // Activate RGI module context by loading the RGI page + // This may set additional session attributes needed for document downloads + try { + await this.client.get(`${BASE_URL}/`, { timeout: 10_000 }); + } catch { + // Non-critical + } } /* ---- High-level parcels --------------------------------------- */