Skip to content

Commit

Permalink
feat(hubspot): Fix processing hubspot responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Oct 14, 2024
1 parent 7a2e16c commit 15ac1aa
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
23 changes: 23 additions & 0 deletions app/services/integrations/aggregator/companies/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ class BaseService < Integrations::Aggregator::Contacts::BaseService
def action_path
"v1/#{provider}/companies"
end

private

def process_hash_result(body)
contact = body['succeededCompanies']&.first
contact_id = contact&.dig('id')
email = contact&.dig('lago_billing_email')

if contact_id
result.contact_id = contact_id
result.email = email if email.present?
else
message = if body.key?('failedCompanies')
body['failedCompanies'].first['validation_errors'].map { |error| error['Message'] }.join(". ")
else
body.dig('error', 'payload', 'message')
end

code = 'Validation error'

deliver_error_webhook(customer:, code:, message:)
end
end
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions app/services/integrations/aggregator/contacts/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ def deliver_success_webhook(customer:, webhook_code:)
def process_hash_result(body)
contact = body['succeededContacts']&.first
contact_id = contact&.dig('id')
email = contact&.dig('email')
email = contact&.dig('lago_billing_email')

if contact_id
result.contact_id = contact_id
result.email = email if email.present?
else
message = body['failedContacts'].first['validation_errors'].map { |error| error['Message'] }.join(". ")
message = if body.key?('failedContacts')
body['failedContacts'].first['validation_errors'].map { |error| error['Message'] }.join(". ")
else
body.dig('error', 'payload', 'message')
end

code = 'Validation error'

deliver_error_webhook(customer:, code:, message:)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
RSpec.describe Integrations::Aggregator::Contacts::CreateService do
subject(:service_call) { described_class.call(integration:, customer:, subsidiary_id:) }

let(:service) { described_class.new(integration:, customer:, subsidiary_id:) }
let(:customer) { create(:customer, :with_same_billing_and_shipping_address, organization:) }
let(:subsidiary_id) { '1' }
let(:organization) { create(:organization) }
Expand Down Expand Up @@ -330,4 +331,84 @@
end
end
end

describe '#process_hash_result' do
subject(:process_hash_result) { service.send(:process_hash_result, body) }

let(:result) { service.instance_variable_get(:@result) }
let(:integration) { create(:netsuite_integration, organization:) }
let(:integration_type) { 'hubspot' }
let(:integration_type_key) { 'hubspot' }

before do
allow(service).to receive(:deliver_error_webhook)
end

context 'when contact is successfully created' do
let(:body) do
{
'succeededContacts' => [
{
'id' => '2e50c200-9a54-4a66-b241-1e75fb87373f',
'lago_billing_email' => 'billing@example.com'
}
]
}
end

it 'sets the contact_id and email in the result' do
process_hash_result

expect(result.contact_id).to eq('2e50c200-9a54-4a66-b241-1e75fb87373f')
expect(result.email).to eq('billing@example.com')
end
end

context 'when contact creation fails' do
let(:body) do
{
'failedContacts' => [
{
'validation_errors' => [
{'Message' => 'Email is invalid'},
{'Message' => 'Name is required'}
]
}
]
}
end

it 'delivers an error webhook' do
process_hash_result

expect(service).to have_received(:deliver_error_webhook).with(
customer:,
code: 'Validation error',
message: 'Email is invalid. Name is required'
)
end
end

context 'when there is a general error' do
let(:body) do
{
'error' => {
'payload' => {
'message' => 'An unexpected error occurred'
}
}
}
end

it 'delivers an error webhook' do
process_hash_result

expect(service).to have_received(:deliver_error_webhook).with(
customer:,
code: 'Validation error',
message: 'An unexpected error occurred'
)
end
end
end
end

0 comments on commit 15ac1aa

Please sign in to comment.