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

Skip to content

Conversation

@devkiran
Copy link
Collaborator

@devkiran devkiran commented Jul 8, 2025

This pull request refactors how partner discounts are handled in the codebase by removing the determinePartnerDiscount function and directly including discount data in program enrollment queries. This improves efficiency and reduces redundant code. Additionally, it updates the affected APIs and utility functions to work with the new approach.

Refactoring partner discount handling:

  • Removed determinePartnerDiscount function: The function determinePartnerDiscount was deleted, as discount data is now directly included in program enrollment queries. (apps/web/lib/partners/determine-partner-discount.ts)

  • Updated getProgramEnrollmentOrThrow function: Added an includeDiscount parameter to allow fetching discount data directly within program enrollment queries. (apps/web/lib/api/programs/get-program-enrollment-or-throw.ts) [1] [2]

API updates:

Utility function updates:

  • Refactored getReferralsEmbedData in referrals utils: Adjusted the logic to fetch discount data directly from the program enrollment query instead of using determinePartnerDiscount. (apps/web/app/(ee)/app.dub.co/embed/referrals/utils.ts) [1] [2] [3]

Summary by CodeRabbit

  • Refactor

    • Streamlined how partner discounts are retrieved by fetching discount data directly with program enrollment, removing separate discount determination steps.
    • Simplified referral reward and commission logic for improved performance and maintainability.
  • Chores

    • Removed obsolete internal discount determination logic to reduce code complexity.

…scount function and integrating discount retrieval directly into program enrollment. Update API responses to include discount information where applicable.
@vercel
Copy link
Contributor

vercel bot commented Jul 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
dub ✅ Ready (Inspect) Visit Preview Jul 10, 2025 4:18am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 8, 2025

Walkthrough

The changes remove the separate determinePartnerDiscount function and instead retrieve the discount directly via the getProgramEnrollmentOrThrow function, which now supports an includeDiscount option. Related code is updated to use this streamlined approach, and the obsolete discount utility is deleted.

Changes

File(s) Change Summary
apps/web/app/(ee)/api/partner-profile/programs/[programId]/route.ts Refactored to retrieve discount directly from program enrollment with includeDiscount instead of separate call.
apps/web/app/(ee)/app.dub.co/embed/referrals/utils.ts Removed import and usage of determinePartnerDiscount; fetches discount directly in enrollment query and returns it.
apps/web/lib/api/programs/get-program-enrollment-or-throw.ts Added includeDiscount parameter to function, enabling direct inclusion of discount data in enrollment retrieval.
apps/web/lib/partners/determine-partner-discount.ts Deleted file; removed the now-obsolete determinePartnerDiscount utility function.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant getProgramEnrollmentOrThrow
    participant DB

    Caller->>getProgramEnrollmentOrThrow: Request enrollment (includeDiscount: true)
    getProgramEnrollmentOrThrow->>DB: Query enrollment + discount
    DB-->>getProgramEnrollmentOrThrow: Enrollment with discount
    getProgramEnrollmentOrThrow-->>Caller: Return enrollment (includes discount)
Loading

Possibly related PRs

  • Improve reward UI/UX #2504: Refactors the partner-profile program API route and removes the determinePartnerDiscount function, updating getProgramEnrollmentOrThrow to include discount data, directly related to this PR.

Poem

A bunny hopped through code so bright,
Tidying discounts left and right.
No more chasing functions gone,
Now discounts come with just one call—
Simpler paths for all to see,
Hopping forward, bug-free! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e854994 and 7b9bde3.

📒 Files selected for processing (1)
  • apps/web/app/(ee)/app.dub.co/embed/referrals/utils.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/web/app/(ee)/app.dub.co/embed/referrals/utils.ts
⏰ 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
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@devkiran
Copy link
Collaborator Author

devkiran commented Jul 8, 2025

@coderabbitai full review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 8, 2025

✅ Actions performed

Full review triggered.

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: 0

🧹 Nitpick comments (1)
apps/web/app/(ee)/app.dub.co/embed/referrals/utils.ts (1)

16-31: Consider using the shared getProgramEnrollmentOrThrow function for consistency.

While the current direct Prisma query approach works correctly, consider using the updated getProgramEnrollmentOrThrow function for consistency across the codebase, especially since it now supports the includeDiscount parameter.

This would provide benefits like:

  • Consistent error handling across the codebase
  • Centralized enrollment fetching logic
  • Automatic handling of program slug vs ID resolution

Example refactor:

