Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughA new event handling function, Changes
Sequence Diagram(s)sequenceDiagram
participant Stripe
participant syncStripeDataToDb
participant DB
participant handleLoopsEvents
participant LoopsAPI
Stripe->>syncStripeDataToDb: Stripe event (subscription update)
syncStripeDataToDb->>DB: Retrieve current premium state
syncStripeDataToDb->>DB: Update premium record with new subscription data
syncStripeDataToDb->>handleLoopsEvents: Call with current and new subscription data
handleLoopsEvents-->>LoopsAPI: createContact / startedTrial / cancelledPremium / completedTrial (as needed)
LoopsAPI-->>handleLoopsEvents: Response
handleLoopsEvents-->>syncStripeDataToDb: Complete
Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
apps/web/app/api/lemon-squeezy/webhook/route.tsOops! Something went wrong! :( ESLint: 9.24.0 ESLint couldn't find an eslint.config.(js|mjs|cjs) file. From ESLint v9.0.0, the default configuration file is now eslint.config.js. https://eslint.org/docs/latest/use/configure/migration-guide If you still have problems after following the migration guide, please stop by apps/web/ee/billing/stripe/loops-events.test.tsOops! Something went wrong! :( ESLint: 9.24.0 ESLint couldn't find an eslint.config.(js|mjs|cjs) file. From ESLint v9.0.0, the default configuration file is now eslint.config.js. https://eslint.org/docs/latest/use/configure/migration-guide If you still have problems after following the migration guide, please stop by apps/web/ee/billing/stripe/loops-events.tsOops! Something went wrong! :( ESLint: 9.24.0 ESLint couldn't find an eslint.config.(js|mjs|cjs) file. From ESLint v9.0.0, the default configuration file is now eslint.config.js. https://eslint.org/docs/latest/use/configure/migration-guide If you still have problems after following the migration guide, please stop by
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (2)
🧰 Additional context used🧬 Code Graph Analysis (1)apps/web/app/api/lemon-squeezy/webhook/route.ts (1)
🔇 Additional comments (10)
✨ 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 (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
apps/web/ee/billing/stripe/loops-events.ts (1)
22-22: Consider typing the newSubscription parameter.The
newSubscriptionparameter is typed asany. Consider creating a proper type interface for better type safety and developer experience.- newSubscription: any; + newSubscription: { + status: string; + trial_end?: number | null; + // Add other fields as needed + };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
apps/web/ee/billing/stripe/loops-events.test.ts(1 hunks)apps/web/ee/billing/stripe/loops-events.ts(1 hunks)apps/web/ee/billing/stripe/sync-stripe.ts(5 hunks)packages/loops/src/loops.ts(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
apps/web/ee/billing/stripe/sync-stripe.ts (1)
apps/web/ee/billing/stripe/loops-events.ts (1)
handleLoopsEvents(10-86)
🔇 Additional comments (14)
apps/web/ee/billing/stripe/sync-stripe.ts (4)
5-5: Good integration design.The import is correctly placed and follows the project's import organization.
17-27: Excellent design pattern for state change detection.Retrieving the current premium state before updating enables proper detection of subscription lifecycle events. The selected fields match exactly what
handleLoopsEventsexpects.
92-94: Good optimization and consistency improvement.Extracting
newTrialEndto a variable improves readability and ensures consistency between the database update and the event handling logic.
119-124: Proper placement of event handling.Calling
handleLoopsEventsafter the database update ensures the subscription data is persisted before triggering side effects. The error handling inhandleLoopsEventsensures Loops failures won't break the sync process.apps/web/ee/billing/stripe/loops-events.ts (4)
38-47: Trial start detection logic is correct.The logic properly checks for:
- Future trial end date
- No previous subscription status
- Handles name extraction correctly
49-69: First payment detection handles multiple scenarios well.The logic correctly identifies first payments from:
- Trial completion
- New direct subscriptions
- Transitions from incomplete status
The conditional logic is well-structured and comprehensive.
71-81: Cancellation detection covers all terminal states.The logic properly handles the main cancellation states:
canceled,unpaid, andincomplete_expired. The status change check prevents duplicate events.
82-85: Excellent error handling strategy.Not throwing errors from Loops API failures ensures that subscription sync operations aren't interrupted by external service issues. This is the correct approach for side-effect operations.
apps/web/ee/billing/stripe/loops-events.test.ts (6)
1-29: Excellent test setup and organization.The test file has proper mocking setup for external dependencies and follows good testing practices with
beforeEachcleanup.
44-125: Comprehensive trial start scenario coverage.The tests cover all important edge cases:
- Normal trial start
- Past trial end dates
- Existing subscription status
- Missing user names
127-237: Thorough first payment scenario testing.The tests validate all the complex first payment detection logic:
- Trial completion
- New subscriptions
- Incomplete transitions
- Null tier handling
- Non-active status handling
239-319: Complete cancellation scenario coverage.Tests cover all cancellation states and the important logic of preventing duplicate cancellation events when status hasn't changed.
321-399: Excellent edge case and error handling coverage.The tests properly validate:
- Null currentPremium handling
- Missing email scenarios
- Admin email fallback
- Graceful error handling
401-448: Good complex scenario testing.The complex scenarios test realistic edge cases like simultaneous trial start and payment, and proper name parsing with multiple spaces.
Summary by CodeRabbit