Skip to content

Commit

Permalink
feat: Send webhook on downgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Oct 16, 2023
1 parent bf385e7 commit 8826575
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
16 changes: 4 additions & 12 deletions app/services/subscriptions/terminate_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,15 @@ def terminate_and_start_next(timestamp:)
# NOTE: Create an invoice for the terminated subscription
# if it has not been billed yet
# or only for the charges if subscription was billed in advance
BillSubscriptionJob.perform_later(
[subscription],
timestamp,
)
BillSubscriptionJob.perform_later([subscription], timestamp)

SendWebhookJob.perform_later(
'subscription.terminated',
subscription,
)
SendWebhookJob.perform_later('subscription.terminated', subscription)
SendWebhookJob.perform_later('subscription.started', next_subscription)

result.subscription = next_subscription
return result unless next_subscription.plan.pay_in_advance?

BillSubscriptionJob.perform_later(
[next_subscription],
timestamp,
)
BillSubscriptionJob.perform_later([next_subscription], timestamp)

result
rescue ActiveRecord::RecordInvalid => e
Expand Down
4 changes: 2 additions & 2 deletions spec/services/subscriptions/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@

it 'enqueues a job to bill the existing subscription' do
create_service.call
expect(BillSubscriptionJob).to have_been_enqueued.at_least(2).times
expect(BillSubscriptionJob).to have_been_enqueued.at_least(1).times
end
end

Expand Down Expand Up @@ -459,7 +459,7 @@

it 'enqueues a job to bill the existing subscription' do
create_service.call
expect(BillSubscriptionJob).to have_been_enqueued.at_least(1).times
expect(BillSubscriptionJob).to have_been_enqueued.at_least(2).times
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/services/subscriptions/terminate_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@
end

it 'enqueues a SendWebhookJob' do
expect do
terminate_service.terminate_and_start_next(timestamp:)
end.to have_enqueued_job(SendWebhookJob)
terminate_service.terminate_and_start_next(timestamp:)
expect(SendWebhookJob).to have_been_enqueued.with('subscription.terminated', subscription)
expect(SendWebhookJob).to have_been_enqueued.with('subscription.started', next_subscription)
end

context 'when terminated subscription is payed in arrear' do
Expand Down

0 comments on commit 8826575

Please sign in to comment.