From 4fef5022551fbba2e105290b469e72be30d921aa Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 25 Nov 2024 18:22:10 +0000 Subject: [PATCH 01/15] fix: failing specs --- app/services/customers/create_service.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/services/customers/create_service.rb b/app/services/customers/create_service.rb index 4211dfebb15..8aaf1a0a33d 100644 --- a/app/services/customers/create_service.rb +++ b/app/services/customers/create_service.rb @@ -92,13 +92,12 @@ def create_from_api(organization:, params:) # NOTE: handle configuration for configured payment providers handle_api_billing_configuration(customer, params, new_customer) - result.customer = customer.reload - IntegrationCustomers::CreateOrUpdateService.call( integration_customers: params[:integration_customers], customer: result.customer, new_customer: ) + result.customer = customer.reload track_customer_created(customer) result From afb53ab252d57e6429187d2ace9906b21159ecd1 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 25 Nov 2024 18:34:21 +0000 Subject: [PATCH 02/15] fix: change order to have correct return --- app/services/customers/update_service.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/customers/update_service.rb b/app/services/customers/update_service.rb index d32eae4a904..061b438217f 100644 --- a/app/services/customers/update_service.rb +++ b/app/services/customers/update_service.rb @@ -146,13 +146,13 @@ def call update_result.raise_if_error! end - result.customer = customer - IntegrationCustomers::CreateOrUpdateService.call( integration_customers: args[:integration_customers], customer: result.customer, new_customer: false ) + + result.customer = customer SendWebhookJob.perform_later('customer.updated', customer) result rescue ActiveRecord::RecordInvalid => e From 6279c0d4a742e6192c163d4f79981f406bb85844 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 25 Nov 2024 18:35:52 +0000 Subject: [PATCH 03/15] fix: change order to have correct return --- app/services/customers/update_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/customers/update_service.rb b/app/services/customers/update_service.rb index 061b438217f..137b2755771 100644 --- a/app/services/customers/update_service.rb +++ b/app/services/customers/update_service.rb @@ -148,7 +148,7 @@ def call IntegrationCustomers::CreateOrUpdateService.call( integration_customers: args[:integration_customers], - customer: result.customer, + customer: customer, new_customer: false ) From 3613814174ce7ba7816fa6f819e7e4bf763cdd66 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 10:13:06 +0000 Subject: [PATCH 04/15] fix: change order to have correct return --- app/services/customers/create_service.rb | 3 +-- app/services/customers/update_service.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/services/customers/create_service.rb b/app/services/customers/create_service.rb index 8aaf1a0a33d..0bc93cc0e98 100644 --- a/app/services/customers/create_service.rb +++ b/app/services/customers/create_service.rb @@ -94,11 +94,10 @@ def create_from_api(organization:, params:) IntegrationCustomers::CreateOrUpdateService.call( integration_customers: params[:integration_customers], - customer: result.customer, + customer: customer, new_customer: ) result.customer = customer.reload - track_customer_created(customer) result rescue BaseService::ServiceFailure => e diff --git a/app/services/customers/update_service.rb b/app/services/customers/update_service.rb index 137b2755771..80556ee1e89 100644 --- a/app/services/customers/update_service.rb +++ b/app/services/customers/update_service.rb @@ -152,7 +152,7 @@ def call new_customer: false ) - result.customer = customer + result.customer = customer.reload SendWebhookJob.perform_later('customer.updated', customer) result rescue ActiveRecord::RecordInvalid => e From 51e20857156f98642f4cc466e16aaa379c27d80c Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 10:26:53 +0000 Subject: [PATCH 05/15] fix: change to sync call --- .../integration_customers/create_or_update_service.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/integration_customers/create_or_update_service.rb b/app/services/integration_customers/create_or_update_service.rb index fcbdfd8698f..5080566bf36 100644 --- a/app/services/integration_customers/create_or_update_service.rb +++ b/app/services/integration_customers/create_or_update_service.rb @@ -22,9 +22,9 @@ def call next if skip_creating_integration_customer? if create_integration_customer? - IntegrationCustomers::CreateJob.perform_later(integration_customer_params:, integration:, customer:) + IntegrationCustomers::CreateJob.perform_now(integration_customer_params:, integration:, customer:) elsif update_integration_customer? - IntegrationCustomers::UpdateJob.perform_later( + IntegrationCustomers::UpdateJob.perform_now( integration_customer_params:, integration:, integration_customer: From 36f95a3bb6f4da16df45fde608f0c4612644375f Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 11:13:48 +0000 Subject: [PATCH 06/15] fix: change to sync call --- .../create_or_update_service.rb | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/app/services/integration_customers/create_or_update_service.rb b/app/services/integration_customers/create_or_update_service.rb index 5080566bf36..78a73c909e8 100644 --- a/app/services/integration_customers/create_or_update_service.rb +++ b/app/services/integration_customers/create_or_update_service.rb @@ -22,9 +22,9 @@ def call next if skip_creating_integration_customer? if create_integration_customer? - IntegrationCustomers::CreateJob.perform_now(integration_customer_params:, integration:, customer:) + handle_creation elsif update_integration_customer? - IntegrationCustomers::UpdateJob.perform_now( + IntegrationCustomers::UpdateJob.perform_later( integration_customer_params:, integration:, integration_customer: @@ -48,6 +48,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 integration&.type == 'Integrations::SalesforceIntegration' + 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 integration&.type == 'Integrations::SalesforceIntegration' + 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 From f83308649f0b4a0913ade996bedc3e35d733dd1b Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 11:14:18 +0000 Subject: [PATCH 07/15] fix: change to sync call --- .../integration_customers/create_or_update_service.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/services/integration_customers/create_or_update_service.rb b/app/services/integration_customers/create_or_update_service.rb index 78a73c909e8..c468b3c4510 100644 --- a/app/services/integration_customers/create_or_update_service.rb +++ b/app/services/integration_customers/create_or_update_service.rb @@ -24,11 +24,7 @@ def call if create_integration_customer? handle_creation elsif update_integration_customer? - IntegrationCustomers::UpdateJob.perform_later( - integration_customer_params:, - integration:, - integration_customer: - ) + handle_update end end end From a5acdc6ab701978a0c7895a65ca620bb7d4d95fa Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 11:33:59 +0000 Subject: [PATCH 08/15] fix: change to sync call --- .../integration_customers/create_or_update_service.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/services/integration_customers/create_or_update_service.rb b/app/services/integration_customers/create_or_update_service.rb index c468b3c4510..e5866b0c592 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 + INTEGRATIONS_REQUIRING_RESPONSE = ['Integrations::SalesforceIntegration'].freeze def initialize(integration_customers:, customer:, new_customer:) @integration_customers = integration_customers&.map { |c| c.to_h.deep_symbolize_keys } @customer = customer @@ -46,7 +47,7 @@ def update_integration_customer? def handle_creation # salesforce don't need to reach a provider so it can be done sync - if integration&.type == 'Integrations::SalesforceIntegration' + if INTEGRATIONS_REQUIRING_RESPONSE.include? integration&.type IntegrationCustomers::CreateJob.perform_now( integration_customer_params: integration_customer_params, integration:, @@ -62,7 +63,7 @@ def handle_creation end def handle_update - if integration&.type == 'Integrations::SalesforceIntegration' + if INTEGRATIONS_REQUIRING_RESPONSE.include? integration&.type IntegrationCustomers::UpdateJob.perform_now( integration_customer_params: integration_customer_params, integration:, From 6253697bade0336b469a275640a239085ff872d6 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 11:37:09 +0000 Subject: [PATCH 09/15] fix: better naming --- .../integration_customers/create_or_update_service.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/services/integration_customers/create_or_update_service.rb b/app/services/integration_customers/create_or_update_service.rb index e5866b0c592..c0c83a595af 100644 --- a/app/services/integration_customers/create_or_update_service.rb +++ b/app/services/integration_customers/create_or_update_service.rb @@ -2,7 +2,7 @@ module IntegrationCustomers class CreateOrUpdateService < ::BaseService - INTEGRATIONS_REQUIRING_RESPONSE = ['Integrations::SalesforceIntegration'].freeze + 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 @@ -47,7 +47,7 @@ def update_integration_customer? def handle_creation # salesforce don't need to reach a provider so it can be done sync - if INTEGRATIONS_REQUIRING_RESPONSE.include? integration&.type + if SYNC_INTEGRATIONS.include? integration&.type IntegrationCustomers::CreateJob.perform_now( integration_customer_params: integration_customer_params, integration:, @@ -63,7 +63,7 @@ def handle_creation end def handle_update - if INTEGRATIONS_REQUIRING_RESPONSE.include? integration&.type + if SYNC_INTEGRATIONS.include? integration&.type IntegrationCustomers::UpdateJob.perform_now( integration_customer_params: integration_customer_params, integration:, From 5d0204f480bd94a5d7aeeae678c6cc2c8fa36f4f Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 12:09:04 +0000 Subject: [PATCH 10/15] fix: added specs --- .../create_or_update_service_spec.rb | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) 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..2fc98f95323 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,30 @@ 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 + expect do + service_call + end.to change(IntegrationCustomers::BaseCustomer, :count).by(1) + end + end end end end From 96969ef83a37667319edfb9cf3c117f302160501 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 12:13:58 +0000 Subject: [PATCH 11/15] fix: added specs --- .../integration_customers/create_or_update_service_spec.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 2fc98f95323..fe4529f1b09 100644 --- a/spec/services/integration_customers/create_or_update_service_spec.rb +++ b/spec/services/integration_customers/create_or_update_service_spec.rb @@ -267,9 +267,10 @@ end it 'processes the job immediately' do - expect do - service_call - end.to change(IntegrationCustomers::BaseCustomer, :count).by(1) + aggregate_failures 'job processing and enqueuing' 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 From 627ece668ba04d8cc7b8fd0d3ad2f78103a0d21c Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 12:15:31 +0000 Subject: [PATCH 12/15] fix: added specs --- .../integration_customers/create_or_update_service_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 fe4529f1b09..1dd5d63fdd6 100644 --- a/spec/services/integration_customers/create_or_update_service_spec.rb +++ b/spec/services/integration_customers/create_or_update_service_spec.rb @@ -267,7 +267,7 @@ end it 'processes the job immediately' do - aggregate_failures 'job processing and enqueuing' 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 From 906a1d4dfa0ef416879e91f0ac6ed0224ed9f7e0 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 12:56:06 +0000 Subject: [PATCH 13/15] fix: added specs --- app/services/customers/create_service.rb | 8 +++++--- app/services/customers/update_service.rb | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/services/customers/create_service.rb b/app/services/customers/create_service.rb index 0bc93cc0e98..6f06746625d 100644 --- a/app/services/customers/create_service.rb +++ b/app/services/customers/create_service.rb @@ -92,12 +92,14 @@ def create_from_api(organization:, params:) # NOTE: handle configuration for configured payment providers handle_api_billing_configuration(customer, params, new_customer) + result.customer = customer.reload + IntegrationCustomers::CreateOrUpdateService.call( integration_customers: params[:integration_customers], - customer: customer, + customer: result.customer, new_customer: ) - result.customer = customer.reload + track_customer_created(customer) result rescue BaseService::ServiceFailure => e @@ -184,7 +186,7 @@ def create(**args) ) create_billing_configuration(customer, billing_configuration) - result.customer = customer + result.customer = customer.reload IntegrationCustomers::CreateOrUpdateService.call( integration_customers: args[:integration_customers], diff --git a/app/services/customers/update_service.rb b/app/services/customers/update_service.rb index 80556ee1e89..245dbeed2d7 100644 --- a/app/services/customers/update_service.rb +++ b/app/services/customers/update_service.rb @@ -146,13 +146,13 @@ def call update_result.raise_if_error! end + result.customer = customer.reload IntegrationCustomers::CreateOrUpdateService.call( integration_customers: args[:integration_customers], - customer: customer, + customer: result.customer, new_customer: false ) - result.customer = customer.reload SendWebhookJob.perform_later('customer.updated', customer) result rescue ActiveRecord::RecordInvalid => e From 07c17597fe1a99bc2356fbfe85db8572fc1871da Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 12:57:26 +0000 Subject: [PATCH 14/15] fix: added specs --- app/services/customers/create_service.rb | 2 +- app/services/customers/update_service.rb | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/services/customers/create_service.rb b/app/services/customers/create_service.rb index 6f06746625d..4211dfebb15 100644 --- a/app/services/customers/create_service.rb +++ b/app/services/customers/create_service.rb @@ -186,7 +186,7 @@ def create(**args) ) create_billing_configuration(customer, billing_configuration) - result.customer = customer.reload + result.customer = customer IntegrationCustomers::CreateOrUpdateService.call( integration_customers: args[:integration_customers], diff --git a/app/services/customers/update_service.rb b/app/services/customers/update_service.rb index 245dbeed2d7..21944f16f8a 100644 --- a/app/services/customers/update_service.rb +++ b/app/services/customers/update_service.rb @@ -146,13 +146,12 @@ def call update_result.raise_if_error! end - result.customer = customer.reload + result.customer = customer IntegrationCustomers::CreateOrUpdateService.call( integration_customers: args[:integration_customers], customer: result.customer, new_customer: false ) - SendWebhookJob.perform_later('customer.updated', customer) result rescue ActiveRecord::RecordInvalid => e From ad3fcebc67b0b73a259f72e15c1102bfd6611264 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 27 Nov 2024 12:57:52 +0000 Subject: [PATCH 15/15] fix: added specs --- app/services/customers/update_service.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/services/customers/update_service.rb b/app/services/customers/update_service.rb index 21944f16f8a..d32eae4a904 100644 --- a/app/services/customers/update_service.rb +++ b/app/services/customers/update_service.rb @@ -147,6 +147,7 @@ def call end result.customer = customer + IntegrationCustomers::CreateOrUpdateService.call( integration_customers: args[:integration_customers], customer: result.customer,