diff --git a/app/jobs/update_account_invoices.rb b/app/jobs/update_account_invoices.rb index ec0b4052d2..fe30371350 100644 --- a/app/jobs/update_account_invoices.rb +++ b/app/jobs/update_account_invoices.rb @@ -35,11 +35,11 @@ class UpdateAccountInvoices account_invoice.billable_periods.order(:enterprise_id, :begins_at).reject{ |bp| bp.turnover == 0 }.each do |billable_period| current_adjustments << billable_period.ensure_correct_adjustment_for(account_invoice.order) end + + account_invoice.save if current_adjustments.any? + + clean_up(account_invoice.order, current_adjustments) end - - account_invoice.save if current_adjustments.any? - - clean_up(account_invoice.order, current_adjustments) end def clean_up(invoice_order, current_adjustments) @@ -56,11 +56,13 @@ class UpdateAccountInvoices end if current_adjustments.empty? - Bugsnag.notify(RuntimeError.new("Empty Persisted Invoice"), { - invoice_order: invoice_order.as_json - }) if invoice_order.persisted? - - invoice_order.destroy + if invoice_order.persisted? + Bugsnag.notify(RuntimeError.new("Empty Persisted Invoice"), { + invoice_order: invoice_order.as_json + }) + else + invoice_order.destroy + end end end diff --git a/spec/jobs/update_account_invoices_spec.rb b/spec/jobs/update_account_invoices_spec.rb index a91523f00a..6b2f6cc87c 100644 --- a/spec/jobs/update_account_invoices_spec.rb +++ b/spec/jobs/update_account_invoices_spec.rb @@ -130,6 +130,14 @@ describe UpdateAccountInvoices do it "snags a bug" do expect(Bugsnag).to have_received(:notify) end + + it "does not save the order" do + expect(june_account_invoice).to_not have_received(:save) + end + + it "does not clean up the order" do + expect(updater).to_not have_received(:clean_up).with(invoice_order, anything) + end end context "where the order is not complete" do @@ -235,17 +243,34 @@ describe UpdateAccountInvoices do before do allow(obsolete_adjustments).to receive(:destroy_all) allow(invoice_order).to receive(:adjustments) { double(:adjustments, where: obsolete_adjustments) } - updater.clean_up(invoice_order, current_adjustments) end it "destroys obsolete adjustments and snags a bug" do + updater.clean_up(invoice_order, current_adjustments) expect(obsolete_adjustments).to have_received(:destroy_all) expect(Bugsnag).to have_received(:notify).with(RuntimeError.new("Obsolete Adjustments"), anything) end - it "destroys the order and snags a bug" do - expect(invoice_order).to have_received(:destroy) - expect(Bugsnag).to have_received(:notify).with(RuntimeError.new("Empty Persisted Invoice"), anything) + context "when the order is not persisted" do + before do + allow(invoice_order).to receive(:persisted?) { false } + end + + it "destroys the order" do + updater.clean_up(invoice_order, current_adjustments) + expect(invoice_order).to have_received(:destroy) + end + end + + context "when the order is persisted" do + before do + allow(invoice_order).to receive(:persisted?) { true } + end + + it "snags a bug" do + updater.clean_up(invoice_order, current_adjustments) + expect(Bugsnag).to have_received(:notify).with(RuntimeError.new("Empty Persisted Invoice"), anything) + end end end @@ -254,16 +279,32 @@ describe UpdateAccountInvoices do before do allow(invoice_order).to receive(:adjustments) { double(:adjustments, where: obsolete_adjustments) } - updater.clean_up(invoice_order, current_adjustments) end it "has no bugs to snag" do expect(Bugsnag).to_not have_received(:notify).with(RuntimeError.new("Obsolete Adjustments"), anything) end - it "destroys the order and snags a bug" do - expect(invoice_order).to have_received(:destroy) - expect(Bugsnag).to have_received(:notify).with(RuntimeError.new("Empty Persisted Invoice"), anything) + context "when the order is not persisted" do + before do + allow(invoice_order).to receive(:persisted?) { false } + end + + it "destroys the order" do + updater.clean_up(invoice_order, current_adjustments) + expect(invoice_order).to have_received(:destroy) + end + end + + context "when the order is persisted" do + before do + allow(invoice_order).to receive(:persisted?) { true } + end + + it "snags a bug" do + updater.clean_up(invoice_order, current_adjustments) + expect(Bugsnag).to have_received(:notify).with(RuntimeError.new("Empty Persisted Invoice"), anything) + end end end end