diff --git a/app/services/automation_rules/conditions_filter_service.rb b/app/services/automation_rules/conditions_filter_service.rb index eb351efbe..a1ef68bf9 100644 --- a/app/services/automation_rules/conditions_filter_service.rb +++ b/app/services/automation_rules/conditions_filter_service.rb @@ -88,7 +88,11 @@ class AutomationRules::ConditionsFilterService < FilterService case current_filter['attribute_type'] when 'standard' - " messages.#{attribute_key} #{filter_operator_value} #{query_operator} " + if current_filter['data_type'] == 'text' + " LOWER(messages.#{attribute_key}) #{filter_operator_value} #{query_operator} " + else + " messages.#{attribute_key} #{filter_operator_value} #{query_operator} " + end end end diff --git a/app/services/filter_service.rb b/app/services/filter_service.rb index a6549b378..1eacc4df2 100644 --- a/app/services/filter_service.rb +++ b/app/services/filter_service.rb @@ -52,6 +52,8 @@ class FilterService query_hash['values'].map { |x| Conversation.statuses[x.to_sym] } when 'message_type' query_hash['values'].map { |x| Message.message_types[x.to_sym] } + when 'content' + string_filter_values(query_hash) else query_hash['values'] end diff --git a/lib/filters/filter_keys.json b/lib/filters/filter_keys.json index 3d662d19b..7d1ed4842 100644 --- a/lib/filters/filter_keys.json +++ b/lib/filters/filter_keys.json @@ -175,7 +175,7 @@ "message_type": { "attribute_name": "Message Type", "input_type": "search_box with name tags/plain text", - "data_type": "text", + "data_type": "numeric", "filter_operators": [ "equal_to", "not_equal_to" ], "attribute_type": "standard" }, diff --git a/spec/listeners/automation_rule_listener_spec.rb b/spec/listeners/automation_rule_listener_spec.rb index 9d4c03159..5ca377df8 100644 --- a/spec/listeners/automation_rule_listener_spec.rb +++ b/spec/listeners/automation_rule_listener_spec.rb @@ -362,16 +362,20 @@ describe AutomationRuleListener do event_name: 'message_created', name: 'Call actions message created based on case in-sensitive filter', description: 'Add labels, assign team after message created', - conditions: [{ 'values': ['KYC'], 'attribute_key': 'content', 'query_operator': nil, 'filter_operator': 'contains' }] + conditions: [ + { 'values': ['KYC'], 'attribute_key': 'content', 'query_operator': nil, 'filter_operator': 'contains' } + ] ) end - let!(:message) { create(:message, account: account, conversation: conversation, message_type: 'incoming', content: 'kyc message') } + let!(:message) { create(:message, account: account, conversation: conversation, message_type: 'incoming', content: 'KyC message') } + let!(:message_2) { create(:message, account: account, conversation: conversation, message_type: 'incoming', content: 'SALE') } + let!(:event) do Events::Base.new('message_created', Time.zone.now, { conversation: conversation, message: message }) end - it 'triggers automation rule based on case in-sensitive filter' do + it 'triggers automation rule on contains filter' do expect(conversation.labels).to eq([]) expect(TeamNotifications::AutomationNotificationMailer).to receive(:conversation_creation) listener.message_created(event) @@ -379,6 +383,23 @@ describe AutomationRuleListener do expect(conversation.labels.pluck(:name)).to contain_exactly('support', 'priority_customer') end + + it 'triggers automation on equal_to filter' do + automation_rule.update!( + conditions: [ + { 'values': ['sale'], 'attribute_key': 'content', 'query_operator': nil, 'filter_operator': 'equal_to' } + ], + actions: [ + { 'action_name' => 'add_label', 'action_params' => %w[sale_enquiry] } + ] + ) + + event = Events::Base.new('message_created', Time.zone.now, { conversation: conversation, message: message_2 }) + listener.message_created(event) + + conversation.reload + expect(conversation.labels.pluck(:name)).to contain_exactly('sale_enquiry') + end end describe '#message_created' do