Files
16b8693e1b fix: standardize contact company field on company_name (#14099)
Standardizes the contact company import/filter/automation contract on
`company_name`.

Closes #14096
Revives #9907

## Why

Contact company is read across the current CRM/contact UI from
`additional_attributes['company_name']`, but CSV import and a few
backend filter/automation paths still used the older `company` key. That
meant imported company values could be saved in a place the dashboard,
sorting, filters, and automation conditions did not consistently read
from.

Based on the production data check, the legacy `company` automation
configuration is effectively dead: the affected account did not have
contacts populated with `additional_attributes['company']`. So this PR
intentionally avoids adding long-term fallback behavior and uses
`company_name` as the single key going forward.

## What changed

- Contact CSV import now writes only `company_name` into
`additional_attributes['company_name']`.
- The example contact import CSV now uses the `company_name` header.
- Contact company sorting/filter config now uses `company_name`.
- Automation condition config now uses `company_name`.
- Existing standard automation conditions with `attribute_key:
'company'` are migrated to `company_name`.
- Existing saved contact filters with standard `attribute_key:
'company'` are migrated to `company_name`.
- Custom attributes named `company` are preserved and are not rewritten
by the migration.

## How to test

- Import a contact CSV with a `company_name` column and confirm the
Contact Company field is populated.
- Sort contacts by Company and confirm imported contacts are ordered
correctly.
- Create/edit an automation with Company as a condition and confirm it
saves with `company_name`.
- Verify existing saved contact filters and automation rules using the
old standard `company` key are migrated to `company_name`.

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Sojan Jose <sojan@pepalo.com>
2026-04-27 18:43:26 +05:30

69 lines
2.3 KiB
Ruby

class DataImport::ContactManager
def initialize(account)
@account = account
end
def build_contact(params)
contact = find_or_initialize_contact(params)
update_contact_attributes(params, contact)
contact
end
def find_or_initialize_contact(params)
contact = find_existing_contact(params)
contact_params = params.slice(:email, :identifier, :phone_number)
contact_params[:phone_number] = format_phone_number(contact_params[:phone_number]) if contact_params[:phone_number].present?
contact ||= @account.contacts.new(contact_params)
contact
end
def find_existing_contact(params)
contact = find_contact_by_identifier(params)
contact ||= find_contact_by_email(params)
contact ||= find_contact_by_phone_number(params)
update_contact_with_merged_attributes(params, contact) if contact.present? && contact.valid?
contact
end
def find_contact_by_identifier(params)
return unless params[:identifier]
@account.contacts.find_by(identifier: params[:identifier])
end
def find_contact_by_email(params)
return unless params[:email]
@account.contacts.from_email(params[:email])
end
def find_contact_by_phone_number(params)
return unless params[:phone_number]
@account.contacts.find_by(phone_number: format_phone_number(params[:phone_number]))
end
def format_phone_number(phone_number)
phone_number.start_with?('+') ? phone_number : "+#{phone_number}"
end
def update_contact_with_merged_attributes(params, contact)
contact.identifier = params[:identifier] if params[:identifier].present?
contact.email = params[:email] if params[:email].present?
contact.phone_number = format_phone_number(params[:phone_number]) if params[:phone_number].present?
update_contact_attributes(params, contact)
contact.save
end
private
def update_contact_attributes(params, contact)
contact.name = params[:name] if params[:name].present?
contact.additional_attributes ||= {}
contact.additional_attributes[:company_name] = params[:company_name] if params[:company_name].present?
contact.additional_attributes[:city] = params[:city] if params[:city].present?
contact.assign_attributes(custom_attributes: contact.custom_attributes.merge(params.except(:identifier, :email, :name, :phone_number)))
end
end