add withdrawl and deposit to FiatBalance and Balance
This commit is contained in:
1
Gemfile
1
Gemfile
@@ -8,6 +8,7 @@ ruby "2.7.4"
|
|||||||
gem "pg", "~> 1.1"
|
gem "pg", "~> 1.1"
|
||||||
gem "puma", "~> 5.0"
|
gem "puma", "~> 5.0"
|
||||||
gem "rails", "~> 6.1.4"
|
gem "rails", "~> 6.1.4"
|
||||||
|
gem "rails-i18n", "~> 6.0"
|
||||||
gem "sass-rails", ">= 6"
|
gem "sass-rails", ">= 6"
|
||||||
gem "turbolinks", "~> 5"
|
gem "turbolinks", "~> 5"
|
||||||
gem "webpacker", "~> 5.0"
|
gem "webpacker", "~> 5.0"
|
||||||
|
|||||||
@@ -216,6 +216,9 @@ GEM
|
|||||||
ruby-graphviz (~> 1.2)
|
ruby-graphviz (~> 1.2)
|
||||||
rails-html-sanitizer (1.3.0)
|
rails-html-sanitizer (1.3.0)
|
||||||
loofah (~> 2.3)
|
loofah (~> 2.3)
|
||||||
|
rails-i18n (6.0.0)
|
||||||
|
i18n (>= 0.7, < 2)
|
||||||
|
railties (>= 6.0.0, < 7)
|
||||||
railties (6.1.4)
|
railties (6.1.4)
|
||||||
actionpack (= 6.1.4)
|
actionpack (= 6.1.4)
|
||||||
activesupport (= 6.1.4)
|
activesupport (= 6.1.4)
|
||||||
@@ -351,6 +354,7 @@ DEPENDENCIES
|
|||||||
pundit
|
pundit
|
||||||
rails (~> 6.1.4)
|
rails (~> 6.1.4)
|
||||||
rails-erd
|
rails-erd
|
||||||
|
rails-i18n (~> 6.0)
|
||||||
rspec-graphql_matchers (~> 1.3)
|
rspec-graphql_matchers (~> 1.3)
|
||||||
rspec-rails
|
rspec-rails
|
||||||
rubocop-rails
|
rubocop-rails
|
||||||
|
|||||||
@@ -25,5 +25,13 @@ class Balance < ApplicationRecord
|
|||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :currency
|
belongs_to :currency
|
||||||
|
|
||||||
validates :amount, presence: true
|
validates :amount, presence: true, numericality: { greater_than_or_equal_to: 0 }
|
||||||
|
|
||||||
|
def withdrawal!(value)
|
||||||
|
update!(amount: amount - value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def deposit!(value)
|
||||||
|
update!(amount: amount + value)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -24,7 +24,17 @@ class FiatBalance < ApplicationRecord
|
|||||||
|
|
||||||
monetize :amount_cents
|
monetize :amount_cents
|
||||||
|
|
||||||
|
validates :amount_cents, numericality: { greater_than_or_equal_to: 0 }
|
||||||
|
|
||||||
def amount_formatted
|
def amount_formatted
|
||||||
amount.format
|
amount.format
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def withdrawal!(value)
|
||||||
|
update!(amount_cents: amount_cents - value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def deposit!(value)
|
||||||
|
update!(amount_cents: amount_cents + value)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -32,6 +32,18 @@ pt-BR:
|
|||||||
|
|
||||||
fiat_balance:
|
fiat_balance:
|
||||||
amount_formatted: Quantia
|
amount_formatted: Quantia
|
||||||
|
amount_cents: Quantia
|
||||||
|
|
||||||
currency:
|
currency:
|
||||||
name: Nome
|
name: Nome
|
||||||
|
|
||||||
|
errors:
|
||||||
|
models:
|
||||||
|
balance:
|
||||||
|
attributes:
|
||||||
|
amount:
|
||||||
|
greater_than_or_equal_to: "saldo insuficiente"
|
||||||
|
fiat_balance:
|
||||||
|
attributes:
|
||||||
|
amount_cents:
|
||||||
|
greater_than_or_equal_to: "saldo insuficiente"
|
||||||
@@ -23,6 +23,6 @@ FactoryBot.define do
|
|||||||
factory :fiat_balance do
|
factory :fiat_balance do
|
||||||
association :user
|
association :user
|
||||||
amount_currency { "BRL" }
|
amount_currency { "BRL" }
|
||||||
amount_cents { Faker::Number.number(digits: 10) }
|
amount_cents { Faker::Number.number(digits: 4) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
# == Schema Information
|
|
||||||
#
|
|
||||||
# Table name: admin_users
|
|
||||||
#
|
|
||||||
# id :bigint not null, primary key
|
|
||||||
# email :string default(""), not null
|
|
||||||
# encrypted_password :string default(""), not null
|
|
||||||
# remember_created_at :datetime
|
|
||||||
# reset_password_sent_at :datetime
|
|
||||||
# reset_password_token :string
|
|
||||||
# created_at :datetime not null
|
|
||||||
# updated_at :datetime not null
|
|
||||||
#
|
|
||||||
# Indexes
|
|
||||||
#
|
|
||||||
# index_admin_users_on_email (email) UNIQUE
|
|
||||||
# index_admin_users_on_reset_password_token (reset_password_token) UNIQUE
|
|
||||||
#
|
|
||||||
require "rails_helper"
|
|
||||||
|
|
||||||
RSpec.describe(AdminUser, type: :model) do
|
|
||||||
pending "add some examples to (or delete) #{__FILE__}"
|
|
||||||
end
|
|
||||||
@@ -32,4 +32,32 @@ RSpec.describe(Balance, type: :model) do
|
|||||||
it { is_expected.to(belong_to(:user)) }
|
it { is_expected.to(belong_to(:user)) }
|
||||||
it { is_expected.to(belong_to(:currency)) }
|
it { is_expected.to(belong_to(:currency)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".withdrwal!" do
|
||||||
|
context "when value is greater than the balance" do
|
||||||
|
it "raise ActiveRecord::RecordInvalid" do
|
||||||
|
balance = build(:balance, amount: 70.342)
|
||||||
|
|
||||||
|
expect { balance.withdrawal!(80) }.to(
|
||||||
|
raise_error(ActiveRecord::RecordInvalid, "A validação falhou: Quantia saldo insuficiente")
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when value is equals to the balance" do
|
||||||
|
it "returns true" do
|
||||||
|
balance = build(:balance, amount: 70.342)
|
||||||
|
|
||||||
|
expect(balance.withdrawal!(70.342)).to(eq(true))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when value is smaller than the balance" do
|
||||||
|
it "returns true" do
|
||||||
|
balance = build(:balance, amount: 70.342)
|
||||||
|
|
||||||
|
expect(balance.withdrawal!(20)).to(eq(true))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -25,4 +25,32 @@ RSpec.describe(FiatBalance, type: :model) do
|
|||||||
describe "associations" do
|
describe "associations" do
|
||||||
it { is_expected.to(belong_to(:user)) }
|
it { is_expected.to(belong_to(:user)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".withdrwal!" do
|
||||||
|
context "when value is greater than the balance" do
|
||||||
|
it "raise ActiveRecord::RecordInvalid" do
|
||||||
|
balance = build(:fiat_balance, amount_cents: 100_00)
|
||||||
|
|
||||||
|
expect { balance.withdrawal!(100_50) }.to(
|
||||||
|
raise_error(ActiveRecord::RecordInvalid, "A validação falhou: Quantia saldo insuficiente")
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when value is equals to the balance" do
|
||||||
|
it "returns true" do
|
||||||
|
balance = build(:fiat_balance, amount_cents: 100_00)
|
||||||
|
|
||||||
|
expect(balance.withdrawal!(100_00)).to(eq(true))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when value is smaller than the balance" do
|
||||||
|
it "returns true" do
|
||||||
|
balance = build(:fiat_balance, amount_cents: 100_00)
|
||||||
|
|
||||||
|
expect(balance.withdrawal!(90_00)).to(eq(true))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,5 +2,41 @@
|
|||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
RSpec.describe(BalancePolicy, type: :policy) do
|
RSpec.describe(BalancePolicy, type: :policy) do
|
||||||
pending "add some examples to (or delete) #{__FILE__}"
|
context "when user has balances" do
|
||||||
|
it "return only balances from a user" do
|
||||||
|
create(:balance)
|
||||||
|
create(:balance)
|
||||||
|
|
||||||
|
user = build(:user)
|
||||||
|
balance = create(:balance, user: user)
|
||||||
|
|
||||||
|
balances = BalancePolicy::Scope.new(user, Balance).resolve
|
||||||
|
|
||||||
|
expect(balances).to(eq([balance]))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when user has not balances" do
|
||||||
|
it "return empty array" do
|
||||||
|
create(:balance)
|
||||||
|
create(:balance)
|
||||||
|
|
||||||
|
user = build(:user)
|
||||||
|
|
||||||
|
balances = BalancePolicy::Scope.new(user, Balance).resolve
|
||||||
|
|
||||||
|
expect(balances).to(eq([]))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when user is nil" do
|
||||||
|
it "return empty array" do
|
||||||
|
create(:balance)
|
||||||
|
create(:balance)
|
||||||
|
|
||||||
|
balances = BalancePolicy::Scope.new(nil, Balance).resolve
|
||||||
|
|
||||||
|
expect(balances).to(eq([]))
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,5 +2,43 @@
|
|||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
RSpec.describe(FiatBalancePolicy, type: :policy) do
|
RSpec.describe(FiatBalancePolicy, type: :policy) do
|
||||||
pending "add some examples to (or delete) #{__FILE__}"
|
describe "::Scope" do
|
||||||
|
context "when user has balances" do
|
||||||
|
it "return only balances from a user" do
|
||||||
|
create(:fiat_balance)
|
||||||
|
create(:fiat_balance)
|
||||||
|
|
||||||
|
user = build(:user)
|
||||||
|
balance = create(:fiat_balance, user: user)
|
||||||
|
|
||||||
|
balances = FiatBalancePolicy::Scope.new(user, FiatBalance).resolve
|
||||||
|
|
||||||
|
expect(balances).to(eq([balance]))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when user has not balances" do
|
||||||
|
it "return empty array" do
|
||||||
|
create(:fiat_balance)
|
||||||
|
create(:fiat_balance)
|
||||||
|
|
||||||
|
user = build(:user)
|
||||||
|
|
||||||
|
balances = FiatBalancePolicy::Scope.new(user, FiatBalance).resolve
|
||||||
|
|
||||||
|
expect(balances).to(eq([]))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when user is nil" do
|
||||||
|
it "return empty array" do
|
||||||
|
create(:fiat_balance)
|
||||||
|
create(:fiat_balance)
|
||||||
|
|
||||||
|
balances = FiatBalancePolicy::Scope.new(nil, FiatBalance).resolve
|
||||||
|
|
||||||
|
expect(balances).to(eq([]))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user