parent
dfcc33cbdd
commit
9e052fd5b2
5 changed files with 39 additions and 5 deletions
|
@ -3,8 +3,9 @@ class ContactIdentifyAction
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
@contact = merge_contact(existing_identified_contact, @contact) if merge_contacts?(existing_identified_contact, @contact)
|
merge_if_existing_identified_contact
|
||||||
@contact = merge_contact(existing_email_contact, @contact) if merge_contacts?(existing_email_contact, @contact)
|
merge_if_existing_email_contact
|
||||||
|
merge_if_existing_phone_number_contact
|
||||||
update_contact
|
update_contact
|
||||||
end
|
end
|
||||||
@contact
|
@contact
|
||||||
|
@ -16,6 +17,18 @@ class ContactIdentifyAction
|
||||||
@account ||= @contact.account
|
@account ||= @contact.account
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def merge_if_existing_identified_contact
|
||||||
|
@contact = merge_contact(existing_identified_contact, @contact) if merge_contacts?(existing_identified_contact, @contact)
|
||||||
|
end
|
||||||
|
|
||||||
|
def merge_if_existing_email_contact
|
||||||
|
@contact = merge_contact(existing_email_contact, @contact) if merge_contacts?(existing_email_contact, @contact)
|
||||||
|
end
|
||||||
|
|
||||||
|
def merge_if_existing_phone_number_contact
|
||||||
|
@contact = merge_contact(existing_phone_number_contact, @contact) if merge_contacts?(existing_phone_number_contact, @contact)
|
||||||
|
end
|
||||||
|
|
||||||
def existing_identified_contact
|
def existing_identified_contact
|
||||||
return if params[:identifier].blank?
|
return if params[:identifier].blank?
|
||||||
|
|
||||||
|
@ -28,6 +41,12 @@ class ContactIdentifyAction
|
||||||
@existing_email_contact ||= Contact.where(account_id: account.id).find_by(email: params[:email])
|
@existing_email_contact ||= Contact.where(account_id: account.id).find_by(email: params[:email])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def existing_phone_number_contact
|
||||||
|
return if params[:phone_number].blank?
|
||||||
|
|
||||||
|
@existing_phone_number_contact ||= Contact.where(account_id: account.id).find_by(phone_number: params[:phone_number])
|
||||||
|
end
|
||||||
|
|
||||||
def merge_contacts?(existing_contact, _contact)
|
def merge_contacts?(existing_contact, _contact)
|
||||||
existing_contact && existing_contact.id != @contact.id
|
existing_contact && existing_contact.id != @contact.id
|
||||||
end
|
end
|
||||||
|
@ -36,7 +55,9 @@ class ContactIdentifyAction
|
||||||
custom_attributes = params[:custom_attributes] ? @contact.custom_attributes.merge(params[:custom_attributes]) : @contact.custom_attributes
|
custom_attributes = params[:custom_attributes] ? @contact.custom_attributes.merge(params[:custom_attributes]) : @contact.custom_attributes
|
||||||
# blank identifier or email will throw unique index error
|
# blank identifier or email will throw unique index error
|
||||||
# TODO: replace reject { |_k, v| v.blank? } with compact_blank when rails is upgraded
|
# TODO: replace reject { |_k, v| v.blank? } with compact_blank when rails is upgraded
|
||||||
@contact.update!(params.slice(:name, :email, :identifier).reject { |_k, v| v.blank? }.merge({ custom_attributes: custom_attributes }))
|
@contact.update!(params.slice(:name, :email, :identifier, :phone_number).reject do |_k, v|
|
||||||
|
v.blank?
|
||||||
|
end.merge({ custom_attributes: custom_attributes }))
|
||||||
ContactAvatarJob.perform_later(@contact, params[:avatar_url]) if params[:avatar_url].present?
|
ContactAvatarJob.perform_later(@contact, params[:avatar_url]) if params[:avatar_url].present?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,6 @@ class Api::V1::Widget::ContactsController < Api::V1::Widget::BaseController
|
||||||
end
|
end
|
||||||
|
|
||||||
def permitted_params
|
def permitted_params
|
||||||
params.permit(:website_token, :identifier, :identifier_hash, :email, :name, :avatar_url, custom_attributes: {})
|
params.permit(:website_token, :identifier, :identifier_hash, :email, :name, :avatar_url, :phone_number, custom_attributes: {})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,6 +29,7 @@ export const actions = {
|
||||||
name: userObject.name,
|
name: userObject.name,
|
||||||
avatar_url: userObject.avatar_url,
|
avatar_url: userObject.avatar_url,
|
||||||
identifier_hash: userObject.identifier_hash,
|
identifier_hash: userObject.identifier_hash,
|
||||||
|
phone_number: userObject.phone_number,
|
||||||
};
|
};
|
||||||
const {
|
const {
|
||||||
data: { pubsub_token: pubsubToken },
|
data: { pubsub_token: pubsubToken },
|
||||||
|
|
|
@ -39,7 +39,8 @@ window.addEventListener('chatwoot:ready', function() {
|
||||||
window.$chatwoot.setUser('<%= user_id %>', {
|
window.$chatwoot.setUser('<%= user_id %>', {
|
||||||
identifier_hash: '<%= user_hash %>',
|
identifier_hash: '<%= user_hash %>',
|
||||||
email: 'jane@example.com',
|
email: 'jane@example.com',
|
||||||
name: 'Jane Doe'
|
name: 'Jane Doe',
|
||||||
|
phone_number: ''
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -45,6 +45,17 @@ describe ::ContactIdentifyAction do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when contact with same phone_number exists' do
|
||||||
|
it 'merges the current contact to phone_number contact' do
|
||||||
|
existing_phone_number_contact = create(:contact, account: account, phone_number: '+919999888877')
|
||||||
|
params = { phone_number: '+919999888877' }
|
||||||
|
result = described_class.new(contact: contact, params: params).perform
|
||||||
|
expect(result.id).to eq existing_phone_number_contact.id
|
||||||
|
expect(result.name).to eq existing_phone_number_contact.name
|
||||||
|
expect { contact.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when contacts with blank identifiers exist and identify action is called with blank identifier' do
|
context 'when contacts with blank identifiers exist and identify action is called with blank identifier' do
|
||||||
it 'updates the attributes of contact passed in to identify action' do
|
it 'updates the attributes of contact passed in to identify action' do
|
||||||
create(:contact, account: account, identifier: '')
|
create(:contact, account: account, identifier: '')
|
||||||
|
|
Loading…
Reference in a new issue