diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index f083a4ce7..c03851099 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -5,70 +5,58 @@ class ReportsController < ApplicationController def index; end def features - forms = Reports::FormDocumentsService.live_form_documents + tag = params[:tag] + forms = Reports::FormDocumentsService.form_documents(tag:) data = Reports::FeatureReportService.new(forms).report - render template: "reports/features", locals: { data: } + render template: "reports/features", locals: { tag:, data: } end def questions_with_answer_type + tag = params[:tag] answer_type = params.require(:answer_type) - forms = Reports::FormDocumentsService.live_form_documents + forms = Reports::FormDocumentsService.form_documents(tag:) 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 - forms = Reports::FormDocumentsService.live_form_documents - questions = Reports::FeatureReportService.new(forms).questions_with_add_another_answer - 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')}" + disposition: "attachment; filename=#{questions_csv_filename(tag, answer_type)}" else - render template: "reports/feature_report", locals: { report: params[:action], records: questions } + render template: "reports/questions_with_answer_type", locals: { tag:, answer_type:, questions: } end end + def questions_with_add_another_answer + tag = params[:tag] + forms = Reports::FormDocumentsService.form_documents(tag:) + questions = Reports::FeatureReportService.new(forms).questions_with_add_another_answer + + questions_feature_report(tag, params[:action], questions) + end + def forms_with_routes - forms = Reports::FormDocumentsService.live_form_documents + tag = params[:tag] + forms = Reports::FormDocumentsService.form_documents(tag:) forms = Reports::FeatureReportService.new(forms).forms_with_routes - 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 + forms_feature_report(tag, params[:action], forms) end def forms_with_payments - forms = Reports::FormDocumentsService.live_form_documents + tag = params[:tag] + forms = Reports::FormDocumentsService.form_documents(tag:) forms = Reports::FeatureReportService.new(forms).forms_with_payments - 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 + forms_feature_report(tag, params[:action], forms) end def forms_with_csv_submission_enabled - forms = Reports::FormDocumentsService.live_form_documents + tag = params[:tag] + forms = Reports::FormDocumentsService.form_documents(tag:) forms = Reports::FeatureReportService.new(forms).forms_with_csv_submission_enabled - 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 + forms_feature_report(tag, params[:action], forms) end def users @@ -120,27 +108,42 @@ def live_forms_csv end def live_questions_csv - answer_type = params[: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 + questions = Reports::FeatureReportService.new(forms).questions send_data Reports::QuestionsCsvReportService.new(questions).csv, type: "text/csv; charset=iso-8859-1", - disposition: "attachment; filename=#{questions_csv_filename(answer_type)}" + disposition: "attachment; filename=#{csv_filename('live_questions_report')}" end private + def questions_feature_report(tag, report, questions) + if params[:format] == "csv" + send_data Reports::QuestionsCsvReportService.new(questions).csv, + type: "text/csv; charset=iso-8859-1", + disposition: "attachment; filename=#{csv_filename("#{tag}_#{report}_report")}" + else + render template: "reports/feature_report", locals: { tag:, report:, records: questions } + end + end + + def forms_feature_report(tag, report, forms) + if params[:format] == "csv" + send_data Reports::FormsCsvReportService.new(forms).csv, + type: "text/csv; charset=iso-8859-1", + disposition: "attachment; filename=#{csv_filename("#{tag}_#{report}_report")}" + else + render template: "reports/feature_report", locals: { tag:, report:, records: forms } + end + end + def check_user_has_permission authorize Report, :can_view_reports? end - def questions_csv_filename(answer_type) - base_name = "live_questions_report" + def questions_csv_filename(tag, answer_type) + base_name = "#{tag}_questions_report" base_name += "_#{answer_type}_answer_type" if answer_type.present? csv_filename(base_name) end diff --git a/app/services/reports/form_documents_service.rb b/app/services/reports/form_documents_service.rb index 557c47531..b99f67447 100644 --- a/app/services/reports/form_documents_service.rb +++ b/app/services/reports/form_documents_service.rb @@ -8,11 +8,19 @@ class << self FormDocumentsResponse = Data.define(:forms, :has_more_results?) + def draft_form_documents + form_documents(tag: "draft") + end + def live_form_documents + form_documents(tag: "live") + end + + def form_documents(tag:) Enumerator.new do |yielder| page = 1 loop do - form_documents_response = live_form_documents_page(page) + form_documents_response = form_documents_page(tag:, page:) form_documents_response.forms.each { |f| yielder << f unless is_in_internal_organisation?(f) } break unless form_documents_response.has_more_results? @@ -36,9 +44,9 @@ def has_csv_submission_enabled?(form_document) private - def live_form_documents_page(page) + def form_documents_page(tag:, page:) uri = URI(FORM_DOCUMENTS_URL) - params = { tag: "live", page:, per_page: Settings.reports.forms_api_forms_per_request_page } + params = { tag:, page:, per_page: Settings.reports.forms_api_forms_per_request_page } uri.query = URI.encode_www_form(params) response = Net::HTTP.get_response(uri, REQUEST_HEADERS) diff --git a/app/views/reports/feature_report.html.erb b/app/views/reports/feature_report.html.erb index f71186f24..a1ce1a80d 100644 --- a/app/views/reports/feature_report.html.erb +++ b/app/views/reports/feature_report.html.erb @@ -1,13 +1,19 @@ -<% set_page_title(t("reports.#{report}.heading"))%> +<% set_page_title(t("reports.#{report}.heading", tag:).upcase_first)%> <% content_for :back_link, govuk_back_link_to(report_features_path, t("reports.back_to_feature_usage")) %>
-

