Skip to content

Commit

Permalink
Add HomeOfficeExcel with a feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
fumimowdan committed Oct 31, 2023
1 parent f19d002 commit 178f7c2
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 16 deletions.
70 changes: 70 additions & 0 deletions app/models/reports/home_office_csv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module Reports
class HomeOfficeCsv < Base
file_ext "csv"

def generate
CSV.generate do |csv|
csv << header
rows.each { |row| csv << row }
end
end

def post_generation_hook
applications.update_all(home_office_csv_downloaded_at: Time.zone.now) # rubocop:disable Rails/SkipsModelValidations
end

private

def rows
applications.map do |application|
[
application.urn,
application.applicant.full_name,
application.applicant.date_of_birth,
nil,
application.applicant.nationality,
nil,
application.applicant.passport_number,
nil,
nil,
nil,
nil,
nil,
nil,
]
end
end

def applications
@applications ||= Application
.joins(:application_progress)
.includes(:applicant)
.where.not(application_progresses: { initial_checks_completed_at: nil })
.where(
application_progresses: {
home_office_checks_completed_at: nil,
rejection_completed_at: nil,
},
home_office_csv_downloaded_at: nil,
)
end

def header
[
"ID",
"Full Name",
"DOB",
"Gender",
"Nationality",
"Place of Birth",
"Passport Number",
"National Insurance Number",
"Address",
"Postcode",
"Email",
"Telephone",
"Reference",
]
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Reports
class HomeOffice < Base
class HomeOfficeExcel < Base
file_ext "xlsx"

HEADER_MAPPINGS_KEY = "header_mappings"
Expand Down
6 changes: 5 additions & 1 deletion app/services/report.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Report
REGISTERED_REPORTS = {
home_office: Reports::HomeOffice,
home_office: Reports::HomeOfficeCsv,
home_office_excel: Reports::HomeOfficeExcel,
standing_data: Reports::StandingData,
payroll: Reports::Payroll,
applications: Reports::Applications,
Expand All @@ -16,6 +17,9 @@ def self.call(...)

def initialize(report_id, **kwargs)
@kwargs = kwargs&.symbolize_keys || {}
if Flipper.enabled?(:home_office_excel) && report_id.to_sym == :home_office
report_id = :home_office_excel
end
@report_class = REGISTERED_REPORTS.with_indifferent_access.fetch(report_id)
rescue KeyError
raise(ArgumentError, "Invalid report id #{report_id}")
Expand Down
8 changes: 4 additions & 4 deletions app/validators/home_office_report_config_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(record)
end

def validate
return if record.report_class != Reports::HomeOffice.name
return if record.report_class != Reports::HomeOfficeExcel.name

validate_workbook
validate_config_worksheet_name
Expand All @@ -39,14 +39,14 @@ def validate_workbook
def validate_worksheet
return if workbook.blank?

record.errors.add(:config, :ho_invalid_worksheet_name) if workbook[record.config.fetch(Reports::HomeOffice::WORKSHEET_NAME_KEY, nil)].blank?
record.errors.add(:config, :ho_invalid_worksheet_name) if workbook[record.config.fetch(Reports::HomeOfficeExcel::WORKSHEET_NAME_KEY, nil)].blank?
end

def validate_config_worksheet_name
record.errors.add(:config, :ho_missing_worksheet_name) if record.config.fetch(Reports::HomeOffice::WORKSHEET_NAME_KEY, nil).blank?
record.errors.add(:config, :ho_missing_worksheet_name) if record.config.fetch(Reports::HomeOfficeExcel::WORKSHEET_NAME_KEY, nil).blank?
end

def validate_config_header_mappings
record.errors.add(:config, :ho_missing_header_mappings) if record.config.fetch(Reports::HomeOffice::HEADER_MAPPINGS_KEY, nil).blank?
record.errors.add(:config, :ho_missing_header_mappings) if record.config.fetch(Reports::HomeOfficeExcel::HEADER_MAPPINGS_KEY, nil).blank?
end
end
4 changes: 2 additions & 2 deletions spec/factories/report_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
factory :home_office_report_template do
file { Rails.root.join("spec/fixtures/test_homeoffice_template.xlsx").read }
filename { "test_homeoffice_template.xlsx" }
report_class { "Reports::HomeOffice" }
report_class { "Reports::HomeOfficeExcel" }
config do
{
"worksheet_name" => "TestData",
Expand All @@ -39,7 +39,7 @@
factory :mocked_home_office_report_template do
file { Rails.root.join("spec/fixtures/test_homeoffice_template.xlsx").read }
filename { "test_homeoffice_template.xlsx" }
report_class { "Reports::HomeOffice" }
report_class { "Reports::HomeOfficeExcel" }
config do
{
"worksheet_name" => "Data",
Expand Down
2 changes: 2 additions & 0 deletions spec/features/admin_console/reports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ def then_the_qa_report_csv_report_is_downloaded

def and_i_click_on_the_home_office_csv_link
create(:mocked_home_office_report_template)
Flipper.enable :home_office_excel
within ".home-office" do
click_on "Download"
end
Flipper.disable :home_office_excel
end

def and_i_click_on_the_standing_data_csv_link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "rails_helper"

module Reports
describe HomeOffice do
describe HomeOfficeExcel do
include ActiveSupport::Testing::TimeHelpers

before { create(:mocked_home_office_report_template) }
Expand All @@ -15,7 +15,7 @@ module Reports
it "returns the filename of the Report" do
frozen_time = Time.zone.local(2023, 7, 17, 12, 30, 45)
travel_to frozen_time do
expected_name = "reports-home-office-20230717-123045.xlsx"
expected_name = "reports-home-office-excel-20230717-123045.xlsx"

report = described_class.new
actual_name = report.filename
Expand Down
28 changes: 22 additions & 6 deletions spec/services/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
subject(:registered_reports) { described_class::REGISTERED_REPORTS }

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

let(:expected_classes) do
[
Reports::HomeOffice,
Reports::HomeOfficeCsv,
Reports::HomeOfficeExcel,
Reports::StandingData,
Reports::Payroll,
Reports::Applications,
Expand Down Expand Up @@ -59,16 +60,31 @@
it { expect(report).to have_received(:generate) }
end

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

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

it { expect(Reports::HomeOffice).to have_received(:new) }
it { expect(Reports::HomeOfficeCsv).to have_received(:new) }
it { expect(report).to have_received(:generate) }
end

context "home office excel report" do
let(:report_id) { "home_office" }
let(:report) { spy(Reports::HomeOfficeExcel) }

before do
Flipper.enable :home_office_excel
allow(Reports::HomeOfficeExcel).to receive(:new).and_return(report)
service.data
Flipper.disable :home_office_excel
end

it { expect(Reports::HomeOfficeExcel).to have_received(:new) }
it { expect(report).to have_received(:generate) }
end
# rubocop:enable RSpec/VerifiedDoubles
Expand Down

0 comments on commit 178f7c2

Please sign in to comment.