-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (tax-integrations): auto retry failed invoice due to api limit (#…
…2884) ## Context Failed invoices due to api limit should be auto retried ## Description This PR uses error detail extension (#2878) to catch all failed invoices due to API limit error and auto-retry them. This is first phase for covering api limit errors. Later, we will turn tax integration to be async and implement throttling mechanism for it.
- Loading branch information
1 parent
db25970
commit 4ee1e09
Showing
4 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Clock | ||
class RetryFailedInvoicesJob < ApplicationJob | ||
include SentryCronConcern | ||
|
||
queue_as 'clock' | ||
|
||
def perform | ||
Invoice | ||
.failed | ||
.joins(:error_details) | ||
.where("error_details.details ? 'tax_error_message'") | ||
.where("error_details.details ->> 'tax_error_message' ILIKE ?", "%API limit%").find_each do |i| | ||
Invoices::RetryService.call(invoice: i) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe Clock::RetryFailedInvoicesJob, job: true do | ||
subject { described_class } | ||
|
||
describe '.perform' do | ||
let(:customer) { create(:customer) } | ||
let(:failed_invoice) do | ||
create( | ||
:invoice, | ||
status: :failed, | ||
created_at: DateTime.parse('20 Jun 2022'), | ||
customer:, | ||
organization: customer.organization | ||
) | ||
end | ||
let(:error_detail) do | ||
create( | ||
:error_detail, | ||
owner: failed_invoice, | ||
organization: customer.organization, | ||
error_code: :tax_error, | ||
details: { | ||
tax_error: 'validationError', | ||
tax_error_message: "You've exceeded your API limit of 10 per second" | ||
} | ||
) | ||
end | ||
let(:finalized_invoice) do | ||
create( | ||
:invoice, | ||
status: :finalized, | ||
created_at: DateTime.parse('20 Jun 2022'), | ||
customer:, | ||
organization: customer.organization | ||
) | ||
end | ||
|
||
before do | ||
failed_invoice | ||
finalized_invoice | ||
error_detail | ||
allow(Invoices::RetryService).to receive(:call) | ||
end | ||
|
||
context 'with invalid product error' do | ||
let(:error_detail) do | ||
create( | ||
:error_detail, | ||
owner: failed_invoice, | ||
organization: customer.organization, | ||
error_code: :tax_error, | ||
details: { | ||
tax_error: 'productExternalIdUnknown' | ||
} | ||
) | ||
end | ||
|
||
it 'does not call the retry service' do | ||
current_date = DateTime.parse('22 Jun 2022') | ||
|
||
travel_to(current_date) do | ||
described_class.perform_now | ||
|
||
expect(Invoices::RetryService).not_to have_received(:call).with(invoice: failed_invoice) | ||
expect(Invoices::RetryService).not_to have_received(:call).with(invoice: finalized_invoice) | ||
end | ||
end | ||
end | ||
|
||
context 'with api limit error' do | ||
it 'calls the retry service' do | ||
current_date = DateTime.parse('22 Jun 2022') | ||
|
||
travel_to(current_date) do | ||
described_class.perform_now | ||
|
||
expect(Invoices::RetryService).to have_received(:call).with(invoice: failed_invoice) | ||
expect(Invoices::RetryService).not_to have_received(:call).with(invoice: finalized_invoice) | ||
end | ||
end | ||
end | ||
end | ||
end |