fix(rgi): correct download flow — confirmOnView + downloadFile by documentPk
The download uses documentPk (not documentTypeId) as the file identifier:
1. confirmOnView/{wid}/{appId}/{documentPk} — confirm view
2. loadDocument/downloadFile/{wid}/{documentPk} — actual download
Removed fileVisibility step (not needed, was causing 404).
Updated page download links to pass documentPk.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -296,7 +296,7 @@ function IssuedDocsPanel({
|
|||||||
</div>
|
</div>
|
||||||
<Button size="sm" variant="outline" className="shrink-0 gap-1" asChild>
|
<Button size="sm" variant="outline" className="shrink-0 gap-1" asChild>
|
||||||
<a
|
<a
|
||||||
href={`/api/eterra/rgi/download-doc?workspaceId=${doc.workspaceId || workspaceId}&applicationId=${doc.applicationId || applicationPk}&docId=${doc.documentTypeId}&fileId=${doc.documentPk}`}
|
href={`/api/eterra/rgi/download-doc?workspaceId=${doc.workspaceId || workspaceId}&applicationId=${doc.applicationId || applicationPk}&documentPk=${doc.documentPk}`}
|
||||||
target="_blank"
|
target="_blank"
|
||||||
rel="noopener noreferrer"
|
rel="noopener noreferrer"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -5,33 +5,22 @@ export const runtime = "nodejs";
|
|||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /api/eterra/rgi/download-doc?workspaceId=127&applicationId=X&docId=Y&fileId=Z
|
* GET /api/eterra/rgi/download-doc?workspaceId=127&applicationId=X&documentPk=Y
|
||||||
*
|
*
|
||||||
* Downloads an issued document from eTerra RGI.
|
* Downloads an issued document from eTerra RGI.
|
||||||
* 3-step flow:
|
* 2-step flow:
|
||||||
* 1. fileVisibility/{wid}/{appId}/{docId} — check access, get fileId
|
* 1. confirmOnView/{wid}/{appId}/{documentPk}
|
||||||
* 2. confirmOnView/{wid}/{appId}/{fileId} — confirm view
|
* 2. loadDocument/downloadFile/{wid}/{documentPk}
|
||||||
* 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 documentPk = req.nextUrl.searchParams.get("documentPk");
|
||||||
let fileId = req.nextUrl.searchParams.get("fileId");
|
|
||||||
|
|
||||||
if (!workspaceId || !applicationId) {
|
if (!workspaceId || !applicationId || !documentPk) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "workspaceId and applicationId are required" },
|
{ error: "workspaceId, applicationId and documentPk are required" },
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!docId && !fileId) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "docId or fileId is required" },
|
|
||||||
{ status: 400 },
|
{ status: 400 },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -44,35 +33,18 @@ export async function GET(req: NextRequest) {
|
|||||||
|
|
||||||
const client = await EterraClient.create(username, password);
|
const client = await EterraClient.create(username, password);
|
||||||
|
|
||||||
// Step 1: Get fileId from visibility check (if not provided)
|
// Step 1: Confirm on view
|
||||||
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 {
|
try {
|
||||||
await client.rgiGet(
|
await client.rgiGet(
|
||||||
`rgi/appdetail/issueddocs/confirmOnView/${workspaceId}/${applicationId}/${fileId}`,
|
`rgi/appdetail/issueddocs/confirmOnView/${workspaceId}/${applicationId}/${documentPk}`,
|
||||||
);
|
);
|
||||||
} catch {
|
} catch {
|
||||||
// Non-critical — continue to download
|
// Non-critical — continue to download
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3: Download
|
// Step 2: Download file
|
||||||
const { data, contentType, filename } = await client.rgiDownload(
|
const { data, contentType, filename } = await client.rgiDownload(
|
||||||
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${fileId}`,
|
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
return new NextResponse(new Uint8Array(data), {
|
return new NextResponse(new Uint8Array(data), {
|
||||||
|
|||||||
Reference in New Issue
Block a user