Skip to content
Draft
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
49 changes: 6 additions & 43 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,18 @@ def questions_with_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')}"
else
render template: "reports/feature_report", locals: { report: params[:action], records: questions }
end
end

def forms_with_routes
forms = Reports::FormDocumentsService.live_form_documents
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
end

def forms_with_payments
forms = Reports::FormDocumentsService.live_form_documents
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
end
def feature_report
report = params[:report].underscore

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

if params[:format] == "csv"
send_data Reports::FormsCsvReportService.new(forms).csv,
send_data Reports::CsvReportService.new(records).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_#{report}_report")}"
else
render template: "reports/feature_report", locals: { report: params[:action], records: forms }
render template: "reports/feature_report", locals: { report:, records: }
end
end

Expand Down
38 changes: 38 additions & 0 deletions app/services/reports/csv_report_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Reports::CsvReportService
delegate :csv, to: :@csv_service

def initialize(records)
record_type = detect_record_type(records)
@csv_service = csv_service_class(record_type).new(records)
end

private

class NilCsvService
def initialize(_records); end

def csv
""
end
end

def csv_service_class(record_type)
return NilCsvService if record_type.nil?

"Reports::#{record_type.capitalize}CsvReportService".constantize
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fear this is might make it more difficult to understand than it makes it easier to maintain.


def detect_record_type(records)
return if records.blank?

type = records.first.fetch("type", "form")

if type == "form"
:forms
elsif type == "question_page"
:questions
else
raise "type of records '#{type}' is not one of 'forms', 'question_page'" unless type
end
end
end
36 changes: 31 additions & 5 deletions app/services/reports/feature_report_service.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
class Reports::FeatureReportService
def self.define_report(name, &block)
@reports ||= []
@reports << name

define_method(name, &block)
end

def self.matches_report?(name)
@reports.include? name.to_sym
end

class Constraint
def self.matches?(request)
name = request.params[:report].underscore
Reports::FeatureReportService.matches_report? name
end
end

private_class_method :define_report

attr_reader :form_documents

def initialize(form_documents)
@form_documents = form_documents
end

def report
def report(name = nil)
unless name.nil?
raise "'#{name}' is not a defined report" unless self.class.matches_report?(name)

return send(name)
end

