diff --git a/app/builders/v2/report_builder.rb b/app/builders/v2/report_builder.rb index 30804b8e8..33b2db820 100644 --- a/app/builders/v2/report_builder.rb +++ b/app/builders/v2/report_builder.rb @@ -41,19 +41,25 @@ class V2::ReportBuilder user when :label label + when :team + team end end def inbox - @inbox ||= account.inboxes.where(id: params[:id]).first + @inbox ||= account.inboxes.find(params[:id]) end def user - @user ||= account.users.where(id: params[:id]).first + @user ||= account.users.find(params[:id]) end 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 def conversations_count diff --git a/app/controllers/api/v2/accounts/reports_controller.rb b/app/controllers/api/v2/accounts/reports_controller.rb index af28fe544..4fabd587e 100644 --- a/app/controllers/api/v2/accounts/reports_controller.rb +++ b/app/controllers/api/v2/accounts/reports_controller.rb @@ -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' 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 def check_authorization diff --git a/app/models/team.rb b/app/models/team.rb index 9f6c2f3ed..1b998eb4c 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -40,4 +40,12 @@ class Team < ApplicationRecord def remove_member(user_id) team_members.find_by(user_id: user_id)&.destroy end + + def messages + account.messages.where(conversation_id: conversations.pluck(:id)) + end + + def events + account.events.where(conversation_id: conversations.pluck(:id)) + end end diff --git a/app/views/api/v2/accounts/reports/teams.csv.erb b/app/views/api/v2/accounts/reports/teams.csv.erb new file mode 100644 index 000000000..55c679273 --- /dev/null +++ b/app/views/api/v2/accounts/reports/teams.csv.erb @@ -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'))] %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 494622381..12cb8ce0f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -52,6 +52,11 @@ en: conversations_count: Conversations count avg_first_response_time: Avg first response 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: notification_title: diff --git a/config/routes.rb b/config/routes.rb index 36fc15e3d..a1c4ceff5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -188,6 +188,7 @@ Rails.application.routes.draw do get :agents get :inboxes get :labels + get :teams end end end diff --git a/spec/controllers/api/v2/accounts/report_controller_spec.rb b/spec/controllers/api/v2/accounts/report_controller_spec.rb index 201c53f16..123119fb5 100644 --- a/spec/controllers/api/v2/accounts/report_controller_spec.rb +++ b/spec/controllers/api/v2/accounts/report_controller_spec.rb @@ -192,4 +192,37 @@ RSpec.describe 'Reports API', type: :request do 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 diff --git a/swagger/parameters/report_type.yml b/swagger/parameters/report_type.yml index 6c78f964d..407478d9d 100644 --- a/swagger/parameters/report_type.yml +++ b/swagger/parameters/report_type.yml @@ -2,6 +2,6 @@ in: query name: report_type schema: type: string - enum: [account,agent,inbox,label] + enum: [account,agent,inbox,label,team] required: true description: Type of report diff --git a/swagger/paths/index.yml b/swagger/paths/index.yml index 2fb77e254..4324b2504 100644 --- a/swagger/paths/index.yml +++ b/swagger/paths/index.yml @@ -314,7 +314,7 @@ public/api/v1/inboxes/{inbox_identifier}/contacts/{contact_identifier}/conversat ### Reports # List -/api/v1/accounts/{id}/reports: +/api/v2/accounts/{id}/reports: parameters: - $ref: '#/parameters/account_id' - $ref: '#/parameters/report_metric' @@ -338,7 +338,7 @@ public/api/v1/inboxes/{inbox_identifier}/contacts/{contact_identifier}/conversat $ref: './reports/index.yml' # Summary -/api/v1/accounts/{id}/reports/summary: +/api/v2/accounts/{id}/reports/summary: parameters: - $ref: '#/parameters/account_id' - $ref: '#/parameters/report_type' diff --git a/swagger/swagger.json b/swagger/swagger.json index c1ffa0835..f4aad11f9 100644 --- a/swagger/swagger.json +++ b/swagger/swagger.json @@ -3088,7 +3088,7 @@ } } }, - "/api/v1/accounts/{id}/reports": { + "/api/v2/accounts/{id}/reports": { "parameters": [ { "$ref": "#/parameters/account_id" @@ -3151,7 +3151,7 @@ } } }, - "/api/v1/accounts/{id}/reports/summary": { + "/api/v2/accounts/{id}/reports/summary": { "parameters": [ { "$ref": "#/parameters/account_id" @@ -4498,7 +4498,8 @@ "account", "agent", "inbox", - "label" + "label", + "team" ] }, "required": true,