Only clean up account invoice orders that aren't already complete, don't attempt to destroy persisted account_invoice orders

This commit is contained in:
Rob Harrington
2015-10-02 10:16:53 +10:00
parent 27741863c5
commit 815df3d667
2 changed files with 60 additions and 17 deletions

View File

@@ -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

View File

@@ -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