diff --git a/app/models/customer.rb b/app/models/customer.rb index b6560bab2d77..16fd8da65f4c 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -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? } diff --git a/app/models/dunning_campaign.rb b/app/models/dunning_campaign.rb index abaff0760c61..b8f0ca6444d8 100644 --- a/app/models/dunning_campaign.rb +++ b/app/models/dunning_campaign.rb @@ -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 diff --git a/spec/models/dunning_campaign_spec.rb b/spec/models/dunning_campaign_spec.rb index 0c89e92877fe..e42c48e880d6 100644 --- a/spec/models/dunning_campaign_spec.rb +++ b/spec/models/dunning_campaign_spec.rb @@ -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) } @@ -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