Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

fix(conversations): render a conversation with a deleted assignee as unassigned (#16123)

Gabriel Jablonski80bb9e05c4db
diff --git a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder
index c9be80ff7ab..fb1fdf390ce 100644
--- a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder
+++ b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder
@@ -12,7 +12,7 @@ json.meta do
json.partial! 'api/v1/models/agent_bot_slim', formats: [:json], resource: conversation.assigned_entity
end
json.assignee_type 'AgentBot'
- elsif conversation.assignee_type == 'User'
+ elsif conversation.assigned_entity.is_a?(User)
json.assignee do
json.partial! 'api/v1/models/agent', formats: [:json], resource: conversation.assigned_entity
end
diff --git a/spec/controllers/api/v1/accounts/conversations_controller_spec.rb b/spec/controllers/api/v1/accounts/conversations_controller_spec.rb
index e22b2a50882..a67c02ab9c2 100644
--- a/spec/controllers/api/v1/accounts/conversations_controller_spec.rb
+++ b/spec/controllers/api/v1/accounts/conversations_controller_spec.rb
@@ -35,6 +35,23 @@ RSpec.describe 'Conversations API', type: :request do
expect(body[:data][:payload].first[:messages].first[:id]).to eq(message.id)
end
+ it 'lists a conversation whose assignee no longer exists as unassigned' do
+ assigned = create(:conversation, account: account, inbox: conversation.inbox, assignee: agent)
+ conversation.update_column(:assignee_id, User.maximum(:id) + 1000) # rubocop:disable Rails/SkipsModelValidations
+
+ get "/api/v1/accounts/#{account.id}/conversations",
+ headers: agent.create_new_auth_token,
+ params: { assignee_type: 'all' },
+ as: :json
+
+ expect(response).to have_http_status(:success)
+ payload = JSON.parse(response.body, symbolize_names: true)[:data][:payload].index_by { |item| item[:id] }
+ expect(payload.keys).to contain_exactly(conversation.display_id, assigned.display_id)
+ expect(payload[conversation.display_id][:meta]).not_to include(:assignee, :assignee_type)
+ expect(payload[assigned.display_id][:meta][:assignee][:id]).to eq(agent.id)
+ expect(payload[assigned.display_id][:meta][:assignee_type]).to eq('User')
+ end
+
it 'returns conversations with empty messages array for conversations with out messages' do
get "/api/v1/accounts/#{account.id}/conversations",
headers: agent.create_new_auth_token,