feat: API to Filter reports by teams (#3066)

Add API to Filter reports by teams

Fixes: #2916
This commit is contained in:
Aswin Dev P.S 2021-09-27 21:12:08 +05:30 committed by GitHub
parent 8b7f6c691a
commit 15aaa8883c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 88 additions and 9 deletions

View file

@ -41,19 +41,25 @@ class V2::ReportBuilder
user user
when :label when :label
label label
when :team
team
end end
end end
def inbox def inbox
@inbox ||= account.inboxes.where(id: params[:id]).first @inbox ||= account.inboxes.find(params[:id])
end end
def user def user
@user ||= account.users.where(id: params[:id]).first @user ||= account.users.find(params[:id])
end end
def label def label
@label ||= account.labels.where(id: params[:id]).first @label ||= account.labels.find(params[:id])
end
def team
@team ||= account.teams.find(params[:id])
end end
def conversations_count def conversations_count

View file

@ -29,6 +29,12 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController
render layout: false, template: 'api/v2/accounts/reports/labels.csv.erb', format: 'csv' render layout: false, template: 'api/v2/accounts/reports/labels.csv.erb', format: 'csv'
end end
def teams
response.headers['Content-Type'] = 'text/csv'
response.headers['Content-Disposition'] = 'attachment; filename=teams_report.csv'
render layout: false, template: 'api/v2/accounts/reports/teams.csv.erb', format: 'csv'
end
private private
def check_authorization def check_authorization

View file

@ -40,4 +40,12 @@ class Team < ApplicationRecord
def remove_member(user_id) def remove_member(user_id)
team_members.find_by(user_id: user_id)&.destroy team_members.find_by(user_id: user_id)&.destroy
end end
def messages
account.messages.where(conversation_id: conversations.pluck(:id))
end
def events
account.events.where(conversation_id: conversations.pluck(:id))
end
end end

View file

@ -0,0 +1,19 @@
<% headers = [
I18n.t('reports.team_csv.team_name'),
I18n.t('reports.team_csv.conversations_count'),
I18n.t('reports.team_csv.avg_first_response_time'),
I18n.t('reports.team_csv.avg_resolution_time')
]
%>
<%= CSV.generate_line headers %>
<% Current.account.teams.each do |team| %>
<% team_report = V2::ReportBuilder.new(Current.account, {
type: :team,
id: team.id,
since: params[:since],
until: params[:until]
}).summary %>
<% row = [ team.name, team_report[:conversations_count], (team_report[:avg_first_response_time]/60).to_i, (team_report[:avg_resolution_time]/60).to_i ] %>
<%= CSV.generate_line row %>
<% end %>
<%= CSV.generate_line [I18n.t('reports.period', since: Date.strptime(params[:since], '%s'), until: Date.strptime(params[:until], '%s'))] %>

View file

@ -52,6 +52,11 @@ en:
conversations_count: Conversations count conversations_count: Conversations count
avg_first_response_time: Avg first response time (Minutes) avg_first_response_time: Avg first response time (Minutes)
avg_resolution_time: Avg resolution time (Minutes) avg_resolution_time: Avg resolution time (Minutes)
team_csv:
team_name: Team name
conversations_count: Conversations count
avg_first_response_time: Avg first response time (Minutes)
avg_resolution_time: Avg resolution time (Minutes)
notifications: notifications:
notification_title: notification_title:

View file

@ -188,6 +188,7 @@ Rails.application.routes.draw do
get :agents get :agents
get :inboxes get :inboxes
get :labels get :labels
get :teams
end end
end end
end end

View file

@ -192,4 +192,37 @@ RSpec.describe 'Reports API', type: :request do
end end
end end
end end
describe 'GET /api/v2/accounts/:account_id/reports/teams' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v2/accounts/#{account.id}/reports/teams.csv"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
params = {
since: 30.days.ago.to_i.to_s,
until: Time.zone.today.to_time.to_i.to_s
}
it 'returns unauthorized for teams' do
get "/api/v2/accounts/#{account.id}/reports/teams.csv",
params: params,
headers: agent.create_new_auth_token
expect(response).to have_http_status(:unauthorized)
end
it 'returns summary' do
get "/api/v2/accounts/#{account.id}/reports/teams.csv",
params: params,
headers: admin.create_new_auth_token
expect(response).to have_http_status(:success)
end
end
end
end end

View file

@ -2,6 +2,6 @@ in: query
name: report_type name: report_type
schema: schema:
type: string type: string
enum: [account,agent,inbox,label] enum: [account,agent,inbox,label,team]
required: true required: true
description: Type of report description: Type of report

View file

@ -314,7 +314,7 @@ public/api/v1/inboxes/{inbox_identifier}/contacts/{contact_identifier}/conversat
### Reports ### Reports
# List # List
/api/v1/accounts/{id}/reports: /api/v2/accounts/{id}/reports:
parameters: parameters:
- $ref: '#/parameters/account_id' - $ref: '#/parameters/account_id'
- $ref: '#/parameters/report_metric' - $ref: '#/parameters/report_metric'
@ -338,7 +338,7 @@ public/api/v1/inboxes/{inbox_identifier}/contacts/{contact_identifier}/conversat
$ref: './reports/index.yml' $ref: './reports/index.yml'
# Summary # Summary
/api/v1/accounts/{id}/reports/summary: /api/v2/accounts/{id}/reports/summary:
parameters: parameters:
- $ref: '#/parameters/account_id' - $ref: '#/parameters/account_id'
- $ref: '#/parameters/report_type' - $ref: '#/parameters/report_type'

View file

@ -3088,7 +3088,7 @@
} }
} }
}, },
"/api/v1/accounts/{id}/reports": { "/api/v2/accounts/{id}/reports": {
"parameters": [ "parameters": [
{ {
"$ref": "#/parameters/account_id" "$ref": "#/parameters/account_id"
@ -3151,7 +3151,7 @@
} }
} }
}, },
"/api/v1/accounts/{id}/reports/summary": { "/api/v2/accounts/{id}/reports/summary": {
"parameters": [ "parameters": [
{ {
"$ref": "#/parameters/account_id" "$ref": "#/parameters/account_id"
@ -4498,7 +4498,8 @@
"account", "account",
"agent", "agent",
"inbox", "inbox",
"label" "label",
"team"
] ]
}, },
"required": true, "required": true,