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

fix (accounting-integrations): send webhook for incomplete payload #2600

Merged
merged 2 commits into from
Sep 19, 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: 10 additions & 0 deletions app/services/integrations/aggregator/base_payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
module Integrations
module Aggregator
class BasePayload
class Failure < BaseService::FailedResult
attr_reader :code

def initialize(result, code:)
@code = code

super(result, code)
end
end

def initialize(integration:)
@integration = integration
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def call
return result if e.error_code.to_i < 500

raise e
rescue Integrations::Aggregator::BasePayload::Failure => e
deliver_error_webhook(customer:, code: e.code, message: e.code.humanize)
end

def call_async
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def item(credit_note_item)
subscription_item
end

return {} unless mapped_item
unless mapped_item
raise Integrations::Aggregator::BasePayload::Failure.new(nil, code: 'invalid_mapping')
end

precise_unit_amount = credit_note_item.amount_cents

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def item(credit_note_item)
subscription_item
end

return {} unless mapped_item
unless mapped_item
raise Integrations::Aggregator::BasePayload::Failure.new(nil, code: 'invalid_mapping')
end

{
'item' => mapped_item.external_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def call
return result if e.error_code.to_i < 500

raise e
rescue Integrations::Aggregator::BasePayload::Failure => e
deliver_error_webhook(customer:, code: e.code, message: e.code.humanize)
end

def call_async
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def item(fee)
subscription_item
end

return {} unless mapped_item
unless mapped_item
raise Integrations::Aggregator::BasePayload::Failure.new(nil, code: 'invalid_mapping')
end

{
'external_id' => mapped_item.external_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def item(fee)
subscription_item
end

return {} unless mapped_item
unless mapped_item
raise Integrations::Aggregator::BasePayload::Failure.new(nil, code: 'invalid_mapping')
end

{
'item' => mapped_item.external_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
'tranid' => credit_note.number,
'entity' => integration_customer.external_customer_id,
'istaxable' => true,
'taxitem' => integration_collection_mapping5.external_id,
'taxitem' => integration_collection_mapping5&.external_id,
'taxamountoverride' => 80.0,
'otherrefnum' => credit_note.number,
'custbody_lago_id' => credit_note.id,
Expand Down Expand Up @@ -340,5 +340,41 @@
end
end
end

context 'when there is payload error' do
let(:integration) { create(:xero_integration, organization:) }
let(:integration_customer) { create(:xero_customer, integration:, customer:) }
let(:lago_client) { instance_double(LagoHttpClient::Client) }
let(:endpoint) { 'https://api.nango.dev/v1/xero/creditnotes' }
let(:integration_collection_mapping1) { nil }
let(:integration_collection_mapping2) { nil }
let(:integration_collection_mapping3) { nil }
let(:integration_collection_mapping4) { nil }
let(:integration_collection_mapping5) { nil }
let(:integration_collection_mapping6) { nil }
let(:integration_mapping_add_on) { nil }
let(:integration_mapping_bm) { nil }
let(:response) { instance_double(Net::HTTPOK) }
let(:headers) do
{
'Connection-Id' => integration.connection_id,
'Authorization' => "Bearer #{ENV["NANGO_SECRET_KEY"]}",
'Provider-Config-Key' => 'xero'
}
end
let(:body) do
path = Rails.root.join('spec/fixtures/integration_aggregator/credit_notes/success_hash_response.json')
File.read(path)
end

before do
allow(lago_client).to receive(:post_with_response).with(params, headers).and_return(response)
allow(response).to receive(:body).and_return(body)
end

it 'sends error webhook' do
expect { service_call }.to have_enqueued_job(SendWebhookJob)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
'tranid' => invoice.id,
'entity' => integration_customer.external_customer_id,
'istaxable' => true,
'taxitem' => integration_collection_mapping5.external_id,
'taxitem' => integration_collection_mapping5&.external_id,
'taxamountoverride' => 80.0,
'otherrefnum' => invoice.number,
'custbody_lago_id' => invoice.id,
Expand Down Expand Up @@ -371,5 +371,41 @@
end
end
end

context 'when there is payload error' do
let(:integration) { create(:xero_integration, organization:) }
let(:integration_customer) { create(:xero_customer, integration:, customer:) }
let(:lago_client) { instance_double(LagoHttpClient::Client) }
let(:endpoint) { 'https://api.nango.dev/v1/xero/invoices' }
let(:integration_collection_mapping1) { nil }
let(:integration_collection_mapping2) { nil }
let(:integration_collection_mapping3) { nil }
let(:integration_collection_mapping4) { nil }
let(:integration_collection_mapping5) { nil }
let(:integration_collection_mapping6) { nil }
let(:integration_mapping_add_on) { nil }
let(:integration_mapping_bm) { nil }
let(:response) { instance_double(Net::HTTPOK) }
let(:headers) do
{
'Connection-Id' => integration.connection_id,
'Authorization' => "Bearer #{ENV["NANGO_SECRET_KEY"]}",
'Provider-Config-Key' => 'xero'
}
end
let(:body) do
path = Rails.root.join('spec/fixtures/integration_aggregator/invoices/success_hash_response.json')
File.read(path)
end

before do
allow(lago_client).to receive(:post_with_response).with(params, headers).and_return(response)
allow(response).to receive(:body).and_return(body)
end

it 'sends error webhook' do
expect { service_call }.to have_enqueued_job(SendWebhookJob)
end
end
end
end