diff --git a/app/services/dunning_campaigns/bulk_process_service.rb b/app/services/dunning_campaigns/bulk_process_service.rb index b00896e6420..447dab754f2 100644 --- a/app/services/dunning_campaigns/bulk_process_service.rb +++ b/app/services/dunning_campaigns/bulk_process_service.rb @@ -16,6 +16,7 @@ def call def eligible_customers Customer + .with_dunning_campaign_not_completed .joins(:organization) .where(exclude_from_dunning_campaign: false) .where("organizations.premium_integrations @> ARRAY[?]::varchar[]", ['auto_dunning']) @@ -30,6 +31,7 @@ def initialize(customer) end def call + return result if customer.dunning_campaign_completed? return result unless threshold return result if max_attempts_reached? return result unless days_between_attempts_satisfied? diff --git a/spec/services/dunning_campaigns/bulk_process_service_spec.rb b/spec/services/dunning_campaigns/bulk_process_service_spec.rb index 0733dc36fa1..93c9901dbb5 100644 --- a/spec/services/dunning_campaigns/bulk_process_service_spec.rb +++ b/spec/services/dunning_campaigns/bulk_process_service_spec.rb @@ -65,6 +65,17 @@ .with(customer:, dunning_campaign_threshold:) end + context "when the customer has completed the dunning campaign" do + let(:customer) do + create :customer, organization:, currency:, dunning_campaign_completed: true + end + + it "does not queue a job for the customer" do + result + expect(DunningCampaigns::ProcessAttemptJob).not_to have_been_enqueued + end + end + context "when organization does not have auto_dunning feature enabled" do let(:organization) { create(:organization, premium_integrations: []) }