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]
|
2020-05-26 17:08:48 +00:00
|
|
|
before_action :check_authorization
|
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
|
|
|
|
|
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(
|
|
|
|
name: permitted_params[:name],
|
|
|
|
greeting_message: permitted_params[:greeting_message],
|
|
|
|
greeting_enabled: permitted_params[:greeting_enabled],
|
|
|
|
channel: channel
|
|
|
|
)
|
2020-04-19 18:10:28 +00:00
|
|
|
@inbox.avatar.attach(permitted_params[:avatar])
|
|
|
|
@inbox.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-16 07:02:34 +00:00
|
|
|
def update
|
2020-04-19 18:10:28 +00:00
|
|
|
@inbox.update(inbox_update_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
|
|
|
return unless @inbox.channel.is_a?(Channel::WebWidget) && inbox_update_params[:channel].present?
|
|
|
|
|
|
|
|
@inbox.channel.update!(inbox_update_params[:channel])
|
|
|
|
update_channel_feature_flags
|
2020-03-16 07:02:34 +00:00
|
|
|
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])
|
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
|
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
def create_channel
|
|
|
|
case permitted_params[:channel][:type]
|
|
|
|
when 'web_widget'
|
|
|
|
Current.account.web_widgets.create!(permitted_params[:channel].except(:type))
|
|
|
|
when 'api'
|
|
|
|
Current.account.api_channels.create!(permitted_params[:channel].except(:type))
|
|
|
|
when 'email'
|
|
|
|
Current.account.email_channels.create!(permitted_params[:channel].except(:type))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-05 12:16:17 +00:00
|
|
|
def update_channel_feature_flags
|
|
|
|
return unless inbox_update_params[:channel].key? :selected_feature_flags
|
|
|
|
|
|
|
|
@inbox.channel.selected_feature_flags = inbox_update_params[:channel][:selected_feature_flags]
|
|
|
|
@inbox.channel.save!
|
|
|
|
end
|
|
|
|
|
2020-04-19 18:10:28 +00:00
|
|
|
def permitted_params
|
2020-06-09 18:24:35 +00:00
|
|
|
params.permit(:id, :avatar, :name, :greeting_message, :greeting_enabled, channel:
|
2020-10-18 18:02:22 +00:00
|
|
|
[:type, :website_url, :widget_color, :welcome_title, :welcome_tagline, :webhook_url, :email, :reply_time])
|
2020-04-19 18:10:28 +00:00
|
|
|
end
|
|
|
|
|
2020-02-19 09:10:03 +00:00
|
|
|
def inbox_update_params
|
2020-06-09 18:24:35 +00:00
|
|
|
params.permit(:enable_auto_assignment, :name, :avatar, :greeting_message, :greeting_enabled,
|
2021-02-23 06:41:15 +00:00
|
|
|
:working_hours_enabled, :out_of_office_message, :timezone,
|
2020-08-05 12:16:17 +00:00
|
|
|
channel: [
|
|
|
|
:website_url,
|
|
|
|
:widget_color,
|
|
|
|
:welcome_title,
|
|
|
|
:welcome_tagline,
|
|
|
|
:webhook_url,
|
|
|
|
:email,
|
2020-10-18 18:02:22 +00:00
|
|
|
:reply_time,
|
2021-02-15 19:28:57 +00:00
|
|
|
:pre_chat_form_enabled,
|
|
|
|
{ pre_chat_form_options: [:pre_chat_message, :require_email] },
|
2020-09-08 05:54:08 +00:00
|
|
|
{ selected_feature_flags: [] }
|
2020-08-05 12:16:17 +00:00
|
|
|
])
|
2020-02-19 09:10:03 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|