Adds a company selector to the contact details form so agents can associate a contact with an existing company directly from the contact page. Closes - None Why Contacts already expose company information through the CRM fields, but the form only accepted free-text company names. As we split company CRM work into smaller PRs, this keeps the contact page aligned with the structured company model while preserving the existing company-name behavior used by automations. What changed - Shows a company dropdown in the contact details form when the Companies feature is enabled. - Keeps legacy free-text company names editable when a contact has no structured `company_id`. - Allows Enterprise contact create/update APIs to accept account-scoped `company_id`. - Syncs `additional_attributes.company_name` when a contact is associated with a company, including the existing email-domain auto-association path. - Serializes `company_id` in the contact model payload so the form can show the current association. How to test 1. Enable Companies for an account and open a contact details page. 2. In Edit contact details, use the Company field to select an existing company. 3. Save the contact and refresh the page. 4. Confirm the selected company remains visible and the contact is associated with that company. 5. Confirm contacts with only a legacy free-text company name still show the text input instead of an empty selector. --------- Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: iamsivin <iamsivin@gmail.com> Co-authored-by: Sony Mathew <2040199+sony-mathew@users.noreply.github.com>
87 lines
3.1 KiB
Ruby
87 lines
3.1 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe Contact, type: :model do
|
|
describe 'company auto-association' do
|
|
let(:account) { create(:account) }
|
|
|
|
context 'when creating a new contact with business email' do
|
|
it 'automatically creates and associates a company' do
|
|
expect do
|
|
create(:contact, email: 'john@acme.com', account: account)
|
|
end.to change(Company, :count).by(1)
|
|
contact = described_class.last
|
|
expect(contact.company).to be_present
|
|
expect(contact.company.domain).to eq('acme.com')
|
|
end
|
|
|
|
it 'does not create company for free email providers' do
|
|
expect do
|
|
create(:contact, email: 'john@gmail.com', account: account)
|
|
end.not_to change(Company, :count)
|
|
end
|
|
end
|
|
|
|
context 'when updating a contact to add email for first time' do
|
|
it 'creates and associates company' do
|
|
contact = create(:contact, email: nil, account: account)
|
|
expect do
|
|
contact.update(email: 'john@acme.com')
|
|
end.to change(Company, :count).by(1)
|
|
contact.reload
|
|
expect(contact.company.domain).to eq('acme.com')
|
|
end
|
|
end
|
|
|
|
context 'when updating a contact that already has a company' do
|
|
it 'does not change company when email changes' do
|
|
existing_company = create(:company, domain: 'oldcompany.com', account: account)
|
|
contact = create(:contact, email: 'john@oldcompany.com', company: existing_company, account: account)
|
|
|
|
expect do
|
|
contact.update(email: 'john@new_company.com')
|
|
end.not_to change(Company, :count)
|
|
contact.reload
|
|
expect(contact.company).to eq(existing_company)
|
|
end
|
|
|
|
it 'updates company activity when contact activity changes' do
|
|
company = create(:company, account: account)
|
|
contact = create(:contact, account: account, company: company)
|
|
|
|
contact.update!(last_activity_at: Time.zone.now)
|
|
|
|
expect(company.reload.last_activity_at).to be_within(1.second).of(contact.last_activity_at)
|
|
end
|
|
end
|
|
|
|
context 'when multiple contacts share the same domain' do
|
|
it 'associates all contacts with the same company' do
|
|
contacts = ['john@acme.com', 'jane@acme.com', 'bob@acme.com']
|
|
contacts.each do |contact|
|
|
create(:contact, email: contact, account: account)
|
|
end
|
|
|
|
expect(Company.where(domain: 'acme.com', account: account).count).to eq(1)
|
|
company = Company.find_by(domain: 'acme.com', account: account)
|
|
expect(company.contacts.count).to eq(contacts.length)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe '#push_event_data' do
|
|
let(:account) { create(:account) }
|
|
let(:company) { create(:company, account: account) }
|
|
let(:contact) { create(:contact, account: account, company: company) }
|
|
|
|
it 'includes company_id when companies feature is enabled' do
|
|
account.enable_features!(:companies)
|
|
|
|
expect(contact.push_event_data[:company_id]).to eq(company.id)
|
|
end
|
|
|
|
it 'does not include company_id when companies feature is disabled' do
|
|
expect(contact.push_event_data).not_to have_key(:company_id)
|
|
end
|
|
end
|
|
end
|