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): unflag customers as dunning campaign completed... #2868

Merged
merged 2 commits into from
Nov 26, 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
1 change: 1 addition & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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_completed, -> { where(dunning_campaign_completed: true) }
scope :with_dunning_campaign_not_completed, -> { where(dunning_campaign_completed: false) }

validates :country, :shipping_country, country_code: true, allow_nil: true
Expand Down
20 changes: 11 additions & 9 deletions app/services/dunning_campaigns/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,17 @@ def reset_customers_if_no_threshold_match
end

def customers_to_reset
@customers_to_reset ||= customers_applied_campaign.or(customers_fallback_campaign)
@customers_to_reset ||= customers_applied_campaign
.or(customers_fallback_campaign)
.with_dunning_campaign_not_completed
end

def customers_applied_campaign
organization
.customers
.with_dunning_campaign_not_completed
.where(applied_dunning_campaign: dunning_campaign)
organization.customers.where(applied_dunning_campaign: dunning_campaign)
end

def customers_fallback_campaign
organization
.customers
.falling_back_to_default_dunning_campaign
.with_dunning_campaign_not_completed
organization.customers.falling_back_to_default_dunning_campaign
end

def handle_applied_to_organization_update
Expand All @@ -112,6 +108,12 @@ def handle_applied_to_organization_update
.update_all(applied_to_organization: false) # rubocop:disable Rails/SkipsModelValidations

organization.reset_customers_last_dunning_campaign_attempt

customers_fallback_campaign.update_all( # rubocop:disable Rails/SkipsModelValidations
dunning_campaign_completed: false,
last_dunning_campaign_attempt_at: nil,
last_dunning_campaign_attempt: 0
)
end

def handle_max_attempts_change
Expand Down
15 changes: 15 additions & 0 deletions spec/services/dunning_campaigns/update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,21 @@
expect { result }.to change { customer.reload.last_dunning_campaign_attempt }.from(1).to(0)
.and change { customer.last_dunning_campaign_attempt_at }.from(a_value).to(nil)
end

it "flags customers as not dunning campaign completed" do
customer = create(
:customer,
organization:,
dunning_campaign_completed: true,
last_dunning_campaign_attempt: 3,
last_dunning_campaign_attempt_at: Time.zone.now
)

expect { result && 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

context "with no dunning campaign record" do
Expand Down