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): Reset customers last attempt on dunning campaign deletion #2840

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class Customer < ApplicationRecord
sequenced scope: ->(customer) { customer.organization.customers.with_discarded },
lock_key: ->(customer) { customer.organization_id }

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? }
validates :currency, inclusion: {in: currency_list}, allow_nil: true
Expand Down
19 changes: 19 additions & 0 deletions app/models/dunning_campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ class DunningCampaign < ApplicationRecord
def self.ransackable_attributes(_auth_object = nil)
%w[name code]
end

def reset_customers_last_attempt
ancorcruz marked this conversation as resolved.
Show resolved Hide resolved
ancorcruz marked this conversation as resolved.
Show resolved Hide resolved
# NOTE: Reset last attempt on customers with the campaign applied explicitly
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
.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

# == Schema Information
Expand Down
5 changes: 1 addition & 4 deletions app/services/dunning_campaigns/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ def call
.update_all(applied_to_organization: false) # rubocop:disable Rails/SkipsModelValidations

# NOTE: Stop and reset existing campaigns.
organization.customers.where(
applied_dunning_campaign_id: nil,
exclude_from_dunning_campaign: false
).update_all( # rubocop:disable Rails/SkipsModelValidations
organization.customers.falling_back_to_default_dunning_campaign.update_all( # rubocop:disable Rails/SkipsModelValidations
ancorcruz marked this conversation as resolved.
Show resolved Hide resolved
last_dunning_campaign_attempt: 0,
last_dunning_campaign_attempt_at: nil
)
Expand Down
1 change: 1 addition & 0 deletions app/services/dunning_campaigns/destroy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def call
return result.forbidden_failure! unless dunning_campaign.organization.auto_dunning_enabled?

ActiveRecord::Base.transaction do
dunning_campaign.reset_customers_last_attempt
dunning_campaign.discard!
dunning_campaign.thresholds.discard_all
end
Expand Down
5 changes: 1 addition & 4 deletions app/services/dunning_campaigns/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ def call
.update_all(applied_to_organization: false) # rubocop:disable Rails/SkipsModelValidations

# NOTE: Stop and reset existing campaigns.
organization.customers.where(
applied_dunning_campaign_id: nil,
exclude_from_dunning_campaign: false
).update_all( # rubocop:disable Rails/SkipsModelValidations
organization.customers.falling_back_to_default_dunning_campaign.update_all( # rubocop:disable Rails/SkipsModelValidations
ancorcruz marked this conversation as resolved.
Show resolved Hide resolved
last_dunning_campaign_attempt: 0,
last_dunning_campaign_attempt_at: nil
)
Expand Down
61 changes: 61 additions & 0 deletions spec/models/dunning_campaign_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,65 @@
expect(described_class.with_discarded).to eq([deleted_dunning_campaign])
end
end

describe "#reset_customers_last_attempt" do
let(:last_dunning_campaign_attempt_at) { Time.current }
let(:organization) { dunning_campaign.organization }

it "resets last attempt on customers with the campaign applied explicitly" do
customer = create(
:customer,
organization:,
applied_dunning_campaign: dunning_campaign,
last_dunning_campaign_attempt: 1,
last_dunning_campaign_attempt_at:
)

expect { dunning_campaign.reset_customers_last_attempt }
.to change { customer.reload.last_dunning_campaign_attempt }.from(1).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:,
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) }

it "resets last attempt on customers falling back to the organization campaign" do
customer = create(
:customer,
organization:,
last_dunning_campaign_attempt: 2,
last_dunning_campaign_attempt_at:
)

expect { dunning_campaign.reset_customers_last_attempt }
.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
6 changes: 6 additions & 0 deletions spec/services/dunning_campaigns/destroy_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
create(:organization, premium_integrations: ["auto_dunning"])
end

it "resets last attempt on customers" do
customer = create(:customer, organization:, applied_dunning_campaign: dunning_campaign, last_dunning_campaign_attempt: 1)

expect { destroy_service.call }.to change { customer.reload.last_dunning_campaign_attempt }.from(1).to(0)
end

it "soft deletes the dunning campaign" do
freeze_time do
expect { destroy_service.call }.to change(DunningCampaign, :count).by(-1)
Expand Down