Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hubspot): Add new attributes to hubspot integration model #2662

Merged
merged 6 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Integrations
module Aggregator
class SyncCustomObjectsAndPropertiesJob < ApplicationJob
queue_as 'integrations'

def perform(integration:)
Integrations::Hubspot::Invoices::DeployObjectJob.perform_later(integration:)
Integrations::Hubspot::Subscriptions::DeployObjectJob.perform_later(integration:)
Integrations::Hubspot::Companies::DeployPropertiesJob.perform_later(integration:)
Integrations::Hubspot::Contacts::DeployPropertiesJob.perform_later(integration:)
end
end
end
end
19 changes: 19 additions & 0 deletions app/jobs/integrations/hubspot/companies/deploy_properties_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Integrations
module Hubspot
module Companies
class DeployPropertiesJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on Integrations::Aggregator::RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(integration:)
result = Integrations::Hubspot::Companies::DeployPropertiesService.call(integration:)
result.raise_if_error!
end
end
end
end
end
19 changes: 19 additions & 0 deletions app/jobs/integrations/hubspot/contacts/deploy_properties_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Integrations
module Hubspot
module Contacts
class DeployPropertiesJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on Integrations::Aggregator::RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(integration:)
result = Integrations::Hubspot::Contacts::DeployPropertiesService.call(integration:)
result.raise_if_error!
end
end
end
end
end
19 changes: 19 additions & 0 deletions app/jobs/integrations/hubspot/invoices/deploy_object_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Integrations
module Hubspot
module Invoices
class DeployObjectJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on Integrations::Aggregator::RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(integration:)
result = Integrations::Hubspot::Invoices::DeployObjectService.call(integration:)
result.raise_if_error!
end
end
end
end
end
19 changes: 19 additions & 0 deletions app/jobs/integrations/hubspot/subscriptions/deploy_object_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Integrations
module Hubspot
module Subscriptions
class DeployObjectJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on Integrations::Aggregator::RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(integration:)
result = Integrations::Hubspot::Subscriptions::DeployObjectService.call(integration:)
result.raise_if_error!
end
end
end
end
end
1 change: 1 addition & 0 deletions app/jobs/send_webhook_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SendWebhookJob < ApplicationJob
'credit_note.created' => Webhooks::CreditNotes::CreatedService,
'credit_note.generated' => Webhooks::CreditNotes::GeneratedService,
'credit_note.provider_refund_failure' => Webhooks::CreditNotes::PaymentProviderRefundFailureService,
'integration.provider_error' => Webhooks::Integrations::ProviderErrorService,
'payment_provider.error' => Webhooks::PaymentProviders::ErrorService,
'payment_request.created' => Webhooks::PaymentRequests::CreatedService,
"payment_request.payment_failure" => Webhooks::PaymentProviders::PaymentRequestPaymentFailureService,
Expand Down
14 changes: 12 additions & 2 deletions app/models/integrations/hubspot_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ module Integrations
class HubspotIntegration < BaseIntegration
validates :connection_id, :private_app_token, :default_targeted_object, presence: true

settings_accessors :default_targeted_object, :sync_subscriptions, :sync_invoices
settings_accessors :default_targeted_object, :sync_subscriptions, :sync_invoices, :subscriptions_object_type_id,
:invoices_object_type_id, :companies_properties_version, :contacts_properties_version,
:subscriptions_properties_version, :invoices_properties_version
secrets_accessors :connection_id, :private_app_token

TARGETED_OBJECTS = %w[Companies Contacts]
TARGETED_OBJECTS = %w[companies contacts].freeze

def companies_object_type_id
'0-2'
end

def contacts_object_type_id
'0-1'
end
end
end

Expand Down
16 changes: 16 additions & 0 deletions app/serializers/v1/integrations/provider_error_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module V1
module Integrations
class ProviderErrorSerializer < ModelSerializer
def serialize
{
lago_integration_id: model.id,
provider: options[:provider],
provider_code: options[:provider_code],
provider_error: options[:provider_error]
}
end
end
end
end
13 changes: 13 additions & 0 deletions app/services/integrations/aggregator/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ def deliver_error_webhook(customer:, code:, message:)
)
end

