-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add subscription plan changes jobs
- Loading branch information
Showing
6 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
spec/jobs/toggle_user_active_subscription_plan_job_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |