Feat: Custom attribute advanced filter (#3818)
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
class Contacts::FilterService < FilterService
|
||||
ATTRIBUTE_MODEL = 'contact_attribute'.freeze
|
||||
|
||||
def perform
|
||||
@contacts = contact_query_builder
|
||||
|
||||
@@ -16,7 +18,7 @@ class Contacts::FilterService < FilterService
|
||||
@query_string += contact_query_string(current_filter, query_hash, current_index)
|
||||
end
|
||||
|
||||
base_relation.where(@query_string, @filter_values.with_indifferent_access)
|
||||
base_relation.select('distinct contacts.id').where(@query_string, @filter_values.with_indifferent_access)
|
||||
end
|
||||
|
||||
def contact_query_string(current_filter, query_hash, current_index)
|
||||
@@ -24,6 +26,8 @@ class Contacts::FilterService < FilterService
|
||||
query_operator = query_hash[:query_operator]
|
||||
filter_operator_value = filter_operation(query_hash, current_index)
|
||||
|
||||
return custom_attribute_query(query_hash, current_index) if current_filter.nil?
|
||||
|
||||
case current_filter['attribute_type']
|
||||
when 'additional_attributes'
|
||||
" LOWER(contacts.additional_attributes ->> '#{attribute_key}') #{filter_operator_value} #{query_operator} "
|
||||
@@ -58,4 +62,18 @@ class Contacts::FilterService < FilterService
|
||||
|
||||
"!= :value_#{current_index}"
|
||||
end
|
||||
|
||||
def custom_attribute_query(query_hash, current_index)
|
||||
attribute_key = query_hash[:attribute_key]
|
||||
query_operator = query_hash[:query_operator]
|
||||
attribute_type = custom_attribute(attribute_key).try(:attribute_display_type)
|
||||
filter_operator_value = filter_operation(query_hash, current_index)
|
||||
attribute_data_type = self.class::ATTRIBUTE_TYPES[attribute_type]
|
||||
|
||||
if custom_attribute(attribute_key)
|
||||
" LOWER(contacts.custom_attributes ->> '#{attribute_key}')::#{attribute_data_type} #{filter_operator_value} #{query_operator} "
|
||||
else
|
||||
' '
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
class Conversations::FilterService < FilterService
|
||||
ATTRIBUTE_MODEL = 'conversation_attribute'.freeze
|
||||
|
||||
def perform
|
||||
@conversations = conversation_query_builder
|
||||
mine_count, unassigned_count, all_count, = set_count_for_all_conversations
|
||||
@@ -30,6 +32,8 @@ class Conversations::FilterService < FilterService
|
||||
query_operator = query_hash[:query_operator]
|
||||
filter_operator_value = filter_operation(query_hash, current_index)
|
||||
|
||||
return custom_attribute_query(query_hash, current_index) if current_filter.nil?
|
||||
|
||||
case current_filter['attribute_type']
|
||||
when 'additional_attributes'
|
||||
" conversations.additional_attributes ->> '#{attribute_key}' #{filter_operator_value} #{query_operator} "
|
||||
@@ -56,4 +60,20 @@ class Conversations::FilterService < FilterService
|
||||
)
|
||||
@conversations.latest.page(current_page)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def custom_attribute_query(query_hash, current_index)
|
||||
attribute_key = query_hash[:attribute_key]
|
||||
query_operator = query_hash[:query_operator]
|
||||
attribute_type = custom_attribute(attribute_key).try(:attribute_display_type)
|
||||
filter_operator_value = filter_operation(query_hash, current_index)
|
||||
attribute_data_type = self.class::ATTRIBUTE_TYPES[attribute_type]
|
||||
|
||||
if custom_attribute(attribute_key)
|
||||
" LOWER(conversations.custom_attributes ->> '#{attribute_key}')::#{attribute_data_type} #{filter_operator_value} #{query_operator} "
|
||||
else
|
||||
' '
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
require 'json'
|
||||
|
||||
class FilterService
|
||||
ATTRIBUTE_MODEL = 'conversation_attribute'.freeze
|
||||
ATTRIBUTE_TYPES = {
|
||||
date: 'date',
|
||||
text: 'text',
|
||||
number: 'numeric',
|
||||
link: 'text',
|
||||
list: 'text',
|
||||
checkbox: 'boolean'
|
||||
}.with_indifferent_access
|
||||
|
||||
def initialize(params, user)
|
||||
@params = params
|
||||
@user = user
|
||||
@@ -24,6 +34,8 @@ class FilterService
|
||||
@filter_values["value_#{current_index}"] = 'IS NOT NULL'
|
||||
when 'is_not_present'
|
||||
@filter_values["value_#{current_index}"] = 'IS NULL'
|
||||
when 'is_greater_than', 'is_less_than'
|
||||
@filter_values["value_#{current_index}"] = lt_gt_filter_values(query_hash)
|
||||
else
|
||||
@filter_values["value_#{current_index}"] = filter_values(query_hash).to_s
|
||||
"= :value_#{current_index}"
|
||||
@@ -43,6 +55,15 @@ class FilterService
|
||||
end
|
||||
end
|
||||
|
||||
def lt_gt_filter_values(query_hash)
|
||||
attribute_key = query_hash[:attribute_key]
|
||||
attribute_type = custom_attribute(attribute_key).try(:attribute_display_type)
|
||||
attribute_data_type = self.class::ATTRIBUTE_TYPES[attribute_type]
|
||||
value = query_hash['values'][0]
|
||||
operator = query_hash['filter_operator'] == 'is_less_than' ? '<' : '>'
|
||||
"#{operator} '#{value}'::#{attribute_data_type}"
|
||||
end
|
||||
|
||||
def set_count_for_all_conversations
|
||||
[
|
||||
@conversations.assigned_to(@user).count,
|
||||
@@ -53,6 +74,12 @@ class FilterService
|
||||
|
||||
private
|
||||
|
||||
def custom_attribute(attribute_key)
|
||||
@custom_attribute = Current.account.custom_attribute_definitions.where(
|
||||
attribute_model: self.class::ATTRIBUTE_MODEL
|
||||
).find_by(attribute_key: attribute_key)
|
||||
end
|
||||
|
||||
def equals_to_filter_string(filter_operator, current_index)
|
||||
return "IN (:value_#{current_index})" if filter_operator == 'equal_to'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user