2020-06-07 08:28:05 +00:00
|
|
|
class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController
|
2022-05-27 03:56:59 +00:00
|
|
|
include Api::V2::Accounts::ReportsHelper
|
2021-06-11 06:14:31 +00:00
|
|
|
before_action :check_authorization
|
|
|
|
|
2021-08-27 17:16:32 +00:00
|
|
|
def index
|
|
|
|
builder = V2::ReportBuilder.new(Current.account, report_params)
|
2020-03-18 11:23:35 +00:00
|
|
|
data = builder.build
|
|
|
|
render json: data
|
|
|
|
end
|
|
|
|
|
2021-08-27 17:16:32 +00:00
|
|
|
def summary
|
|
|
|
render json: summary_metrics
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|
|
|
|
|
2020-11-16 14:11:52 +00:00
|
|
|
def agents
|
2022-05-27 03:56:59 +00:00
|
|
|
@report_data = generate_agents_report
|
|
|
|
generate_csv('agents_report', 'api/v2/accounts/reports/agents.csv.erb')
|
2020-11-16 14:11:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def inboxes
|
2022-05-27 03:56:59 +00:00
|
|
|
@report_data = generate_inboxes_report
|
|
|
|
generate_csv('inboxes_report', 'api/v2/accounts/reports/inboxes.csv.erb')
|
2020-11-16 14:11:52 +00:00
|
|
|
end
|
|
|
|
|
2021-08-27 17:16:32 +00:00
|
|
|
def labels
|
2022-05-27 03:56:59 +00:00
|
|
|
@report_data = generate_labels_report
|
|
|
|
generate_csv('labels_report', 'api/v2/accounts/reports/labels.csv.erb')
|
2021-08-27 17:16:32 +00:00
|
|
|
end
|
|
|
|
|
2021-09-27 15:42:08 +00:00
|
|
|
def teams
|
2022-05-27 03:56:59 +00:00
|
|
|
@report_data = generate_teams_report
|
|
|
|
generate_csv('teams_report', 'api/v2/accounts/reports/teams.csv.erb')
|
2021-09-27 15:42:08 +00:00
|
|
|
end
|
|
|
|
|
2022-03-29 05:01:52 +00:00
|
|
|
def conversations
|
|
|
|
return head :unprocessable_entity if params[:type].blank?
|
|
|
|
|
|
|
|
render json: conversation_metrics
|
|
|
|
end
|
|
|
|
|
2020-03-18 11:23:35 +00:00
|
|
|
private
|
|
|
|
|
2022-05-27 03:56:59 +00:00
|
|
|
def generate_csv(filename, template)
|
|
|
|
response.headers['Content-Type'] = 'text/csv'
|
|
|
|
response.headers['Content-Disposition'] = "attachment; filename=#{filename}.csv"
|
|
|
|
render layout: false, template: template, format: 'csv'
|
|
|
|
end
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
def check_authorization
|
|
|
|
raise Pundit::NotAuthorizedError unless Current.account_user.administrator?
|
|
|
|
end
|
|
|
|
|
2022-04-08 07:18:18 +00:00
|
|
|
def common_params
|
2020-03-18 11:23:35 +00:00
|
|
|
{
|
2021-08-27 17:16:32 +00:00
|
|
|
type: params[:type].to_sym,
|
2022-02-15 11:40:49 +00:00
|
|
|
id: params[:id],
|
2022-04-08 07:18:18 +00:00
|
|
|
group_by: params[:group_by],
|
|
|
|
business_hours: ActiveModel::Type::Boolean.new.cast(params[:business_hours])
|
2022-03-14 12:45:27 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-04-08 07:18:18 +00:00
|
|
|
def current_summary_params
|
|
|
|
common_params.merge({
|
|
|
|
since: range[:current][:since],
|
|
|
|
until: range[:current][:until]
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-03-14 12:45:27 +00:00
|
|
|
def previous_summary_params
|
2022-04-08 07:18:18 +00:00
|
|
|
common_params.merge({
|
|
|
|
since: range[:previous][:since],
|
|
|
|
until: range[:previous][:until]
|
|
|
|
})
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|
|
|
|
|
2021-08-27 17:16:32 +00:00
|
|
|
def report_params
|
2022-04-08 07:18:18 +00:00
|
|
|
common_params.merge({
|
|
|
|
metric: params[:metric],
|
|
|
|
since: params[:since],
|
|
|
|
until: params[:until],
|
|
|
|
timezone_offset: params[:timezone_offset]
|
|
|
|
})
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|
|
|
|
|
2022-03-29 05:01:52 +00:00
|
|
|
def conversation_params
|
|
|
|
{
|
|
|
|
type: params[:type].to_sym,
|
2022-04-25 14:34:41 +00:00
|
|
|
user_id: params[:user_id],
|
|
|
|
page: params[:page].presence || 1
|
2022-03-29 05:01:52 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-03-14 12:45:27 +00:00
|
|
|
def range
|
|
|
|
{
|
|
|
|
current: {
|
|
|
|
since: params[:since],
|
|
|
|
until: params[:until]
|
|
|
|
},
|
|
|
|
previous: {
|
|
|
|
since: (params[:since].to_i - (params[:until].to_i - params[:since].to_i)).to_s,
|
|
|
|
until: params[:since]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-08-27 17:16:32 +00:00
|
|
|
def summary_metrics
|
2022-03-14 12:45:27 +00:00
|
|
|
summary = V2::ReportBuilder.new(Current.account, current_summary_params).summary
|
|
|
|
summary[:previous] = V2::ReportBuilder.new(Current.account, previous_summary_params).summary
|
|
|
|
summary
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|
2022-03-29 05:01:52 +00:00
|
|
|
|
|
|
|
def conversation_metrics
|
|
|
|
V2::ReportBuilder.new(Current.account, conversation_params).conversation_metrics
|
|
|
|
end
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|