Skip to content

Commit

Permalink
fix(wallet): Add validation rule on rate_amount (#2547)
Browse files Browse the repository at this point in the history
## Context

This PR adds a validation rule to `wallet#rate_amount` to make sure it
will always be a positive value.

It will fixes the following dead job related to wallets with rate_amount
= 0

```
ActiveRecord::RecordInvalid

Validation failed: Ongoing balance is not a number, Ongoing usage balance is not a number (ActiveRecord::RecordInvalid)
```
  • Loading branch information
vincent-pochet authored Sep 9, 2024
1 parent f6e4810 commit 963f4b2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Wallet < ApplicationRecord
monetize :balance_cents, :ongoing_balance_cents, :ongoing_usage_balance_cents
monetize :consumed_amount_cents

validates :rate_amount, numericality: {greater_than: 0}

STATUSES = [
:active,
:terminated
Expand Down
4 changes: 4 additions & 0 deletions spec/models/wallet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
RSpec.describe Wallet, type: :model do
subject(:wallet) { build(:wallet) }

describe 'validations' do
it { is_expected.to validate_numericality_of(:rate_amount).is_greater_than(0) }
end

describe 'currency=' do
it 'assigns the currency to all amounts' do
wallet.currency = 'CAD'
Expand Down

0 comments on commit 963f4b2

Please sign in to comment.