feat: Add support for labels in automations (#11658)
- Add support for using labels as an action event for automation - Fix duplicated conversation_updated event dispatch for labels Fixes https://github.com/chatwoot/chatwoot/issues/8539 and multiple issues around duplication related to label change events. --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
@@ -151,13 +151,36 @@ class AutomationRules::ConditionsFilterService < FilterService
|
||||
" #{table_name}.additional_attributes ->> '#{attribute_key}' #{filter_operator_value} #{query_operator} "
|
||||
when 'standard'
|
||||
if attribute_key == 'labels'
|
||||
" tags.id #{filter_operator_value} #{query_operator} "
|
||||
build_label_query_string(query_hash, current_index, query_operator)
|
||||
else
|
||||
" #{table_name}.#{attribute_key} #{filter_operator_value} #{query_operator} "
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def build_label_query_string(query_hash, current_index, query_operator)
|
||||
case query_hash['filter_operator']
|
||||
when 'equal_to'
|
||||
return " 1=0 #{query_operator} " if query_hash['values'].blank?
|
||||
|
||||
value_placeholder = "value_#{current_index}"
|
||||
@filter_values[value_placeholder] = query_hash['values'].first
|
||||
" tags.name = :#{value_placeholder} #{query_operator} "
|
||||
when 'not_equal_to'
|
||||
return " 1=0 #{query_operator} " if query_hash['values'].blank?
|
||||
|
||||
value_placeholder = "value_#{current_index}"
|
||||
@filter_values[value_placeholder] = query_hash['values'].first
|
||||
" tags.name != :#{value_placeholder} #{query_operator} "
|
||||
when 'is_present'
|
||||
" tags.id IS NOT NULL #{query_operator} "
|
||||
when 'is_not_present'
|
||||
" tags.id IS NULL #{query_operator} "
|
||||
else
|
||||
" tags.id #{filter_operation(query_hash, current_index)} #{query_operator} "
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def base_relation
|
||||
@@ -166,7 +189,21 @@ class AutomationRules::ConditionsFilterService < FilterService
|
||||
).joins(
|
||||
'LEFT OUTER JOIN messages on messages.conversation_id = conversations.id'
|
||||
)
|
||||
|
||||
# Only add label joins when label conditions exist
|
||||
if label_conditions?
|
||||
records = records.joins(
|
||||
'LEFT OUTER JOIN taggings ON taggings.taggable_id = conversations.id AND taggings.taggable_type = \'Conversation\''
|
||||
).joins(
|
||||
'LEFT OUTER JOIN tags ON taggings.tag_id = tags.id'
|
||||
)
|
||||
end
|
||||
|
||||
records = records.where(messages: { id: @options[:message].id }) if @options[:message].present?
|
||||
records
|
||||
end
|
||||
|
||||
def label_conditions?
|
||||
@rule.conditions.any? { |condition| condition['attribute_key'] == 'labels' }
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user