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(salesforce): Fill IntegrationCustomer on salesforce integration #2864

Merged
merged 15 commits into from
Nov 27, 2024
Merged
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