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
|
||||
|
||||
Reference in New Issue
Block a user