fix: phone number handling in leadsquared (#11527)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
Shivam Mishra
2025-05-22 13:34:27 +05:30
committed by GitHub
parent 4b417ce9e7
commit 99de5f4257
3 changed files with 38 additions and 2 deletions

View File

@@ -31,7 +31,11 @@ class Crm::Leadsquared::LeadFinderService
def find_by_phone_number(contact)
return if contact.phone_number.blank?
search_by_field(contact.phone_number)
lead_data = Crm::Leadsquared::Mappers::ContactMapper.map(contact)
return if lead_data.blank? || lead_data['Mobile'].nil?
search_by_field(lead_data['Mobile'])
end
def search_by_field(value)

View File

@@ -20,11 +20,29 @@ class Crm::Leadsquared::Mappers::ContactMapper
'FirstName' => contact.name.presence,
'LastName' => contact.last_name.presence,
'EmailAddress' => contact.email.presence,
'Mobile' => contact.phone_number.presence,
'Mobile' => formatted_phone_number,
'Source' => brand_name
}.compact
end
def formatted_phone_number
# it seems like leadsquared needs a different phone number format
# it's not documented anywhere, so don't bother trying to look up online
# After some trial and error, I figured out the format, its +<country_code>-<national_number>
return nil if contact.phone_number.blank?
parsed = TelephoneNumber.parse(contact.phone_number)
return contact.phone_number unless parsed.valid?
country_code = parsed.country.country_code
e164 = parsed.e164_number
e164 = e164.sub(/^\+/, '')
national_number = e164.sub(/^#{Regexp.escape(country_code)}/, '')
"+#{country_code}-#{national_number}"
end
def brand_name
::GlobalConfig.get('BRAND_NAME')['BRAND_NAME'] || 'Chatwoot'
end

View File

@@ -16,6 +16,7 @@ RSpec.describe Crm::Leadsquared::Mappers::ContactMapper do
name: 'John',
last_name: 'Doe',
email: 'john@example.com',
# the phone number is intentionally wrong
phone_number: '+1234567890'
)
@@ -29,6 +30,19 @@ RSpec.describe Crm::Leadsquared::Mappers::ContactMapper do
'Source' => 'Test Brand'
)
end
it 'represents the phone number correctly' do
contact.update!(
name: 'John',
last_name: 'Doe',
email: 'john@example.com',
phone_number: '+917507684392'
)
mapped_data = described_class.map(contact)
expect(mapped_data).to include('Mobile' => '+91-7507684392')
end
end
end
end