From a08099bbcc85df5905057ee50ff16c04f64261c3 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Fri, 2 Dec 2022 10:37:32 +0530 Subject: [PATCH] fix: Custom attr filter on conversations (#5978) --- app/services/filter_service.rb | 27 ++++++++++--------- .../conversations/filter_service_spec.rb | 11 ++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/services/filter_service.rb b/app/services/filter_service.rb index 5cb589e1b..bbc949efa 100644 --- a/app/services/filter_service.rb +++ b/app/services/filter_service.rb @@ -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' - " 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 = 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' diff --git a/spec/services/conversations/filter_service_spec.rb b/spec/services/conversations/filter_service_spec.rb index a17351ec3..a3e6edc27 100644 --- a/spec/services/conversations/filter_service_spec.rb +++ b/spec/services/conversations/filter_service_spec.rb @@ -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] = [