Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@devkiran
Copy link
Collaborator

@devkiran devkiran commented Sep 23, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Analytics responses now consistently return numbers for clicks, leads, sales, and sale amount, coercing string inputs and defaulting to 0 when missing.
    • Standardized parsing across single and multi-ID analytics endpoints to prevent unparsed data from reaching clients.
    • Ensures a consistent response shape and types, reducing the need for client-side casting and error handling.
    • Improves reliability of reported counts and monetary totals across analytics views and integrations.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 23, 2025

Walkthrough

Updated 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

Cohort / File(s) Summary of Edits
Analytics count parsing
apps/web/lib/analytics/get-analytics.ts
Wrapped count-endpoint results with analyticsResponse["count"].parse(...) for both single linkId and aggregated linkIds paths.
Zod numeric coercion in analytics response
apps/web/lib/zod/schemas/analytics-response.ts
Switched clicks, leads, sales, saleAmount from z.number() to z.coerce.number(); descriptions and defaults unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I thump my paws at parsed delight,
Counts now tidy, numbers right;
Clicks and sales hop into form,
Coerced to number, safe and warm.
In fields of zod I nibble cheer—
Clean carrots of data, crisp and clear. 🥕🐇

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Fix the partner analytics API response" accurately and concisely reflects the main change in this PR—correcting analytics API output by parsing DB rows with the analyticsResponse.count schema and coercing numeric fields—so it is clear and appropriate for reviewers scanning history.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-partner-analytics

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@devkiran devkiran requested a review from steven-tey September 23, 2025 18:58
@vercel
Copy link
Contributor

vercel bot commented Sep 23, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Updated (UTC)
dub Ready Ready Preview Sep 23, 2025 9:07pm

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between 99b7c98 and 3c0d189.

📒 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

Comment on lines +71 to 73

return analyticsResponse["count"].parse(response.rows[0]);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

Comment on lines +89 to 91

return analyticsResponse["count"].parse(response.rows[0]);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

Suggested change
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.

@steven-tey steven-tey merged commit 9482066 into main Sep 23, 2025
8 checks passed
@steven-tey steven-tey deleted the fix-partner-analytics branch September 23, 2025 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants