diff --git a/app/admin/billing/invoices.rb b/app/admin/billing/invoices.rb index 1cfb1c360..dfb5af05b 100644 --- a/app/admin/billing/invoices.rb +++ b/app/admin/billing/invoices.rb @@ -31,8 +31,15 @@ def scoped_collection end batch_action :approve, confirm: 'Are you sure?', if: proc { authorized?(:approve) } do |selection| - active_admin_config.resource_class.find(selection).each(&:approve) - redirect_to collection_path, notice: "#{active_admin_config.resource_label.pluralize} are approved!" + active_admin_config.resource_class.find(selection).each do |record| + BillingInvoice::Approve.call(invoice: record) + flash[:notice] = "#{active_admin_config.resource_label.pluralize} are approved!" + rescue BillingInvoice::Approve::Error => e + flash[:error] = "##{record.id}, #{e.message}" + break + end + + redirect_to collection_path end member_action :approve, method: :post do diff --git a/spec/features/billing/invoices/approve_invoice_feature_spec.rb b/spec/features/billing/invoices/approve_invoice_feature_spec.rb index af5e6432a..1ee1554ce 100644 --- a/spec/features/billing/invoices/approve_invoice_feature_spec.rb +++ b/spec/features/billing/invoices/approve_invoice_feature_spec.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true -RSpec.describe 'Approve invoide feature', type: :feature do - subject { click_action_item 'Approve' } - +RSpec.describe 'Approve invoice feature', type: :feature do include_context :login_as_admin + let(:approved_state_id) { Billing::InvoiceState::APPROVED } + let!(:invoice) { FactoryBot.create(:invoice, :manual, :pending, :with_vendor_account) } + context 'when valid data' do - let!(:invoice) { FactoryBot.create(:invoice, :manual, :pending, :with_vendor_account) } - let(:approved_state_id) { Billing::InvoiceState::APPROVED } + subject { click_action_item 'Approve' } before { visit invoice_path(invoice) } @@ -19,8 +19,25 @@ end end + context 'when approve from batch action' do + subject { click_button :OK } + + before do + visit invoices_path + check class: 'toggle_all' + click_batch_action 'Approve Selected' + end + + it 'should approve invoice', :js do + subject + + expect(page).to have_flash_message 'Invoices are approved!', type: :notice + expect(invoice.reload).to have_attributes(state_id: approved_state_id) + end + end + context 'when invalid data', :js do - let!(:invoice) { FactoryBot.create(:invoice, :pending, :with_vendor_account) } + subject { click_action_item 'Approve' } before do allow(BillingInvoice::Approve).to receive(:call).and_raise(BillingInvoice::Approve::Error, 'error')