diff --git a/app/graphql/types/invoices/object.rb b/app/graphql/types/invoices/object.rb index 20ba71f0fb2..c33402b18b5 100644 --- a/app/graphql/types/invoices/object.rb +++ b/app/graphql/types/invoices/object.rb @@ -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 @@ -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 diff --git a/app/models/error_detail.rb b/app/models/error_detail.rb index 61e226f24c2..e468a57eb45 100644 --- a/app/models/error_detail.rb +++ b/app/models/error_detail.rb @@ -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 diff --git a/app/serializers/v1/invoice_serializer.rb b/app/serializers/v1/invoice_serializer.rb index 4676ce67a1e..eb1b4485c57 100644 --- a/app/serializers/v1/invoice_serializer.rb +++ b/app/serializers/v1/invoice_serializer.rb @@ -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 diff --git a/app/services/invoices/retry_service.rb b/app/services/invoices/retry_service.rb index c4ebec8230c..5bcd105a65c 100644 --- a/app/services/invoices/retry_service.rb +++ b/app/services/invoices/retry_service.rb @@ -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? diff --git a/spec/services/invoices/retry_service_spec.rb b/spec/services/invoices/retry_service_spec.rb index dca3856e2f7..03cd804ce35 100644 --- a/spec/services/invoices/retry_service_spec.rb +++ b/spec/services/invoices/retry_service_spec.rb @@ -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 @@ -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