<%= t("reports.#{report}.heading")%>

+

<%= t("reports.#{report}.heading", tag:).upcase_first %>

-

<%=govuk_link_to(t("reports.#{report}.download_csv"), url_for(format: :csv))%>

+ <% unless records.empty? %> +

<%=govuk_link_to(t("reports.#{report}.download_csv", tag:).upcase_first, url_for(format: :csv))%>

+ <% end %>
- <%= govuk_table(**report_table(records)) %> + <% if records.empty? %> +

<%= t("reports.#{report}.empty", tag:) %>

+ <% else %> + <%= govuk_table(**report_table(records)) %> + <% end %>
diff --git a/app/views/reports/features.html.erb b/app/views/reports/features.html.erb index 725646a7c..dcecea0fb 100644 --- a/app/views/reports/features.html.erb +++ b/app/views/reports/features.html.erb @@ -1,29 +1,29 @@ -<% set_page_title(t(".title")) %> +<% set_page_title(t(".title", tag:)) %> <% content_for :back_link, govuk_back_link_to(reports_path, t("reports.back_link")) %>
-

<%= t(".title") %>

+

<%= t(".title", tag:) %>

<%= t(".features.heading") %>

<%= govuk_summary_list do |summary_list| %> <%= summary_list.with_row do |row| %> - <%= row.with_key(text: t(".features.total_live_forms")) %> + <%= row.with_key(text: t(".features.total_forms", tag:).upcase_first) %> <%= row.with_value(text: data[:total_forms]) %> <% end %> <%= summary_list.with_row do |row| %> - <%= row.with_key(text: t(".features.live_forms_with_routes")) %> + <%= row.with_key(text: t(".features.forms_with_routes", tag:).upcase_first) %> <%= row.with_value(text: govuk_link_to(data[:forms_with_routing], report_forms_with_routes_path, no_visited_state: true)) %> <% end %> <%= summary_list.with_row do |row| %> - <%= row.with_key(text: t(".features.live_forms_with_payments")) %> + <%= row.with_key(text: t(".features.forms_with_payments", tag:).upcase_first) %> <%= row.with_value(text: govuk_link_to(data[:forms_with_payment], report_forms_with_payments_path, no_visited_state: true)) %> <% end %> <%= summary_list.with_row do |row| %> - <%= row.with_key(text: t(".features.live_forms_with_add_another_answer")) %> + <%= row.with_key(text: t(".features.forms_with_add_another_answer", tag:).upcase_first) %> <%= row.with_value(text: govuk_link_to(data[:forms_with_add_another_answer], report_questions_with_add_another_answer_path, no_visited_state: true)) %> <% end %> <%= summary_list.with_row do |row| %> - <%= row.with_key(text: t(".features.live_forms_with_csv_submission_enabled")) %> + <%= row.with_key(text: t(".features.forms_with_csv_submission_enabled", tag:).upcase_first) %> <%= row.with_value(text: govuk_link_to(data[:forms_with_csv_submission_enabled], report_forms_with_csv_submission_enabled_path, no_visited_state: true)) %> <% end %> <% end %> @@ -35,12 +35,14 @@ <%= table.with_head do |head| %> <%= head.with_row do |row| %> <%= row.with_cell(text: t(".answer_types.table_headings.answer_type")) %> - <%= row.with_cell(text: t(".answer_types.table_headings.number_of_forms"), numeric: true) %> - <%= row.with_cell(text: t(".answer_types.table_headings.number_of_pages"), numeric: true) %> + <%= row.with_cell(text: t(".answer_types.table_headings.number_of_forms", tag:), numeric: true) %> + <%= row.with_cell(text: t(".answer_types.table_headings.number_of_pages", tag:), numeric: true) %> <% end %> <% end %> - <% answer_type_links = { "selection" => report_selection_questions_summary_path } %> + <% answer_type_links = { + "selection" => tag == "live" ? report_selection_questions_summary_path : nil, + } %> <%= table.with_body do |body| %> <% Page::ANSWER_TYPES.each do |answer_type| %> <%= body.with_row do |row| %> diff --git a/app/views/reports/index.html.erb b/app/views/reports/index.html.erb index 4a6cadd08..5a1000543 100644 --- a/app/views/reports/index.html.erb +++ b/app/views/reports/index.html.erb @@ -4,7 +4,8 @@

<%= t(".title") %>