fix(rgi): exhaustive download debug — tries 7 URL patterns + GET/POST confirmOnView
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -4,17 +4,6 @@ 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.
|
||||
* Flow:
|
||||
* 1. fileVisibility/{wid}/{appId}/{documentTypeId} — get file info
|
||||
* 2. confirmOnView/{wid}/{appId}/{documentPk}
|
||||
* 3. loadDocument/downloadFile/{wid}/{documentPk}
|
||||
*
|
||||
* Add &debug=1 to see diagnostic info instead of downloading.
|
||||
*/
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const workspaceId = req.nextUrl.searchParams.get("workspaceId");
|
||||
@@ -37,68 +26,90 @@ export async function GET(req: NextRequest) {
|
||||
}
|
||||
|
||||
const client = await EterraClient.create(username, password);
|
||||
const results: Record<string, unknown> = {};
|
||||
const log: { step: string; result: unknown }[] = [];
|
||||
|
||||
// Step 1: fileVisibility (if documentTypeId provided)
|
||||
// Step 1: fileVisibility (GET)
|
||||
if (documentTypeId) {
|
||||
try {
|
||||
const vis = await client.rgiGet(
|
||||
const r = await client.rgiGet(
|
||||
`rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentTypeId}`,
|
||||
);
|
||||
results.fileVisibility = vis;
|
||||
log.push({ step: "fileVisibility(GET,documentTypeId)", result: r });
|
||||
} catch (e) {
|
||||
results.fileVisibilityError = e instanceof Error ? e.message : String(e);
|
||||
log.push({ step: "fileVisibility(GET,documentTypeId)", result: { error: String(e instanceof Error ? e.message : e) } });
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: confirmOnView
|
||||
// Step 1b: fileVisibility with documentPk
|
||||
try {
|
||||
const confirm = await client.rgiGet(
|
||||
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}`,
|
||||
);
|
||||
results.confirmOnView = confirm;
|
||||
log.push({ step: "confirmOnView(GET)", result: r });
|
||||
} catch (e) {
|
||||
results.confirmOnViewError = e instanceof Error ? e.message : String(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 multiple download URL patterns to find the right one
|
||||
// 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 (const pattern of patterns) {
|
||||
for (let i = 0; i < patterns.length; i++) {
|
||||
try {
|
||||
const { data, contentType, filename } = await client.rgiDownload(pattern);
|
||||
results[`download_${pattern.split('/').slice(-1)[0]}`] = {
|
||||
success: true,
|
||||
contentType,
|
||||
filename,
|
||||
size: data.length,
|
||||
pattern,
|
||||
};
|
||||
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) {
|
||||
results[`download_${pattern.split('/').slice(-1)[0]}`] = {
|
||||
success: false,
|
||||
error: e instanceof Error ? e.message : String(e),
|
||||
pattern,
|
||||
};
|
||||
log.push({
|
||||
step: `download[${i}]`,
|
||||
result: { success: false, error: String(e instanceof Error ? e.message : e), url: patterns[i] },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(results, { status: 200 });
|
||||
return NextResponse.json({ log });
|
||||
}
|
||||
|
||||
// Step 3: Download — try multiple patterns
|
||||
const downloadPatterns = [
|
||||
// Non-debug: try download patterns
|
||||
const patterns = [
|
||||
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
||||
`rgi/appdetail/issueddocs/downloadFile/${workspaceId}/${applicationId}/${documentPk}`,
|
||||
`rgi/appdetail/issueddocs/download/${workspaceId}/${applicationId}/${documentPk}`,
|
||||
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${applicationId}/${documentPk}`,
|
||||
`appDetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
||||
];
|
||||
|
||||
for (const pattern of downloadPatterns) {
|
||||
for (const pattern of patterns) {
|
||||
try {
|
||||
const { data, contentType, filename } = await client.rgiDownload(pattern);
|
||||
if (data.length > 0) {
|
||||
@@ -112,14 +123,11 @@ export async function GET(req: NextRequest) {
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Try next pattern
|
||||
// next pattern
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: "Download failed — all URL patterns returned errors", diagnostics: results },
|
||||
{ status: 500 },
|
||||
);
|
||||
return NextResponse.json({ error: "Download failed", log }, { status: 500 });
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Eroare server";
|
||||
return NextResponse.json({ error: message }, { status: 500 });
|
||||
|
||||
Reference in New Issue
Block a user