diff --git a/app/services/integration_customers/create_or_update_service.rb b/app/services/integration_customers/create_or_update_service.rb index fcbdfd8698f..c0c83a595af 100644 --- a/app/services/integration_customers/create_or_update_service.rb +++ b/app/services/integration_customers/create_or_update_service.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/services/integration_customers/create_or_update_service_spec.rb b/spec/services/integration_customers/create_or_update_service_spec.rb index 678c54f6d0b..1dd5d63fdd6 100644 --- a/spec/services/integration_customers/create_or_update_service_spec.rb +++ b/spec/services/integration_customers/create_or_update_service_spec.rb @@ -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