Skip to content

Commit

Permalink
feat(hubspot): Improve custom object service to first check if the ob…
Browse files Browse the repository at this point in the history
…ject exists
  • Loading branch information
ivannovosad committed Oct 15, 2024
1 parent 2a78df6 commit a634659
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/services/integrations/aggregator/custom_object_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }

Expand Down

0 comments on commit a634659

Please sign in to comment.