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
ATTRIBUTE_MODEL = 'conversation_attribute'.freeze
ATTRIBUTE_TYPES = {
date: 'date',
text: 'text',
number: 'numeric',
link: 'text',
list: 'text',
checkbox: 'boolean'
date: 'date', text: 'text', number: 'numeric', link: 'text', list: 'text', checkbox: 'boolean'
}.with_indifferent_access
def initialize(params, user)
@ -60,7 +55,7 @@ class FilterService
end
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)
else
query_hash['values']
@ -125,11 +120,13 @@ class FilterService
query_operator = query_hash[:query_operator]
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} "
else
" (#{table_name}.custom_attributes ->> '#{@attribute_key}')::#{attribute_data_type} #{filter_operator_value} #{query_operator} "
end
query + not_in_custom_attr_query(table_name, query_hash, attribute_data_type)
end
def custom_attribute(attribute_key, account, custom_attribute_type)
@ -140,6 +137,12 @@ class FilterService
).find_by(attribute_key: attribute_key)
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)
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
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
unassigned_conversation.update_labels('support')
params[:payload] = [