Compare commits

...
7 changed files with 66 additions and 10 deletions
+13 -2
View File
@@ -9,7 +9,7 @@ class ContactIdentifyAction
pattr_initialize [:contact!, :params!, { retain_original_contact_name: false, discard_invalid_attrs: false }]
def perform
@attributes_to_update = [:identifier, :name, :email, :phone_number]
@attributes_to_update = [:identifier, :name, :email, :phone_number, :country_code, :location]
ActiveRecord::Base.transaction do
merge_if_existing_identified_contact
@@ -99,7 +99,8 @@ class ContactIdentifyAction
def update_contact
@contact.attributes = params.slice(*@attributes_to_update).reject do |_k, v|
v.blank?
end.merge({ custom_attributes: custom_attributes, additional_attributes: additional_attributes })
end.merge({ custom_attributes: custom_attributes, additional_attributes: additional_attributes, location: contact_location,
country_code: contact_country_code })
# blank identifier or email will throw unique index error
# TODO: replace reject { |_k, v| v.blank? } with compact_blank when rails is upgraded
@contact.discard_invalid_attrs if discard_invalid_attrs
@@ -117,6 +118,16 @@ class ContactIdentifyAction
).perform
end
# TODO: Remove this once we standardize the location field across the app
def contact_location
params[:location] || params[:additional_attributes].fetch(:city, nil)
end
# TODO: Remove this once we standardize the location field across the app
def contact_country_code
params[:country_code] || params[:additional_attributes].fetch(:country, nil)
end
def custom_attributes
return @contact.custom_attributes if params[:custom_attributes].blank?
@@ -49,14 +49,35 @@ class ContactInboxWithContactBuilder
end
def create_contact
account.contacts.create!(
name: contact_attributes[:name] || ::Haikunator.haikunate(1000),
contact_params = build_contact_params
account.contacts.create!(contact_params)
end
def build_contact_params
{
name: contact_name,
phone_number: contact_attributes[:phone_number],
email: contact_attributes[:email],
identifier: contact_attributes[:identifier],
location: contact_location,
country_code: contact_country_code,
additional_attributes: contact_attributes[:additional_attributes],
custom_attributes: contact_attributes[:custom_attributes]
)
}
end
def contact_name
contact_attributes[:name] || ::Haikunator.haikunate(1000)
end
def contact_location
# TODO: Deprecate the additional_attributes['city'] in favor of country_code
contact_attributes[:location] || contact_attributes.dig(:additional_attributes, 'city')
end
def contact_country_code
# TODO: Deprecate the additional_attributes['country_code'] in favor of country_code
contact_attributes[:country_code] || contact_attributes.dig(:additional_attributes, 'country_code')
end
def find_contact
@@ -89,6 +89,9 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
def update
@contact.assign_attributes(contact_update_params)
# TODO: Remove this once we standardize the location field across the app
@contact.country_code = params[:country_code] || params.dig(:additional_attributes, 'country_code')
@contact.location = params[:location] || params.dig(:additional_attributes, 'city')
@contact.save!
process_avatar_from_url
end
@@ -148,7 +151,8 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
end
def permitted_params
params.permit(:name, :identifier, :email, :phone_number, :avatar, :blocked, :avatar_url, additional_attributes: {}, custom_attributes: {})
params.permit(:name, :identifier, :email, :phone_number, :avatar, :location, :country_code, :blocked, :avatar_url, additional_attributes: {},
custom_attributes: {})
end
def contact_custom_attributes
@@ -70,7 +70,18 @@ class Api::V1::Widget::ContactsController < Api::V1::Widget::BaseController
end
def permitted_params
params.permit(:website_token, :identifier, :identifier_hash, :email, :name, :avatar_url, :phone_number, custom_attributes: {},
additional_attributes: {})
params.permit(
:website_token,
:identifier,
:identifier_hash,
:email,
:name,
:avatar_url,
:location,
:country_code,
:phone_number,
custom_attributes: {},
additional_attributes: {}
)
end
end
@@ -43,6 +43,6 @@ class Public::Api::V1::Inboxes::ContactsController < Public::Api::V1::InboxesCon
end
def permitted_params
params.permit(:identifier, :identifier_hash, :email, :name, :avatar_url, :phone_number, custom_attributes: {})
params.permit(:identifier, :identifier_hash, :email, :name, :avatar_url, :location, :city, :phone_number, custom_attributes: {})
end
end
+3
View File
@@ -14,9 +14,12 @@ class ContactIpLookupJob < ApplicationJob
return unless geocoder_result
contact.additional_attributes ||= {}
# TODO: Deprecate these fields in favor of the new location field
contact.additional_attributes['city'] = geocoder_result.city
contact.additional_attributes['country'] = geocoder_result.country
contact.additional_attributes['country_code'] = geocoder_result.country_code
contact.country_code = geocoder_result.country_code
contact.location = geocoder_result.city
contact.save!
end
+7 -1
View File
@@ -60,9 +60,15 @@ class DataImport::ContactManager
def update_contact_attributes(params, contact)
contact.name = params[:name] if params[:name].present?
update_additional_attributes(params, contact)
contact.country_code = params[:country_code] if params[:country_code].present?
contact.location = params[:location] if params[:location].present?
contact.assign_attributes(custom_attributes: contact.custom_attributes.merge(params.except(:identifier, :email, :name, :phone_number)))
end
def update_additional_attributes(params, contact)
contact.additional_attributes ||= {}
contact.additional_attributes[:company] = params[:company] if params[:company].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