Skip to content

Commit

Permalink
feat(salesforce): Fill IntegrationCustomer on salesforce integration (#…
Browse files Browse the repository at this point in the history
…2864)

## Context

This PR introduces the logic to handle the IntegrationCustomer
association specifically for Salesforce integrations. It ensures that
Salesforce-related IntegrationCustomer records are processed
synchronously since we don't need to reach a provider on this case and
we need the response of the IntegrationCustomer on the serializer ( on
customer create and update )
  • Loading branch information
brunomiguelpinto authored Nov 27, 2024
1 parent c59caed commit f4b16a1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
42 changes: 36 additions & 6 deletions app/services/integration_customers/create_or_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module IntegrationCustomers
class CreateOrUpdateService < ::BaseService
SYNC_INTEGRATIONS = ['Integrations::SalesforceIntegration'].freeze
def initialize(integration_customers:, customer:, new_customer:)
@integration_customers = integration_customers&.map { |c| c.to_h.deep_symbolize_keys }
@customer = customer
Expand All @@ -22,13 +23,9 @@ def call
next if skip_creating_integration_customer?

if create_integration_customer?
IntegrationCustomers::CreateJob.perform_later(integration_customer_params:, integration:, customer:)
handle_creation
elsif update_integration_customer?
IntegrationCustomers::UpdateJob.perform_later(
integration_customer_params:,
integration:,
integration_customer:
)
handle_update
end
end
end
Expand All @@ -48,6 +45,39 @@ def update_integration_customer?
!new_customer && integration_customer
end

def handle_creation
# salesforce don't need to reach a provider so it can be done sync
if SYNC_INTEGRATIONS.include? integration&.type
IntegrationCustomers::CreateJob.perform_now(
integration_customer_params: integration_customer_params,
integration:,
customer:
)
else
IntegrationCustomers::CreateJob.perform_later(
integration_customer_params: integration_customer_params,
integration:,
customer:
)
end
end

def handle_update
if SYNC_INTEGRATIONS.include? integration&.type
IntegrationCustomers::UpdateJob.perform_now(
integration_customer_params: integration_customer_params,
integration:,
integration_customer:
)
else
IntegrationCustomers::UpdateJob.perform_later(
integration_customer_params: integration_customer_params,
integration:,
integration_customer:
)
end
end

def sanitize_integration_customers
updated_int_customers = integration_customers.reject { |m| m[:id].nil? }.map { |m| m[:id] }
not_needed_ids = customer.integration_customers.pluck(:id) - updated_int_customers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,31 @@
expect { service_call }.to have_enqueued_job(IntegrationCustomers::CreateJob).exactly(:once)
end
end

context 'when adding a sync integration customer' do
let(:integration_salesforce) { create(:salesforce_integration, organization:) }
let(:integration_customers) do
[
{
integration_type: 'salesforce',
integration_code: integration_salesforce.code,
sync_with_provider: true,
external_customer_id: "12345"
}
]
end

before do
IntegrationCustomers::BaseCustomer.destroy_all
end

it 'processes the job immediately' do
aggregate_failures do
expect { service_call }.to change(IntegrationCustomers::BaseCustomer, :count).by(1)
expect { service_call }.not_to have_enqueued_job(IntegrationCustomers::CreateJob)
end
end
end
end
end
end

0 comments on commit f4b16a1

Please sign in to comment.