filter orphan conversations

This commit is contained in:
Tanmay Deep Sharma
2026-01-20 17:34:47 +05:30
parent 78e78f6ef6
commit f7ab3a689c
3 changed files with 12 additions and 6 deletions
@@ -160,6 +160,9 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro
def conversation
@conversation ||= Current.account.conversations.find_by!(display_id: params[:id])
# Return 404 if conversation has no valid contact or contact_inbox
raise ActiveRecord::RecordNotFound if @conversation.contact.blank?
authorize @conversation, :show?
end
+7
View File
@@ -63,6 +63,7 @@ class ConversationFinder
set_assignee_type
find_all_conversations
filter_orphan_conversations
filter_by_status unless params[:q]
filter_by_team
filter_by_labels
@@ -166,6 +167,12 @@ class ConversationFinder
@conversations = @conversations.where(contact_inboxes: { source_id: params[:source_id] })
end
def filter_orphan_conversations
# Filter out conversations where the contact has been deleted
# Using INNER JOIN is most efficient - only returns conversations with existing contacts
@conversations = @conversations.joins(:contact)
end
def set_count_for_all_conversations
[
@conversations.assigned_to(current_user).count,
@@ -3,12 +3,8 @@
# Everywhere else we use conversation builder in partials folder
json.meta do
if conversation.contact
json.sender do
json.partial! 'api/v1/models/contact', formats: [:json], resource: conversation.contact
end
else
json.sender nil
json.sender do
json.partial! 'api/v1/models/contact', formats: [:json], resource: conversation.contact
end
json.channel conversation.inbox.try(:channel_type)
if conversation.assigned_entity.is_a?(AgentBot)