Created
February 4, 2018 16:42
-
-
Save ro-fdm/7b98a8944492ae0df91bbdd447c6d546 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Transfer | |
def initialize(payment) | |
@payment = payment | |
end | |
def run | |
ActiveRecord::Base.transaction do | |
if @payment.origin.bank == @payment.destination.bank | |
intra_bank | |
else | |
inter_bank | |
end | |
update_balances | |
end | |
end | |
def intra_bank | |
@payment.comision = 0 | |
@payment.status = "OK" | |
@payment.save! | |
end | |
def inter_bank | |
@payment.comision = 5 | |
@payment.status = "OK" | |
@payment.save! | |
end | |
def update_balances | |
origin = @payment.origin | |
origin.balance = origin.balance - @payment.amount - @payment.comision | |
origin.save! | |
destination = @payment.destination | |
destination.balance = destination.balance + @payment.amount | |
destination.save! | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rails_helper" | |
describe Transfer do | |
let(:user) { create(:user) } | |
let(:bank) { FactoryBot.create(:bank) } | |
let(:origin_account){ FactoryBot.create(:bank_account, bank: bank, balance: 15) } | |
let(:destination_account) { FactoryBot.create(:bank_account, bank: bank, balance: 10) } | |
let(:payment_intra_bank){ FactoryBot.create(:payment, | |
amount: 1, | |
origin: origin_account, | |
destination: destination_account, | |
kind: "transfer" )} | |
let(:bank2) { FactoryBot.create(:bank) } | |
let(:destination_account_different_bank){ FactoryBot.create(:bank_account, bank: bank2, balance: 10) } | |
let(:payment_inter_bank){ FactoryBot.create(:payment, | |
amount: 1, | |
origin: origin_account, | |
destination: destination_account_different_bank, | |
kind: "transfer" )} | |
it "transfer intra bank" do | |
transfer = payment_intra_bank.payment_service | |
result = transfer.new(payment_intra_bank).run! | |
expect(result).to be_truthy | |
expect(payment_intra_bank.comision).to eq(0) | |
expect(payment_intra_bank.origin.balance).to eq(14) | |
expect(payment_intra_bank.destination.balance).to eq(11) | |
end | |
it "transfer inter bank" do | |
transfer = payment_inter_bank.payment_service | |
result = transfer.new(payment_inter_bank).run! | |
expect(result).to be_truthy | |
expect(payment_inter_bank.comision).to eq(5) | |
expect(payment_inter_bank.origin.balance).to eq(9) | |
expect(payment_inter_bank.destination.balance).to eq(11) | |
end | |
it "transfer fails" do | |
transfer = payment_inter_bank.payment_service | |
origin = payment_inter_bank.origin | |
origin.update_columns(balance: 0) | |
result = transfer.new(payment_inter_bank).run! | |
expect(result).to be_falsey | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment