Conversation
📝 WalkthroughWalkthroughModify subscription upgrade flow to first retrieve the Stripe subscription, remove any 100% discount when moving to basic, optionally add Changes
Sequence DiagramsequenceDiagram
participant Client
participant API as upgrade_downgrade/2
participant DB as Database
participant Stripe as Stripe API
Client->>API: Request subscription upgrade
API->>Stripe: Retrieve Stripe subscription
Stripe-->>API: Stripe subscription (discount info)
API->>DB: Read subscription / user SAN balance
DB-->>API: Return SAN balance
API->>API: maybe_remove_coupon(params, db_sub, _plan, stripe_sub)
API->>API: maybe_add_san_coupon(params, db_sub, stripe_sub)
API->>Stripe: Update subscription with final params
Stripe-->>API: Update confirmation
API-->>Client: Upgrade result
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@lib/sanbase/stripe/stripe_api.ex`:
- Around line 283-295: The duplicate Stripe call happens because both
maybe_remove_coupon/3 and maybe_add_san_coupon/2 call
retrieve_subscription(db_subscription.stripe_id); refactor the flow to call
retrieve_subscription once (e.g., in the upgrade pipeline before those helpers)
and pass the returned stripe_subscription into both helpers by changing their
signatures to maybe_remove_coupon(..., stripe_subscription) and
maybe_add_san_coupon(..., stripe_subscription), updating all call sites to
supply the fetched stripe_subscription and removing the internal
retrieve_subscription calls from those functions.
🧹 Nitpick comments (1)
lib/sanbase/stripe/stripe_api.ex (1)
290-290: Consider extracting the coupon ID to a module attribute.The coupon ID
"SAN_HOLDER_1000"is hardcoded. Extracting it to a module attribute would improve maintainability and make it easier to change or reference consistently.♻️ Suggested refactor
defmodule Sanbase.StripeApi do `@moduledoc` """ Module wrapping communication with Stripe. """ + + `@san_holder_coupon` "SAN_HOLDER_1000"Then reference as
@san_holder_couponin the function.
Changes
Ticket
Checklist:
Summary by CodeRabbit
New Features
Behavior
Tests