feat: Update custom filter for present/not_present operator (#3361)
This commit is contained in:
parent
58aae7a4a9
commit
564fa5f392
3 changed files with 55 additions and 17 deletions
|
@ -14,18 +14,16 @@ class FilterService
|
||||||
|
|
||||||
def filter_operation(query_hash, current_index)
|
def filter_operation(query_hash, current_index)
|
||||||
case query_hash[:filter_operator]
|
case query_hash[:filter_operator]
|
||||||
when 'equal_to'
|
when 'equal_to', 'not_equal_to'
|
||||||
@filter_values["value_#{current_index}"] = filter_values(query_hash)
|
@filter_values["value_#{current_index}"] = filter_values(query_hash)
|
||||||
"IN (:value_#{current_index})"
|
equals_to_filter_string(query_hash[:filter_operator], current_index)
|
||||||
when 'not_equal_to'
|
when 'contains', 'does_not_contain'
|
||||||
@filter_values["value_#{current_index}"] = filter_values(query_hash)
|
|
||||||
"NOT IN (:value_#{current_index})"
|
|
||||||
when 'contains'
|
|
||||||
@filter_values["value_#{current_index}"] = "%#{filter_values(query_hash)}%"
|
@filter_values["value_#{current_index}"] = "%#{filter_values(query_hash)}%"
|
||||||
"LIKE :value_#{current_index}"
|
like_filter_string(query_hash[:filter_operator], current_index)
|
||||||
when 'does_not_contain'
|
when 'is_present'
|
||||||
@filter_values["value_#{current_index}"] = "%#{filter_values(query_hash)}%"
|
@filter_values["value_#{current_index}"] = 'IS NOT NULL'
|
||||||
"NOT LIKE :value_#{current_index}"
|
when 'is_not_present'
|
||||||
|
@filter_values["value_#{current_index}"] = 'IS NULL'
|
||||||
else
|
else
|
||||||
@filter_values["value_#{current_index}"] = filter_values(query_hash).to_s
|
@filter_values["value_#{current_index}"] = filter_values(query_hash).to_s
|
||||||
"= :value_#{current_index}"
|
"= :value_#{current_index}"
|
||||||
|
@ -47,4 +45,18 @@ class FilterService
|
||||||
@conversations.count
|
@conversations.count
|
||||||
]
|
]
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -35,7 +35,7 @@
|
||||||
"filter_operators": [ "equal_to", "not_equal_to", "contains", "does_not_contain", "is_present", "is_not_present" ],
|
"filter_operators": [ "equal_to", "not_equal_to", "contains", "does_not_contain", "is_present", "is_not_present" ],
|
||||||
"attribute_type": "standard"
|
"attribute_type": "standard"
|
||||||
},
|
},
|
||||||
"id": {
|
"display_id": {
|
||||||
"attribute_name": "Conversation Identifier",
|
"attribute_name": "Conversation Identifier",
|
||||||
"input_type": "textbox",
|
"input_type": "textbox",
|
||||||
"data_type": "Number",
|
"data_type": "Number",
|
||||||
|
|
|
@ -6,15 +6,17 @@ describe ::Conversations::FilterService do
|
||||||
let!(:account) { create(:account) }
|
let!(:account) { create(:account) }
|
||||||
let!(:user_1) { create(:user, account: account) }
|
let!(:user_1) { create(:user, account: account) }
|
||||||
let!(:user_2) { 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) }
|
let!(:inbox) { create(:inbox, account: account, enable_auto_assignment: false) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
create(:inbox_member, user: user_1, inbox: inbox)
|
create(:inbox_member, user: user_1, inbox: inbox)
|
||||||
create(:inbox_member, user: user_2, 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,
|
create(:conversation, account: account, inbox: inbox, assignee: user_1, campaign_id: campaign_1.id,
|
||||||
status: 'pending', additional_attributes: { 'browser_language': 'en' })
|
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' })
|
status: 'pending', additional_attributes: { 'browser_language': 'en' })
|
||||||
create(:conversation, account: account, inbox: inbox, assignee: user_2)
|
create(:conversation, account: account, inbox: inbox, assignee: user_2)
|
||||||
# unassigned conversation
|
# unassigned conversation
|
||||||
|
@ -29,14 +31,14 @@ describe ::Conversations::FilterService do
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
attribute_key: 'browser_language',
|
attribute_key: 'browser_language',
|
||||||
filter_operator: 'equal_to',
|
filter_operator: 'contains',
|
||||||
values: ['en'],
|
values: 'en',
|
||||||
query_operator: 'AND'
|
query_operator: 'AND'
|
||||||
}.with_indifferent_access,
|
}.with_indifferent_access,
|
||||||
{
|
{
|
||||||
attribute_key: 'status',
|
attribute_key: 'status',
|
||||||
filter_operator: 'equal_to',
|
filter_operator: 'not_equal_to',
|
||||||
values: %w[open pending],
|
values: %w[resolved],
|
||||||
query_operator: nil
|
query_operator: nil
|
||||||
}.with_indifferent_access
|
}.with_indifferent_access
|
||||||
]
|
]
|
||||||
|
@ -71,6 +73,30 @@ describe ::Conversations::FilterService do
|
||||||
result = filter_service.new(params, user_1).perform
|
result = filter_service.new(params, user_1).perform
|
||||||
expect(result.length).to be 2
|
expect(result.length).to be 2
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue