feat: Add filter APIs for Contacts and Conversations (#3264)
This commit is contained in:
@@ -233,10 +233,12 @@ RSpec.describe 'Contacts API', type: :request do
|
||||
let!(:contact2) { create(:contact, :with_email, name: 'testcontact', account: account, email: 'test@test.com') }
|
||||
|
||||
it 'returns all contacts when query is empty' do
|
||||
get "/api/v1/accounts/#{account.id}/contacts/filter",
|
||||
params: { q: [] },
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
post "/api/v1/accounts/#{account.id}/contacts/filter",
|
||||
params: {
|
||||
payload: []
|
||||
},
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.body).to include(contact2.email)
|
||||
|
||||
@@ -112,7 +112,7 @@ RSpec.describe 'Conversations API', type: :request do
|
||||
describe 'GET /api/v1/accounts/{account.id}/conversations/filter' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
get "/api/v1/accounts/#{account.id}/conversations/filter", params: { q: 'test' }
|
||||
post "/api/v1/accounts/#{account.id}/conversations/filter", params: { q: 'test' }
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
@@ -129,16 +129,15 @@ RSpec.describe 'Conversations API', type: :request do
|
||||
end
|
||||
|
||||
it 'returns all conversations with empty query' do
|
||||
get "/api/v1/accounts/#{account.id}/conversations/filter",
|
||||
headers: agent.create_new_auth_token,
|
||||
params: { q: 'test1' },
|
||||
as: :json
|
||||
post "/api/v1/accounts/#{account.id}/conversations/filter",
|
||||
headers: agent.create_new_auth_token,
|
||||
params: { payload: [] },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
response_data = JSON.parse(response.body, symbolize_names: true)
|
||||
|
||||
expect(response_data.count).to eq(1)
|
||||
expect(response_data[0][:messages][0][:content]).to include(Message.first.content)
|
||||
expect(response_data.count).to eq(2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
56
spec/services/contacts/filter_service_spec.rb
Normal file
56
spec/services/contacts/filter_service_spec.rb
Normal file
@@ -0,0 +1,56 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe ::Contacts::FilterService do
|
||||
subject(:filter_service) { described_class }
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:user_1) { create(:user, account: account) }
|
||||
let!(:user_2) { create(:user, account: account) }
|
||||
let!(:inbox) { create(:inbox, account: account, enable_auto_assignment: false) }
|
||||
let!(:contact) { create(:contact, account: account, additional_attributes: { 'browser_language': 'en' }) }
|
||||
|
||||
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, contact: contact)
|
||||
create(:conversation, account: account, inbox: inbox)
|
||||
Current.account = account
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
context 'with query present' do
|
||||
let!(:params) { { payload: [], page: 1 } }
|
||||
let(:payload) do
|
||||
[
|
||||
{
|
||||
attribute_key: 'browser_language',
|
||||
filter_operator: 'equal_to',
|
||||
values: ['en'],
|
||||
query_operator: nil
|
||||
}.with_indifferent_access
|
||||
]
|
||||
end
|
||||
|
||||
it 'filter contacts by additional_attributes' do
|
||||
params[:payload] = payload
|
||||
result = filter_service.new(params, user_1).perform
|
||||
expect(result.length).to be 2
|
||||
end
|
||||
|
||||
it 'filter conversations by tags' do
|
||||
Contact.last.update_labels('support')
|
||||
params[:payload] = [
|
||||
{
|
||||
attribute_key: 'labels',
|
||||
filter_operator: 'equal_to',
|
||||
values: [1],
|
||||
query_operator: nil
|
||||
}.with_indifferent_access
|
||||
]
|
||||
|
||||
result = filter_service.new(params, user_1).perform
|
||||
expect(result.length).to be 2
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
76
spec/services/conversations/filter_service_spec.rb
Normal file
76
spec/services/conversations/filter_service_spec.rb
Normal file
@@ -0,0 +1,76 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe ::Conversations::FilterService do
|
||||
subject(:filter_service) { described_class }
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:user_1) { create(:user, account: account) }
|
||||
let!(:user_2) { create(:user, 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,
|
||||
status: 'pending', additional_attributes: { 'browser_language': 'en' })
|
||||
create(:conversation, account: account, inbox: inbox, assignee: user_1,
|
||||
status: 'pending', additional_attributes: { 'browser_language': 'en' })
|
||||
create(:conversation, account: account, inbox: inbox, assignee: user_2)
|
||||
# unassigned conversation
|
||||
create(:conversation, account: account, inbox: inbox)
|
||||
Current.account = account
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
context 'with query present' do
|
||||
let!(:params) { { payload: [], page: 1 } }
|
||||
let(:payload) do
|
||||
[
|
||||
{
|
||||
attribute_key: 'browser_language',
|
||||
filter_operator: 'equal_to',
|
||||
values: ['en'],
|
||||
query_operator: 'AND'
|
||||
}.with_indifferent_access,
|
||||
{
|
||||
attribute_key: 'status',
|
||||
filter_operator: 'equal_to',
|
||||
values: %w[open pending],
|
||||
query_operator: nil
|
||||
}.with_indifferent_access
|
||||
]
|
||||
end
|
||||
|
||||
it 'filter conversations by custom_attributes and status' do
|
||||
params[:payload] = payload
|
||||
result = filter_service.new(params, user_1).perform
|
||||
conversations = Conversation.where("additional_attributes ->> 'browser_language' IN (?) AND status IN (?)", ['en'], [1, 2])
|
||||
expect(result.length).to be conversations.count
|
||||
end
|
||||
|
||||
it 'filter conversations by tags' do
|
||||
Conversation.last.update_labels('support')
|
||||
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: 'labels',
|
||||
filter_operator: 'equal_to',
|
||||
values: [1],
|
||||
query_operator: nil
|
||||
}.with_indifferent_access
|
||||
]
|
||||
result = filter_service.new(params, user_1).perform
|
||||
expect(result.length).to be 2
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user