mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-31 02:57:57 +00:00 
			
		
		
		
	feat: update contacts filter query (#6802)
- Update contacts API query to be faster
This commit is contained in:
		| @@ -119,11 +119,12 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController | |||||||
|   end |   end | ||||||
|  |  | ||||||
|   def fetch_contacts_with_conversation_count(contacts) |   def fetch_contacts_with_conversation_count(contacts) | ||||||
|     contacts_with_conversation_count = filtrate(contacts).left_outer_joins(:conversations) |     conversation_count_sub_query = 'SELECT COUNT(*) FROM "conversations" WHERE "conversations"."contact_id" = "contacts"."id"' | ||||||
|                                                          .select('contacts.*, COUNT(conversations.id) as conversations_count') |     contacts_with_conversation_count = filtrate(contacts) | ||||||
|                                                          .group('contacts.id') |                                        .select("contacts.*, (#{conversation_count_sub_query}) as conversations_count") | ||||||
|                                                          .includes([{ avatar_attachment: [:blob] }]) |                                        .group('contacts.id') | ||||||
|                                                          .page(@current_page).per(RESULTS_PER_PAGE) |                                        .includes([{ avatar_attachment: [:blob] }]) | ||||||
|  |                                        .page(@current_page).per(RESULTS_PER_PAGE) | ||||||
|  |  | ||||||
|     return contacts_with_conversation_count.includes([{ contact_inboxes: [:inbox] }]) if @include_contact_inboxes |     return contacts_with_conversation_count.includes([{ contact_inboxes: [:inbox] }]) if @include_contact_inboxes | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Shivam Mishra
					Shivam Mishra