Skip to content

Commit

Permalink
Fix: add kept default scope to error_details (#2392)
Browse files Browse the repository at this point in the history
## Description

Added default scope to error_details, that filters kept errors
  • Loading branch information
annvelents authored Aug 9, 2024
1 parent 141ba22 commit e9d44a5
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
6 changes: 2 additions & 4 deletions app/graphql/types/invoices/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Object < Types::BaseObject
field :external_integration_id, String, null: true
field :integration_syncable, GraphQL::Types::Boolean, null: false

delegate :error_details, to: :object

def applied_taxes
object.applied_taxes.order(tax_rate: :desc)
end
Expand All @@ -78,10 +80,6 @@ def external_integration_id
resource_type: :invoice
)&.external_id
end

def error_details
object.error_details.kept
end
end
end
end
1 change: 1 addition & 0 deletions app/models/error_detail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class ErrorDetail < ApplicationRecord
include Discard::Model
self.discard_column = :deleted_at
default_scope -> { kept }

belongs_to :owner, polymorphic: true
belongs_to :organization
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/v1/invoice_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def applied_taxes

def error_details
::CollectionSerializer.new(
model.error_details.kept,
model.error_details,
::V1::Invoices::ErrorDetailSerializer,
collection_name: 'error_details'
).serialize
Expand Down
2 changes: 1 addition & 1 deletion app/services/invoices/retry_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def call
return result.not_found_failure!(resource: 'invoice') unless invoice
return result.not_allowed_failure!(code: 'invalid_status') unless invoice.failed?

invoice.error_details.tax_error.kept.update_all(deleted_at: Time.current) # rubocop:disable Rails/SkipsModelValidations
invoice.error_details.tax_error.discard_all
taxes_result = Integrations::Aggregator::Taxes::Invoices::CreateService.call(invoice:, fees: invoice.fees)

unless taxes_result.success?
Expand Down
8 changes: 5 additions & 3 deletions spec/services/invoices/retry_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@

it 'discards previous tax errors' do
expect { retry_service.call }
.to change(invoice.error_details.tax_error.kept, :count).from(1).to(0)
.to change(invoice.error_details.tax_error, :count).from(1).to(0)
end

it 'updates the issuing date and payment due date' do
Expand Down Expand Up @@ -304,9 +304,11 @@
end

it 'resolves old tax error and creates new one' do
old_error_id = invoice.reload.error_details.last.id
retry_service.call
aggregate_failures do
expect { retry_service.call }.to change(invoice.error_details.tax_error, :count).from(1).to(2)
expect(invoice.error_details.tax_error.kept.count).to be(1)
expect(invoice.error_details.tax_error.last.id).not_to eql(old_error_id)
expect(invoice.error_details.tax_error.count).to be(1)
expect(invoice.error_details.tax_error.order(created_at: :asc).last.discarded?).to be(false)
end
end
Expand Down

0 comments on commit e9d44a5

Please sign in to comment.