feat: Add AgentBot APIs (#2323)

This commit is contained in:
Sojan Jose
2021-06-01 22:34:25 +05:30
committed by GitHub
parent 9d16e52e33
commit 22965be6dc
31 changed files with 552 additions and 67 deletions

View File

@@ -0,0 +1,35 @@
class Api::V1::Accounts::AgentBotsController < Api::V1::Accounts::BaseController
before_action :current_account
before_action :check_authorization
before_action :agent_bot, except: [:index, :create]
def index
@agent_bots = AgentBot.where(account_id: [nil, Current.account.id])
end
def show; end
def create
@agent_bot = Current.account.agent_bots.create!(permitted_params)
end
def update
@agent_bot.update!(permitted_params)
end
def destroy
@agent_bot.destroy
head :ok
end
private
def agent_bot
@agent_bot = AgentBot.where(account_id: [nil, Current.account.id]).find(params[:id]) if params[:action] == 'show'
@agent_bot ||= Current.account.agent_bots.find(params[:id])
end
def permitted_params
params.permit(:name, :description, :outgoing_url)
end
end

View File

@@ -38,6 +38,10 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
update_channel_feature_flags
end
def agent_bot
@agent_bot = @inbox.agent_bot
end
def set_agent_bot
if @agent_bot
agent_bot_inbox = @inbox.agent_bot_inbox || AgentBotInbox.new(inbox: @inbox)

View File

@@ -1,7 +0,0 @@
class Api::V1::AgentBotsController < Api::BaseController
skip_before_action :authenticate_user!
def index
render json: AgentBot.all
end
end

View File

@@ -0,0 +1,35 @@
class Platform::Api::V1::AgentBotsController < PlatformController
before_action :set_resource, except: [:index, :create]
before_action :validate_platform_app_permissible, except: [:index, :create]
def index
@resources = @platform_app.platform_app_permissibles.where(permissible_type: 'AgentBot').all
end
def create
@resource = AgentBot.new(agent_bot_params)
@resource.save!
@platform_app.platform_app_permissibles.find_or_create_by(permissible: @resource)
end
def show; end
def update
@resource.update!(agent_bot_params)
end
def destroy
@resource.destroy!
head :ok
end
private
def set_resource
@resource = AgentBot.find(params[:id])
end
def agent_bot_params
params.permit(:name, :description, :account_id, :outgoing_url)
end
end

View File

@@ -15,8 +15,7 @@ class AgentBotDashboard < Administrate::BaseDashboard
description: Field::String,
outgoing_url: Field::String,
created_at: Field::DateTime,
updated_at: Field::DateTime,
hide_input_for_bot_conversations: Field::Boolean
updated_at: Field::DateTime
}.freeze
# COLLECTION_ATTRIBUTES
@@ -39,7 +38,6 @@ class AgentBotDashboard < Administrate::BaseDashboard
name
description
outgoing_url
hide_input_for_bot_conversations
].freeze
# FORM_ATTRIBUTES
@@ -49,7 +47,6 @@ class AgentBotDashboard < Administrate::BaseDashboard
name
description
outgoing_url
hide_input_for_bot_conversations
].freeze
# COLLECTION_FILTERS

View File

