fix: Avoid joining tables to fix the distinct value query (#7965)

DISTINCT query with custom attributes return an error. To avoid the error, this PR refactors the query to include tags only when it is required.

Fixes #7931
Fixes #7836

Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Pranav Raj S
2023-09-22 19:48:56 -07:00
committed by GitHub
parent dd0930d75e
commit ff915dd2ea
4 changed files with 94 additions and 30 deletions

View File

@@ -15,7 +15,7 @@ describe Contacts::FilterService do
create(:inbox_member, user: first_user, inbox: inbox)
create(:inbox_member, user: second_user, inbox: inbox)
create(:conversation, account: account, inbox: inbox, assignee: first_user, contact: en_contact)
create(:conversation, account: account, inbox: inbox)
create(:conversation, account: account, inbox: inbox, contact: el_contact)
Current.account = account
create(:custom_attribute_definition,
@@ -38,8 +38,7 @@ describe Contacts::FilterService do
describe '#perform' do
before do
en_contact.add_labels(%w[random_label support])
el_contact.update_labels('random_label')
en_contact.update_labels(%w[random_label support])
cs_contact.update_labels('support')
en_contact.update!(custom_attributes: { contact_additional_information: 'test custom data' })
@@ -60,6 +59,70 @@ describe Contacts::FilterService do
]
end
context 'with label filter' do
it 'returns equal_to filter results properly' do
params[:payload] = [
{
attribute_key: 'labels',
filter_operator: 'equal_to',
values: ['support'],
query_operator: nil
}.with_indifferent_access
]
result = filter_service.new(params, first_user).perform
expect(result[:contacts].length).to be 2
expect(result[:contacts].first.label_list).to include('support')
expect(result[:contacts].last.label_list).to include('support')
end
it 'returns not_equal_to filter results properly' do
params[:payload] = [
{
attribute_key: 'labels',
filter_operator: 'not_equal_to',
values: ['support'],
query_operator: nil
}.with_indifferent_access
]
result = filter_service.new(params, first_user).perform
expect(result[:contacts].length).to be 1
expect(result[:contacts].first.id).to eq el_contact.id
end
it 'returns is_present filter results properly' do
params[:payload] = [
{
attribute_key: 'labels',
filter_operator: 'is_present',
values: [],
query_operator: nil
}.with_indifferent_access
]
result = filter_service.new(params, first_user).perform
expect(result[:contacts].length).to be 2
expect(result[:contacts].first.label_list).to include('support')
expect(result[:contacts].last.label_list).to include('support')
end
it 'returns is_not_present filter results properly' do
params[:payload] = [
{
attribute_key: 'labels',
filter_operator: 'is_not_present',
values: [],
query_operator: nil
}.with_indifferent_access
]
result = filter_service.new(params, first_user).perform
expect(result[:contacts].length).to be 1
expect(result[:contacts].first.id).to eq el_contact.id
end
end
it 'filter contacts by additional_attributes' do
params[:payload] = payload
result = filter_service.new(params, first_user).perform
@@ -83,25 +146,7 @@ describe Contacts::FilterService do
expect(result[:contacts].first.name).to eq(en_contact.name)
end
it 'filter contact by tags' do
label_id = cs_contact.labels.last.id
params[:payload] = [
{
attribute_key: 'labels',
filter_operator: 'equal_to',
values: [label_id],
query_operator: nil
}.with_indifferent_access
]
result = filter_service.new(params, first_user).perform
expect(result[:contacts].length).to be 2
expect(result[:contacts].first.label_list).to include('support')
expect(result[:contacts].last.label_list).to include('support')
end
it 'filter by custom_attributes and labels' do
label_id = cs_contact.labels.last.id
params[:payload] = [
{
attribute_key: 'customer_type',
@@ -112,7 +157,7 @@ describe Contacts::FilterService do
{
attribute_key: 'labels',
filter_operator: 'equal_to',
values: [label_id],
values: ['support'],
query_operator: 'AND'
}.with_indifferent_access,
{