Skip to content

Commit

Permalink
Move Qa reports to Reports section
Browse files Browse the repository at this point in the history
  • Loading branch information
fumimowdan committed Sep 25, 2023
1 parent ffd6d14 commit adb8fbf
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 109 deletions.
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
16 changes: 5 additions & 11 deletions app/controllers/system_admin/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +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")

# TODO: Execute report is background job
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,
applications: Reports::Applications.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
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
20 changes: 20 additions & 0 deletions app/views/system_admin/reports/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,23 @@
<%= 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>
76 changes: 0 additions & 76 deletions spec/features/admin_console/download_qa_csv_file_spec.rb

This file was deleted.

40 changes: 40 additions & 0 deletions spec/features/admin_console/reports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@
then_the_payroll_data_csv_report_is_downloaded
end

it "exports Application CSV" do
given_i_am_signed_as_an_admin
when_i_am_in_the_reports_page
and_i_click_on_the_applications_csv_link

then_the_applications_csv_report_is_downloaded
end

it "exports Qa report CSV" do
given_i_am_signed_as_an_admin
when_i_am_in_the_reports_page
and_i_click_on_the_qa_report_csv_button

then_the_qa_report_csv_report_is_downloaded
end

private

def then_the_standing_data_csv_report_is_downloaded
Expand All @@ -49,6 +65,18 @@ def then_the_payroll_data_csv_report_is_downloaded
expect(page.response_headers["Content-Disposition"]).to match(/filename="Payroll-Report.*/)
end

def then_the_applications_csv_report_is_downloaded
expect(page.response_headers["Content-Type"]).to match(/text\/csv/)
expect(page.response_headers["Content-Disposition"]).to include "attachment"
expect(page.response_headers["Content-Disposition"]).to match(/filename="Applications-Report.*/)
end

def then_the_qa_report_csv_report_is_downloaded
expect(page.response_headers["Content-Type"]).to match(/text\/csv/)
expect(page.response_headers["Content-Disposition"]).to include "attachment"
expect(page.response_headers["Content-Disposition"]).to match(/filename="QA-Report-initial_checks*/)
end

def and_i_click_on_the_home_office_csv_link
within ".home-office" do
click_on "Download"
Expand All @@ -67,6 +95,18 @@ def and_i_click_on_the_payroll_data_csv_link
end
end

def and_i_click_on_the_applications_csv_link
within ".applications" do
click_on "Download"
end
end

def and_i_click_on_the_qa_report_csv_button
within ".applications-qa" do
click_on "Download"
end
end

def when_i_am_in_the_reports_page
visit reports_path
end
Expand Down
76 changes: 76 additions & 0 deletions spec/services/report_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require "rails_helper"

RSpec.describe Report do
subject(:report) { described_class }

describe "registered reports" do
subject(:registered_reports) { described_class::REGISTERED_REPORTS }

let(:expected_ids) do
%i[home_office standing_data payroll applications qa]
end

let(:expected_classes) do
[
Reports::HomeOffice,
Reports::StandingData,
Reports::Payroll,
Reports::Applications,
Reports::QaReport,
]
end

it { expect(registered_reports.keys).to match_array(expected_ids) }
it { expect(registered_reports.values).to match_array(expected_classes) }
end

describe ".call" do
subject(:service) { described_class.new(report_id, status:) }

let(:report_id) { "qa" }
let(:status) { "initial_checks" }

context "report_name" do
it { expect(service.report_name).to eq("Reports::qareport") }
end

context "filename" do
include ActiveSupport::Testing::TimeHelpers
it "returns the name of the Report" do
frozen_time = Time.zone.local(2023, 7, 17, 12, 30, 45)
travel_to frozen_time do
expected_name = "QA-Report-initial_checks-2023_07_17-12_30_45.csv"

expect(service.filename).to eq(expected_name)
end
end
end

# rubocop:disable RSpec/VerifiedDoubles
context "qa report data" do
let(:report) { spy(Reports::QaReport) }

before do
allow(Reports::QaReport).to receive(:new).and_return(report)
service.data
end

it { expect(Reports::QaReport).to have_received(:new).with(kind_of(Array), status) }
it { expect(report).to have_received(:csv) }
end

context "other report data" do
let(:report_id) { "home_office" }
let(:report) { spy(Reports::HomeOffice) }

before do
allow(Reports::HomeOffice).to receive(:new).and_return(report)
service.data
end

it { expect(Reports::HomeOffice).to have_received(:new) }
it { expect(report).to have_received(:csv) }
end
# rubocop:enable RSpec/VerifiedDoubles
end
end

0 comments on commit adb8fbf

Please sign in to comment.