Sciweave marketing export#5
Conversation
📝 WalkthroughWalkthroughAdds a new API endpoint for exporting Sciweave marketing consent data with configurable format support and implements a corresponding UI export feature in the users datatable toolbar that allows exporting Sciweave marketing emails in CSV, XLS, or XLSX formats with error handling and user feedback. Changes
Sequence Diagram(s)sequenceDiagram
actor User
participant UI as Data Table Toolbar
participant API as API Route Handler
participant Upstream as NODES_API_URL
participant Browser as Browser Download
User->>UI: Click "Export Sciweave<br/>Marketing Emails" (format)
UI->>UI: Set loading state
UI->>API: GET /api/export-sciweave-marketing-consent<br/>?format={format}
API->>Upstream: Forward request with credentials
alt Successful Export
Upstream-->>API: Export data (blob)
API-->>UI: Return response
UI->>Browser: Trigger blob download
Browser-->>User: Download file
UI->>UI: Show success toast
else Error Occurs
Upstream-->>API: Error response
API->>API: Log error
API-->>UI: 500 with error JSON
UI->>UI: Show error toast
end
UI->>UI: Clear loading state
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@src/app/api/export-sciweave-marketing-consent/route.ts`:
- Around line 19-21: The catch block returns the raw error object (err) which
serializes inconsistently; change it to return a stable payload with a message
string by extracting err.message (or using String(err) if undefined) and pass
NextResponse.json({ message: message }, { status: 500 }); keep
console.error/console.error(err) or console.error(err.stack) for server logs but
do not return the raw err object; update the catch in route.ts (the catch
handling around NextResponse.json) accordingly.
- Around line 7-10: The code reads the format query via
request.nextUrl.searchParams.get("format") and forwards it to the upstream fetch
without validation; add a whitelist (e.g., allow only "csv" and "json") and if
the provided format is missing or not one of the allowed values return a 400
response immediately with a clear error message, otherwise continue using the
validated format when building the fetch URL to
`${NODES_API_URL}/v1/admin/users/export-sciweave-marketing-consent?format=${format}`;
update the handler around the format variable (where
request.nextUrl.searchParams.get is called) to perform this check and
short-circuit invalid requests.
| const format = request.nextUrl.searchParams.get("format") || "csv"; | ||
| const response = await fetch( | ||
| `${NODES_API_URL}/v1/admin/users/export-sciweave-marketing-consent?format=${format}`, | ||
| { |
There was a problem hiding this comment.
Validate the format query and reject unsupported values.
Whitelisting avoids unexpected formats reaching the upstream export and returns a clear 400 to callers.
🛠️ Suggested fix
- const format = request.nextUrl.searchParams.get("format") || "csv";
+ const formatParam = request.nextUrl.searchParams.get("format") ?? "csv";
+ if (!["csv", "xls", "xlsx"].includes(formatParam)) {
+ return NextResponse.json(
+ { message: "Invalid format. Use csv, xls, or xlsx." },
+ { status: 400 }
+ );
+ }
+ const format = formatParam;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const format = request.nextUrl.searchParams.get("format") || "csv"; | |
| const response = await fetch( | |
| `${NODES_API_URL}/v1/admin/users/export-sciweave-marketing-consent?format=${format}`, | |
| { | |
| const formatParam = request.nextUrl.searchParams.get("format") ?? "csv"; | |
| if (!["csv", "xls", "xlsx"].includes(formatParam)) { | |
| return NextResponse.json( | |
| { message: "Invalid format. Use csv, xls, or xlsx." }, | |
| { status: 400 } | |
| ); | |
| } | |
| const format = formatParam; | |
| const response = await fetch( | |
| `${NODES_API_URL}/v1/admin/users/export-sciweave-marketing-consent?format=${format}`, | |
| { |
🤖 Prompt for AI Agents
In `@src/app/api/export-sciweave-marketing-consent/route.ts` around lines 7 - 10,
The code reads the format query via request.nextUrl.searchParams.get("format")
and forwards it to the upstream fetch without validation; add a whitelist (e.g.,
allow only "csv" and "json") and if the provided format is missing or not one of
the allowed values return a 400 response immediately with a clear error message,
otherwise continue using the validated format when building the fetch URL to
`${NODES_API_URL}/v1/admin/users/export-sciweave-marketing-consent?format=${format}`;
update the handler around the format variable (where
request.nextUrl.searchParams.get is called) to perform this check and
short-circuit invalid requests.
| } catch (err) { | ||
| console.error(err); | ||
| return NextResponse.json({ error: err }, { status: 500 }); |
There was a problem hiding this comment.
Return a stable error payload (message) instead of the raw error object.
The client expects a message; returning the raw error can serialize poorly and exposes inconsistent shapes.
🛠️ Suggested fix
} catch (err) {
console.error(err);
- return NextResponse.json({ error: err }, { status: 500 });
+ const message =
+ err instanceof Error
+ ? err.message
+ : "Failed to export Sciweave marketing consent data";
+ return NextResponse.json({ message }, { status: 500 });
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| } catch (err) { | |
| console.error(err); | |
| return NextResponse.json({ error: err }, { status: 500 }); | |
| } catch (err) { | |
| console.error(err); | |
| const message = | |
| err instanceof Error | |
| ? err.message | |
| : "Failed to export Sciweave marketing consent data"; | |
| return NextResponse.json({ message }, { status: 500 }); | |
| } |
🤖 Prompt for AI Agents
In `@src/app/api/export-sciweave-marketing-consent/route.ts` around lines 19 - 21,
The catch block returns the raw error object (err) which serializes
inconsistently; change it to return a stable payload with a message string by
extracting err.message (or using String(err) if undefined) and pass
NextResponse.json({ message: message }, { status: 500 }); keep
console.error/console.error(err) or console.error(err.stack) for server logs but
do not return the raw err object; update the catch in route.ts (the catch
handling around NextResponse.json) accordingly.
Summary by CodeRabbit
Release Notes
✏️ Tip: You can customize this high-level summary in your review settings.