-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hubspot): Save portal id to hubspot integration
- Loading branch information
1 parent
7c71efc
commit e3c19d6
Showing
15 changed files
with
295 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Integrations | ||
module Hubspot | ||
class SavePortalIdJob < ApplicationJob | ||
queue_as 'integrations' | ||
|
||
retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3 | ||
|
||
def perform(integration:) | ||
result = Integrations::Hubspot::SavePortalIdService.call(integration:) | ||
result.raise_if_error! | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
app/services/integrations/aggregator/account_information_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Integrations | ||
module Aggregator | ||
class AccountInformationService < BaseService | ||
def action_path | ||
'v1/account-information' | ||
end | ||
|
||
def call | ||
response = http_client.get(headers:) | ||
|
||
result.account_information = OpenStruct.new(response) | ||
result | ||
end | ||
|
||
private | ||
|
||
def headers | ||
{ | ||
'Connection-Id' => integration.connection_id, | ||
'Authorization' => "Bearer #{secret_key}", | ||
'Provider-Config-Key' => provider_key | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/services/integrations/hubspot/save_portal_id_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Integrations | ||
module Hubspot | ||
class SavePortalIdService < BaseService | ||
def initialize(integration:) | ||
@integration = integration | ||
super | ||
end | ||
|
||
def call | ||
return result unless integration.type == 'Integrations::HubspotIntegration' | ||
return result if integration.portal_id.present? | ||
|
||
account_information_result = Integrations::Aggregator::AccountInformationService.call(integration:) | ||
|
||
integration.update!(portal_id: account_information_result.account_information.id) | ||
|
||
result | ||
rescue ActiveRecord::RecordInvalid => e | ||
result.record_validation_failure!(record: e.record) | ||
end | ||
|
||
private | ||
|
||
attr_reader :integration | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
spec/fixtures/integration_aggregator/account_information_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"id": "1234567890", | ||
"type": "STANDARD", | ||
"timeZone": "US/Eastern", | ||
"companyCurrency": "USD", | ||
"additionalCurrencies": [], | ||
"utcOffset": "-04:00", | ||
"utcOffsetMilliseconds": -14400000, | ||
"uiDomain": "app.hubspot.com", | ||
"dataHostingLocation": "na1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Integrations::Hubspot::SavePortalIdJob, type: :job do | ||
describe '#perform' do | ||
subject(:job) { described_class } | ||
|
||
let(:service) { instance_double(Integrations::Hubspot::SavePortalIdService) } | ||
let(:integration) { create(:hubspot_integration) } | ||
let(:result) { BaseService::Result.new } | ||
|
||
before do | ||
allow(Integrations::Hubspot::SavePortalIdService).to receive(:new).and_return(service) | ||
allow(service).to receive(:call).and_return(result) | ||
end | ||
|
||
it 'saves portal id to the integration' do | ||
described_class.perform_now(integration:) | ||
|
||
expect(Integrations::Hubspot::SavePortalIdService).to have_received(:new) | ||
expect(service).to have_received(:call) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
spec/services/integrations/aggregator/account_information_service_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Integrations::Aggregator::AccountInformationService do | ||
subject(:account_information_service) { described_class.new(integration:) } | ||
|
||
let(:integration) { create(:hubspot_integration) } | ||
|
||
describe '.call' do | ||
let(:lago_client) { instance_double(LagoHttpClient::Client) } | ||
let(:endpoint) { 'https://api.nango.dev/v1/account-information' } | ||
|
||
let(:headers) do | ||
{ | ||
'Connection-Id' => integration.connection_id, | ||
'Authorization' => "Bearer #{ENV["NANGO_SECRET_KEY"]}", | ||
'Provider-Config-Key' => 'hubspot' | ||
} | ||
end | ||
|
||
let(:aggregator_response) do | ||
path = Rails.root.join('spec/fixtures/integration_aggregator/account_information_response.json') | ||
JSON.parse(File.read(path)) | ||
end | ||
|
||
before do | ||
allow(LagoHttpClient::Client).to receive(:new).with(endpoint).and_return(lago_client) | ||
allow(lago_client).to receive(:get).with(headers:).and_return(aggregator_response) | ||
end | ||
|
||
it 'successfully fetches account information' do | ||
result = account_information_service.call | ||
account_information = result.account_information | ||
|
||
aggregate_failures do | ||
expect(LagoHttpClient::Client).to have_received(:new).with(endpoint) | ||
expect(lago_client).to have_received(:get) | ||
expect(account_information.id).to eq('1234567890') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
spec/services/integrations/hubspot/save_portal_id_service_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Integrations::Hubspot::SavePortalIdService do | ||
describe '#call' do | ||
let(:portal_id) { '123456' } | ||
let(:integration) { create(:hubspot_integration) } | ||
let(:service_call) { described_class.call(integration:) } | ||
let(:result) { BaseService::Result.new } | ||
let(:account_information) { OpenStruct.new(id: portal_id) } | ||
|
||
before do | ||
result.account_information = account_information | ||
allow(Integrations::Aggregator::AccountInformationService).to receive(:call).and_return(result) | ||
end | ||
|
||
context 'when the service is successful' do | ||
it 'saves the portal ID to the integration' do | ||
expect { service_call }.to change { integration.reload.portal_id }.to(portal_id) | ||
end | ||
|
||
it 'returns a success result' do | ||
result = service_call | ||
expect(result).to be_success | ||
end | ||
end | ||
|
||
context 'when the service fails' do | ||
before do | ||
allow(integration).to receive(:update!).and_raise(ActiveRecord::RecordInvalid.new(integration)) | ||
end | ||
|
||
it 'does not change the portal ID' do | ||
expect { service_call }.not_to change { integration.reload.portal_id } | ||
end | ||
|
||
it 'returns an error message' do | ||
result = service_call | ||
|
||
aggregate_failures do | ||
expect(result).not_to be_success | ||
expect(result.error).to be_a(BaseService::ValidationFailure) | ||
end | ||
end | ||
end | ||
end | ||
end |