diff --git a/.rubocop.yml b/.rubocop.yml index b1c69ec70..8f8473547 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -57,7 +57,7 @@ Metrics/BlockLength: - db/schema.rb Metrics/ModuleLength: Exclude: - - lib/woot_message_seeder.rb + - lib/seeders/message_seeder.rb Rails/ApplicationController: Exclude: - 'app/controllers/api/v1/widget/messages_controller.rb' diff --git a/app/controllers/api/v1/accounts/canned_responses_controller.rb b/app/controllers/api/v1/accounts/canned_responses_controller.rb index 16e16e4a6..bbfa9c4b7 100644 --- a/app/controllers/api/v1/accounts/canned_responses_controller.rb +++ b/app/controllers/api/v1/accounts/canned_responses_controller.rb @@ -33,7 +33,7 @@ class Api::V1::Accounts::CannedResponsesController < Api::V1::Accounts::BaseCont def canned_responses if params[:search] - Current.account.canned_responses.where('short_code ILIKE ?', "#{params[:search]}%") + Current.account.canned_responses.where('short_code ILIKE :search OR content ILIKE :search', search: "%#{params[:search]}%") else Current.account.canned_responses end diff --git a/app/presenters/agent_bot_presenter.rb b/app/presenters/agent_bot_presenter.rb new file mode 100644 index 000000000..2fa61f59c --- /dev/null +++ b/app/presenters/agent_bot_presenter.rb @@ -0,0 +1,7 @@ +class AgentBotPresenter < SimpleDelegator + def access_token + return if account_id.blank? + + Current.account.id == account_id ? super&.token : nil + end +end diff --git a/app/views/api/v1/accounts/agent_bots/create.json.jbuilder b/app/views/api/v1/accounts/agent_bots/create.json.jbuilder index 453ef5cb4..f84bfc80d 100644 --- a/app/views/api/v1/accounts/agent_bots/create.json.jbuilder +++ b/app/views/api/v1/accounts/agent_bots/create.json.jbuilder @@ -1 +1 @@ -json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: @agent_bot +json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: AgentBotPresenter.new(@agent_bot) diff --git a/app/views/api/v1/accounts/agent_bots/index.json.jbuilder b/app/views/api/v1/accounts/agent_bots/index.json.jbuilder index d65188ff2..e77d418fb 100644 --- a/app/views/api/v1/accounts/agent_bots/index.json.jbuilder +++ b/app/views/api/v1/accounts/agent_bots/index.json.jbuilder @@ -1,3 +1,3 @@ json.array! @agent_bots do |agent_bot| - json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: agent_bot + json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: AgentBotPresenter.new(agent_bot) end diff --git a/app/views/api/v1/accounts/agent_bots/show.json.jbuilder b/app/views/api/v1/accounts/agent_bots/show.json.jbuilder index 453ef5cb4..f84bfc80d 100644 --- a/app/views/api/v1/accounts/agent_bots/show.json.jbuilder +++ b/app/views/api/v1/accounts/agent_bots/show.json.jbuilder @@ -1 +1 @@ -json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: @agent_bot +json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: AgentBotPresenter.new(@agent_bot) diff --git a/app/views/api/v1/accounts/agent_bots/update.json.jbuilder b/app/views/api/v1/accounts/agent_bots/update.json.jbuilder index 453ef5cb4..f84bfc80d 100644 --- a/app/views/api/v1/accounts/agent_bots/update.json.jbuilder +++ b/app/views/api/v1/accounts/agent_bots/update.json.jbuilder @@ -1 +1 @@ -json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: @agent_bot +json.partial! 'api/v1/models/agent_bot.json.jbuilder', resource: AgentBotPresenter.new(@agent_bot) diff --git a/app/views/api/v1/models/_agent_bot.json.jbuilder b/app/views/api/v1/models/_agent_bot.json.jbuilder index d9d1eaa17..044624fd8 100644 --- a/app/views/api/v1/models/_agent_bot.json.jbuilder +++ b/app/views/api/v1/models/_agent_bot.json.jbuilder @@ -3,3 +3,4 @@ json.name resource.name json.description resource.description json.outgoing_url resource.outgoing_url json.account_id resource.account_id +json.access_token resource.access_token if resource.access_token.present? diff --git a/app/views/platform/api/v1/models/_agent_bot.json.jbuilder b/app/views/platform/api/v1/models/_agent_bot.json.jbuilder index 1aa152186..8e04e98d7 100644 --- a/app/views/platform/api/v1/models/_agent_bot.json.jbuilder +++ b/app/views/platform/api/v1/models/_agent_bot.json.jbuilder @@ -3,3 +3,4 @@ json.name resource.name json.description resource.description json.outgoing_url resource.name json.account_id resource.account_id +json.access_token resource.access_token.token diff --git a/db/seeds.rb b/db/seeds.rb index 2cce3e7cf..306d8ba2d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -60,20 +60,20 @@ unless Rails.env.production? ) # sample email collect - WootMessageSeeder.create_sample_email_collect_message conversation + Seeders::MessageSeeder.create_sample_email_collect_message conversation Message.create!(content: 'Hello', account: account, inbox: inbox, conversation: conversation, message_type: :incoming) # sample card - WootMessageSeeder.create_sample_cards_message conversation + Seeders::MessageSeeder.create_sample_cards_message conversation # input select - WootMessageSeeder.create_sample_input_select_message conversation + Seeders::MessageSeeder.create_sample_input_select_message conversation # form - WootMessageSeeder.create_sample_form_message conversation + Seeders::MessageSeeder.create_sample_form_message conversation # articles - WootMessageSeeder.create_sample_articles_message conversation + Seeders::MessageSeeder.create_sample_articles_message conversation # csat - WootMessageSeeder.create_sample_csat_collect_message conversation + Seeders::MessageSeeder.create_sample_csat_collect_message conversation CannedResponse.create!(account: account, short_code: 'start', content: 'Hello welcome to chatwoot.') end diff --git a/lib/seeders/account_seeder.rb b/lib/seeders/account_seeder.rb new file mode 100644 index 000000000..65fe5a4e1 --- /dev/null +++ b/lib/seeders/account_seeder.rb @@ -0,0 +1,27 @@ +## Class to generate sample data for a chatwoot test Account. +############################################################ +### Usage ##### +# +# # Seed an account with all data types in this class +# Seeders::AccountSeeder.new(account: account).seed! +# +# # When you want to seed only a specific type of data +# Seeders::AccountSeeder.new(account: account).seed_canned_responses +# # Seed specific number of objects +# Seeders::AccountSeeder.new(account: account).seed_canned_responses(count: 10) +# +############################################################ + +class Seeders::AccountSeeder + pattr_initialize [:account!] + + def seed! + seed_canned_responses + end + + def seed_canned_responses(count: 50) + count.times do + account.canned_responses.create(content: Faker::Quote.fortune_cookie, short_code: Faker::Alphanumeric.alpha(number: 10)) + end + end +end diff --git a/lib/woot_message_seeder.rb b/lib/seeders/message_seeder.rb similarity index 99% rename from lib/woot_message_seeder.rb rename to lib/seeders/message_seeder.rb index 2e38868d8..5159225bd 100644 --- a/lib/woot_message_seeder.rb +++ b/lib/seeders/message_seeder.rb @@ -1,4 +1,4 @@ -module WootMessageSeeder +module Seeders::MessageSeeder def self.create_sample_email_collect_message(conversation) Message.create!( account: conversation.account, diff --git a/spec/controllers/api/v1/accounts/agent_bots_controller_spec.rb b/spec/controllers/api/v1/accounts/agent_bots_controller_spec.rb index 60442e6d6..ab3b578fc 100644 --- a/spec/controllers/api/v1/accounts/agent_bots_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/agent_bots_controller_spec.rb @@ -25,6 +25,8 @@ RSpec.describe 'Agent Bot API', type: :request do expect(response).to have_http_status(:success) expect(response.body).to include(agent_bot.name) expect(response.body).to include(global_bot.name) + expect(response.body).to include(agent_bot.access_token.token) + expect(response.body).not_to include(global_bot.access_token.token) end end end @@ -46,6 +48,7 @@ RSpec.describe 'Agent Bot API', type: :request do expect(response).to have_http_status(:success) expect(response.body).to include(agent_bot.name) + expect(response.body).to include(agent_bot.access_token.token) end it 'will show a global agent bot' do @@ -56,6 +59,7 @@ RSpec.describe 'Agent Bot API', type: :request do expect(response).to have_http_status(:success) expect(response.body).to include(global_bot.name) + expect(response.body).not_to include(global_bot.access_token.token) end end end @@ -113,6 +117,7 @@ RSpec.describe 'Agent Bot API', type: :request do expect(response).to have_http_status(:success) expect(agent_bot.reload.name).to eq('test_updated') + expect(response.body).to include(agent_bot.access_token.token) end it 'would not update the agent bot when agent' do @@ -134,6 +139,7 @@ RSpec.describe 'Agent Bot API', type: :request do expect(response).to have_http_status(:not_found) expect(agent_bot.reload.name).not_to eq('test_updated') + expect(response.body).not_to include(global_bot.access_token.token) end end end