2020-06-07 08:28:05 +00:00
|
|
|
class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
|
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]
|
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
|
|
|
|
|
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
|
2021-09-09 18:30:52 +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]
|
2020-08-05 12:16:17 +00:00
|
|
|
|
2021-09-09 18:30:52 +00:00
|
|
|
channel_attributes = get_channel_attributes(@inbox.channel_type)
|
|
|
|
@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
|
|
|
|
@inbox.destroy
|
|
|
|
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
|
|
|
|
case permitted_params[:channel][:type]
|
|
|
|
when 'web_widget'
|
2021-09-09 18:30:52 +00:00
|
|
|
Current.account.web_widgets.create!(permitted_params(Channel::WebWidget::EDITABLE_ATTRS)[:channel].except(:type))
|
2020-07-21 06:45:24 +00:00
|
|
|
when 'api'
|
2021-09-09 18:30:52 +00:00
|
|
|
Current.account.api_channels.create!(permitted_params(Channel::Api::EDITABLE_ATTRS)[:channel].except(:type))
|
2020-07-21 06:45:24 +00:00
|
|
|
when 'email'
|
2021-09-09 18:30:52 +00:00
|
|
|
Current.account.email_channels.create!(permitted_params(Channel::Email::EDITABLE_ATTRS)[:channel].except(:type))
|
2021-09-10 20:01:17 +00:00
|
|
|
when 'line'
|
|
|
|
Current.account.line_channels.create!(permitted_params(Channel::Line::EDITABLE_ATTRS)[:channel].except(:type))
|
2021-09-09 18:30:52 +00:00
|
|
|
when 'telegram'
|
|
|
|
Current.account.telegram_channels.create!(permitted_params(Channel::Telegram::EDITABLE_ATTRS)[:channel].except(:type))
|
2020-07-21 06:45:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-05 12:16:17 +00:00
|
|
|
def update_channel_feature_flags
|
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
|
|
|
|
|
2021-09-09 18:30:52 +00:00
|
|
|
def permitted_params(channel_attributes = [])
|
|
|
|
params.permit(
|
|
|
|
:name, :avatar, :greeting_enabled, :greeting_message, :enable_email_collect, :csat_survey_enabled,
|
|
|
|
:enable_auto_assignment, :working_hours_enabled, :out_of_office_message, :timezone,
|
|
|
|
channel: [:type, *channel_attributes]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_channel_attributes(channel_type)
|
|
|
|
case channel_type
|
|
|
|
when 'Channel::WebWidget'
|
|
|
|
Channel::WebWidget::EDITABLE_ATTRS
|
|
|
|
when 'Channel::Api'
|
|
|
|
Channel::Api::EDITABLE_ATTRS
|
|
|
|
when 'Channel::Email'
|
|
|
|
Channel::Email::EDITABLE_ATTRS
|
|
|
|
when 'Channel::Telegram'
|
|
|
|
Channel::Telegram::EDITABLE_ATTRS
|
2021-09-10 20:01:17 +00:00
|
|
|
when 'Channel::Line'
|
|
|
|
Channel::Line::EDITABLE_ATTRS
|
2021-09-09 18:30:52 +00:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2020-02-19 09:10:03 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|