-  const programEnrollment = await prisma.programEnrollment.findUnique({
-    where: {
-      partnerId_programId: {
-        partnerId,
-        programId,
-      },
-    },
-    include: {
-      links: true,
-      program: true,
-      clickReward: true,
-      leadReward: true,
-      saleReward: true,
-      discount: true,
-    },
-  });
+  const programEnrollment = await getProgramEnrollmentOrThrow({
+    partnerId,
+    programId,
+    includeRewards: true,
+    includeDiscount: true,
+  });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c129e82 and e854994.

📒 Files selected for processing (4)
  • apps/web/app/(ee)/api/partner-profile/programs/[programId]/route.ts (1 hunks)
  • apps/web/app/(ee)/app.dub.co/embed/referrals/utils.ts (1 hunks)
  • apps/web/lib/api/programs/get-program-enrollment-or-throw.ts (2 hunks)
  • apps/web/lib/partners/determine-partner-discount.ts (0 hunks)
💤 Files with no reviewable changes (1)
  • apps/web/lib/partners/determine-partner-discount.ts
🧰 Additional context used
🧠 Learnings (2)
apps/web/app/(ee)/app.dub.co/embed/referrals/utils.ts (2)
Learnt from: devkiran
PR: dubinc/dub#2448
File: packages/email/src/templates/partner-program-summary.tsx:0-0
Timestamp: 2025-05-29T04:45:18.504Z
Learning: In the PartnerProgramSummary email template (packages/email/src/templates/partner-program-summary.tsx), the stat titles are hardcoded constants ("Clicks", "Leads", "Sales", "Earnings") that will always match the ICONS object keys after toLowerCase() conversion, so icon lookup failures are not possible.
Learnt from: devkiran
PR: dubinc/dub#2177
File: apps/web/lib/api/links/bulk-create-links.ts:66-84
Timestamp: 2025-06-06T07:59:03.120Z
Learning: In apps/web/lib/api/links/bulk-create-links.ts, the team accepts the risk of potential undefined results from links.find() operations when building invalidLinks arrays, because existing links are fetched from the database based on the input links, so matches are expected to always exist.
apps/web/lib/api/programs/get-program-enrollment-or-throw.ts (1)
Learnt from: devkiran
PR: dubinc/dub#2448
File: packages/email/src/templates/partner-program-summary.tsx:0-0
Timestamp: 2025-05-29T04:45:18.504Z
Learning: In the PartnerProgramSummary email template (packages/email/src/templates/partner-program-summary.tsx), the stat titles are hardcoded constants ("Clicks", "Leads", "Sales", "Earnings") that will always match the ICONS object keys after toLowerCase() conversion, so icon lookup failures are not possible.
🧬 Code Graph Analysis (1)
apps/web/app/(ee)/app.dub.co/embed/referrals/utils.ts (1)
apps/web/lib/partners/sort-rewards-by-event-order.ts (1)
  • sortRewardsByEventOrder (9-22)
🔇 Additional comments (10)
apps/web/app/(ee)/api/partner-profile/programs/[programId]/route.ts (2)

12-12: LGTM: Correct implementation of discount inclusion.

The addition of includeDiscount: true parameter correctly enables fetching discount data directly within the program enrollment query, eliminating the need for separate discount determination logic.


15-15: LGTM: Proper destructuring of discount data.

The destructuring correctly extracts the discount from the program enrollment result, consistent with the refactoring to include discount data directly in enrollment queries.

apps/web/lib/api/programs/get-program-enrollment-or-throw.ts (5)

10-11: LGTM: Correct parameter addition for discount inclusion.

The includeDiscount parameter is properly added with a sensible default value of false, maintaining backward compatibility.


16-17: LGTM: Type definition correctly updated.

The type definition properly reflects the new optional parameter structure.


26-28: LGTM: Clean include object construction.

The conditional inclusion of the partner relation is properly implemented using the spread operator pattern.


34-36: LGTM: Correct discount inclusion logic.

The conditional inclusion of the discount relation follows the same pattern as other optional includes and correctly implements the discount fetching functionality.


6-18: No action needed—named parameters in use
All existing calls to getProgramEnrollmentOrThrow pass an object literal (destructured named parameters), so reordering the properties does not affect callers.

apps/web/app/(ee)/app.dub.co/embed/referrals/utils.ts (3)

29-29: LGTM: Correct discount inclusion in Prisma query.

The addition of discount: true to the include object properly enables fetching discount data directly within the enrollment query.


51-52: LGTM: Clean destructuring of enrollment data.

The destructuring correctly extracts all required fields including the new discount field from the program enrollment result.


58-61: LGTM: Improved rewards array construction.

The simplified approach of destructuring individual rewards directly is more readable and maintainable than the previous implementation.

@steven-tey steven-tey merged commit 89c80fa into main Jul 10, 2025
8 checks passed
@steven-tey steven-tey deleted the improve-discount-fetch branch July 10, 2025 15:20
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