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 17, 2023
1 parent b0d1513 commit d98ef28
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
3 changes: 2 additions & 1 deletion app/services/subscriptions/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ def upgrade_subscription
# NOTE: When upgrading, the new subscription becomes active immediatly
# The previous one must be terminated
Subscriptions::TerminateService.call(subscription: current_subscription)

new_subscription.mark_as_active!
SendWebhookJob.perform_later('subscription.started', new_subscription)
perform_later(job_class: SendWebhookJob, arguments: ['subscription.started', new_subscription])

if plan.pay_in_advance?
# NOTE: Since job is launched from inside a db transaction
Expand Down
27 changes: 10 additions & 17 deletions app/services/subscriptions/terminate_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ def call
# NOTE: Pending next subscription should be canceled as well
subscription.next_subscription&.mark_as_canceled!

SendWebhookJob.perform_later(
'subscription.terminated',
subscription,
)
# NOTE: Wait to ensure job is performed at the end of the database transaction.
# See https://github.com/getlago/lago-api/blob/main/app/services/subscriptions/create_service.rb#L46.
SendWebhookJob.set(wait: 2.seconds).perform_later('subscription.terminated', subscription)

result.subscription = subscription
result
Expand All @@ -60,23 +59,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 All @@ -89,7 +80,9 @@ def terminate_and_start_next(timestamp:)

def bill_subscription
if async
BillSubscriptionJob.perform_later([subscription], subscription.terminated_at)
# NOTE: Wait to ensure job is performed at the end of the database transaction.
# See https://github.com/getlago/lago-api/blob/main/app/services/subscriptions/create_service.rb#L46.
BillSubscriptionJob.set(wait: 2.seconds).perform_later([subscription], subscription.terminated_at)
else
BillSubscriptionJob.perform_now([subscription], subscription.terminated_at)
end
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 d98ef28

Please sign in to comment.