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

fix(wallet): Validate currency #2999

Merged
merged 1 commit into from
Dec 23, 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
2 changes: 2 additions & 0 deletions app/models/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class Wallet < ApplicationRecord
include PaperTrailTraceable
include Currencies

belongs_to :customer, -> { with_discarded }

Expand All @@ -15,6 +16,7 @@ class Wallet < ApplicationRecord
monetize :ongoing_balance_cents, :ongoing_usage_balance_cents, with_model_currency: :balance_currency

validates :rate_amount, numericality: {greater_than: 0}
validates :currency, inclusion: {in: currency_list}

STATUSES = [
:active,
Expand Down
6 changes: 3 additions & 3 deletions app/services/wallets/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def call
wallet = Wallet.new(attributes)

ActiveRecord::Base.transaction do
Customers::UpdateCurrencyService
.call(customer: result.current_customer, currency: params[:currency])
.raise_if_error!
if params[:currency].present?
Customers::UpdateCurrencyService.call!(customer: result.current_customer, currency: params[:currency])
end

wallet.currency = wallet.customer.currency
wallet.save!
Expand Down
20 changes: 20 additions & 0 deletions spec/services/wallets/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@
service_result
expect(customer.reload.currency).to eq('EUR')
end

context 'when no currency is provided' do
let(:params) do
{
name: 'New Wallet',
customer:,
organization_id: organization.id,
currency: nil,
rate_amount: '1.00',
expiration_at:,
paid_credits:,
granted_credits:
}
end

it 'returns an error' do
expect(service_result).not_to be_success
expect(service_result.error.messages[:currency]).to eq(['value_is_invalid'])
end
end
end

context 'when wallet have transaction metadata' do
Expand Down
Loading