diff --git a/app/services/whatsapp/channel_creation_service.rb b/app/services/whatsapp/channel_creation_service.rb index 154f55520..74882c7e5 100644 --- a/app/services/whatsapp/channel_creation_service.rb +++ b/app/services/whatsapp/channel_creation_service.rb @@ -10,7 +10,7 @@ class Whatsapp::ChannelCreationService validate_parameters! existing_channel = find_existing_channel - raise "Channel already exists: #{existing_channel.phone_number}" if existing_channel + raise I18n.t('errors.whatsapp.phone_number_already_exists', phone_number: existing_channel.phone_number) if existing_channel create_channel_with_inbox end @@ -26,7 +26,6 @@ class Whatsapp::ChannelCreationService def find_existing_channel Channel::Whatsapp.find_by( - account: @account, phone_number: @phone_info[:phone_number] ) end diff --git a/config/locales/en.yml b/config/locales/en.yml index ad54b8dfa..d0f256146 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -90,6 +90,7 @@ en: token_exchange_failed: 'Failed to exchange code for access token. Please try again.' invalid_token_permissions: 'The access token does not have the required permissions for WhatsApp.' phone_info_fetch_failed: 'Failed to fetch phone number information. Please try again.' + phone_number_already_exists: 'Channel already exists for this phone number: %{phone_number}, please contact support if the error persists' reauthorization: generic: 'Failed to reauthorize WhatsApp. Please try again.' not_supported: 'Reauthorization is not supported for this type of WhatsApp channel.' diff --git a/spec/services/whatsapp/channel_creation_service_spec.rb b/spec/services/whatsapp/channel_creation_service_spec.rb index 403fb71b8..983af6c78 100644 --- a/spec/services/whatsapp/channel_creation_service_spec.rb +++ b/spec/services/whatsapp/channel_creation_service_spec.rb @@ -62,14 +62,19 @@ describe Whatsapp::ChannelCreationService do end end - context 'when channel already exists' do + context 'when channel already exists for the phone number' do + let(:different_account) { create(:account) } + before do - create(:channel_whatsapp, account: account, phone_number: '+1234567890', + create(:channel_whatsapp, account: different_account, phone_number: '+1234567890', provider: 'whatsapp_cloud', sync_templates: false, validate_provider_config: false) end - it 'raises an error' do - expect { service.perform }.to raise_error(/Channel already exists/) + it 'raises an error even if the channel belongs to a different account' do + expect { service.perform }.to raise_error( + RuntimeError, + I18n.t('errors.whatsapp.phone_number_already_exists', phone_number: '+1234567890') + ) end end