set count of the conversations and updated specs

This commit is contained in:
Tejaswini 2021-10-25 12:00:07 +05:30
parent e2cb2c34fa
commit 01f1d5216f
6 changed files with 162 additions and 110 deletions

View file

@ -33,13 +33,14 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro
def filter def filter
@conversations = ::Conversations::FilterService.new(JSON.parse(params[:body]), current_user).perform @conversations = ::Conversations::FilterService.new(JSON.parse(params[:body]), current_user).perform
@conversations_count = @conversations.count
end end
def mute def mute
@conversation.mute! @conversation.mute!
head :ok head :ok
end end
F
def unmute def unmute
@conversation.unmute! @conversation.unmute!
head :ok head :ok

View file

@ -1,6 +1,18 @@
class Conversations::FilterService < FilterService class Conversations::FilterService < FilterService
def perform def perform
conversation_query_builder @conversations = conversation_query_builder
mine_count, unassigned_count, all_count, = set_count_for_all_conversations
assigned_count = all_count - unassigned_count
{
conversations: @conversations,
count: {
mine_count: mine_count,
assigned_count: assigned_count,
unassigned_count: unassigned_count,
all_count: all_count
}
}
end end
def conversation_query_builder def conversation_query_builder

View file

@ -33,12 +33,18 @@ class FilterService
end end
def filter_values(query_hash) def filter_values(query_hash)
values = query_hash['values'].map { |x| x['name'].downcase } if query_hash['attribute_key'] == 'labels' || query_hash['attribute_key'] == 'browser_language'
query_hash['values'].map { |x| x['name'] }
if query_hash['attribute_key'] == 'status'
values.collect { |v| Conversation.statuses[v] }
else else
values query_hash['values'].map { |x| x['id'] }
end end
end end
def set_count_for_all_conversations
[
@conversations.assigned_to(@user).count,
@conversations.unassigned.count,
@conversations.count
]
end
end end

View file

@ -1,3 +1,10 @@
json.meta do
json.mine_count @conversations_count[:mine_count]
json.unassigned_count @conversations_count[:unassigned_count]
json.all_count @conversations_count[:all_count]
end
json.payload do
json.array! @conversations do |conversation| json.array! @conversations do |conversation|
json.partial! 'api/v1/models/conversation.json.jbuilder', conversation: conversation json.partial! 'api/v1/models/conversation.json.jbuilder', conversation: conversation
end end
end

View file

@ -1,6 +1,5 @@
{ {
"conversations": "conversations": {
{
"status": { "status": {
"attribute_name": "Status", "attribute_name": "Status",
"input_type": "multi_select", "input_type": "multi_select",
@ -79,8 +78,7 @@
"attribute_type": "additional_attributes" "attribute_type": "additional_attributes"
} }
}, },
"contacts": "contacts": {
{
"company_name": { "company_name": {
"attribute_name": "Company Name", "attribute_name": "Company Name",
"input_type": "multi_select", "input_type": "multi_select",
@ -94,6 +92,6 @@
"data_type": "text", "data_type": "text",
"filter_operators": [ "equal_to", "not_equal_to", "contains", "does_not_contain" ], "filter_operators": [ "equal_to", "not_equal_to", "contains", "does_not_contain" ],
"attribute_type": "additional_attributes" "attribute_type": "additional_attributes"
}, }
} }
} }

View file

@ -29,13 +29,27 @@ describe ::Conversations::FilterService do
{ {
attribute_key: 'browser_language', attribute_key: 'browser_language',
filter_operator: 'equal_to', filter_operator: 'equal_to',
values: ['en'], values: [
{
id: 0,
name: 'en'
}
],
query_operator: 'AND' query_operator: 'AND'
}, },
{ {
attribute_key: 'status', attribute_key: 'status',
filter_operator: 'equal_to', filter_operator: 'equal_to',
values: [1, 2], values: [
{
id: 0,
name: 'open'
},
{
id: 2,
name: 'pending'
}
],
query_operator: nil query_operator: nil
} }
] ]
@ -48,23 +62,37 @@ describe ::Conversations::FilterService do
end end
it 'filter conversations by tags' do it 'filter conversations by tags' do
conversations.last.labels << 'support' Conversation.last.update_labels('support')
params = [ params = [
{ {
attribute_key: 'assignee_id', attribute_key: 'assignee_id',
filter_operator: 'equal_to', filter_operator: 'equal_to',
values: [user_1.id, user_2.id], values: [
{
id: user_1.id,
name: user_1.name
},
{
id: user_2.id,
name: user_2.name
}
],
query_operator: 'AND' query_operator: 'AND'
}, },
{ {
attribute_key: 'labels', attribute_key: 'labels',
filter_operator: 'equal_to', filter_operator: 'equal_to',
values: ['support'], values: [
{
id: 1,
name: 'support'
}
],
query_operator: nil query_operator: nil
} }
] ]
result = filter_service.new(params, user_1).perform result = filter_service.new(params, user_1).perform
expect(result.length).to be 5 expect(result.length).to be 2
end end
end end
end end