diff --git a/app/services/integrations/aggregator/custom_object_service.rb b/app/services/integrations/aggregator/custom_object_service.rb index b055ed70cba..496dba0de05 100644 --- a/app/services/integrations/aggregator/custom_object_service.rb +++ b/app/services/integrations/aggregator/custom_object_service.rb @@ -17,6 +17,8 @@ def call result.custom_object = OpenStruct.new(response) result + rescue LagoHttpClient::HttpError => e + result.service_failure!(code: e.error_code, message: e.message) end private diff --git a/app/services/integrations/hubspot/invoices/deploy_object_service.rb b/app/services/integrations/hubspot/invoices/deploy_object_service.rb index e0d84973108..0b65e78fac4 100644 --- a/app/services/integrations/hubspot/invoices/deploy_object_service.rb +++ b/app/services/integrations/hubspot/invoices/deploy_object_service.rb @@ -13,13 +13,17 @@ def action_path def call return unless integration.type == 'Integrations::HubspotIntegration' return result if integration.invoices_properties_version == VERSION + + custom_object_result = Integrations::Aggregator::CustomObjectService.call(integration:, name: 'LagoInvoices') + if custom_object_result.success? + save_object_type_id(custom_object_result.custom_object&.objectTypeId) + return result + end + response = nil ActiveRecord::Base.transaction do response = http_client.post_with_response(payload, headers) - integration.settings = integration.reload.settings - integration.invoices_object_type_id = response['objectTypeId'] - integration.invoices_properties_version = VERSION - integration.save! + save_object_type_id(response['objectTypeId']) end result.response = response result @@ -30,6 +34,13 @@ def call private + def save_object_type_id(object_type_id) + integration.settings = integration.reload.settings + integration.invoices_object_type_id = object_type_id + integration.invoices_properties_version = VERSION + integration.save! + end + def headers { 'Provider-Config-Key' => 'hubspot', diff --git a/spec/services/integrations/hubspot/invoices/deploy_object_service_spec.rb b/spec/services/integrations/hubspot/invoices/deploy_object_service_spec.rb index ac036d4c827..ca3bc7284bb 100644 --- a/spec/services/integrations/hubspot/invoices/deploy_object_service_spec.rb +++ b/spec/services/integrations/hubspot/invoices/deploy_object_service_spec.rb @@ -9,14 +9,25 @@ describe '.call' do let(:http_client) { instance_double(LagoHttpClient::Client) } + let(:http_client_get) { instance_double(LagoHttpClient::Client) } let(:endpoint) { "https://api.nango.dev/v1/hubspot/object" } + let(:customer_object_endpoint) { "https://api.nango.dev/v1/hubspot/custom-object" } let(:response) { instance_double('Response', success?: true) } + let(:get_response) do + path = Rails.root.join('spec/fixtures/integration_aggregator/custom_object_response.json') + JSON.parse(File.read(path)) + end + before do allow(LagoHttpClient::Client).to receive(:new) .with(endpoint) .and_return(http_client) + allow(LagoHttpClient::Client).to receive(:new) + .with(customer_object_endpoint) + .and_return(http_client_get) allow(http_client).to receive(:post_with_response).and_return(response) + allow(http_client_get).to receive(:get).and_raise LagoHttpClient::HttpError.new('error', 'error', nil) allow(response).to receive(:[]).with('objectTypeId').and_return('123') integration.invoices_properties_version = nil @@ -53,6 +64,50 @@ end end + context 'when custom object service returns a valid objectTypeId' do + let(:custom_object_result) do + instance_double( + 'CustomObjectResult', + success?: true, + custom_object: instance_double('CustomObject', objectTypeId: '123') + ) + end + + before do + allow(http_client).to receive(:get).and_return(get_response) + allow(Integrations::Aggregator::CustomObjectService).to receive(:call).and_return(custom_object_result) + end + + it 'saves the objectTypeId and updates the invoices_properties_version' do + deploy_object_service.call + + aggregate_failures do + expect(integration.reload.invoices_object_type_id).to eq('123') + expect(integration.reload.invoices_properties_version).to eq(described_class::VERSION) + end + end + end + + context 'when custom object service does not return a valid objectTypeId' do + let(:custom_object_result) do + instance_double('CustomObjectResult', success?: false) + end + + before do + allow(Integrations::Aggregator::CustomObjectService).to receive(:call).and_return(custom_object_result) + end + + it 'makes an API call and updates the invoices_properties_version' do + deploy_object_service.call + + aggregate_failures do + expect(LagoHttpClient::Client).to have_received(:new).with(endpoint) + expect(http_client).to have_received(:post_with_response) + expect(integration.reload.invoices_properties_version).to eq(described_class::VERSION) + end + end + end + context 'when an HTTP error occurs' do let(:error) { LagoHttpClient::HttpError.new('error message', '{"error": {"message": "unknown failure"}}', nil) }