Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: extend tax error details for validationError #2878

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions app/services/fees/one_off_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def apply_provider_taxes(fees_result)
taxes_result = Integrations::Aggregator::Taxes::Invoices::CreateService.call(invoice:, fees: fees_result)

unless taxes_result.success?
create_error_detail(taxes_result.error.code)
create_error_detail(taxes_result.error)

return taxes_result
end
Expand All @@ -116,15 +116,17 @@ def apply_provider_taxes(fees_result)
taxes_result
end

def create_error_detail(code)
def create_error_detail(error)
error_result = ErrorDetails::CreateService.call(
owner: invoice,
organization: invoice.organization,
params: {
error_code: :tax_error,
details: {
tax_error: code
}
tax_error: error.code
}.tap do |details|
details[:tax_error_message] = error.error_message if error.code == 'validationError'
end
}
)
error_result.raise_if_error!
Expand Down
10 changes: 6 additions & 4 deletions app/services/invoices/calculate_fees_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def call
taxes_result = fetch_taxes_for_invoice

unless taxes_result.success?
create_error_detail(taxes_result.error.code)
create_error_detail(taxes_result.error)

# only fail invoices that are finalizing
invoice.failed! if finalizing_invoice?
Expand Down Expand Up @@ -338,15 +338,17 @@ def customer_provider_taxation?
@customer_provider_taxation ||= invoice.customer.anrok_customer
end

def create_error_detail(code)
def create_error_detail(error)
error_result = ErrorDetails::CreateService.call(
owner: invoice,
organization: invoice.organization,
params: {
error_code: :tax_error,
details: {
tax_error: code
}
tax_error: error.code
}.tap do |details|
details[:tax_error_message] = error.error_message if error.code == 'validationError'
end
}
)
error_result.raise_if_error!
Expand Down
10 changes: 6 additions & 4 deletions app/services/invoices/progressive_billing_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def call
taxes_result = Integrations::Aggregator::Taxes::Invoices::CreateService.call(invoice:, fees: invoice.fees)

unless taxes_result.success?
create_error_detail(taxes_result.error.code)
create_error_detail(taxes_result.error)
invoice.failed!

return result.service_failure!(code: 'tax_error', message: taxes_result.error.code)
Expand Down Expand Up @@ -154,15 +154,17 @@ def customer_provider_taxation?
@customer_provider_taxation ||= invoice.customer.anrok_customer
end

def create_error_detail(code)
def create_error_detail(error)
error_result = ErrorDetails::CreateService.call(
owner: invoice,
organization: invoice.organization,
params: {
error_code: :tax_error,
details: {
tax_error: code
}
tax_error: error.code
}.tap do |details|
details[:tax_error_message] = error.error_message if error.code == 'validationError'
end
}
)
error_result.raise_if_error!
Expand Down
10 changes: 6 additions & 4 deletions app/services/invoices/retry_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def call
taxes_result = Integrations::Aggregator::Taxes::Invoices::CreateService.call(invoice:, fees: invoice.fees)

unless taxes_result.success?
create_error_detail(taxes_result.error.code)
create_error_detail(taxes_result.error)
return result.validation_failure!(errors: {tax_error: [taxes_result.error.code]})
end

Expand Down Expand Up @@ -109,15 +109,17 @@ def customer
@customer ||= invoice.customer
end

def create_error_detail(code)
def create_error_detail(error)
error_result = ErrorDetails::CreateService.call(
owner: invoice,
organization: invoice.organization,
params: {
error_code: :tax_error,
details: {
tax_error: code
}
tax_error: error.code
}.tap do |details|
details[:tax_error_message] = error.error_message if error.code == 'validationError'
end
}
)
error_result.raise_if_error!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"succeededInvoices": [],
"failedInvoices": [
{
"issuing_date": "2024-12-03",
"currency": "EUR",
"contact": {
"external_id": "sync-1-test-1",
"name": "sync-1-test-1",
"address_line_1": "88 rue du Chemin Vert",
"city": "Paris",
"zip": "75011",
"country": "fr",
"taxable": true,
"tax_number": "FR86894827773"
},
"fees": [
{
"item_id": "sync-1",
"item_code": "lago_default_b2b",
"amount_cents": 2000
}
],
"validation_errors": "You've exceeded your API limit of 10 per second"
}
]
}
22 changes: 22 additions & 0 deletions spec/services/fees/one_off_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,28 @@
expect(invoice.reload.error_details.first.details['tax_error']).to eq('taxDateTooFarInFuture')
end
end

