From 75c2a7cb2ff6bdea16908c6fd804817aaa06bd34 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Thu, 14 Jan 2021 14:21:41 +0530 Subject: [PATCH] fix: Resolve name only if assignee exists during unassignment (#1649) --- app/models/conversation.rb | 2 +- .../assignments_controller_spec.rb | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 19d0cd99f..6cd2ea298 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -231,7 +231,7 @@ class Conversation < ApplicationRecord def create_assignee_change(user_name) return unless user_name - params = { assignee_name: assignee.name, user_name: user_name }.compact + params = { assignee_name: assignee&.name, user_name: user_name }.compact key = assignee_id ? 'assigned' : 'removed' key = 'self_assigned' if self_assign? assignee_id content = I18n.t("conversations.activity.assignee.#{key}", **params) diff --git a/spec/controllers/api/v1/accounts/conversations/assignments_controller_spec.rb b/spec/controllers/api/v1/accounts/conversations/assignments_controller_spec.rb index 2e24fd871..859798530 100644 --- a/spec/controllers/api/v1/accounts/conversations/assignments_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/conversations/assignments_controller_spec.rb @@ -29,5 +29,25 @@ RSpec.describe 'Conversation Assignment API', type: :request do expect(conversation.reload.assignee).to eq(agent) end end + + context 'when conversation already has an assignee' do + let(:agent) { create(:user, account: account, role: :agent) } + + before do + conversation.update!(assignee: agent) + end + + it 'unassigns a user from the conversation' do + params = { assignee_id: 0 } + post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id), + params: params, + headers: agent.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(conversation.reload.assignee).to eq(nil) + expect(conversation.messages.last.content).to eq("Conversation unassigned by #{agent.name}") + end + end end end