-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Refactor partner discount fetching #2624
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
…scount function and integrating discount retrieval directly into program enrollment. Update API responses to include discount information where applicable.
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThe changes remove the separate Changes
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)
Possibly related PRs
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ 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)
✨ 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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
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: 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
getProgramEnrollmentOrThrowfunction for consistency across the codebase, especially since it now supports theincludeDiscountparameter.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
📒 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: trueparameter 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
includeDiscountparameter is properly added with a sensible default value offalse, 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 togetProgramEnrollmentOrThrowpass 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: trueto 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.
This pull request refactors how partner discounts are handled in the codebase by removing the
determinePartnerDiscountfunction 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
determinePartnerDiscountfunction: The functiondeterminePartnerDiscountwas deleted, as discount data is now directly included in program enrollment queries. (apps/web/lib/partners/determine-partner-discount.ts)Updated
getProgramEnrollmentOrThrowfunction: Added anincludeDiscountparameter 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:
route.tsin partner profile API: Updated theGEThandler to include discount data directly from program enrollment, removing the need for a separate call todeterminePartnerDiscount. (apps/web/app/(ee)/api/partner-profile/programs/[programId]/route.ts) (apps/web/app/(ee)/api/partner-profile/programs/[programId]/route.tsL3, apps/web/app/(ee)/api/partner-profile/programs/[programId]/route.tsR12-R15)Utility function updates:
getReferralsEmbedDatain referrals utils: Adjusted the logic to fetch discount data directly from the program enrollment query instead of usingdeterminePartnerDiscount. (apps/web/app/(ee)/app.dub.co/embed/referrals/utils.ts) [1] [2] [3]Summary by CodeRabbit
Refactor
Chores