diff --git a/app/models/concerns/activity_message_handler.rb b/app/models/concerns/activity_message_handler.rb index c783580d1..cdab4e17d 100644 --- a/app/models/concerns/activity_message_handler.rb +++ b/app/models/concerns/activity_message_handler.rb @@ -73,7 +73,8 @@ module ActivityMessageHandler end def generate_team_change_activity_key - key = team_id ? 'assigned' : 'removed' + team = Team.find_by(id: team_id) + key = team.present? ? 'assigned' : 'removed' key += '_with_assignee' if key == 'assigned' && saved_change_to_assignee_id? && assignee key end diff --git a/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb b/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb index c7bb494c2..e6128ca82 100644 --- a/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb @@ -5,10 +5,11 @@ RSpec.describe 'Api::V1::Accounts::BulkActionsController', type: :request do let(:account) { create(:account) } let(:agent_1) { create(:user, account: account, role: :agent) } let(:agent_2) { create(:user, account: account, role: :agent) } + let(:team_1) { create(:team, account: account) } before do - create(:conversation, account_id: account.id, status: :open) - create(:conversation, account_id: account.id, status: :open) + create(:conversation, account_id: account.id, status: :open, team_id: team_1.id) + create(:conversation, account_id: account.id, status: :open, team_id: team_1.id) create(:conversation, account_id: account.id, status: :open) create(:conversation, account_id: account.id, status: :open) end @@ -55,6 +56,44 @@ RSpec.describe 'Api::V1::Accounts::BulkActionsController', type: :request do expect(Conversation.first.assignee_id).to be_nil end + it 'Bulk update conversation team id to none' do + params = { type: 'Conversation', fields: { team_id: 0 }, ids: Conversation.first(1).pluck(:display_id) } + expect(Conversation.first.team).not_to be_nil + + perform_enqueued_jobs do + post "/api/v1/accounts/#{account.id}/bulk_actions", + headers: agent.create_new_auth_token, + params: params + + expect(response).to have_http_status(:success) + end + + expect(Conversation.first.team).to be_nil + + last_activity_message = Conversation.first.messages.activity.last + + expect(last_activity_message.content).to eq("Unassigned from #{team_1.name} by #{agent.name}") + end + + it 'Bulk update conversation team id to team' do + params = { type: 'Conversation', fields: { team_id: team_1.id }, ids: Conversation.last(2).pluck(:display_id) } + expect(Conversation.last.team_id).to be_nil + + perform_enqueued_jobs do + post "/api/v1/accounts/#{account.id}/bulk_actions", + headers: agent.create_new_auth_token, + params: params + + expect(response).to have_http_status(:success) + end + + expect(Conversation.last.team).to eq(team_1) + + last_activity_message = Conversation.last.messages.activity.last + + expect(last_activity_message.content).to eq("Assigned to #{team_1.name} by #{agent.name}") + end + it 'Bulk update conversation assignee id' do params = { type: 'Conversation', fields: { assignee_id: agent_1.id }, ids: Conversation.first(3).pluck(:display_id) }