Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 51 additions & 36 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,70 @@ class ReportsController < ApplicationController
def index; end

def features
data = Reports::FeatureReportService.report
forms = Reports::FormDocumentsService.live_form_documents
data = Reports::FeatureReportService.new(forms).report

render template: "reports/features", locals: { data: }
end

def questions_with_answer_type
answer_type = params.require(:answer_type)
questions = Reports::FeatureReportService.questions_with_answer_type(answer_type)
forms = Reports::FormDocumentsService.live_form_documents
questions = Reports::FeatureReportService.new(forms).questions_with_answer_type(answer_type)

render template: "reports/questions_with_answer_type", locals: { answer_type:, questions: }
end

def questions_with_add_another_answer
questions = Reports::FeatureReportService.live_questions_with_add_another_answer
forms = Reports::FormDocumentsService.live_form_documents
questions = Reports::FeatureReportService.new(forms).questions_with_add_another_answer

render template: "reports/questions_with_add_another_answer", locals: { questions: }
if params[:format] == "csv"
send_data Reports::QuestionsCsvReportService.new(questions).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_questions_with_add_another_answer_report')}"
else
render template: "reports/feature_report", locals: { report: params[:action], records: questions }
end
end

def forms_with_routes
forms = Reports::FeatureReportService.live_forms_with_routes
forms = Reports::FormDocumentsService.live_form_documents
forms = Reports::FeatureReportService.new(forms).forms_with_routes

render template: "reports/forms_with_routes", locals: { forms: forms }
if params[:format] == "csv"
send_data Reports::FormsCsvReportService.new(forms).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_forms_with_routes_report')}"
else
render template: "reports/feature_report", locals: { report: params[:action], records: forms }
end
end

def forms_with_payments
forms = Reports::FeatureReportService.live_forms_with_payments
forms = Reports::FormDocumentsService.live_form_documents
forms = Reports::FeatureReportService.new(forms).forms_with_payments

render template: "reports/forms_with_payments", locals: { forms: forms }
if params[:format] == "csv"
send_data Reports::FormsCsvReportService.new(forms).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_forms_with_payments_report')}"
else
render template: "reports/feature_report", locals: { report: params[:action], records: forms }
end
end

def forms_with_csv_submission_enabled
forms = Reports::FeatureReportService.live_forms_with_csv_submission_enabled
forms = Reports::FormDocumentsService.live_form_documents
forms = Reports::FeatureReportService.new(forms).forms_with_csv_submission_enabled

render template: "reports/forms_with_csv_submission_enabled", locals: { forms: forms }
if params[:format] == "csv"
send_data Reports::FormsCsvReportService.new(forms).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_forms_with_csv_submission_enabled_report')}"
else
render template: "reports/feature_report", locals: { report: params[:action], records: forms }
end
end

def users
Expand Down Expand Up @@ -82,42 +112,27 @@ def selection_questions_with_checkboxes
def csv_downloads; end

def live_forms_csv
send_data Reports::CsvReportsService.new.live_forms_csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_forms_report')}"
end
forms = Reports::FormDocumentsService.live_form_documents

def live_forms_with_routes_csv
send_data Reports::CsvReportsService.new.live_forms_with_routes_csv,
send_data Reports::FormsCsvReportService.new(forms).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_forms_with_routes_report')}"
end

def live_forms_with_payments_csv
send_data Reports::CsvReportsService.new.live_forms_with_payments_csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_forms_with_payments_report')}"
end

def live_forms_with_csv_submission_enabled_csv
send_data Reports::CsvReportsService.new.live_forms_with_csv_submission_enabled_csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_forms_with_csv_submission_enabled_report')}"
disposition: "attachment; filename=#{csv_filename('live_forms_report')}"
end

def live_questions_csv
answer_type = params[:answer_type]
send_data Reports::CsvReportsService.new.live_questions_csv(answer_type:),
forms = Reports::FormDocumentsService.live_form_documents
questions = if answer_type
Reports::FeatureReportService.new(forms).questions_with_answer_type(answer_type)
else
Reports::FeatureReportService.new(forms).questions
end

send_data Reports::QuestionsCsvReportService.new(questions).csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{questions_csv_filename(answer_type)}"
end

def live_questions_with_add_another_answer_csv
send_data Reports::CsvReportsService.new.live_questions_with_add_another_answer_csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{csv_filename('live_questions_with_add_another_answer_report')}"
end

private

def check_user_has_permission
Expand Down
92 changes: 92 additions & 0 deletions app/helpers/report_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module ReportHelper
def report_table(records)
type = records.first.fetch("type", "form")

if type == "form"
with_routes = records.first.dig("metadata", "number_of_routes").present?

with_routes ? report_forms_with_routes_table(records) : report_forms_table(records)
elsif type == "question_page"
report_questions_table(records)
else
raise "type of records '#{type}' is not one of 'forms', 'question_page'"
end
end

def report_forms_table(forms)
{
head: report_forms_table_head,
rows: report_forms_table_rows(forms),
}
end

def report_forms_with_routes_table(forms)
{
head: report_forms_with_routes_table_head,
rows: report_forms_with_routes_table_rows(forms),
}
end

def report_questions_table(questions)
{
head: report_questions_table_head,
rows: report_questions_table_rows(questions),
}
end

def report_forms_table_head
[
I18n.t("reports.form_or_questions_list_table.headings.form_name"),
I18n.t("reports.form_or_questions_list_table.headings.organisation"),
]
end

def report_forms_table_rows(forms)
forms.map { |form| report_forms_table_row(form) }
end

def report_forms_with_routes_table_head
[
*report_forms_table_head,
I18n.t("reports.form_or_questions_list_table.headings.number_of_routes"),
]
end

def report_forms_with_routes_table_rows(forms)
forms.map { |form| report_forms_with_routes_table_row(form) }
end

def report_questions_table_head
[
*report_forms_table_head,
I18n.t("reports.form_or_questions_list_table.headings.question_text"),
]
end

def report_questions_table_rows(questions)
questions.map { |question| report_questions_table_row(question) }
end

private

def report_forms_table_row(form)
[
govuk_link_to(form["content"]["name"], live_form_pages_path(form_id: form["form_id"])),
form.dig("group", "organisation", "name") || "",
]
end

def report_forms_with_routes_table_row(form)
[
*report_forms_table_row(form),
form["metadata"]["number_of_routes"].to_s,
]
end

def report_questions_table_row(question)
[
*report_forms_table_row(question["form"]),
question["data"]["question_text"],
]
end
end
Loading