Skip to content

Commit

Permalink
Flag customer as dunning campaign completed...
Browse files Browse the repository at this point in the history
... on dunning campaign attempt execution when customer last attempt
count reaches dunning campaign max attempts.

Also, use customer dunning_campaign_completed? as guard clause instead
of comparing the number of attempts executed against the campaign max
attempts
  • Loading branch information
ancorcruz authored and rsempe committed Nov 28, 2024
1 parent 5b61d03 commit c4420a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
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
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

0 comments on commit c4420a6

Please sign in to comment.