2020-06-07 08:28:05 +00:00
|
|
|
class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
|
2020-11-10 09:55:26 +00:00
|
|
|
RESULTS_PER_PAGE = 15
|
2019-08-14 09:48:44 +00:00
|
|
|
protect_from_forgery with: :null_session
|
|
|
|
|
|
|
|
before_action :check_authorization
|
2020-11-10 09:55:26 +00:00
|
|
|
before_action :set_current_page, only: [:index, :active, :search]
|
2019-08-14 09:48:44 +00:00
|
|
|
before_action :fetch_contact, only: [:show, :update]
|
|
|
|
|
|
|
|
def index
|
2020-11-11 10:32:14 +00:00
|
|
|
@contacts_count = resolved_contacts.count
|
|
|
|
@contacts = fetch_contact_last_seen_at(resolved_contacts)
|
2020-11-10 09:55:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def search
|
|
|
|
render json: { error: 'Specify search string with parameter q' }, status: :unprocessable_entity if params[:q].blank? && return
|
|
|
|
|
2020-11-23 05:30:07 +00:00
|
|
|
contacts = resolved_contacts.where('name LIKE :search OR email LIKE :search OR phone_number LIKE :search', search: "%#{params[:q]}%")
|
2020-11-10 09:55:26 +00:00
|
|
|
@contacts_count = contacts.count
|
|
|
|
@contacts = fetch_contact_last_seen_at(contacts)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-10-05 18:00:27 +00:00
|
|
|
# returns online contacts
|
|
|
|
def active
|
2020-11-10 09:55:26 +00:00
|
|
|
contacts = Current.account.contacts.where(id: ::OnlineStatusTracker
|
2020-10-05 18:00:27 +00:00
|
|
|
.get_available_contact_ids(Current.account.id))
|
2020-11-10 09:55:26 +00:00
|
|
|
@contacts_count = contacts.count
|
|
|
|
@contacts = contacts.page(@current_page)
|
2020-10-05 18:00:27 +00:00
|
|
|
end
|
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
def show; end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def create
|
2020-07-21 06:45:24 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
2020-08-21 14:00:27 +00:00
|
|
|
@contact = Current.account.contacts.new(contact_params)
|
2020-10-27 20:44:36 +00:00
|
|
|
set_ip
|
2020-07-21 06:45:24 +00:00
|
|
|
@contact.save!
|
|
|
|
@contact_inbox = build_contact_inbox
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2020-10-27 20:44:36 +00:00
|
|
|
@contact.assign_attributes(contact_update_params)
|
|
|
|
set_ip
|
|
|
|
@contact.save!
|
2020-08-22 18:35:07 +00:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
render json: {
|
|
|
|
message: e.record.errors.full_messages.join(', '),
|
|
|
|
contact: Contact.find_by(email: contact_params[:email])
|
|
|
|
}, status: :unprocessable_entity
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-11-10 09:55:26 +00:00
|
|
|
private
|
2020-08-10 06:45:29 +00:00
|
|
|
|
2020-11-11 10:32:14 +00:00
|
|
|
def resolved_contacts
|
|
|
|
@resolved_contacts ||= Current.account.contacts
|
|
|
|
.where.not(email: [nil, ''])
|
|
|
|
.or(Current.account.contacts.where.not(phone_number: [nil, '']))
|
|
|
|
.order('LOWER(name)')
|
|
|
|
end
|
|
|
|
|
2020-11-10 09:55:26 +00:00
|
|
|
def set_current_page
|
|
|
|
@current_page = params[:page] || 1
|
2020-08-10 06:45:29 +00:00
|
|
|
end
|
|
|
|
|
2020-11-10 09:55:26 +00:00
|
|
|
def fetch_contact_last_seen_at(contacts)
|
|
|
|
contacts.left_outer_joins(:conversations)
|
|
|
|
.select('contacts.*, COUNT(conversations.id) as conversations_count, MAX(conversations.contact_last_seen_at) as last_seen_at')
|
|
|
|
.group('contacts.id')
|
|
|
|
.includes([{ avatar_attachment: [:blob] }, { contact_inboxes: [:inbox] }])
|
|
|
|
.page(@current_page).per(RESULTS_PER_PAGE)
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
def build_contact_inbox
|
|
|
|
return if params[:inbox_id].blank?
|
|
|
|
|
2020-08-10 06:45:29 +00:00
|
|
|
inbox = Current.account.inboxes.find(params[:inbox_id])
|
2020-07-21 06:45:24 +00:00
|
|
|
source_id = params[:source_id] || SecureRandom.uuid
|
|
|
|
ContactInbox.create(contact: @contact, inbox: inbox, source_id: source_id)
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def contact_params
|
2020-08-22 18:35:07 +00:00
|
|
|
params.require(:contact).permit(:name, :email, :phone_number, additional_attributes: {}, custom_attributes: {})
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-08-21 14:00:27 +00:00
|
|
|
def contact_custom_attributes
|
|
|
|
return @contact.custom_attributes.merge(contact_params[:custom_attributes]) if contact_params[:custom_attributes]
|
|
|
|
|
|
|
|
@contact.custom_attributes
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-08-21 14:00:27 +00:00
|
|
|
def contact_update_params
|
|
|
|
# we want the merged custom attributes not the original one
|
|
|
|
contact_params.except(:custom_attributes).merge({ custom_attributes: contact_custom_attributes })
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_contact
|
2020-09-19 07:16:34 +00:00
|
|
|
@contact = Current.account.contacts.includes(contact_inboxes: [:inbox]).find(params[:id])
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2020-10-27 20:44:36 +00:00
|
|
|
|
|
|
|
def set_ip
|
|
|
|
return if @contact.account.feature_enabled?('ip_lookup')
|
|
|
|
|
|
|
|
@contact[:additional_attributes][:created_at_ip] ||= request.remote_ip
|
|
|
|
@contact[:additional_attributes][:updated_at_ip] = request.remote_ip
|
|
|
|
end
|
2019-10-20 08:47:26 +00:00
|
|
|
end
|