2022-06-23 10:18:56 +00:00
|
|
|
# retain_original_contact_name: false / true
|
|
|
|
# In case of setUser we want to update the name of the identified contact,
|
|
|
|
# which is the default behaviour
|
|
|
|
#
|
|
|
|
# But, In case of contact merge during prechat form contact update.
|
|
|
|
# We don't want to update the name of the identified original contact.
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
class ContactIdentifyAction
|
2022-06-23 10:18:56 +00:00
|
|
|
pattr_initialize [:contact!, :params!, { retain_original_contact_name: false }]
|
2020-04-03 07:34:58 +00:00
|
|
|
|
|
|
|
def perform
|
2022-06-23 10:18:56 +00:00
|
|
|
@attributes_to_update = [:identifier, :name, :email, :phone_number]
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
2021-08-13 11:56:09 +00:00
|
|
|
merge_if_existing_identified_contact
|
|
|
|
merge_if_existing_email_contact
|
|
|
|
merge_if_existing_phone_number_contact
|
2020-04-03 07:34:58 +00:00
|
|
|
update_contact
|
|
|
|
end
|
|
|
|
@contact
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def account
|
|
|
|
@account ||= @contact.account
|
|
|
|
end
|
|
|
|
|
2021-08-13 11:56:09 +00:00
|
|
|
def merge_if_existing_identified_contact
|
2022-06-23 10:18:56 +00:00
|
|
|
return unless merge_contacts?(existing_identified_contact, :identifier)
|
|
|
|
|
|
|
|
process_contact_merge(existing_identified_contact)
|
2021-08-13 11:56:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def merge_if_existing_email_contact
|
2022-06-23 10:18:56 +00:00
|
|
|
return unless merge_contacts?(existing_email_contact, :email)
|
|
|
|
|
|
|
|
process_contact_merge(existing_email_contact)
|
2021-08-13 11:56:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def merge_if_existing_phone_number_contact
|
2022-06-23 10:18:56 +00:00
|
|
|
return unless merge_contacts?(existing_phone_number_contact, :phone_number)
|
|
|
|
return unless mergable_phone_contact?
|
|
|
|
|
|
|
|
process_contact_merge(existing_phone_number_contact)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_contact_merge(mergee_contact)
|
|
|
|
@contact = merge_contact(mergee_contact, @contact)
|
|
|
|
@attributes_to_update.delete(:name) if retain_original_contact_name
|
2021-08-13 11:56:09 +00:00
|
|
|
end
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
def existing_identified_contact
|
|
|
|
return if params[:identifier].blank?
|
|
|
|
|
2022-06-23 10:18:56 +00:00
|
|
|
@existing_identified_contact ||= account.contacts.find_by(identifier: params[:identifier])
|
2020-04-03 07:34:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def existing_email_contact
|
|
|
|
return if params[:email].blank?
|
|
|
|
|
2022-06-23 10:18:56 +00:00
|
|
|
@existing_email_contact ||= account.contacts.find_by(email: params[:email])
|
2020-04-03 07:34:58 +00:00
|
|
|
end
|
|
|
|
|
2021-08-13 11:56:09 +00:00
|
|
|
def existing_phone_number_contact
|
|
|
|
return if params[:phone_number].blank?
|
|
|
|
|
2022-06-23 10:18:56 +00:00
|
|
|
@existing_phone_number_contact ||= account.contacts.find_by(phone_number: params[:phone_number])
|
2021-08-13 11:56:09 +00:00
|
|
|
end
|
|
|
|
|
2022-06-23 10:18:56 +00:00
|
|
|
def merge_contacts?(existing_contact, key)
|
|
|
|
return if existing_contact.blank?
|
|
|
|
|
|
|
|
return true if params[:identifier].blank?
|
|
|
|
|
|
|
|
# we want to prevent merging contacts with different identifiers
|
|
|
|
if existing_contact.identifier.present? && existing_contact.identifier != params[:identifier]
|
|
|
|
# we will remove attribute from update list
|
|
|
|
@attributes_to_update.delete(key)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# case: contact 1: email: 1@test.com, phone: 123456789
|
|
|
|
# params: email: 2@test.com, phone: 123456789
|
|
|
|
# we don't want to overwrite 1@test.com since email parameter takes higer priority
|
|
|
|
def mergable_phone_contact?
|
|
|
|
return true if params[:email].blank?
|
|
|
|
|
|
|
|
if existing_phone_number_contact.email.present? && existing_phone_number_contact.email != params[:email]
|
|
|
|
@attributes_to_update.delete(:phone_number)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
true
|
2020-04-03 07:34:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_contact
|
2022-06-23 10:18:56 +00:00
|
|
|
params_to_update = params.slice(*@attributes_to_update).reject do |_k, v|
|
|
|
|
v.blank?
|
|
|
|
end.merge({ custom_attributes: custom_attributes, additional_attributes: additional_attributes })
|
2021-03-27 06:57:48 +00:00
|
|
|
# blank identifier or email will throw unique index error
|
|
|
|
# TODO: replace reject { |_k, v| v.blank? } with compact_blank when rails is upgraded
|
2022-06-23 10:18:56 +00:00
|
|
|
@contact.update!(params_to_update)
|
2020-04-03 07:34:58 +00:00
|
|
|
ContactAvatarJob.perform_later(@contact, params[:avatar_url]) if params[:avatar_url].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_contact(base_contact, merge_contact)
|
2022-06-23 10:18:56 +00:00
|
|
|
return base_contact if base_contact.id == merge_contact.id
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
ContactMergeAction.new(
|
|
|
|
account: account,
|
|
|
|
base_contact: base_contact,
|
|
|
|
mergee_contact: merge_contact
|
|
|
|
).perform
|
|
|
|
end
|
2022-02-28 06:40:55 +00:00
|
|
|
|
|
|
|
def custom_attributes
|
2022-06-23 10:18:56 +00:00
|
|
|
return @contact.custom_attributes if params[:custom_attributes].blank?
|
|
|
|
|
|
|
|
(@contact.custom_attributes || {}).deep_merge(params[:custom_attributes].stringify_keys)
|
2022-02-28 06:40:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def additional_attributes
|
2022-06-23 10:18:56 +00:00
|
|
|
return @contact.additional_attributes if params[:additional_attributes].blank?
|
|
|
|
|
|
|
|
(@contact.additional_attributes || {}).deep_merge(params[:additional_attributes].stringify_keys)
|
2022-02-28 06:40:55 +00:00
|
|
|
end
|
2020-04-03 07:34:58 +00:00
|
|
|
end
|