-
Notifications
You must be signed in to change notification settings - Fork 498
fix ungrouped include by default #924
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
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughAdds logic in getSubscriptions to include ungrouped include-by-default products as active subscriptions. Updates tests to cover ungrouped default behavior and default handling in getSubscriptions. Exports getSubscriptions from the payments module and updates test imports accordingly. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant C as Caller
participant P as payments.getSubscriptions
participant DB as Database
participant CAT as Catalog Rules
C->>P: getSubscriptions(customerId)
P->>DB: Fetch existing subscriptions
DB-->>P: Subscriptions[]
P->>CAT: Determine include-by-default products (grouped)
CAT-->>P: Default products by catalog
P->>P: Merge catalog-defaults when no prior subs
alt Ungrouped include-by-default products
P->>CAT: Find ungrouped defaults (no catalogId)
CAT-->>P: Ungrouped defaults
P->>P: Inject as active subscriptions if not present
end
P-->>C: Final Subscriptions[]
note over P: New step adds ungrouped defaults
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🧰 Additional context used📓 Path-based instructions (2)**/*.{ts,tsx,js,jsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/*.test.{ts,tsx,js}📄 CodeRabbit inference engine (AGENTS.md)
Files:
🧬 Code graph analysis (2)apps/backend/src/lib/payments.tsx (1)
apps/backend/src/lib/payments.test.tsx (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). (10)
🔇 Additional comments (3)
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 |
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.
Greptile Overview
Summary
This PR addresses a critical bug in the payments system where ungrouped offers with 'include-by-default' pricing were not being properly included in customer subscriptions. The core issue was that the existing logic only handled grouped default offers but completely missed ungrouped ones, potentially causing free tier or default features to be unavailable to customers who should have automatic access.The fix operates within Stack Auth's payments infrastructure, which manages subscription offers that can be either grouped or ungrouped. Offers marked as 'include-by-default' should automatically appear in a customer's subscription without requiring explicit purchase - this is typically used for free tiers or baseline features. The payment system's getSubscriptions function is responsible for aggregating both explicitly purchased subscriptions and these automatic default offers.
The changes enhance the subscription calculation logic by:
- Adding validation to prevent configuration errors where multiple 'include-by-default' offers exist within the same group
- Implementing proper handling for ungrouped default offers by filtering offers where
groupIdis undefined andpricesis 'include-by-default' - Automatically adding these ungrouped default offers as active subscriptions with appropriate default values
The implementation integrates seamlessly with the existing payments architecture, maintaining backward compatibility while extending functionality to cover the previously missed edge case of ungrouped default offers.
Important Files Changed
Changed Files
| Filename | Score | Overview |
|---|---|---|
| apps/backend/src/lib/payments.tsx | 4/5 | Fixed critical bug where ungrouped 'include-by-default' offers were not included in subscriptions, added validation for multiple default offers in same group |
| apps/backend/src/lib/payments.test.tsx | 5/5 | Added comprehensive test coverage for ungrouped default offers functionality and validation logic |
Confidence score: 4/5
- This PR addresses a well-defined bug with a focused solution that maintains system integrity
- Score reflects solid implementation with comprehensive test coverage, but payments logic is inherently complex
- Pay close attention to the validation logic in payments.tsx to ensure edge cases are properly handled
Sequence Diagram
sequenceDiagram
participant User
participant "Test Suite" as Test
participant "getItemQuantityForCustomer" as GetQty
participant "getSubscriptions" as GetSubs
participant "Mock Prisma" as Prisma
participant "Mock Tenancy" as Tenancy
User->>Test: "Run ungrouped include-by-default test"
Test->>Tenancy: "createMockTenancy with ungrouped offer"
Tenancy-->>Test: "tenancy config with ungrouped include-by-default offer"
Test->>Prisma: "createMockPrisma with empty subscription findMany"
Prisma-->>Test: "mock prisma returning []"
Test->>GetQty: "getItemQuantityForCustomer(prisma, tenancy, itemId, customerId, customerType)"
GetQty->>GetSubs: "getSubscriptions(prisma, tenancy, customerType, customerId)"
GetSubs->>Prisma: "subscription.findMany() for db subscriptions"
Prisma-->>GetSubs: "empty array []"
GetSubs->>GetSubs: "Process ungrouped defaults with prices='include-by-default'"
GetSubs->>GetSubs: "Filter ungrouped offers with include-by-default pricing"
GetSubs->>GetSubs: "Add ungrouped default subscription to list"
GetSubs-->>GetQty: "subscriptions array with ungrouped default"
GetQty->>GetQty: "Process subscription included items"
GetQty->>GetQty: "Calculate item quantity: 5 * 1 = 5"
GetQty->>GetQty: "Add transaction to ledger"
GetQty->>GetQty: "computeLedgerBalanceAtNow(transactions, now)"
GetQty-->>Test: "quantity: 5"
Test->>Test: "expect(qty).toBe(5)"
Test-->>User: "Test passes ✓"
2 files reviewed, no comments
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.
Review by RecurseML
🔍 Review performed on 04cc7a3..a914562
✨ No bugs found, your code is sparkling clean
✅ Files analyzed, no issues (1)
• apps/backend/src/lib/payments.tsx
⏭️ Files skipped (1)
| Locations |
|---|
apps/backend/src/lib/payments.test.tsx |
- Resolved merge conflicts in apps/backend/src/lib/payments.tsx - Updated terminology from offers/groups to products/catalogs - Fixed TypeScript errors in payments.test.tsx - All linting and type checks pass
High-level PR Summary
This PR fixes a bug where ungrouped offers with
include-by-defaultpricing were not being automatically included in subscription lists. The fix ensures that ungrouped default offers are now properly added to the subscriptions array when no explicit database subscription exists for them, matching the existing behavior for grouped offers. Additionally, it adds validation to prevent multipleinclude-by-defaultoffers from being configured within the same group.⏱️ Estimated Review Time: 15-30 minutes
💡 Review Order Suggestion
apps/backend/src/lib/payments.tsxapps/backend/src/lib/payments.test.tsxImportant
Fixes bug to include ungrouped
include-by-defaultoffers in subscriptions and adds validation for multiple offers in the same group.getSubscriptions()inpayments.tsxto include ungroupedinclude-by-defaultoffers in subscriptions when no explicit subscription exists.getSubscriptions()to prevent multipleinclude-by-defaultoffers in the same group.payments.test.tsxto verify inclusion of ungroupedinclude-by-defaultoffers and validation of multiple offers in the same group.payments.test.tsxto includegetSubscriptions.This description was created by
for 565acc3. You can customize this summary. It will automatically update as commits are pushed.
Summary by CodeRabbit
New Features
Improvements
Tests