diff --git a/app/api/v2/admin/adjustments.rb b/app/api/v2/admin/adjustments.rb index e073cbe1c0..faf0a1fe93 100644 --- a/app/api/v2/admin/adjustments.rb +++ b/app/api/v2/admin/adjustments.rb @@ -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 diff --git a/spec/api/v2/admin/adjustments_spec.rb b/spec/api/v2/admin/adjustments_spec.rb index ef88852645..11daca6c24 100644 --- a/spec/api/v2/admin/adjustments_spec.rb +++ b/spec/api/v2/admin/adjustments_spec.rb @@ -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) } }