Skip to content

Add SAN discount for upgrades#4986

Open
tspenov wants to merge 2 commits intomasterfrom
fix-san-discount-upgrades
Open

Add SAN discount for upgrades#4986
tspenov wants to merge 2 commits intomasterfrom
fix-san-discount-upgrades

Conversation

@tspenov
Copy link
Collaborator

@tspenov tspenov commented Jan 30, 2026

Changes

Ticket

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have tried to find clearer solution before commenting hard-to-understand parts of code
  • I have added tests that prove my fix is effective or that my feature works

Summary by CodeRabbit

  • New Features

    • SAN token holders with 1,000+ tokens now automatically receive the SAN_HOLDER_1000 discount on subscription upgrade if they don't already have an active discount.
  • Behavior

    • Improved handling of existing discounts during upgrades to avoid incorrectly reapplying or removing coupons.
  • Tests

    • Added comprehensive tests covering SAN-based discount application and edge cases during subscription upgrades.

@coderabbitai
Copy link

coderabbitai bot commented Jan 30, 2026

📝 Walkthrough

Walkthrough

Modify subscription upgrade flow to first retrieve the Stripe subscription, remove any 100% discount when moving to basic, optionally add SAN_HOLDER_1000 coupon based on DB SAN balance and existing Stripe discount, then update the Stripe subscription. New private helper added and tests covering these scenarios included.

Changes

Cohort / File(s) Summary
Stripe subscription logic
lib/sanbase/stripe/stripe_api.ex
Refactor upgrade_downgrade/2 pipeline to fetch Stripe subscription early, change maybe_remove_coupon/3maybe_remove_coupon/4 to accept the stripe_subscription, add new private maybe_add_san_coupon(params, db_subscription, stripe_subscription) that applies SAN_HOLDER_1000 when user SAN ≥1000 and no existing Stripe discount, then update subscription with resulting params. Variable renamed to updated_stripe_subscription in pipeline.
Subscription upgrade tests
test/sanbase/billing/subscription_test.exs
Add tests under "update_subscription - SAN discount on upgrade" to verify: coupon applied for ≥1000 SAN and no existing discount; coupon not applied if Stripe subscription already has a discount; not applied if SAN <1000; and upgrade success paths. Uses Stripe mocks and test helpers.

Sequence Diagram

sequenceDiagram
    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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐇 I nibble code and count my SAN,
A thousand hops — a coupon's plan.
Remove the old, add one that's new,
Upgrade done with carrot-chew! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add SAN discount for upgrades' is clear, specific, and directly matches the core changes shown in the summary: new SAN-based discount logic (SAN_HOLDER_1000 coupon) applied during subscription upgrades.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-san-discount-upgrades

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@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: 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_coupon in the function.

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.

1 participant

Comments