@@ -1,8 +1,5 @@
export default {
computed: {
hideInputForBotConversations() {
return window.chatwootWebChannel.hideInputForBotConversations;
},
useInboxAvatarForBot() {
return window.chatwootWidgetDefaults.useInboxAvatarForBot;
},

View File

@@ -3,7 +3,6 @@ import configMixin from '../configMixin';
import Vue from 'vue';
global.chatwootWebChannel = {
hideInputForBotConversations: true,
avatarUrl: 'https://test.url',
hasAConnectedAgentBot: 'AgentBot',
enabledFeatures: ['emoji_picker', 'attachments'],
@@ -25,12 +24,10 @@ describe('configMixin', () => {
const wrapper = createWrapper(vm);
expect(wrapper.vm.hasEmojiPickerEnabled).toBe(true);
expect(wrapper.vm.hasAttachmentsEnabled).toBe(true);
expect(wrapper.vm.hideInputForBotConversations).toBe(true);
expect(wrapper.vm.hasAConnectedAgentBot).toBe(true);
expect(wrapper.vm.useInboxAvatarForBot).toBe(true);
expect(wrapper.vm.inboxAvatarUrl).toBe('https://test.url');
expect(wrapper.vm.channelConfig).toEqual({
hideInputForBotConversations: true,
avatarUrl: 'https://test.url',
hasAConnectedAgentBot: 'AgentBot',
enabledFeatures: ['emoji_picker', 'attachments'],

View File

@@ -62,10 +62,7 @@
leave-class="opacity-100 transform translate-y-0"
leave-to-class="opacity-0 transform "
>
<div
v-if="showInputTextArea && currentView === 'messageView'"
class="input-wrap"
>
<div v-if="currentView === 'messageView'" class="input-wrap">
<chat-footer />
</div>
<team-availability
@@ -146,15 +143,6 @@ export default {
fileUploadSizeLimit() {
return MAXIMUM_FILE_UPLOAD_SIZE;
},
showInputTextArea() {
if (this.hideInputForBotConversations) {
if (this.isOpen) {
return true;
}
return false;
}
return true;
},
isHeaderCollapsed() {
if (!this.hasIntroText || this.conversationSize) {
return true;

View File

@@ -33,6 +33,7 @@ class Account < ApplicationRecord
has_many :account_users, dependent: :destroy
has_many :agent_bot_inboxes, dependent: :destroy
has_many :agent_bots, dependent: :destroy
has_many :data_imports, dependent: :destroy
has_many :users, through: :account_users
has_many :inboxes, dependent: :destroy

View File

@@ -2,13 +2,21 @@
#
# Table name: agent_bots
#
# id :bigint not null, primary key
# description :string
# hide_input_for_bot_conversations :boolean default(FALSE)
# name :string
# outgoing_url :string
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# description :string
# name :string
# outgoing_url :string
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint
#
# Indexes
#
# index_agent_bots_on_account_id (account_id)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id)
#
class AgentBot < ApplicationRecord
@@ -18,6 +26,7 @@ class AgentBot < ApplicationRecord
has_many :agent_bot_inboxes, dependent: :destroy
has_many :inboxes, through: :agent_bot_inboxes
has_many :messages, as: :sender, dependent: :restrict_with_exception
belongs_to :account, dependent: :destroy, optional: true
def available_name
name

View File

@@ -0,0 +1,21 @@
class AgentBotPolicy < ApplicationPolicy
def index?
@account_user.administrator? || @account_user.agent?
end
def update?
@account_user.administrator?
end
def show?
@account_user.administrator? || @account_user.agent?
end
def create?
@account_user.administrator?
end
def destroy?
@account_user.administrator?
end
end

View File

@@ -27,6 +27,10 @@ class InboxPolicy < ApplicationPolicy
true
end
def agent_bot?
true
end
def campaigns?
@account_user.administrator?
end

View File

@@ -0,0 +1 @@
json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: @agent_bot

View File

@@ -0,0 +1,3 @@
json.array! @agent_bots do |agent_bot|
json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: agent_bot
end

View File

@@ -0,0 +1 @@
json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: @agent_bot

View File

@@ -0,0 +1 @@
json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: @agent_bot

View File

@@ -0,0 +1,3 @@
json.agent_bot do
json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: @agent_bot if @agent_bot.present?
end

View File

@@ -0,0 +1,5 @@
json.id resource.id
json.name resource.name
json.description resource.description
json.outgoing_url resource.name
json.account_id resource.account_id

View File

@@ -0,0 +1 @@
json.partial! 'platform/api/v1/models/agent_bot.json.jbuilder', resource: @resource

View File

@@ -0,0 +1,3 @@
json.array! @resources do |resource|
json.partial! 'platform/api/v1/models/agent_bot.json.jbuilder', resource: resource.permissible
end

View File

@@ -0,0 +1 @@
json.partial! 'platform/api/v1/models/agent_bot.json.jbuilder', resource: @resource

View File

@@ -0,0 +1 @@
json.partial! 'platform/api/v1/models/agent_bot.json.jbuilder', resource: @resource

View File

@@ -0,0 +1,5 @@
json.id resource.id
json.name resource.name
json.description resource.description
json.outgoing_url resource.name
json.account_id resource.account_id

View File

@@ -8,9 +8,6 @@
window.chatwootWebChannel = {
avatarUrl: '<%= @web_widget.inbox.avatar_url %>',
hasAConnectedAgentBot: '<%= @web_widget.inbox.agent_bot&.name %>',
<% if @web_widget.inbox.agent_bot %>
hideInputForBotConversations: <%= @web_widget.inbox.agent_bot.hide_input_for_bot_conversations %>,
<% end %>
locale: '<%= @web_widget.account.locale %>',
websiteName: '<%= @web_widget.inbox.name %>',
websiteToken: '<%= @web_widget.website_token %>',