Skip to content

Commit

Permalink
feat(InvoiceError) - Improve create_for handling (#2770)
Browse files Browse the repository at this point in the history
## description

If an invoice already has an invoice error, and still fails to generate.
We'd have a duplicate primary key violation.
we'd rather simply update the existing error object.
  • Loading branch information
nudded authored Nov 4, 2024
1 parent 3e1eb85 commit c1e3b24
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions app/models/invoice_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ class InvoiceError < ApplicationRecord
# NOTE! Invoice errors will have the same id as the invoice they belong to.
def self.create_for(invoice:, error:)
return unless invoice

create(id: invoice.id,
instance = find_or_create_by(id: invoice.id)
instance.update(
backtrace: error.backtrace,
error: error.inspect.to_json,
invoice: invoice.to_json(except: :file),
subscriptions: invoice.subscriptions.to_json)
subscriptions: invoice.subscriptions.to_json
)
instance
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/models/invoice_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,13 @@
invoice_error = described_class.create_for(invoice:, error:)
expect(invoice_error.subscriptions).to eq("[]")
end

it "updates when create_for is called with the same invoice" do
invoice_error = described_class.create_for(invoice:, error:)
expect(invoice_error.id).to eq(invoice.id)

invoice_error = described_class.create_for(invoice:, error:)
expect(invoice_error.id).to eq(invoice.id)
end
end
end

0 comments on commit c1e3b24

Please sign in to comment.