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): flag customer as dunning campaign completed on last attempt execution #2889

Merged
merged 3 commits into from
Nov 28, 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
8 changes: 8 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ def overdue_balance_cents
invoices.payment_overdue.where(currency:).sum(:total_amount_cents)
end

def reset_dunning_campaign!
update!(
dunning_campaign_completed: false,
last_dunning_campaign_attempt: 0,
last_dunning_campaign_attempt_at: nil
)
end

private

def ensure_slug
Expand Down
10 changes: 4 additions & 6 deletions app/services/customers/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ def call
customer.exclude_from_dunning_campaign = args[:exclude_from_dunning_campaign]
customer.applied_dunning_campaign = nil if args[:exclude_from_dunning_campaign]
end

if customer.applied_dunning_campaign_id_changed? || customer.exclude_from_dunning_campaign_changed?
customer.last_dunning_campaign_attempt = 0
customer.last_dunning_campaign_attempt_at = nil
customer.dunning_campaign_completed = false
end
end

ActiveRecord::Base.transaction do
Expand All @@ -119,6 +113,10 @@ def call
customer.payment_provider_code = nil
end

if customer.applied_dunning_campaign_id_changed? || customer.exclude_from_dunning_campaign_changed?
customer.reset_dunning_campaign!
end

customer.save!
customer.reload

Expand Down
5 changes: 3 additions & 2 deletions app/services/dunning_campaigns/process_attempt_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def call
return result unless applicable_dunning_campaign?
return result unless dunning_campaign_threshold_reached?
return result unless days_between_attempts_passed?
return result if dunning_campaign_completed?
return result if customer.dunning_campaign_completed?

ActiveRecord::Base.transaction do
payment_request_result = PaymentRequests::CreateService.call(
Expand All @@ -30,6 +30,7 @@ def call

customer.increment(:last_dunning_campaign_attempt)
customer.last_dunning_campaign_attempt_at = Time.zone.now
customer.dunning_campaign_completed = last_dunning_campaign_attempt?
customer.save!

result.customer = customer
Expand Down Expand Up @@ -64,7 +65,7 @@ def days_between_attempts_passed?
(customer.last_dunning_campaign_attempt_at + dunning_campaign.days_between_attempts.days).past?
end

def dunning_campaign_completed?
def last_dunning_campaign_attempt?
customer.last_dunning_campaign_attempt >= dunning_campaign.max_attempts
end

Expand Down
6 changes: 1 addition & 5 deletions app/services/invoices/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ def call
invoice.payment_overdue = false

if invoice.payment_requests.where.not(dunning_campaign_id: nil).exists?
invoice.customer.update!(
dunning_campaign_completed: false,
last_dunning_campaign_attempt: 0,
last_dunning_campaign_attempt_at: nil
)
invoice.customer.reset_dunning_campaign!
end
end

Expand Down
6 changes: 1 addition & 5 deletions app/services/payment_requests/payments/adyen_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,7 @@ def reset_customer_dunning_campaign_status(payment_status)
return unless payment_status_succeeded?(payment_status)
return unless payable.try(:dunning_campaign)

customer.update!(
dunning_campaign_completed: false,
last_dunning_campaign_attempt: 0,
last_dunning_campaign_attempt_at: nil
)
customer.reset_dunning_campaign!
end
end
end
Expand Down
6 changes: 1 addition & 5 deletions app/services/payment_requests/payments/gocardless_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,7 @@ def reset_customer_dunning_campaign_status(payment_status)
return unless payment_status_succeeded?(payment_status)
return unless payable.try(:dunning_campaign)

customer.update!(
dunning_campaign_completed: false,
last_dunning_campaign_attempt: 0,
last_dunning_campaign_attempt_at: nil
)
customer.reset_dunning_campaign!
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/models/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,22 @@
end
end
end

describe "#reset_dunning_campaign!" do
let(:customer) do
create(
:customer,
dunning_campaign_completed: true,
last_dunning_campaign_attempt: 5,
last_dunning_campaign_attempt_at: 1.day.ago
)
end

it "changes dunning campaign status counters" do
expect { customer.reset_dunning_campaign! && customer.reload }
.to change(customer, :dunning_campaign_completed).to(false)
.and change(customer, :last_dunning_campaign_attempt).to(0)
.and change(customer, :last_dunning_campaign_attempt_at).to(nil)
end
end
end
29 changes: 24 additions & 5 deletions spec/services/dunning_campaigns/process_attempt_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,28 @@

it "updates customer last dunning attempt data" do
freeze_time do
expect { result }
.to change(customer.reload, :last_dunning_campaign_attempt).by(1)
.and change(customer.reload, :last_dunning_campaign_attempt_at).to(Time.zone.now)
expect { result && customer.reload }
.to change(customer, :last_dunning_campaign_attempt).by(1)
.and change(customer, :last_dunning_campaign_attempt_at).to(Time.zone.now)
.and not_change(customer, :dunning_campaign_completed).from(false)
end
end

context "when dunning campaign max attemp is reached" do
let(:customer) do
create(
:customer,
organization:,
currency:,
last_dunning_campaign_attempt: dunning_campaign.max_attempts - 1,
last_dunning_campaign_attempt_at: dunning_campaign.days_between_attempts.days.ago,
dunning_campaign_completed: false
)
end

it "updates customer's dunning campaign completed flag" do
expect { result && customer.reload }
.to change(customer, :dunning_campaign_completed).to(true)
end
end

Expand Down Expand Up @@ -117,13 +136,13 @@
end
end

context "when the customer reaches dunning campaign max attempts" do
context "when the customer has completed the dunning campaign" do
let(:customer) do
create(
:customer,
organization:,
currency:,
last_dunning_campaign_attempt: dunning_campaign.max_attempts
dunning_campaign_completed: true
)
end

Expand Down