Skip to content

Commit

Permalink
feat: add subscription plan changes jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
castrolem committed Oct 18, 2019
1 parent 1448634 commit e09b0d6
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/plan_changes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def new
def create
head :not_authorized unless current_user

@plan = UserPlanChange.new(user_id: plan_change_params[:user_id], old_plan_id: plan_change_params[:old_plan_id], new_plan_id: plan_change_params[:new_plan_id])
@plan = UserPlanChange.new(user_id: plan_change_params[:user_id], old_plan_id: plan_change_params[:old_plan_id], new_plan_id: plan_change_params[:new_plan_id], status: 'pending')

respond_to do |format|
if @plan.save
Expand Down
12 changes: 12 additions & 0 deletions app/jobs/change_subscription_plans_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class ChangeSubscriptionPlansJob < ApplicationJob
queue_as :default

def perform
pending_user_plan_changes = UserPlanChange.where(status: 'pending')
pending_user_plan_changes.each do |pending_user_plan_change|
ToggleUserActiveSubscriptionPlanJob.perform_later(pending_user_plan_change)
end
end
end
26 changes: 26 additions & 0 deletions app/jobs/toggle_user_active_subscription_plan_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class ToggleUserActiveSubscriptionPlanJob < ApplicationJob
queue_as :default

def perform(user_plan_change)
# find the user
user = User.find(user_plan_change.user_id)

# disable current user subscription.
old_subscription = user.active_subscription
old_subscription.update(active: false)

# creates a new subscription with the new plan details.
new_subscription = Subscription.new(
user_id: user_plan_change.user_id,
plan_id: user_plan_change.new_plan_id,
last_charge_at: old_subscription.last_charge_at,
active: true
)

if new_subscription.save
UserPlanChange.find(user_plan_change.id).update(status: 'finished')
end
end
end
3 changes: 2 additions & 1 deletion spec/features/plan_change_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

it 'I am able to downgrade/upgrade my plan', js: true do
allow_any_instance_of(SessionProvider).to receive(:current_user).and_return(user)

expect(UserPlanChange.where(user_id: user.id).length).to be(0)
visit "/users/#{user.id}"

within '#contact-data' do
Expand All @@ -32,5 +32,6 @@
end

expect(page).to have_content('We have changed your subscription successfully.')
expect(UserPlanChange.where(user_id: user.id).length).to be(1)
end
end
33 changes: 33 additions & 0 deletions spec/jobs/change_subscription_plans_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ChangeSubscriptionPlansJob, type: :job do
let(:user) { FactoryBot.create(:user) }
let!(:old_plan) { FactoryBot.create(:plan) }
let!(:new_plan) { FactoryBot.create(:plan) }
let!(:active_subscription) { FactoryBot.create(:subscription, user: user, plan: old_plan) }
let!(:plan_change) { FactoryBot.create(:user_plan_change, user: user, old_plan_id: old_plan.id, new_plan_id: new_plan.id, status: 'pending') }

subject(:job) { described_class.perform_later }

it 'queues the job' do
expect { job }
.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(1)
end

it 'is in default queue' do
expect(ChangeSubscriptionPlansJob.new.queue_name).to eq('default')
end

it 'executes perform' do
expect do
described_class.perform_now
end.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(1)
end

after do
clear_enqueued_jobs
clear_performed_jobs
end
end
39 changes: 39 additions & 0 deletions spec/jobs/toggle_user_active_subscription_plan_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ToggleUserActiveSubscriptionPlanJob do
let(:user) { FactoryBot.create(:user) }
let!(:old_plan) { FactoryBot.create(:plan) }
let!(:new_plan) { FactoryBot.create(:plan) }
let!(:active_subscription) { FactoryBot.create(:subscription, user: user, plan: old_plan) }
let!(:plan_change) { FactoryBot.create(:user_plan_change, user: user, old_plan_id: old_plan.id, new_plan_id: new_plan.id, status: 'pending') }

subject(:job) { described_class.perform_later(plan_change) }

it 'queues the job' do
expect { job }
.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(1)
end

it 'is in default queue' do
expect(ToggleUserActiveSubscriptionPlanJob.new.queue_name).to eq('default')
end

it 'executes perform' do
expect(Subscription.count).to eq(1)
expect(user.active_subscription.plan.id).to eq(old_plan.id)

perform_enqueued_jobs { job }

expect(Subscription.count).to eq(2)
user.active_subscription.reload
expect(user.active_subscription.plan.id).to eq(new_plan.id)
expect(plan_change.reload.status).to eq('finished')
end

after do
clear_enqueued_jobs
clear_performed_jobs
end
end

0 comments on commit e09b0d6

Please sign in to comment.