From 7ffb7ef1ed51021d801c6dde26560cf432af917e Mon Sep 17 00:00:00 2001 From: Ivan Novosad Date: Mon, 14 Oct 2024 15:15:47 +0200 Subject: [PATCH] feat(hubspot): Fix processing hubspot responses --- .../aggregator/companies/base_service.rb | 23 ++++++ .../aggregator/contacts/base_service.rb | 9 ++- .../contacts/create_service_spec.rb | 81 +++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) diff --git a/app/services/integrations/aggregator/companies/base_service.rb b/app/services/integrations/aggregator/companies/base_service.rb index 325fcfc1a264..8d1935c0fafb 100644 --- a/app/services/integrations/aggregator/companies/base_service.rb +++ b/app/services/integrations/aggregator/companies/base_service.rb @@ -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 diff --git a/app/services/integrations/aggregator/contacts/base_service.rb b/app/services/integrations/aggregator/contacts/base_service.rb index 431dfe82fe14..f47d094782a4 100644 --- a/app/services/integrations/aggregator/contacts/base_service.rb +++ b/app/services/integrations/aggregator/contacts/base_service.rb @@ -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:) diff --git a/spec/services/integrations/aggregator/contacts/create_service_spec.rb b/spec/services/integrations/aggregator/contacts/create_service_spec.rb index 250cd3212dfd..eb7a222e5093 100644 --- a/spec/services/integrations/aggregator/contacts/create_service_spec.rb +++ b/spec/services/integrations/aggregator/contacts/create_service_spec.rb @@ -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) } @@ -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