Skip to content

Commit

Permalink
feat(dunning): Stop and reset counters for customers
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Nov 8, 2024
1 parent fe3da1a commit 347506b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
5 changes: 1 addition & 4 deletions app/services/dunning_campaigns/bulk_process_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ def call
return result if max_attempts_reached?
return result unless days_between_attempts_satisfied?

DunningCampaigns::ProcessAttemptJob.perform_later(
customer: customer,
dunning_campaign_threshold: threshold
)
DunningCampaigns::ProcessAttemptJob.perform_later(customer:, dunning_campaign_threshold: threshold)

result
end
Expand Down
9 changes: 9 additions & 0 deletions app/services/dunning_campaigns/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ def call
.dunning_campaigns
.applied_to_organization
.update_all(applied_to_organization: false) # rubocop:disable Rails/SkipsModelValidations

# NOTE: Stop and reset existing campaigns.
organization.customers.where(
applied_dunning_campaign_id: nil,
exclude_from_dunning_campaign: false
).update_all( # rubocop:disable Rails/SkipsModelValidations
last_dunning_campaign_attempt: 0,
last_dunning_campaign_attempt_at: nil
)
end

dunning_campaign = organization.dunning_campaigns.create!(
Expand Down
9 changes: 9 additions & 0 deletions app/services/dunning_campaigns/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ def call
.applied_to_organization
.update_all(applied_to_organization: false) # rubocop:disable Rails/SkipsModelValidations

# NOTE: Stop and reset existing campaigns.
organization.customers.where(
applied_dunning_campaign_id: nil,
exclude_from_dunning_campaign: false
).update_all( # rubocop:disable Rails/SkipsModelValidations
last_dunning_campaign_attempt: 0,
last_dunning_campaign_attempt_at: nil
)

dunning_campaign.applied_to_organization = params[:applied_to_organization]
end

Expand Down
7 changes: 7 additions & 0 deletions spec/services/dunning_campaigns/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@
.to(false)
end
end

it "stops and resets counters on customers" do
customer = create(:customer, organization:, last_dunning_campaign_attempt: 1, last_dunning_campaign_attempt_at: Time.current)

expect { create_service.call }.to change { customer.reload.last_dunning_campaign_attempt }.from(1).to(0)
.and change { customer.last_dunning_campaign_attempt_at }.from(a_value).to(nil)
end
end

context "with validation error" do
Expand Down
7 changes: 7 additions & 0 deletions spec/services/dunning_campaigns/update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@
.to(false)
end
end

it "stops and resets counters on customers" do
customer = create(:customer, organization:, last_dunning_campaign_attempt: 1, last_dunning_campaign_attempt_at: Time.current)

expect { result }.to change { customer.reload.last_dunning_campaign_attempt }.from(1).to(0)
.and change { customer.last_dunning_campaign_attempt_at }.from(a_value).to(nil)
end
end

context "with no dunning campaign record" do
Expand Down

0 comments on commit 347506b

Please sign in to comment.