feat(analytics): add export to CSV and PDF functionality#238
feat(analytics): add export to CSV and PDF functionality#238Kritika200520 wants to merge 2 commits into
Conversation
|
Someone is attempting to deploy a commit to the vishnukothakapu's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughAdds a GET API at ChangesAnalytics Export
Sequence DiagramsequenceDiagram
participant User as User
participant Dashboard as AnalyticsOverview
participant ExportAPI as Export API
participant Prisma as Prisma
User->>Dashboard: Open AnalyticsOverview
Dashboard->>ExportAPI: GET /api/analytics/export?format=csv
ExportAPI->>Prisma: Query click events for user (include link)
Prisma-->>ExportAPI: Return click records
ExportAPI-->>Dashboard: Return CSV attachment (or JSON)
Dashboard-->>User: Trigger CSV download or open printable PDF window
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly Related Issues
Suggested Reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@app/api/analytics/export/route.ts`:
- Around line 6-17: The current auth check only tests for a truthy session but
getSession can return an empty session object; update the guard after calling
getSession(req) to verify session.user and session.user.id (e.g., if (!session
|| !session.user || !session.user.id) return new NextResponse("Unauthorized", {
status: 401 })), so the route returns 401 for invalidated/deleted tokens instead
of proceeding and throwing later; modify the logic around getSession and the
NextResponse creation to use this stronger check.
- Around line 30-37: The CSV generation in route.ts builds fields (url, referer,
country, device, browser, os) from user-controlled click values but only escapes
quotes, leaving cells that start with =, +, -, or @ vulnerable to CSV formula
injection; update the sanitization used before appending to csvContent so each
field first strips/escapes internal quotes as currently done and then, if the
raw value begins with any of the dangerous prefix characters, prepend a safe
prefix (for example a single quote) to neutralize spreadsheet formulas before
wrapping in quotes; apply this same fix to the variables url, referer, country,
device, browser, and os used when constructing csvContent.
- Around line 15-35: The code queries prisma.click and reads fields that don't
exist; change the delegate to prisma.clickEvent.findMany and include the related
link (include: { link: true }) so you can access link data; then update CSV
generation to use the ClickEvent schema fields: use
click.createdAt.toISOString(), platform/link value from click.link?.url or
fallback to click.linkId, referrer from click.referrer, location from
click.country, device from click.deviceType, and browser/os from click.userAgent
(or parse userAgent later if needed); also keep the same CSV quoting/replace
logic but applied to those ClickEvent properties and update the header names to
match.
In `@app/dashboard/AnalyticsOverview.tsx`:
- Around line 147-152: Replace the onClick handlers on the Radix
DropdownMenuItem instances so they use the onSelect activation prop instead (for
the items that call exportToCSV and exportToPDF). Locate the DropdownMenuItem
elements that reference exportToCSV and exportToPDF and change their prop from
onClick={...} to onSelect={...} (ensuring the handlers still invoke the same
functions or wrap them in an arrow function if necessary) so keyboard activation
(Enter/Space) triggers the exports.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: cac5ddf5-9e8e-4214-8f5e-b26b476bc202
📒 Files selected for processing (2)
app/api/analytics/export/route.tsapp/dashboard/AnalyticsOverview.tsx
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Hi @Kritika200520, Issues
import { prisma } from "@/lib/db";
import { getSession } from "@/lib/auth"; The error suggests that "getSession" does not exist in "lib/auth.ts". Please:
Then push the updated changes. |
Anushreebasics
left a comment
There was a problem hiding this comment.
Wrapping fields in double quotes and escaping internal quotes is fine. Ensure fields with newlines are quoted too (current approach does this).
…imports and ClickEvent fields
Summary
Adds the ability for authenticated users to export their analytics data directly from the dashboard as a CSV file or a PDF report.
Closes #[222]
Changes Made
app/dashboard/AnalyticsOverview.tsx/api/analytics/exportendpointwindow.print()app/api/analytics/export/route.ts(new file)/api/analytics/export401 Unauthorizedif the user is not logged in?format=csvquery param (defaults to csv)Timestamp,Platform Link,Referrer,Location,Device,Browser,OSHow to Test
.csvfile downloads with your full click historyAcceptance Criteria Met
/api/analytics/exportreturns formatted dataAnalyticsOverview.tsxSummary by CodeRabbit
New Features
UI