Skip to content

feat(payment-gated-subs): Delegate downgrade logic to its own service#5502

Open
osmarluz wants to merge 3 commits into
mainfrom
feat/payment-gated-subs-3/refactor-downgrade-flow
Open

feat(payment-gated-subs): Delegate downgrade logic to its own service#5502
osmarluz wants to merge 3 commits into
mainfrom
feat/payment-gated-subs-3/refactor-downgrade-flow

Conversation

@osmarluz
Copy link
Copy Markdown
Contributor

@osmarluz osmarluz commented May 13, 2026

Context

Subscriptions::PlanUpgradeService already owns the upgrade flow, but downgrades stayed inline in Subscriptions::CreateService. This PR brings the two paths into symmetry ahead of the payment-gating work for plan changes (Milestone 3). Pure refactor — no behavior change.

Description

  • Subscriptions::PlanDowngradeService (new) owns the downgrade flow, mirroring Subscriptions::PlanUpgradeService. Returns result.subscription = current_subscription — the load-bearing difference from upgrades, which return the new subscription.

  • Subscriptions::CreateService#downgrade_subscription collapses to a one-line delegation to the new service.

@osmarluz osmarluz changed the title feat(payment-gated-subs): delegate downgrade logic to its own service feat(payment-gated-subs): Delegate downgrade logic to its own service May 13, 2026
@osmarluz osmarluz requested a review from ancorcruz May 13, 2026 10:57
Copy link
Copy Markdown
Contributor

@ancorcruz ancorcruz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a case covering the rescue ActiveRecord::RecordInvalid branch? in spec/services/subscriptions/plan_downgrade_service_spec.rb. Something like:

context "when creating the next subscription fails validation" do
  before do
    allow_any_instance_of(Subscription)
      .to receive(:save!)
      .and_raise(ActiveRecord::RecordInvalid.new(Subscription.new))
  end

  it "returns a record validation failure" do
    expect(result).not_to be_success
    expect(result.error).to be_a(BaseService::ValidationFailure)
  end
end

Right now the rescue branch is uncovered for a freshly-extracted service — would rather pin it down.

Comment thread spec/services/subscriptions/create_service_spec.rb
Comment thread app/services/subscriptions/plan_downgrade_service.rb Outdated
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.

2 participants