Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dunning): Stop and reset counters for customers #2796

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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