Skip to content

Commit

Permalink
feat(dunning): Restet last attempt only when dunning campaign is not …
Browse files Browse the repository at this point in the history
…completed
  • Loading branch information
rsempe committed Nov 22, 2024
1 parent cd6d978 commit d11f8f3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Customer < ApplicationRecord
scope :falling_back_to_default_dunning_campaign, -> {
where(applied_dunning_campaign_id: nil, exclude_from_dunning_campaign: false)
}
scope :with_dunning_campaign_not_completed, -> { where(dunning_campaign_completed: false) }

validates :country, :shipping_country, country_code: true, allow_nil: true
validates :document_locale, language_code: true, unless: -> { document_locale.nil? }
Expand Down
13 changes: 8 additions & 5 deletions app/models/dunning_campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@ def self.ransackable_attributes(_auth_object = nil)

def reset_customers_last_attempt
# NOTE: Reset last attempt on customers with the campaign applied explicitly
customers.update_all( # rubocop:disable Rails/SkipsModelValidations
customers.with_dunning_campaign_not_completed.update_all( # rubocop:disable Rails/SkipsModelValidations
last_dunning_campaign_attempt: 0,
last_dunning_campaign_attempt_at: nil
)

# NOTE: Reset last attempt on customers falling back to the organization campaign
if applied_to_organization?
organization.customers.falling_back_to_default_dunning_campaign.update_all( # rubocop:disable Rails/SkipsModelValidations
last_dunning_campaign_attempt: 0,
last_dunning_campaign_attempt_at: nil
)
organization.customers
.falling_back_to_default_dunning_campaign
.with_dunning_campaign_not_completed
.update_all( # rubocop:disable Rails/SkipsModelValidations
last_dunning_campaign_attempt: 0,
last_dunning_campaign_attempt_at: nil
)
end
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/models/dunning_campaign_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@
.and change { customer.last_dunning_campaign_attempt_at }.from(last_dunning_campaign_attempt_at).to(nil)
end

it "does not reset last attempt on customers with dunning campaign already completed" do
customer = create(
:customer,
organization:,
applied_dunning_campaign: dunning_campaign,
last_dunning_campaign_attempt: 1,
dunning_campaign_completed: true
)

expect { dunning_campaign.reset_customers_last_attempt }
.not_to change { customer.reload.last_dunning_campaign_attempt }.from(1)
end

context "when applied to organization" do
subject(:dunning_campaign) { create(:dunning_campaign, applied_to_organization: true) }

Expand All @@ -79,6 +92,18 @@
.to change { customer.reload.last_dunning_campaign_attempt }.from(2).to(0)
.and change { customer.last_dunning_campaign_attempt_at }.from(last_dunning_campaign_attempt_at).to(nil)
end

it "does not reset last attempt on customers with dunning campaign already completed" do
customer = create(
:customer,
organization:,
last_dunning_campaign_attempt: 2,
dunning_campaign_completed: true
)

expect { dunning_campaign.reset_customers_last_attempt }
.not_to change { customer.reload.last_dunning_campaign_attempt }.from(2)
end
end
end
end

0 comments on commit d11f8f3

Please sign in to comment.