-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix the partner analytics API response #2879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughUpdated analytics count handling to parse database rows with the zod schema in get-analytics.ts. Adjusted the analytics response schema to coerce numeric fields (clicks, leads, sales, saleAmount) to numbers. No exported API signatures or broader control flow were changed. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests
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 |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (2)
apps/web/lib/zod/schemas/analytics-response.ts (1)
32-41: Coercion is right; also enforce integer and non‑negative invariants.Counts and cents should be integers ≥ 0. Tighten the schema to prevent accidental fractional/negative values.
Potential caveat: z.coerce.number() will also coerce booleans and empty strings (true -> 1, '' -> 0). Confirm PlanetScale driver never returns those for these fields; if uncertain, consider a stricter preprocessor.
Apply:
- clicks: z.coerce - .number() + clicks: z.coerce + .number() + .int() + .nonnegative() .describe("The total number of clicks") .default(0), - leads: z.coerce.number().describe("The total number of leads").default(0), - sales: z.coerce.number().describe("The total number of sales").default(0), + leads: z.coerce.number().int().nonnegative().describe("The total number of leads").default(0), + sales: z.coerce.number().int().nonnegative().describe("The total number of sales").default(0), - saleAmount: z.coerce - .number() + saleAmount: z.coerce + .number() + .int() + .nonnegative() .describe("The total amount of sales, in cents") .default(0),apps/web/lib/analytics/get-analytics.ts (1)
71-73: Confirm saleAmount magnitude won’t exceed JS safe integer.Coercing DB strings/BigInts to number can lose precision for very large cents totals. If this can happen, return strings or use BigInt/Decimal instead for saleAmount in the count path.
Also applies to: 89-91
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
apps/web/lib/analytics/get-analytics.ts(2 hunks)apps/web/lib/zod/schemas/analytics-response.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
apps/web/lib/analytics/get-analytics.ts (1)
apps/web/lib/zod/schemas/analytics-response.ts (1)
analyticsResponse(29-492)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build
|
|
||
| return analyticsResponse["count"].parse(response.rows[0]); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: parse will throw when linkId not found (no rows).
response.rows[0] can be undefined; parsing it raises. Guard with a safe fallback.
- return analyticsResponse["count"].parse(response.rows[0]);
+ return analyticsResponse["count"].parse(response.rows[0] ?? {});Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In apps/web/lib/analytics/get-analytics.ts around lines 71 to 73, the code calls
analyticsResponse["count"].parse(response.rows[0]) but response.rows[0] can be
undefined when no rows are returned; guard this by checking response.rows and
response.rows.length (or response.rows[0] !== undefined) before parsing and
provide a safe fallback object or default values to parse (or return a default
count/result) so parse never receives undefined.
|
|
||
| return analyticsResponse["count"].parse(response.rows[0]); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same undefined-row risk for aggregated linkIds.
Guard to avoid runtime error when no matching links or SUM returns no row.
- return analyticsResponse["count"].parse(response.rows[0]);
+ return analyticsResponse["count"].parse(response.rows[0] ?? {});📝 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.
| return analyticsResponse["count"].parse(response.rows[0]); | |
| } | |
| return analyticsResponse["count"].parse(response.rows[0] ?? {}); | |
| } |
🤖 Prompt for AI Agents
In apps/web/lib/analytics/get-analytics.ts around lines 89 to 91, the code
assumes response.rows[0] exists which can be undefined when no matching links or
an aggregate returns no row; add a guard checking response.rows and
response.rows.length > 0 (or that response.rows[0] is defined) before calling
analyticsResponse["count"].parse, and if absent return a safe default parsed
value (e.g., a count of 0 or an appropriate empty object) so the parse call
never receives undefined.
Summary by CodeRabbit