diff --git a/app/models/reports/home_office_csv.rb b/app/models/reports/home_office_csv.rb new file mode 100644 index 00000000..571808b6 --- /dev/null +++ b/app/models/reports/home_office_csv.rb @@ -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 diff --git a/app/models/reports/home_office.rb b/app/models/reports/home_office_excel.rb similarity index 98% rename from app/models/reports/home_office.rb rename to app/models/reports/home_office_excel.rb index 7b22ce82..d7e2b3a8 100644 --- a/app/models/reports/home_office.rb +++ b/app/models/reports/home_office_excel.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Reports - class HomeOffice < Base + class HomeOfficeExcel < Base file_ext "xlsx" HEADER_MAPPINGS_KEY = "header_mappings" diff --git a/app/services/report.rb b/app/services/report.rb index dcce92ea..f4852e0f 100644 --- a/app/services/report.rb +++ b/app/services/report.rb @@ -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, @@ -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}") diff --git a/app/validators/home_office_report_config_validator.rb b/app/validators/home_office_report_config_validator.rb index 07646c51..2b73887c 100644 --- a/app/validators/home_office_report_config_validator.rb +++ b/app/validators/home_office_report_config_validator.rb @@ -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 @@ -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 diff --git a/spec/factories/report_templates.rb b/spec/factories/report_templates.rb index 470014cc..51a3433d 100644 --- a/spec/factories/report_templates.rb +++ b/spec/factories/report_templates.rb @@ -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", @@ -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", diff --git a/spec/features/admin_console/reports_spec.rb b/spec/features/admin_console/reports_spec.rb index a334e88f..b08b9926 100644 --- a/spec/features/admin_console/reports_spec.rb +++ b/spec/features/admin_console/reports_spec.rb @@ -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 diff --git a/spec/models/reports/home_office_spec.rb b/spec/models/reports/home_office_excel_spec.rb similarity index 97% rename from spec/models/reports/home_office_spec.rb rename to spec/models/reports/home_office_excel_spec.rb index ffa5f560..9370146c 100644 --- a/spec/models/reports/home_office_spec.rb +++ b/spec/models/reports/home_office_excel_spec.rb @@ -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) } @@ -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 diff --git a/spec/services/report_spec.rb b/spec/services/report_spec.rb index 96d94aeb..e8890b98 100644 --- a/spec/services/report_spec.rb +++ b/spec/services/report_spec.rb @@ -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, @@ -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