2020-06-07 08:28:05 +00:00
|
|
|
class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseController
|
2020-05-03 06:47:27 +00:00
|
|
|
include Events::Types
|
2021-07-23 09:54:07 +00:00
|
|
|
include DateRangeHelper
|
2020-08-25 17:34:02 +00:00
|
|
|
|
2021-10-20 12:44:56 +00:00
|
|
|
before_action :conversation, except: [:index, :meta, :search, :create, :filter]
|
2022-10-13 22:12:04 +00:00
|
|
|
before_action :inbox, :contact, :contact_inbox, only: [:create]
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def index
|
|
|
|
result = conversation_finder.perform
|
|
|
|
@conversations = result[:conversations]
|
|
|
|
@conversations_count = result[:count]
|
|
|
|
end
|
|
|
|
|
2020-05-03 06:45:10 +00:00
|
|
|
def meta
|
|
|
|
result = conversation_finder.perform
|
|
|
|
@conversations_count = result[:count]
|
|
|
|
end
|
|
|
|
|
2020-09-23 09:57:41 +00:00
|
|
|
def search
|
|
|
|
result = conversation_finder.perform
|
|
|
|
@conversations = result[:conversations]
|
|
|
|
@conversations_count = result[:count]
|
|
|
|
end
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
def create
|
2021-04-15 09:43:01 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@conversation = ::Conversation.create!(conversation_params)
|
|
|
|
Messages::MessageBuilder.new(Current.user, @conversation, params[:message]).perform if params[:message].present?
|
|
|
|
end
|
2020-04-10 11:12:37 +00:00
|
|
|
end
|
|
|
|
|
2020-03-08 16:38:25 +00:00
|
|
|
def show; end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2021-10-20 12:44:56 +00:00
|
|
|
def filter
|
2021-11-01 08:27:04 +00:00
|
|
|
result = ::Conversations::FilterService.new(params.permit!, current_user).perform
|
|
|
|
@conversations = result[:conversations]
|
|
|
|
@conversations_count = result[:count]
|
2021-10-20 12:44:56 +00:00
|
|
|
end
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
def mute
|
|
|
|
@conversation.mute!
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2020-10-08 06:32:08 +00:00
|
|
|
def unmute
|
|
|
|
@conversation.unmute!
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2020-08-17 05:55:13 +00:00
|
|
|
def transcript
|
2021-06-08 17:15:01 +00:00
|
|
|
render json: { error: 'email param missing' }, status: :unprocessable_entity and return if params[:email].blank?
|
|
|
|
|
|
|
|
ConversationReplyMailer.with(account: @conversation.account).conversation_transcript(@conversation, params[:email])&.deliver_later
|
2020-08-17 05:55:13 +00:00
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def toggle_status
|
2020-06-02 18:20:39 +00:00
|
|
|
if params[:status]
|
2021-07-23 09:54:07 +00:00
|
|
|
set_conversation_status
|
|
|
|
@status = @conversation.save!
|
2020-06-02 18:20:39 +00:00
|
|
|
else
|
|
|
|
@status = @conversation.toggle_status
|
|
|
|
end
|
2022-02-08 11:09:19 +00:00
|
|
|
assign_conversation if @conversation.status == 'open' && Current.user.is_a?(User) && Current.user&.agent?
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-05-03 06:47:27 +00:00
|
|
|
def toggle_typing_status
|
2020-09-08 05:54:08 +00:00
|
|
|
case params[:typing_status]
|
|
|
|
when 'on'
|
2021-11-01 08:20:07 +00:00
|
|
|
trigger_typing_event(CONVERSATION_TYPING_ON, params[:is_private])
|
2020-09-08 05:54:08 +00:00
|
|
|
when 'off'
|
2021-11-01 08:20:07 +00:00
|
|
|
trigger_typing_event(CONVERSATION_TYPING_OFF, params[:is_private])
|
2020-05-03 06:47:27 +00:00
|
|
|
end
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def update_last_seen
|
2021-11-30 05:15:36 +00:00
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
|
|
|
@conversation.update_column(:agent_last_seen_at, DateTime.now.utc)
|
|
|
|
@conversation.update_column(:assignee_last_seen_at, DateTime.now.utc) if assignee?
|
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2021-09-22 05:16:48 +00:00
|
|
|
def custom_attributes
|
|
|
|
@conversation.custom_attributes = params.permit(custom_attributes: {})[:custom_attributes]
|
|
|
|
@conversation.save!
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
private
|
|
|
|
|
2021-07-23 09:54:07 +00:00
|
|
|
def set_conversation_status
|
|
|
|
status = params[:status] == 'bot' ? 'pending' : params[:status]
|
|
|
|
@conversation.status = status
|
|
|
|
@conversation.snoozed_until = parse_date_time(params[:snoozed_until].to_s) if params[:snoozed_until]
|
|
|
|
end
|
|
|
|
|
2022-02-08 11:09:19 +00:00
|
|
|
def assign_conversation
|
|
|
|
@agent = Current.account.users.find(current_user.id)
|
|
|
|
@conversation.update_assignee(@agent)
|
|
|
|
end
|
|
|
|
|
2021-11-01 08:20:07 +00:00
|
|
|
def trigger_typing_event(event, is_private)
|
2020-05-04 17:37:56 +00:00
|
|
|
user = current_user.presence || @resource
|
2021-11-01 08:20:07 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(event, Time.zone.now, conversation: @conversation, user: user, is_private: is_private)
|
2020-05-04 17:37:56 +00:00
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
def conversation
|
2021-06-11 06:14:31 +00:00
|
|
|
@conversation ||= Current.account.conversations.find_by!(display_id: params[:id])
|
|
|
|
authorize @conversation.inbox, :show?
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2022-10-13 22:12:04 +00:00
|
|
|
def inbox
|
|
|
|
return if params[:inbox_id].blank?
|
|
|
|
|
|
|
|
@inbox = Current.account.inboxes.find(params[:inbox_id])
|
|
|
|
authorize @inbox, :show?
|
|
|
|
end
|
|
|
|
|
|
|
|
def contact
|
|
|
|
return if params[:contact_id].blank?
|
|
|
|
|
|
|
|
@contact = Current.account.contacts.find(params[:contact_id])
|
|
|
|
end
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
def contact_inbox
|
2021-04-15 09:43:01 +00:00
|
|
|
@contact_inbox = build_contact_inbox
|
|
|
|
|
2022-10-13 22:12:04 +00:00
|
|
|
# fallback for the old case where we do look up only using source id
|
|
|
|
# In future we need to change this and make sure we do look up on combination of inbox_id and source_id
|
|
|
|
# and deprecate the support of passing only source_id as the param
|
2020-04-10 11:12:37 +00:00
|
|
|
@contact_inbox ||= ::ContactInbox.find_by!(source_id: params[:source_id])
|
2021-06-11 06:14:31 +00:00
|
|
|
authorize @contact_inbox.inbox, :show?
|
2020-04-10 11:12:37 +00:00
|
|
|
end
|
|
|
|
|
2021-04-15 09:43:01 +00:00
|
|
|
def build_contact_inbox
|
2022-10-13 22:12:04 +00:00
|
|
|
return if @inbox.blank? || @contact.blank?
|
2021-06-11 06:14:31 +00:00
|
|
|
|
2021-04-15 09:43:01 +00:00
|
|
|
ContactInboxBuilder.new(
|
2022-10-13 22:12:04 +00:00
|
|
|
contact: @contact,
|
|
|
|
inbox: @inbox,
|
2021-04-15 09:43:01 +00:00
|
|
|
source_id: params[:source_id]
|
|
|
|
).perform
|
|
|
|
end
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
def conversation_params
|
2021-04-15 09:43:01 +00:00
|
|
|
additional_attributes = params[:additional_attributes]&.permit! || {}
|
2021-09-22 05:16:48 +00:00
|
|
|
custom_attributes = params[:custom_attributes]&.permit! || {}
|
2021-06-07 14:34:31 +00:00
|
|
|
status = params[:status].present? ? { status: params[:status] } : {}
|
2021-07-21 16:32:43 +00:00
|
|
|
|
|
|
|
# TODO: temporary fallback for the old bot status in conversation, we will remove after couple of releases
|
|
|
|
status = { status: 'pending' } if status[:status] == 'bot'
|
2020-04-10 11:12:37 +00:00
|
|
|
{
|
2020-06-07 08:28:05 +00:00
|
|
|
account_id: Current.account.id,
|
2020-04-10 11:12:37 +00:00
|
|
|
inbox_id: @contact_inbox.inbox_id,
|
|
|
|
contact_id: @contact_inbox.contact_id,
|
2021-01-07 08:17:38 +00:00
|
|
|
contact_inbox_id: @contact_inbox.id,
|
2021-07-23 09:54:07 +00:00
|
|
|
additional_attributes: additional_attributes,
|
2021-09-22 05:16:48 +00:00
|
|
|
custom_attributes: custom_attributes,
|
2021-10-11 09:47:30 +00:00
|
|
|
snoozed_until: params[:snoozed_until],
|
|
|
|
assignee_id: params[:assignee_id],
|
|
|
|
team_id: params[:team_id]
|
2021-06-07 14:34:31 +00:00
|
|
|
}.merge(status)
|
2020-04-10 11:12:37 +00:00
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def conversation_finder
|
|
|
|
@conversation_finder ||= ConversationFinder.new(current_user, params)
|
|
|
|
end
|
2021-09-23 15:29:10 +00:00
|
|
|
|
|
|
|
def assignee?
|
|
|
|
@conversation.assignee_id? && current_user == @conversation.assignee
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|