Skip to content

Commit

Permalink
Fix: Agent Reports counts when they have access to multiple accounts (#…
Browse files Browse the repository at this point in the history
…4663)

This change restricts the agent report API to fetch agent metrics from the current account.

Fixes: #4660
  • Loading branch information
aswindevps committed May 11, 2022
1 parent 329d305 commit 41b8901
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
16 changes: 8 additions & 8 deletions app/helpers/report_helper.rb
Expand Up @@ -17,38 +17,38 @@ def scope
end

def conversations_count
(get_grouped_values scope.conversations).count
(get_grouped_values scope.conversations.where(account_id: account.id)).count
end

def incoming_messages_count
(get_grouped_values scope.messages.incoming.unscope(:order)).count
(get_grouped_values scope.messages.where(account_id: account.id).incoming.unscope(:order)).count
end

def outgoing_messages_count
(get_grouped_values scope.messages.outgoing.unscope(:order)).count
(get_grouped_values scope.messages.where(account_id: account.id).outgoing.unscope(:order)).count
end

def resolutions_count
(get_grouped_values scope.conversations.resolved).count
(get_grouped_values scope.conversations.where(account_id: account.id).resolved).count
end

def avg_first_response_time
grouped_reporting_events = (get_grouped_values scope.reporting_events.where(name: 'first_response'))
grouped_reporting_events = (get_grouped_values scope.reporting_events.where(name: 'first_response', account_id: account.id))
return grouped_reporting_events.average(:value_in_business_hours) if params[:business_hours]

grouped_reporting_events.average(:value)
end

def avg_resolution_time
grouped_reporting_events = (get_grouped_values scope.reporting_events.where(name: 'conversation_resolved'))
grouped_reporting_events = (get_grouped_values scope.reporting_events.where(name: 'conversation_resolved', account_id: account.id))
return grouped_reporting_events.average(:value_in_business_hours) if params[:business_hours]

grouped_reporting_events.average(:value)
end

def avg_resolution_time_summary
reporting_events = scope.reporting_events
.where(name: 'conversation_resolved', created_at: range)
.where(name: 'conversation_resolved', account_id: account.id, created_at: range)
avg_rt = params[:business_hours] ? reporting_events.average(:value_in_business_hours) : reporting_events.average(:value)

return 0 if avg_rt.blank?
Expand All @@ -58,7 +58,7 @@ def avg_resolution_time_summary

def avg_first_response_time_summary
reporting_events = scope.reporting_events
.where(name: 'first_response', created_at: range)
.where(name: 'first_response', account_id: account.id, created_at: range)
avg_frt = params[:business_hours] ? reporting_events.average(:value_in_business_hours) : reporting_events.average(:value)

return 0 if avg_frt.blank?
Expand Down
34 changes: 34 additions & 0 deletions spec/controllers/api/v2/accounts/report_controller_spec.rb
Expand Up @@ -223,6 +223,40 @@
expect(response).to have_http_status(:success)
end
end

context 'when an agent has access to multiple accounts' do
let(:account1) { create(:account) }
let(:account2) { create(:account) }

let(:params) do
super().merge(
type: :agent,
since: 30.days.ago.to_i.to_s,
until: date_timestamp.to_s
)
end

it 'returns agent metrics from the current account' do
admin1 = create(:user, account: account1, role: :administrator)
inbox1 = create(:inbox, account: account1)
inbox2 = create(:inbox, account: account2)

create(:account_user, user: admin1, account: account2)
create(:conversation, account: account1, inbox: inbox1,
assignee: admin1, created_at: Time.zone.today - 2.days)
create(:conversation, account: account2, inbox: inbox2,
assignee: admin1, created_at: Time.zone.today - 2.days)

get "/api/v2/accounts/#{account1.id}/reports/summary",
params: params.merge({ id: admin1.id }),
headers: admin1.create_new_auth_token

expect(response).to have_http_status(:success)

json_response = JSON.parse(response.body)
expect(json_response['conversations_count']).to eq(1)
end
end
end

describe 'GET /api/v2/accounts/:account_id/reports/inboxes' do
Expand Down

0 comments on commit 41b8901

Please sign in to comment.