Fixes company-contact name drift when a company is renamed or deleted. Closes: N/A ## Why Contacts keep a denormalized `additional_attributes.company_name` for display and filtering. Company rename/delete flows could leave that copied value stale even though the actual `company_id` relationship changed. ## What changed - Enqueues an async company contact-name sync job when a company name changes. - Moves company deletion into `Companies::DeleteJob`. - The delete job unlinks linked contacts, clears only the copied `company_name`, and then deletes the company. - Uses bulk JSON updates for the cleanup path so contact records are not saved, which avoids contact update callbacks, webhook dispatch, and automation side effects. ## How to test - Link a contact to a company, rename the company, and confirm the contact company name updates after the job runs. - Delete a company with linked contacts and confirm the delete job removes the company, unassigns linked contacts, and preserves other contact additional attributes. --------- Co-authored-by: Sony Mathew <sony@chatwoot.com> Co-authored-by: Sony Mathew <2040199+sony-mathew@users.noreply.github.com>
85 lines
2.8 KiB
Ruby
85 lines
2.8 KiB
Ruby
# == Schema Information
|
|
#
|
|
# Table name: companies
|
|
#
|
|
# additional_attributes :jsonb
|
|
# custom_attributes :jsonb
|
|
# last_activity_at :datetime
|
|
# id :bigint not null, primary key
|
|
# contacts_count :integer
|
|
# description :text
|
|
# domain :string
|
|
# name :string not null
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
# account_id :bigint not null
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_companies_on_account_and_domain (account_id,domain) UNIQUE WHERE (domain IS NOT NULL)
|
|
# index_companies_on_account_id (account_id)
|
|
# index_companies_on_name_and_account_id (name,account_id)
|
|
#
|
|
class Company < ApplicationRecord
|
|
include Avatarable
|
|
|
|
ACTIVITY_ROLLUP_INTERVAL = 5.minutes
|
|
|
|
validates :account_id, presence: true
|
|
validates :name, presence: true, length: { maximum: Limits::COMPANY_NAME_LENGTH_LIMIT }
|
|
validates :domain, allow_blank: true, format: {
|
|
with: /\A[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)+\z/,
|
|
message: I18n.t('errors.companies.domain.invalid')
|
|
}
|
|
validates :domain, uniqueness: { scope: :account_id }, if: -> { domain.present? }
|
|
validates :description, length: { maximum: Limits::COMPANY_DESCRIPTION_LENGTH_LIMIT }
|
|
validates :custom_attributes, jsonb_attributes_length: true
|
|
|
|
belongs_to :account
|
|
has_many :contacts, dependent: :nullify
|
|
before_validation :prepare_jsonb_attributes
|
|
after_create_commit :fetch_favicon, if: -> { domain.present? }
|
|
after_update_commit :enqueue_contact_company_name_sync, if: :saved_change_to_name?
|
|
|
|
scope :ordered_by_name, -> { order(:name) }
|
|
scope :search_by_name_or_domain, lambda { |query|
|
|
where('name ILIKE :search OR domain ILIKE :search', search: "%#{query.strip}%")
|
|
}
|
|
|
|
scope :order_on_contacts_count, lambda { |direction|
|
|
order(
|
|
Arel::Nodes::SqlLiteral.new(
|
|
sanitize_sql_for_order("\"companies\".\"contacts_count\" #{direction} NULLS LAST")
|
|
)
|
|
)
|
|
}
|
|
scope :order_on_last_activity_at, lambda { |direction|
|
|
order(
|
|
Arel::Nodes::SqlLiteral.new(
|
|
sanitize_sql_for_order("\"companies\".\"last_activity_at\" #{direction} NULLS LAST")
|
|
)
|
|
)
|
|
}
|
|
|
|
def record_activity_at!(activity_at)
|
|
return if last_activity_at.present? && last_activity_at > activity_at - ACTIVITY_ROLLUP_INTERVAL
|
|
|
|
update!(last_activity_at: activity_at)
|
|
end
|
|
|
|
private
|
|
|
|
def prepare_jsonb_attributes
|
|
self.additional_attributes = {} unless additional_attributes.is_a?(Hash)
|
|
self.custom_attributes = {} unless custom_attributes.is_a?(Hash)
|
|
end
|
|
|
|
def fetch_favicon
|
|
Avatar::AvatarFromFaviconJob.set(wait: 5.seconds).perform_later(self)
|
|
end
|
|
|
|
def enqueue_contact_company_name_sync
|
|
Companies::SyncContactNamesJob.perform_later(company_id: id)
|
|
end
|
|
end
|