Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update report page #273

Merged
merged 2 commits into from
Sep 25, 2023
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
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ AllCops:
- 'bin/*'
- 'db/schema.rb'
- 'node_modules/**/*'
- 'spec/models/reports/applications_spec.rb'

Style/MethodCallWithArgsParentheses:
AllowParenthesesInMultilineCall: true
Expand Down
13 changes: 0 additions & 13 deletions app/controllers/system_admin/applicants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,6 @@ def index
session[:application_ids] = results.map(&:id)
end

def download_qa_csv
status = session[:filter_status]
application_ids = session[:application_ids]

applications = Application.where(id: application_ids).reject(&:qa?)

applications.each(&:mark_as_qa!)

report = Reports::QaReport.new(applications, status)
create_audit(action: "Downloaded QA CSV report (#{status.humanize})")
send_data(report.csv, filename: report.name)
end

def duplicates
@pagy, @duplicates = pagy(DuplicateApplication.search(params[:search]).select("DISTINCT ON (urn) *"))
end
Expand Down
14 changes: 5 additions & 9 deletions app/controllers/system_admin/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@ class ReportsController < AdminController
def index; end

def show
report = find_report(params[:id])
create_audit(action: "Downloaded #{report.class.to_s.capitalize} report")
service = Report.call(params[:id], **report_params)
create_audit(action: "Downloaded #{service.report_name} report")

send_data(report.csv, filename: report.name)
send_data(service.data, filename: service.filename)
end

private

def find_report(report_id)
{
home_office: Reports::HomeOffice.new,
standing_data: Reports::StandingData.new,
payroll: Reports::Payroll.new,
}.with_indifferent_access.fetch(report_id)
def report_params
params.permit(:id, :status)
end
end
end
14 changes: 14 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@ def mailto_irp_express_interest
def banner_feedback_form
govuk_link_to("feedback", "https://forms.office.com/e/p45Wm1Vmxg", target: "_blank")
end

def application_statuses
ApplicationProgress
.statuses
.keys
.map { |status| [status.humanize, status] }
end

def application_statuses_options(selected: nil, all_statuses: false)
statuses = application_statuses
statuses = application_statuses.unshift(["All statuses", ""]) if all_statuses

options_for_select(statuses, selected:)
end
end
79 changes: 79 additions & 0 deletions app/models/reports/applications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module Reports
class Applications
def name
current_time = Time.zone.now.strftime("%Y%m%d-%H%M%S")

"Applications-Report-#{current_time}.csv"
end

def csv
CSV.generate do |csv|
csv << header
rows.find_each(batch_size: 50) { |row| csv << columns(row) }
end
end

private

def header
%i[
ip_address
given_name
middle_name
family_name
email_address
phone_number
date_of_birth
sex
passport_number
nationality
student_loan
address_line_1
address_line_2
city
postcode
application_date
application_route
status
date_of_entry
start_date
subject
urn
visa_type
].map { _1.to_s.titleize }
end

def columns(application)
applicant = application.applicant
[
applicant.ip_address,
applicant.given_name,
applicant.middle_name,
applicant.family_name,
applicant.email_address,
applicant.phone_number,
applicant.date_of_birth,
applicant.sex,
applicant.passport_number,
applicant.nationality,
applicant.student_loan,
applicant.address.address_line_1,
applicant.address.address_line_2,
applicant.address.city,
applicant.address.postcode,
application.application_date,
application.application_route,
application.status,
application.date_of_entry,
application.start_date,
application.subject,
application.urn,
application.visa_type,
]
end

def rows
Application.includes(:applicant, :application_progress, applicant: :address)
end
end
end
61 changes: 61 additions & 0 deletions app/services/report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Report
REGISTERED_REPORTS = {
home_office: Reports::HomeOffice,
standing_data: Reports::StandingData,
payroll: Reports::Payroll,
applications: Reports::Applications,
qa: Reports::QaReport,
}.freeze

def self.call(...)
service = new(...)
service.data
service
end

def initialize(report_id, **kwargs)
@kwargs = kwargs&.symbolize_keys || {}
@report_class = REGISTERED_REPORTS.with_indifferent_access.fetch(report_id)
rescue KeyError
raise(ArgumentError, "Invalid report id #{report_id}")
end

def report_name
report_class.to_s.capitalize
end

def filename
report.name
end

def data
report.csv
end

private

attr_reader :report_class, :kwargs

def report
return @report if @report
return @report = report_class.new(*report_args) if report_args

@report = report_class.new
end

def report_args
return qa_report_args if report_class == Reports::QaReport

nil
end

def qa_report_args
return @qa_report_args if @qa_report_args

status = kwargs.fetch(:status)
applications = Application.filter_by_status(status).reject(&:qa?)
applications.each(&:mark_as_qa!)

@qa_report_args = [applications, status]
end
end
10 changes: 1 addition & 9 deletions app/views/system_admin/applicants/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<% statuses = options_for_select(
ApplicationProgress.statuses.keys.map { |status| [status.humanize, status] }.unshift(['All statuses', '']),
selected: params[:status]
)
%>
<%= form_with(url: applicants_path, method: :get, id: :search) do |f| %>
<div class="full-width">
<%= f.govuk_text_field :search, value: params[:search], label: { text: 'Search' }, hint: { text: 'Search by name, email, passport number or unique reference number (URN)' } %>
</div>

<div class="row">
<div class="column">
<%= f.govuk_select :status, statuses, label: { text: "Filter by application status" } %>
<%= f.govuk_select :status, application_statuses_options(selected: params[:status], all_statuses: true), label: { text: "Filter by application status" } %>
</div>

<div class="column">
Expand All @@ -25,9 +20,6 @@
<div class="full-width">
<%= f.govuk_submit 'Search', secondary: true %>
</div>
<% if session[:filter_status].present? %>
<%= link_to "Download QA CSV", download_qa_csv_applicants_path, class: "govuk-button" %>
<% end %>
<% end %>

<%= govuk_table(classes: "applicants-table") do |table|
Expand Down
30 changes: 30 additions & 0 deletions app/views/system_admin/reports/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,33 @@
<%= link_to "Download", report_path(:payroll), class: "govuk-button" %>
</p>
</div>

<div class="applications">
<h2 class="govuk-heading-m">Applications Data report</h2>
<p class="govuk-body">
Download a CSV file of all applications
</p>
<p>
<%= link_to "Download", report_path(:applications), class: "govuk-button" %>
</p>
</div>

<div class="applications-qa">
<h2 class="govuk-heading-m">QA reports</h2>
<p class="govuk-body">
Download a QA CSV file
</p>
<p>

<%= form_with(url: report_path(:qa), method: :get) do |f| %>
<div class="row">
<div class="column">
<%= f.govuk_select :status, application_statuses_options, label: { text: "Filter by application status" } %>
</div>
<div class="full-width">
<%= f.govuk_submit 'Download', class: "govuk-button"%>
</div>
</div>
<% end %>
</p>
</div>
7 changes: 1 addition & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@
end

scope module: :system_admin, path: "system-admin" do
resources :applicants, only: %i[index show edit update] do
collection do
get :download_qa_csv
end
end

resources :applicants, only: %i[index show edit update]
resources :users, except: %i[show]
resource :settings, only: %i[edit update]
get "/dashboard", to: "dashboard#show"
Expand Down
76 changes: 0 additions & 76 deletions spec/features/admin_console/download_qa_csv_file_spec.rb

This file was deleted.

Loading