def deliver_integration_error_webhook(integration:, code:, message:)
SendWebhookJob.perform_later(
'integration.provider_error',
integration,
provider:,
provider_code: integration.code,
provider_error: {
message:,
error_code: code
}
)
end

def deliver_tax_error_webhook(customer:, code:, message:)
SendWebhookJob.perform_later(
'customer.tax_provider_error',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# frozen_string_literal: true

module Integrations
module Hubspot
module Companies
class DeployPropertiesService < Integrations::Aggregator::BaseService
VERSION = 1

def action_path
"v1/hubspot/properties"
end

def call
return unless integration.type == 'Integrations::HubspotIntegration'
return result if integration.companies_properties_version == VERSION
response = nil
ActiveRecord::Base.transaction do
response = http_client.post_with_response(payload, headers)
integration.settings = integration.reload.settings
integration.companies_properties_version = VERSION
integration.save!
end
result.response = response
result
rescue LagoHttpClient::HttpError => e
message = message(e)
deliver_integration_error_webhook(integration:, code: 'integration_error', message:)
end

private

def headers
{
'Provider-Config-Key' => 'hubspot',
'Authorization' => "Bearer #{secret_key}",
'Connection-Id' => integration.connection_id
}
end

def payload
{
objectType: "companies",
inputs: [
{
groupName: "companyinformation",
name: "lago_customer_id",
label: "Lago Customer Id",
type: "string",
fieldType: "text",
displayOrder: -1,
hasUniqueValue: true,
searchableInGlobalSearch: true,
formField: true
},
{
groupName: "companyinformation",
name: "lago_customer_external_id",
label: "Lago Customer External Id",
type: "string",
fieldType: "text",
displayOrder: -1,
searchableInGlobalSearch: true,
formField: true
},
{
groupName: "companyinformation",
name: "lago_billing_email",
label: "Lago Billing Email",
type: "string",
fieldType: "text",
searchableInGlobalSearch: true,
formField: true
},
{
groupName: "companyinformation",
name: "lago_tax_identification_number",
label: "Lago Tax Identification Number",
type: "string",
fieldType: "text",
searchableInGlobalSearch: true,
formField: true
}
]
}.freeze
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

module Integrations
module Hubspot
module Contacts
class DeployPropertiesService < Integrations::Aggregator::BaseService
VERSION = 1

def action_path
"v1/hubspot/properties"
end

def call
return unless integration.type == 'Integrations::HubspotIntegration'
return result if integration.contacts_properties_version == VERSION
response = nil
ActiveRecord::Base.transaction do
response = http_client.post_with_response(payload, headers)
integration.settings = integration.reload.settings
integration.contacts_properties_version = VERSION
integration.save!
end
result.response = response
result
rescue LagoHttpClient::HttpError => e
message = message(e)
deliver_integration_error_webhook(integration:, code: 'integration_error', message:)
end

private

def headers
{
'Provider-Config-Key' => 'hubspot',
'Authorization' => "Bearer #{secret_key}",
'Connection-Id' => integration.connection_id
}
end

def payload
{
objectType: "contacts",
inputs: [
{
groupName: "contactinformation",
name: "lago_customer_id",
label: "Lago Customer Id",
type: "string",
fieldType: "text",
displayOrder: -1,
hasUniqueValue: true,
searchableInGlobalSearch: true,
formField: true
},
{
groupName: "contactinformation",
name: "lago_customer_external_id",
label: "Lago Customer External Id",
type: "string",
fieldType: "text",
displayOrder: -1,
hasUniqueValue: true,
searchableInGlobalSearch: true,
formField: true
},
{
groupName: "contactinformation",
name: "lago_billing_email",
label: "Lago Billing Email",
type: "string",
fieldType: "text",
searchableInGlobalSearch: true,
formField: true
}
]
}.freeze
end
end
end
end
end
1 change: 1 addition & 0 deletions app/services/integrations/hubspot/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def call

if integration.type == 'Integrations::HubspotIntegration'
Integrations::Aggregator::SendPrivateAppTokenJob.perform_later(integration:)
Integrations::Aggregator::SyncCustomObjectsAndPropertiesJob.perform_later(integration:)
end

result.integration = integration
Expand Down
Loading