Skip to content

Commit

Permalink
Chore: delete invoice errors for sequential number generation when fi…
Browse files Browse the repository at this point in the history
…nishing invoice generation (#3092)

## Context

We have invoices that are generated, so they have invoice_number, but
the invoice_error storing details of not generated number still exists

## Description

Added migration to delete invoice_errors that are not needed anymore;
Added methods to delete invoice_errors when invoice is successfully
generated
  • Loading branch information
annvelents authored Jan 23, 2025
1 parent 2f9d1a9 commit 88b480b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
13 changes: 13 additions & 0 deletions app/services/invoices/refresh_draft_and_finalize_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def call

result.invoice = invoice.reload
after_commit do
clear_invoice_errors(invoice)
unless invoice.closed?
SendWebhookJob.perform_later('invoice.created', invoice)
GeneratePdfAndNotifyJob.perform_later(invoice:, email: should_deliver_email?)
Expand Down Expand Up @@ -78,5 +79,17 @@ def should_deliver_email?
License.premium? &&
invoice.organization.email_settings.include?('invoice.finalized')
end

def clear_invoice_errors(invoice)
invoice_error = InvoiceError.where(id: invoice.id)
return if invoice_error.blank?

delete_generating_sequence_number_error(invoice_error)
end

def delete_generating_sequence_number_error(invoice_error)
error = invoice_error.where('backtrace LIKE ?', '%generate_organization_sequential_id%').first
error&.delete
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class DeleteSequenceGenerationInvoiceErrorForGeneratedInvoices < ActiveRecord::Migration[7.1]
GENERATED_INVOICE_STATUSES = [1, 6].freeze

def change
InvoiceError.joins("LEFT JOIN invoices ON invoices.id = invoice_errors.id")
.where("invoice_errors.backtrace LIKE ?", "%generate_organization_sequential_id%")
.where(invoices: {status: GENERATED_INVOICE_STATUSES}).find_each do |ie|
ie.delete
end
end
end
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions spec/services/invoices/refresh_draft_and_finalize_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,35 @@
expect { finalize_service.call }.not_to change { invoice.reload.status }
end
end

context 'when invoice has invoice_errors' do
before do
InvoiceError.create(
id: invoice.id,
backtrace: "[\"/app/app/models/invoice.rb:432:in 'generate_organization_sequential_id'\", \"/app/app/models/invoice.rb:395:in...",
error: "\"#\\u003cSequenced::SequenceError: Unable to acquire lock on the database\\u003e\"",
invoice: invoice.to_json(except: :file),
subscriptions: invoice.subscriptions.to_json
)
end

context 'when successfully generated the invoice' do
it 'deletes the invoice_errors' do
expect { finalize_service.call }.to change(InvoiceError, :count).by(-1)
expect(InvoiceError.find_by(id: invoice.id)).to be_nil
end
end

context 'when failed to generate the invoice' do
before do
allow(Invoices::RefreshDraftService).to receive(:call).and_return(BaseService::Result.new.service_failure!(code: 'code', message: 'message'))
end

it 'does not delete the invoice_errors' do
expect { finalize_service.call }.to raise_error(BaseService::ServiceFailure)
expect(InvoiceError.find_by(id: invoice.id)).to be_present
end
end
end
end
end

0 comments on commit 88b480b

Please sign in to comment.