context 'with api limit error' do
let(:body) do
p = Rails.root.join('spec/fixtures/integration_aggregator/taxes/invoices/api_limit_response.json')
File.read(p)
end

it 'returns and store proper error details' do
result = one_off_service.create

aggregate_failures do
expect(result).not_to be_success
expect(result.error.code).to eq('tax_error')
expect(result.error.error_message).to eq('validationError')

expect(invoice.reload.error_details.count).to eq(1)
expect(invoice.reload.error_details.first.details['tax_error']).to eq('validationError')
expect(invoice.reload.error_details.first.details['tax_error_message'])
.to eq("You've exceeded your API limit of 10 per second")
end
end
end
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/services/invoices/calculate_fees_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,28 @@
expect(invoice.reload.error_details.first.details['tax_error']).to eq('taxDateTooFarInFuture')
end
end

context 'with api limit error' do
let(:body) do
p = Rails.root.join('spec/fixtures/integration_aggregator/taxes/invoices/api_limit_response.json')
File.read(p)
end

it 'returns and store proper error details' do
result = invoice_service.call

aggregate_failures do
expect(result).not_to be_success
expect(result.error.code).to eq('tax_error')
expect(result.error.error_message).to eq('validationError')

expect(invoice.reload.error_details.count).to eq(1)
expect(invoice.reload.error_details.first.details['tax_error']).to eq('validationError')
expect(invoice.reload.error_details.first.details['tax_error_message'])
.to eq("You've exceeded your API limit of 10 per second")
end
end
end
end

context 'when calculating fees for draft invoice' do
Expand Down
24 changes: 24 additions & 0 deletions spec/services/invoices/progressive_billing_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,30 @@
expect(invoice.error_details.count).to eq(1)
expect(invoice.error_details.first.details['tax_error']).to eq('taxDateTooFarInFuture')
end

context 'with api limit error' do
let(:body) do
p = Rails.root.join('spec/fixtures/integration_aggregator/taxes/invoices/api_limit_response.json')
File.read(p)
end

it 'returns and store proper error details' do
result = create_service.call

aggregate_failures do
expect(result).not_to be_success
expect(result.error.code).to eq('tax_error')
expect(result.error.error_message).to eq('validationError')

invoice = customer.invoices.order(created_at: :desc).first

expect(invoice.reload.error_details.count).to eq(1)
expect(invoice.reload.error_details.first.details['tax_error']).to eq('validationError')
expect(invoice.reload.error_details.first.details['tax_error_message'])
.to eq("You've exceeded your API limit of 10 per second")
end
end
end
end
end

Expand Down
31 changes: 31 additions & 0 deletions spec/services/invoices/retry_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,37 @@
expect(invoice.error_details.tax_error.order(created_at: :asc).last.discarded?).to be(false)
end
end

context 'with api limit error' do
let(:body) do
p = Rails.root.join('spec/fixtures/integration_aggregator/taxes/invoices/api_limit_response.json')
File.read(p)
end

it 'keeps invoice in failed status' do
result = retry_service.call

expect(result).not_to be_success
expect(result.error).to be_a(BaseService::ValidationFailure)
expect(invoice.reload.status).to eq('failed')
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(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)
expect(invoice.error_details.tax_error.order(created_at: :asc).last.details['tax_error'])
.to eq('validationError')
expect(invoice.error_details.tax_error.order(created_at: :asc).last.details['tax_error_message'])
.to eq("You've exceeded your API limit of 10 per second")
end
end
end
end
end
end