Skip to content

Commit

Permalink
feat(dunning): Allow soft deletion on dunning campaign
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Nov 20, 2024
1 parent 1168eda commit be55f77
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 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 @@ -17,6 +19,7 @@ class DunningCampaign < ApplicationRecord
validates :max_attempts, numericality: {greater_than: 0}
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 Down
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

0 comments on commit be55f77

Please sign in to comment.