report = {
total_forms: 0,
forms_with_payment: 0,
Expand Down Expand Up @@ -54,27 +80,27 @@ def questions_with_answer_type(answer_type)
end
end

def questions_with_add_another_answer
define_report :questions_with_add_another_answer do
form_documents.flat_map do |form|
form["content"]["steps"]
.select { |step| step["data"]["is_repeatable"] }
.map { |step| questions_details(form, step) }
end
end

def forms_with_routes
define_report :forms_with_routes do
form_documents
.select { |form| Reports::FormDocumentsService.has_routes?(form) }
.map { |form| form_with_routes_details(form) }
end

def forms_with_payments
define_report :forms_with_payments do
form_documents
.select { |form| Reports::FormDocumentsService.has_payments?(form) }
.map { |form| form_details(form) }
end

def forms_with_csv_submission_enabled
define_report :forms_with_csv_submission_enabled do
form_documents
.select { |form| Reports::FormDocumentsService.has_csv_submission_enabled?(form) }
.map { |form| form_details(form) }
Expand Down
8 changes: 4 additions & 4 deletions app/views/reports/features.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
<% end %>
<%= summary_list.with_row do |row| %>
<%= row.with_key(text: t(".features.live_forms_with_routes")) %>
<%= row.with_value(text: govuk_link_to(data[:forms_with_routing], report_forms_with_routes_path, no_visited_state: true)) %>
<%= row.with_value(text: govuk_link_to(data[:forms_with_routing], feature_report_path(report: "forms-with-routes"), no_visited_state: true)) %>
<% end %>
<%= summary_list.with_row do |row| %>
<%= row.with_key(text: t(".features.live_forms_with_payments")) %>
<%= row.with_value(text: govuk_link_to(data[:forms_with_payment], report_forms_with_payments_path, no_visited_state: true)) %>
<%= row.with_value(text: govuk_link_to(data[:forms_with_payment], feature_report_path(report: "forms-with-payments"), 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_value(text: govuk_link_to(data[:forms_with_add_another_answer], report_questions_with_add_another_answer_path, no_visited_state: true)) %>
<%= row.with_value(text: govuk_link_to(data[:forms_with_add_another_answer], feature_report_path(report: "questions-with-add-another-answer"), 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_value(text: govuk_link_to(data[:forms_with_csv_submission_enabled], report_forms_with_csv_submission_enabled_path, no_visited_state: true)) %>
<%= row.with_value(text: govuk_link_to(data[:forms_with_csv_submission_enabled], feature_report_path(report: "forms-with-csv-submission-enabled"), no_visited_state: true)) %>
<% end %>
<% end %>

Expand Down
7 changes: 2 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,8 @@

scope "/features" do
get "/", to: "reports#features", as: :report_features
get "questions-with-answer-type/:answer_type", to: "reports#questions_with_answer_type", as: :report_questions_with_answer_type
get "questions-with-add-another-answer", to: "reports#questions_with_add_another_answer", as: :report_questions_with_add_another_answer
get "forms-with-routes", to: "reports#forms_with_routes", as: :report_forms_with_routes
get "forms-with-payments", to: "reports#forms_with_payments", as: :report_forms_with_payments
get "forms-with-csv-submission-enabled", to: "reports#forms_with_csv_submission_enabled", as: :report_forms_with_csv_submission_enabled
get "/questions-with-answer-type/:answer_type", to: "reports#questions_with_answer_type", as: :report_questions_with_answer_type
get "/:report", constraints: Reports::FeatureReportService::Constraint, to: "reports#feature_report", as: :feature_report
end

get "users", to: "reports#users", as: :report_users
Expand Down
32 changes: 16 additions & 16 deletions spec/requests/reports_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
before do
login_as_standard_user

get report_questions_with_add_another_answer_path
get feature_report_path(report: "questions-with-add-another-answer")
end

it "returns http code 403" do
Expand All @@ -209,7 +209,7 @@
before do
login_as_organisation_admin_user

get report_questions_with_add_another_answer_path
get feature_report_path(report: "questions-with-add-another-answer")
end

it "returns http code 403" do
Expand All @@ -225,7 +225,7 @@
before do
login_as_super_admin_user

get report_questions_with_add_another_answer_path
get feature_report_path(report: "questions-with-add-another-answer")
end

it "returns http code 200" do
Expand All @@ -251,7 +251,7 @@
before do
login_as_standard_user

get report_forms_with_routes_path
get feature_report_path(report: "forms-with-routes")
end

it "returns http code 403" do
Expand All @@ -267,7 +267,7 @@
before do
login_as_organisation_admin_user

get report_forms_with_routes_path
get feature_report_path(report: "forms-with-routes")
end

it "returns http code 403" do
Expand All @@ -283,7 +283,7 @@
before do
login_as_super_admin_user

get report_forms_with_routes_path
get feature_report_path(report: "forms-with-routes")
end

it "returns http code 200" do
Expand All @@ -309,7 +309,7 @@
before do
login_as_standard_user

get report_forms_with_payments_path
get feature_report_path(report: "forms-with-payments")
end

it "returns http code 403" do
Expand All @@ -325,7 +325,7 @@
before do
login_as_organisation_admin_user

get report_forms_with_payments_path
get feature_report_path(report: "forms-with-payments")
end

it "returns http code 403" do
Expand All @@ -341,7 +341,7 @@
before do
login_as_super_admin_user

get report_forms_with_payments_path
get feature_report_path(report: "forms-with-payments")
end

it "returns http code 200" do
Expand All @@ -367,7 +367,7 @@
before do
login_as_standard_user

get report_forms_with_csv_submission_enabled_path
get feature_report_path(report: "forms-with-csv-submission-enabled")
end

it "returns http code 403" do
Expand All @@ -383,7 +383,7 @@
before do
login_as_organisation_admin_user

get report_forms_with_csv_submission_enabled_path
get feature_report_path(report: "forms-with-csv-submission-enabled")
end

it "returns http code 403" do
Expand All @@ -399,7 +399,7 @@
before do
login_as_super_admin_user

get report_forms_with_csv_submission_enabled_path
get feature_report_path(report: "forms-with-csv-submission-enabled")
end

it "returns http code 200" do
Expand Down Expand Up @@ -736,7 +736,7 @@

travel_to Time.utc(2025, 5, 15, 15, 31, 57)

get report_forms_with_routes_path(format: :csv)
get feature_report_path(report: "forms-with-routes", format: :csv)
end

it_behaves_like "csv response"
Expand All @@ -762,7 +762,7 @@

travel_to Time.utc(2025, 5, 15, 15, 31, 57)

get report_forms_with_payments_path(format: :csv)
get feature_report_path(report: "forms-with-payments", format: :csv)
end

it_behaves_like "csv response"
Expand All @@ -787,7 +787,7 @@

travel_to Time.utc(2025, 5, 15, 15, 31, 57)

get report_forms_with_csv_submission_enabled_path(format: :csv)
get feature_report_path(report: "forms-with-csv-submission-enabled", format: :csv)
end

it_behaves_like "csv response"
Expand Down Expand Up @@ -834,7 +834,7 @@

travel_to Time.utc(2025, 5, 15, 15, 31, 57)

get report_questions_with_add_another_answer_path(format: :csv)
get feature_report_path(report: "questions-with-add-another-answer", format: :csv)
end

it_behaves_like "csv response"
Expand Down
Loading