fix: Custom attr filter on conversations (#5978)

This commit is contained in:
Tejaswini Chile 2022-12-02 10:37:32 +05:30 committed by GitHub
parent e35638588a
commit a08099bbcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 12 deletions

View file

@ -3,12 +3,7 @@ require 'json'
class FilterService class FilterService
ATTRIBUTE_MODEL = 'conversation_attribute'.freeze ATTRIBUTE_MODEL = 'conversation_attribute'.freeze
ATTRIBUTE_TYPES = { ATTRIBUTE_TYPES = {
date: 'date', date: 'date', text: 'text', number: 'numeric', link: 'text', list: 'text', checkbox: 'boolean'
text: 'text',
number: 'numeric',
link: 'text',
list: 'text',
checkbox: 'boolean'
}.with_indifferent_access }.with_indifferent_access
def initialize(params, user) def initialize(params, user)
@ -60,7 +55,7 @@ class FilterService
end end
def case_insensitive_values(query_hash) def case_insensitive_values(query_hash)
if query_hash['custom_attribute_type'].present? && query_hash['values'][0].is_a?(String) if @custom_attribute_type.present? && query_hash['values'][0].is_a?(String)
string_filter_values(query_hash) string_filter_values(query_hash)
else else
query_hash['values'] query_hash['values']
@ -125,11 +120,13 @@ class FilterService
query_operator = query_hash[:query_operator] query_operator = query_hash[:query_operator]
table_name = attribute_model == 'conversation_attribute' ? 'conversations' : 'contacts' table_name = attribute_model == 'conversation_attribute' ? 'conversations' : 'contacts'
if attribute_data_type == 'text' query = if attribute_data_type == 'text'
" LOWER(#{table_name}.custom_attributes ->> '#{@attribute_key}')::#{attribute_data_type} #{filter_operator_value} #{query_operator} " " LOWER(#{table_name}.custom_attributes ->> '#{@attribute_key}')::#{attribute_data_type} #{filter_operator_value} #{query_operator} "
else else
" (#{table_name}.custom_attributes ->> '#{@attribute_key}')::#{attribute_data_type} #{filter_operator_value} #{query_operator} " " (#{table_name}.custom_attributes ->> '#{@attribute_key}')::#{attribute_data_type} #{filter_operator_value} #{query_operator} "
end end
query + not_in_custom_attr_query(table_name, query_hash, attribute_data_type)
end end
def custom_attribute(attribute_key, account, custom_attribute_type) def custom_attribute(attribute_key, account, custom_attribute_type)
@ -140,6 +137,12 @@ class FilterService
).find_by(attribute_key: attribute_key) ).find_by(attribute_key: attribute_key)
end end
def not_in_custom_attr_query(table_name, query_hash, attribute_data_type)
return '' unless query_hash[:filter_operator] == 'not_equal_to'
" OR (#{table_name}.custom_attributes ->> '#{@attribute_key}')::#{attribute_data_type} IS NULL "
end
def equals_to_filter_string(filter_operator, current_index) def equals_to_filter_string(filter_operator, current_index)
return "IN (:value_#{current_index})" if filter_operator == 'equal_to' return "IN (:value_#{current_index})" if filter_operator == 'equal_to'

View file

@ -86,6 +86,17 @@ describe ::Conversations::FilterService do
expect(result.length).to be conversations.count expect(result.length).to be conversations.count
end end
it 'filter conversations by additional_attributes with NOT_IN filter' do
payload = [{ attribute_key: 'conversation_type', filter_operator: 'not_equal_to', values: 'platinum', query_operator: nil,
custom_attribute_type: 'conversation_attribute' }.with_indifferent_access]
params[:payload] = payload
result = filter_service.new(params, user_1).perform
conversations = Conversation.where(
"custom_attributes ->> 'conversation_type' NOT IN (?) OR custom_attributes ->> 'conversation_type' IS NULL", ['platinum']
)
expect(result[:count][:all_count]).to be conversations.count
end
it 'filter conversations by tags' do it 'filter conversations by tags' do
unassigned_conversation.update_labels('support') unassigned_conversation.update_labels('support')
params[:payload] = [ params[:payload] = [