Skip to content

Commit

Permalink
Fix: Error on adjustment creation without member
Browse files Browse the repository at this point in the history
  • Loading branch information
chumaknadya authored and mnaichuk committed Apr 8, 2021
1 parent df123ef commit c2668d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 5 additions & 3 deletions app/api/v2/admin/adjustments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@ class Adjustments < Grape::API
if adjustment.amount < 0
account_number_hash = ::Operations.split_account_number(account_number: adjustment.receiving_account_number)
member = Member.find_by(uid: account_number_hash[:member_uid])
balance = member.get_account(account_number_hash[:currency_id]).balance
if member.present?
balance = member.get_account(account_number_hash[:currency_id]).balance

if adjustment.amount.abs() > balance
error!({ errors: ['admin.adjustment.user_insufficient_balance'] }, 422)
if adjustment.amount.abs() > balance
error!({ errors: ['admin.adjustment.user_insufficient_balance'] }, 422)
end
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/api/v2/admin/adjustments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,22 @@
}.not_to change { member.get_account(adjustment.currency).balance }
end

context 'adjustment without member' do
let!(:adjustment) { create(:adjustment, currency_id: 'btc', receiving_account_number: "btc-402-") }

it 'should accept adjustment' do
adjustment.update(amount: -10000000.0)

expect {
api_post '/api/v2/admin/adjustments/action', token: token, params: { id: adjustment.id, action: :accept }
}.to change { adjustment.reload.state }.to('accepted')
.and change { Operations::Asset.count }.by(1)
.and change { Operations::Expense.count }.by(1)

expect(response).to be_successful
end
end

context 'already accepted' do
let!(:adjustment) { create(:adjustment, currency_id: 'btc', receiving_account_number: "btc-202-#{member.uid}").tap { |a| a.accept!(validator: member) } }

Expand Down

0 comments on commit c2668d0

Please sign in to comment.