chore: Adds API for agent bot avatar upload (#8533)

Adds API for agent bot avatar upload
- accounts/agent_bot
- platform/agent_bot
This commit is contained in:
Sojan Jose
2023-12-11 19:02:11 -08:00
committed by GitHub
parent 890515edfd
commit 376de685fb
9 changed files with 138 additions and 11 deletions

View File

@@ -10,11 +10,18 @@ class Api::V1::Accounts::AgentBotsController < Api::V1::Accounts::BaseController
def show; end def show; end
def create def create
@agent_bot = Current.account.agent_bots.create!(permitted_params) @agent_bot = Current.account.agent_bots.create!(permitted_params.except(:avatar_url))
process_avatar_from_url
end end
def update def update
@agent_bot.update!(permitted_params) @agent_bot.update!(permitted_params.except(:avatar_url))
process_avatar_from_url
end
def avatar
@agent_bot.avatar.purge if @agent_bot.avatar.attached?
@agent_bot
end end
def destroy def destroy
@@ -30,6 +37,10 @@ class Api::V1::Accounts::AgentBotsController < Api::V1::Accounts::BaseController
end end
def permitted_params def permitted_params
params.permit(:name, :description, :outgoing_url, :bot_type, bot_config: [:csml_content]) params.permit(:name, :description, :outgoing_url, :avatar, :avatar_url, :bot_type, bot_config: [:csml_content])
end
def process_avatar_from_url
::Avatar::AvatarFromUrlJob.perform_later(@agent_bot, params[:avatar_url]) if params[:avatar_url].present?
end end
end end

View File

@@ -83,14 +83,14 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
@contact = Current.account.contacts.new(permitted_params.except(:avatar_url)) @contact = Current.account.contacts.new(permitted_params.except(:avatar_url))
@contact.save! @contact.save!
@contact_inbox = build_contact_inbox @contact_inbox = build_contact_inbox
process_avatar process_avatar_from_url
end end
end end
def update def update
@contact.assign_attributes(contact_update_params) @contact.assign_attributes(contact_update_params)
@contact.save! @contact.save!
process_avatar if permitted_params[:avatar].present? || permitted_params[:avatar_url].present? process_avatar_from_url
end end
def destroy def destroy
@@ -174,7 +174,7 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
@contact = Current.account.contacts.includes(contact_inboxes: [:inbox]).find(params[:id]) @contact = Current.account.contacts.includes(contact_inboxes: [:inbox]).find(params[:id])
end end
def process_avatar def process_avatar_from_url
::Avatar::AvatarFromUrlJob.perform_later(@contact, params[:avatar_url]) if params[:avatar_url].present? ::Avatar::AvatarFromUrlJob.perform_later(@contact, params[:avatar_url]) if params[:avatar_url].present?
end end

View File

@@ -9,13 +9,15 @@ class Platform::Api::V1::AgentBotsController < PlatformController
def show; end def show; end
def create def create
@resource = AgentBot.new(agent_bot_params) @resource = AgentBot.new(agent_bot_params.except(:avatar_url))
@resource.save! @resource.save!
process_avatar_from_url
@platform_app.platform_app_permissibles.find_or_create_by(permissible: @resource) @platform_app.platform_app_permissibles.find_or_create_by(permissible: @resource)
end end
def update def update
@resource.update!(agent_bot_params) @resource.update!(agent_bot_params.except(:avatar_url))
process_avatar_from_url
end end
def destroy def destroy
@@ -23,6 +25,11 @@ class Platform::Api::V1::AgentBotsController < PlatformController
head :ok head :ok
end end
def avatar
@resource.avatar.purge if @resource.avatar.attached?
@resource
end
private private
def set_resource def set_resource
@@ -30,6 +37,10 @@ class Platform::Api::V1::AgentBotsController < PlatformController
end end
def agent_bot_params def agent_bot_params
params.permit(:name, :description, :account_id, :outgoing_url) params.permit(:name, :description, :account_id, :outgoing_url, :avatar, :avatar_url)
end
def process_avatar_from_url
::Avatar::AvatarFromUrlJob.perform_later(@resource, params[:avatar_url]) if params[:avatar_url].present?
end end
end end

View File

@@ -18,4 +18,8 @@ class AgentBotPolicy < ApplicationPolicy
def destroy? def destroy?
@account_user.administrator? @account_user.administrator?
end end
def avatar?
@account_user.administrator?
end
end end

View File

@@ -0,0 +1 @@
json.partial! 'api/v1/models/agent_bot', formats: [:json], resource: AgentBotPresenter.new(@agent_bot)

View File

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

View File

@@ -45,7 +45,9 @@ Rails.application.routes.draw do
end end
resource :bulk_actions, only: [:create] resource :bulk_actions, only: [:create]
resources :agents, only: [:index, :create, :update, :destroy] resources :agents, only: [:index, :create, :update, :destroy]
resources :agent_bots, only: [:index, :create, :show, :update, :destroy] resources :agent_bots, only: [:index, :create, :show, :update, :destroy] do
delete :avatar, on: :member
end
resources :contact_inboxes, only: [] do resources :contact_inboxes, only: [] do
collection do collection do
post :filter post :filter
@@ -329,7 +331,9 @@ Rails.application.routes.draw do
get :login get :login
end end
end end
resources :agent_bots, only: [:index, :create, :show, :update, :destroy] resources :agent_bots, only: [:index, :create, :show, :update, :destroy] do
delete :avatar, on: :member
end
resources :accounts, only: [:create, :show, :update, :destroy] do resources :accounts, only: [:create, :show, :update, :destroy] do
resources :account_users, only: [:index, :create] do resources :account_users, only: [:index, :create] do
collection do collection do

View File

@@ -141,6 +141,28 @@ RSpec.describe 'Agent Bot API', type: :request do
expect(agent_bot.reload.name).not_to eq('test_updated') expect(agent_bot.reload.name).not_to eq('test_updated')
expect(response.body).not_to include(global_bot.access_token.token) expect(response.body).not_to include(global_bot.access_token.token)
end end
it 'updates avatar' do
# no avatar before upload
expect(agent_bot.avatar.attached?).to be(false)
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
patch "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}",
headers: admin.create_new_auth_token,
params: valid_params.merge(avatar: file)
expect(response).to have_http_status(:success)
agent_bot.reload
expect(agent_bot.avatar.attached?).to be(true)
end
it 'updated avatar with avatar_url' do
patch "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}",
headers: admin.create_new_auth_token,
params: valid_params.merge(avatar_url: 'http://example.com/avatar.png'),
as: :json
expect(response).to have_http_status(:success)
expect(Avatar::AvatarFromUrlJob).to have_been_enqueued.with(agent_bot, 'http://example.com/avatar.png')
end
end end
end end
@@ -183,4 +205,29 @@ RSpec.describe 'Agent Bot API', type: :request do
end end
end end
end end
describe 'DELETE /api/v1/accounts/{account.id}/agent_bots/:id/avatar' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
before do
agent_bot.avatar.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
end
it 'delete agent_bot avatar' do
delete "/api/v1/accounts/#{account.id}/agent_bots/#{agent_bot.id}/avatar",
headers: admin.create_new_auth_token,
as: :json
expect { agent_bot.avatar.attachment.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect(response).to have_http_status(:success)
end
end
end
end end

View File

@@ -140,6 +140,26 @@ RSpec.describe 'Platform Agent Bot API', type: :request do
data = response.parsed_body data = response.parsed_body
expect(data['name']).to eq('test123') expect(data['name']).to eq('test123')
end end
it 'updates avatar' do
# no avatar before upload
create(:platform_app_permissible, platform_app: platform_app, permissible: agent_bot)
expect(agent_bot.avatar.attached?).to be(false)
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
patch "/platform/api/v1/agent_bots/#{agent_bot.id}", params: { name: 'test123' }.merge(avatar: file),
headers: { api_access_token: platform_app.access_token.token }
expect(response).to have_http_status(:success)
agent_bot.reload
expect(agent_bot.avatar.attached?).to be(true)
end
it 'updated avatar with avatar_url' do
create(:platform_app_permissible, platform_app: platform_app, permissible: agent_bot)
patch "/platform/api/v1/agent_bots/#{agent_bot.id}", params: { name: 'test123' }.merge(avatar_url: 'http://example.com/avatar.png'),
headers: { api_access_token: platform_app.access_token.token }
expect(response).to have_http_status(:success)
expect(Avatar::AvatarFromUrlJob).to have_been_enqueued.with(agent_bot, 'http://example.com/avatar.png')
end
end end
end end
@@ -169,4 +189,32 @@ RSpec.describe 'Platform Agent Bot API', type: :request do
end end
end end
end end
describe 'DELETE /platform/api/v1/agent_bots/{agent_bot_id}/avatar' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/platform/api/v1/agent_bots/#{agent_bot.id}/avatar"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:platform_app) { create(:platform_app) }
before do
agent_bot.avatar.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
create(:platform_app_permissible, platform_app: platform_app, permissible: agent_bot)
end
it 'delete agent_bot avatar' do
delete "/platform/api/v1/agent_bots/#{agent_bot.id}/avatar",
headers: { api_access_token: platform_app.access_token.token },
as: :json
expect { agent_bot.avatar.attachment.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect(response).to have_http_status(:success)
end
end
end
end end