fix(contacts): normalize country display reads

This commit is contained in:
Sojan Jose
2026-05-26 18:55:11 +05:30
parent b495aad1e6
commit 4705a5c3ff
11 changed files with 148 additions and 3 deletions
@@ -54,6 +54,22 @@ RSpec.describe 'Contacts API', type: :request do
expect(contact_inboxes_source_ids).to include(contact_inbox.source_id)
end
it 'returns canonical country attributes for contact display' do
contact.update!(additional_attributes: { country: 'United States' })
get "/api/v1/accounts/#{account.id}/contacts",
headers: admin.create_new_auth_token,
as: :json
response_body = response.parsed_body
response_contact = response_body['payload'].find { |item| item['id'] == contact.id }
expect(response_contact['additional_attributes']).to include(
'country_code' => 'US',
'country' => 'United States'
)
end
it 'returns all contacts without contact inboxes' do
get "/api/v1/accounts/#{account.id}/contacts?include_contact_inboxes=false",
headers: admin.create_new_auth_token,
@@ -99,6 +99,19 @@ RSpec.describe Account::ContactsExportJob do
expect(row['labels']).to eq('vip')
end
it 'exports canonical country codes from legacy country data' do
legacy_country_contact = create(:contact, account: account, email: 'legacy-country@example.com',
additional_attributes: { country: 'United States' })
described_class.perform_now(account.id, user.id, %w[email country_code], {})
csv_content = account.contacts_export.download.force_encoding('UTF-8').delete_prefix("\xEF\xBB\xBF")
csv_data = CSV.parse(csv_content, headers: true)
row = csv_data.find { |record| record['email'] == legacy_country_contact.email }
expect(row['country_code']).to eq('US')
end
it 'bulk loads labels while exporting contacts' do
create(:label, account: account, title: 'vip')
create(:label, account: account, title: 'support')
+36
View File
@@ -93,6 +93,42 @@ RSpec.describe Contact do
end
end
describe '#canonical_country_code' do
it 'prefers the contact country_code column when it has a valid country code' do
contact = create(:contact, additional_attributes: { country_code: 'IN', country: 'India' })
contact[:country_code] = 'US'
expect(contact.canonical_country_code).to eq('US')
expect(contact.canonical_country_name).to eq('United States')
end
it 'falls back to additional_attributes country_code' do
contact = create(:contact, additional_attributes: { country_code: 'in' })
expect(contact.canonical_country_code).to eq('IN')
expect(contact.canonical_country_name).to eq('India')
end
it 'normalizes legacy country names from additional_attributes country' do
contact = create(:contact, additional_attributes: { country: 'United States' })
expect(contact.canonical_country_code).to eq('US')
expect(contact.canonical_country_name).to eq('United States')
end
end
describe '#additional_attributes_with_canonical_country' do
it 'returns additional attributes with normalized country values without persisting them' do
contact = create(:contact, additional_attributes: { country: 'United States' })
expect(contact.additional_attributes_with_canonical_country).to include(
'country_code' => 'US',
'country' => 'United States'
)
expect(contact.reload.additional_attributes).not_to include('country_code')
end
end
context 'when a contact is created' do
it 'has contact type "visitor" by default' do
contact = create(:contact)
@@ -48,6 +48,16 @@ RSpec.describe LlmFormatter::ContactLlmFormatter do
end
end
context 'when contact has legacy country data' do
before do
contact.update!(additional_attributes: { country: 'United States' })
end
it 'uses the canonical country code' do
expect(formatter.format).to include('Country Code: US')
end
end
context 'when contact has custom attributes' do
let!(:custom_attribute) do
create(:custom_attribute_definition, account: account, attribute_model: 'contact_attribute', attribute_display_name: 'Company')