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): Allow soft deletion on dunning campaign #2833

Merged
merged 2 commits into from
Nov 20, 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
9 changes: 7 additions & 2 deletions app/models/dunning_campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class DunningCampaign < ApplicationRecord
include PaperTrailTraceable
include Discard::Model
self.discard_column = :deleted_at

ORDERS = %w[name code].freeze

Expand All @@ -15,8 +17,9 @@ class DunningCampaign < ApplicationRecord
validates :name, presence: true
validates :days_between_attempts, numericality: {greater_than: 0}
validates :max_attempts, numericality: {greater_than: 0}
validates :code, uniqueness: {scope: :organization_id}
validates :code, uniqueness: {scope: :organization_id}, unless: :deleted_at

default_scope -> { kept }
scope :applied_to_organization, -> { where(applied_to_organization: true) }
scope :with_currency_threshold, ->(currencies) {
joins(:thresholds)
Expand All @@ -37,6 +40,7 @@ def self.ransackable_attributes(_auth_object = nil)
# applied_to_organization :boolean default(FALSE), not null
# code :string not null
# days_between_attempts :integer default(1), not null
# deleted_at :datetime
# description :text
# max_attempts :integer default(1), not null
# name :string not null
Expand All @@ -46,8 +50,9 @@ def self.ransackable_attributes(_auth_object = nil)
#
# Indexes
#
# index_dunning_campaigns_on_deleted_at (deleted_at)
# index_dunning_campaigns_on_organization_id (organization_id)
# index_dunning_campaigns_on_organization_id_and_code (organization_id,code) UNIQUE
# index_dunning_campaigns_on_organization_id_and_code (organization_id,code) UNIQUE WHERE (deleted_at IS NULL)
# index_unique_applied_to_organization_per_organization (organization_id) UNIQUE WHERE (applied_to_organization = true)
#
# Foreign Keys
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20241120085057_add_deleted_at_to_dunning_campaigns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class AddDeletedAtToDunningCampaigns < ActiveRecord::Migration[7.1]
def change
add_column :dunning_campaigns, :deleted_at, :timestamp

safety_assured do
add_index :dunning_campaigns, :deleted_at
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class UpdateUniqueIndexOnDunningCampaigns < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def change
remove_index :dunning_campaigns, %i[organization_id code], unique: true, algorithm: :concurrently

add_index :dunning_campaigns,
[:organization_id, :code],
unique: true,
where: "deleted_at IS NULL",
algorithm: :concurrently
end
end
6 changes: 4 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions spec/models/dunning_campaign_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,32 @@
it { is_expected.to validate_numericality_of(:max_attempts).is_greater_than(0) }

it { is_expected.to validate_uniqueness_of(:code).scoped_to(:organization_id) }

describe "code validation" do
let(:code) { "123456" }
let(:organization) { create(:organization) }

it "validates uniqueness of code scoped to organization_id excluding deleted records" do
create(:dunning_campaign, code:, organization:)
new_record = build(:dunning_campaign, code:, organization:)

expect(new_record).not_to be_valid
expect(new_record.errors[:code]).to include("value_already_exist")

# Records with deleted_at set should not conflict
deleted_record = create(:dunning_campaign, :deleted, code:, organization:)
expect(deleted_record).to be_valid
end
end

describe "default scope" do
let(:deleted_dunning_campaign) { create(:dunning_campaign, :deleted) }

before { deleted_dunning_campaign }

it "only returns non-deleted dunning_campaign objects" do
expect(described_class.all).to eq([])
expect(described_class.with_discarded).to eq([deleted_dunning_campaign])
end
end
end