2020-06-07 08:28:05 +00:00
|
|
|
class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
|
2021-11-19 06:22:27 +00:00
|
|
|
include Api::V1::InboxesHelper
|
2020-04-19 18:10:28 +00:00
|
|
|
before_action :fetch_inbox, except: [:index, :create]
|
2020-04-07 05:11:18 +00:00
|
|
|
before_action :fetch_agent_bot, only: [:set_agent_bot]
|
2022-02-14 10:25:08 +00:00
|
|
|
before_action :validate_limit, only: [:create]
|
2021-09-04 12:26:46 +00:00
|
|
|
# we are already handling the authorization in fetch inbox
|
|
|
|
before_action :check_authorization, except: [:show]
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def index
|
2020-11-10 09:55:26 +00:00
|
|
|
@inboxes = policy_scope(Current.account.inboxes.order_by_name.includes(:channel, { avatar_attachment: [:blob] }))
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2021-09-04 12:26:46 +00:00
|
|
|
def show; end
|
|
|
|
|
2022-05-23 13:54:07 +00:00
|
|
|
# Deprecated: This API will be removed in 2.7.0
|
2021-04-20 08:16:20 +00:00
|
|
|
def assignable_agents
|
|
|
|
@assignable_agents = (Current.account.users.where(id: @inbox.members.select(:user_id)) + Current.account.administrators).uniq
|
|
|
|
end
|
|
|
|
|
2021-04-29 16:53:32 +00:00
|
|
|
def campaigns
|
|
|
|
@campaigns = @inbox.campaigns
|
|
|
|
end
|
|
|
|
|
2021-08-31 09:42:05 +00:00
|
|
|
def avatar
|
|
|
|
@inbox.avatar.attachment.destroy! if @inbox.avatar.attached?
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2020-04-19 18:10:28 +00:00
|
|
|
def create
|
|
|
|
ActiveRecord::Base.transaction do
|
2020-07-21 06:45:24 +00:00
|
|
|
channel = create_channel
|
2020-06-09 18:24:35 +00:00
|
|
|
@inbox = Current.account.inboxes.build(
|
2021-09-09 18:30:52 +00:00
|
|
|
{
|
|
|
|
name: inbox_name(channel),
|
|
|
|
channel: channel
|
|
|
|
}.merge(
|
|
|
|
permitted_params.except(:channel)
|
|
|
|
)
|
2020-06-09 18:24:35 +00:00
|
|
|
)
|
2020-04-19 18:10:28 +00:00
|
|
|
@inbox.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-16 07:02:34 +00:00
|
|
|
def update
|
2022-06-13 14:48:38 +00:00
|
|
|
@inbox.update!(permitted_params.except(:channel))
|
2021-02-23 06:41:15 +00:00
|
|
|
@inbox.update_working_hours(params.permit(working_hours: Inbox::OFFISABLE_ATTRS)[:working_hours]) if params[:working_hours]
|
2021-09-09 18:30:52 +00:00
|
|
|
channel_attributes = get_channel_attributes(@inbox.channel_type)
|
2021-10-28 09:56:20 +00:00
|
|
|
|
|
|
|
# Inbox update doesn't necessarily need channel attributes
|
|
|
|
return if permitted_params(channel_attributes)[:channel].blank?
|
|
|
|
|
2022-03-22 06:44:17 +00:00
|
|
|
if @inbox.inbox_type == 'Email'
|
|
|
|
validate_email_channel(channel_attributes)
|
|
|
|
@inbox.channel.reauthorized!
|
|
|
|
end
|
2022-03-15 11:24:33 +00:00
|
|
|
|
2021-10-28 09:56:20 +00:00
|
|
|
@inbox.channel.update!(permitted_params(channel_attributes)[:channel])
|
2020-08-05 12:16:17 +00:00
|
|
|
update_channel_feature_flags
|
2020-03-16 07:02:34 +00:00
|
|
|
end
|
|
|
|
|
2021-06-01 17:04:25 +00:00
|
|
|
def agent_bot
|
|
|
|
@agent_bot = @inbox.agent_bot
|
|
|
|
end
|
|
|
|
|
2020-04-07 05:11:18 +00:00
|
|
|
def set_agent_bot
|
|
|
|
if @agent_bot
|
|
|
|
agent_bot_inbox = @inbox.agent_bot_inbox || AgentBotInbox.new(inbox: @inbox)
|
|
|
|
agent_bot_inbox.agent_bot = @agent_bot
|
|
|
|
agent_bot_inbox.save!
|
|
|
|
elsif @inbox.agent_bot_inbox.present?
|
|
|
|
@inbox.agent_bot_inbox.destroy!
|
|
|
|
end
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def destroy
|
2022-03-24 07:58:25 +00:00
|
|
|
@inbox.destroy!
|
2019-08-14 09:48:44 +00:00
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_inbox
|
2020-06-07 08:28:05 +00:00
|
|
|
@inbox = Current.account.inboxes.find(params[:id])
|
2021-06-11 06:14:31 +00:00
|
|
|
authorize @inbox, :show?
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-04-07 05:11:18 +00:00
|
|
|
def fetch_agent_bot
|
|
|
|
@agent_bot = AgentBot.find(params[:agent_bot]) if params[:agent_bot]
|
|
|
|
end
|
|
|
|
|
2021-09-09 18:30:52 +00:00
|
|
|
def inbox_name(channel)
|
|
|
|
return channel.try(:bot_name) if channel.is_a?(Channel::Telegram)
|
|
|
|
|
|
|
|
permitted_params[:name]
|
|
|
|
end
|
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
def create_channel
|
2022-02-03 23:22:13 +00:00
|
|
|
return unless %w[web_widget api email line telegram whatsapp sms].include?(permitted_params[:channel][:type])
|
|
|
|
|
|
|
|
account_channels_method.create!(permitted_params(channel_type_from_params::EDITABLE_ATTRS)[:channel].except(:type))
|
2020-07-21 06:45:24 +00:00
|
|
|
end
|
|
|
|
|
2020-08-05 12:16:17 +00:00
|
|
|
def update_channel_feature_flags
|
2021-09-14 17:14:53 +00:00
|
|
|
return unless @inbox.web_widget?
|
2021-09-09 18:30:52 +00:00
|
|
|
return unless permitted_params(Channel::WebWidget::EDITABLE_ATTRS)[:channel].key? :selected_feature_flags
|
2020-08-05 12:16:17 +00:00
|
|
|
|
2021-09-09 18:30:52 +00:00
|
|
|
@inbox.channel.selected_feature_flags = permitted_params(Channel::WebWidget::EDITABLE_ATTRS)[:channel][:selected_feature_flags]
|
2020-08-05 12:16:17 +00:00
|
|
|
@inbox.channel.save!
|
|
|
|
end
|
|
|
|
|
2022-06-13 14:48:38 +00:00
|
|
|
def inbox_attributes
|
|
|
|
[:name, :avatar, :greeting_enabled, :greeting_message, :enable_email_collect, :csat_survey_enabled,
|
|
|
|
:enable_auto_assignment, :working_hours_enabled, :out_of_office_message, :timezone, :allow_messages_after_resolved]
|
|
|
|
end
|
|
|
|
|
2021-09-09 18:30:52 +00:00
|
|
|
def permitted_params(channel_attributes = [])
|
|
|
|
params.permit(
|
2022-06-13 14:48:38 +00:00
|
|
|
*inbox_attributes,
|
2021-09-09 18:30:52 +00:00
|
|
|
channel: [:type, *channel_attributes]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-02-03 23:22:13 +00:00
|
|
|
def channel_type_from_params
|
|
|
|
{
|
|
|
|
'web_widget' => Channel::WebWidget,
|
|
|
|
'api' => Channel::Api,
|
|
|
|
'email' => Channel::Email,
|
|
|
|
'line' => Channel::Line,
|
|
|
|
'telegram' => Channel::Telegram,
|
|
|
|
'whatsapp' => Channel::Whatsapp,
|
|
|
|
'sms' => Channel::Sms
|
|
|
|
}[permitted_params[:channel][:type]]
|
|
|
|
end
|
|
|
|
|
2021-09-09 18:30:52 +00:00
|
|
|
def get_channel_attributes(channel_type)
|
2022-02-22 10:36:04 +00:00
|
|
|
if channel_type.constantize.const_defined?(:EDITABLE_ATTRS)
|
2021-12-20 14:08:38 +00:00
|
|
|
channel_type.constantize::EDITABLE_ATTRS.presence
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2020-02-19 09:10:03 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2022-06-13 14:48:38 +00:00
|
|
|
|
|
|
|
Api::V1::Accounts::InboxesController.prepend_mod_with('Api::V1::Accounts::InboxesController')
|