From 564fa5f3925c76193614a8c925223f3658b1a299 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Fri, 12 Nov 2021 11:30:39 +0530 Subject: [PATCH] feat: Update custom filter for present/not_present operator (#3361) --- app/services/filter_service.rb | 32 +++++++++++----- lib/filters/filter_keys.json | 2 +- .../conversations/filter_service_spec.rb | 38 ++++++++++++++++--- 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/app/services/filter_service.rb b/app/services/filter_service.rb index e855ea52c..9715ba7fc 100644 --- a/app/services/filter_service.rb +++ b/app/services/filter_service.rb @@ -14,18 +14,16 @@ class FilterService def filter_operation(query_hash, current_index) case query_hash[:filter_operator] - when 'equal_to' + when 'equal_to', 'not_equal_to' @filter_values["value_#{current_index}"] = filter_values(query_hash) - "IN (:value_#{current_index})" - when 'not_equal_to' - @filter_values["value_#{current_index}"] = filter_values(query_hash) - "NOT IN (:value_#{current_index})" - when 'contains' + equals_to_filter_string(query_hash[:filter_operator], current_index) + when 'contains', 'does_not_contain' @filter_values["value_#{current_index}"] = "%#{filter_values(query_hash)}%" - "LIKE :value_#{current_index}" - when 'does_not_contain' - @filter_values["value_#{current_index}"] = "%#{filter_values(query_hash)}%" - "NOT LIKE :value_#{current_index}" + like_filter_string(query_hash[:filter_operator], current_index) + when 'is_present' + @filter_values["value_#{current_index}"] = 'IS NOT NULL' + when 'is_not_present' + @filter_values["value_#{current_index}"] = 'IS NULL' else @filter_values["value_#{current_index}"] = filter_values(query_hash).to_s "= :value_#{current_index}" @@ -47,4 +45,18 @@ class FilterService @conversations.count ] end + + private + + def equals_to_filter_string(filter_operator, current_index) + return "IN (:value_#{current_index})" if filter_operator == 'equal_to' + + "NOT IN (:value_#{current_index})" + end + + def like_filter_string(filter_operator, current_index) + return "LIKE :value_#{current_index}" if filter_operator == 'contains' + + "NOT LIKE :value_#{current_index}" + end end diff --git a/lib/filters/filter_keys.json b/lib/filters/filter_keys.json index 4edcd389e..6c284d1b5 100644 --- a/lib/filters/filter_keys.json +++ b/lib/filters/filter_keys.json @@ -35,7 +35,7 @@ "filter_operators": [ "equal_to", "not_equal_to", "contains", "does_not_contain", "is_present", "is_not_present" ], "attribute_type": "standard" }, - "id": { + "display_id": { "attribute_name": "Conversation Identifier", "input_type": "textbox", "data_type": "Number", diff --git a/spec/services/conversations/filter_service_spec.rb b/spec/services/conversations/filter_service_spec.rb index 02430c197..4ee9450c9 100644 --- a/spec/services/conversations/filter_service_spec.rb +++ b/spec/services/conversations/filter_service_spec.rb @@ -6,15 +6,17 @@ describe ::Conversations::FilterService do let!(:account) { create(:account) } let!(:user_1) { create(:user, account: account) } let!(:user_2) { create(:user, account: account) } + let!(:campaign_1) { create(:campaign, title: 'Test Campaign', account: account) } + let!(:campaign_2) { create(:campaign, title: 'Campaign', account: account) } let!(:inbox) { create(:inbox, account: account, enable_auto_assignment: false) } before do create(:inbox_member, user: user_1, inbox: inbox) create(:inbox_member, user: user_2, inbox: inbox) create(:conversation, account: account, inbox: inbox, assignee: user_1) - create(:conversation, account: account, inbox: inbox, assignee: user_1, + create(:conversation, account: account, inbox: inbox, assignee: user_1, campaign_id: campaign_1.id, status: 'pending', additional_attributes: { 'browser_language': 'en' }) - create(:conversation, account: account, inbox: inbox, assignee: user_1, + create(:conversation, account: account, inbox: inbox, assignee: user_1, campaign_id: campaign_2.id, status: 'pending', additional_attributes: { 'browser_language': 'en' }) create(:conversation, account: account, inbox: inbox, assignee: user_2) # unassigned conversation @@ -29,14 +31,14 @@ describe ::Conversations::FilterService do [ { attribute_key: 'browser_language', - filter_operator: 'equal_to', - values: ['en'], + filter_operator: 'contains', + values: 'en', query_operator: 'AND' }.with_indifferent_access, { attribute_key: 'status', - filter_operator: 'equal_to', - values: %w[open pending], + filter_operator: 'not_equal_to', + values: %w[resolved], query_operator: nil }.with_indifferent_access ] @@ -71,6 +73,30 @@ describe ::Conversations::FilterService do result = filter_service.new(params, user_1).perform expect(result.length).to be 2 end + + it 'filter conversations by is_present filter_operator' do + params[:payload] = [ + { + attribute_key: 'assignee_id', + filter_operator: 'equal_to', + values: [ + user_1.id, + user_2.id + ], + query_operator: 'AND' + }.with_indifferent_access, + { + attribute_key: 'campaign_id', + filter_operator: 'is_present', + values: [], + query_operator: nil + }.with_indifferent_access + ] + result = filter_service.new(params, user_1).perform + + expect(result[:count][:all_count]).to be 2 + expect(result[:conversations].pluck(:campaign_id).sort).to eq [campaign_2.id, campaign_1.id].sort + end end end end