Skip to content

Commit

Permalink
feat(dunning): Fix unique validation on dunning campaign threshold cu…
Browse files Browse the repository at this point in the history
…rrency
  • Loading branch information
rsempe committed Nov 25, 2024
1 parent 24063b4 commit 3e04f85
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
4 changes: 3 additions & 1 deletion app/models/dunning_campaign_threshold.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class DunningCampaignThreshold < ApplicationRecord

validates :amount_cents, numericality: {greater_than_or_equal_to: 0}
validates :currency, inclusion: {in: currency_list}
validates :currency, uniqueness: {scope: :dunning_campaign_id}, unless: :deleted_at
validates :currency,
uniqueness: {conditions: -> { where(deleted_at: nil) }, scope: :dunning_campaign_id},
unless: :deleted_at

default_scope -> { kept }
end
Expand Down
14 changes: 7 additions & 7 deletions spec/models/dunning_campaign_threshold_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
let(:dunning_campaign) { create(:dunning_campaign) }

it "validates uniqueness of currency scoped to dunning_campaign_id excluding deleted records" do
create(:dunning_campaign_threshold, currency:, dunning_campaign:)
new_record = build(:dunning_campaign_threshold, currency:, dunning_campaign:)

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

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

record1 = create(:dunning_campaign_threshold, currency:, dunning_campaign:)
expect(record1).to be_valid

record2 = build(:dunning_campaign_threshold, currency:, dunning_campaign:)
expect(record2).not_to be_valid
expect(record2.errors[:currency]).to include("value_already_exist")
end
end

Expand Down

0 comments on commit 3e04f85

Please sign in to comment.