diff --git a/app/models/reports/applications.rb b/app/models/reports/applications.rb index 1b8d1927..e9e5bbe5 100644 --- a/app/models/reports/applications.rb +++ b/app/models/reports/applications.rb @@ -1,6 +1,6 @@ module Reports class Applications < Base - def csv + def generate CSV.generate do |csv| csv << header rows.find_each(batch_size: 50) { |row| csv << columns(row) } diff --git a/app/models/reports/base.rb b/app/models/reports/base.rb index 70fa021b..f2949099 100644 --- a/app/models/reports/base.rb +++ b/app/models/reports/base.rb @@ -7,13 +7,23 @@ def initialize(**kwargs) attr_reader :name, :kwargs + class << self + def file_ext(value) + @file_ext = value + end + + def get_file_ext + @file_ext || "csv" + end + end + def filename current_time = Time.zone.now.strftime("%Y%m%d-%H%M%S") - "#{name}-#{current_time}.csv" + "#{name}-#{current_time}.#{self.class.get_file_ext}" end - def csv; end + def generate; end def post_generation_hook; end end diff --git a/app/models/reports/payroll.rb b/app/models/reports/payroll.rb index 630be075..c1b35d38 100644 --- a/app/models/reports/payroll.rb +++ b/app/models/reports/payroll.rb @@ -2,7 +2,7 @@ module Reports class Payroll < Base - def csv + def generate CSV.generate do |csv| csv << header rows.each { |row| csv << row } diff --git a/app/models/reports/qa_report.rb b/app/models/reports/qa_report.rb index 9f6871cb..60a04336 100644 --- a/app/models/reports/qa_report.rb +++ b/app/models/reports/qa_report.rb @@ -5,7 +5,7 @@ def initialize(...) @name = [@name, status].join("-") end - def csv + def generate CSV.generate do |csv| csv << header rows.each { |row| csv << row } diff --git a/app/models/reports/standing_data.rb b/app/models/reports/standing_data.rb index 0cf68e6e..c4a90c6b 100644 --- a/app/models/reports/standing_data.rb +++ b/app/models/reports/standing_data.rb @@ -2,7 +2,7 @@ module Reports class StandingData < Base - def csv + def generate CSV.generate do |csv| csv << header rows.each { |row| csv << row } diff --git a/app/services/report.rb b/app/services/report.rb index ff1ae156..dcce92ea 100644 --- a/app/services/report.rb +++ b/app/services/report.rb @@ -26,7 +26,7 @@ def initialize(report_id, **kwargs) delegate :post_generation_hook, to: :report def data - @data ||= report.csv + @data ||= report.generate end private diff --git a/spec/features/admin_console/reports_spec.rb b/spec/features/admin_console/reports_spec.rb index 2db39a78..55a2dea5 100644 --- a/spec/features/admin_console/reports_spec.rb +++ b/spec/features/admin_console/reports_spec.rb @@ -54,7 +54,7 @@ def then_the_standing_data_csv_report_is_downloaded end def then_the_home_office_csv_report_is_downloaded - expect(page.response_headers["Content-Type"]).to match(/text\/csv/) + expect(page.response_headers["Content-Type"]).to match(/application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet/) expect(page.response_headers["Content-Disposition"]).to include "attachment" expect(page.response_headers["Content-Disposition"]).to match(/filename="reports-home-office.*/) end diff --git a/spec/models/reports/applications_spec.rb b/spec/models/reports/applications_spec.rb index a327be5a..5ab48214 100644 --- a/spec/models/reports/applications_spec.rb +++ b/spec/models/reports/applications_spec.rb @@ -66,17 +66,17 @@ module Reports ].join(",") end - it { expect(report.csv).to include(header) } + it { expect(report.generate).to include(header) } end context "returns all applications" do - it { expect(report.csv).to include(application.urn) } - it { expect(report.csv).to include(initial.urn) } - it { expect(report.csv).to include(home_office.urn) } - it { expect(report.csv).to include(school.urn) } - it { expect(report.csv).to include(banking.urn) } - it { expect(report.csv).to include(paid.urn) } - it { expect(report.csv).to include(rejected.urn) } + it { expect(report.generate).to include(application.urn) } + it { expect(report.generate).to include(initial.urn) } + it { expect(report.generate).to include(home_office.urn) } + it { expect(report.generate).to include(school.urn) } + it { expect(report.generate).to include(banking.urn) } + it { expect(report.generate).to include(paid.urn) } + it { expect(report.generate).to include(rejected.urn) } end end end diff --git a/spec/models/reports/payroll_spec.rb b/spec/models/reports/payroll_spec.rb index 59d1946b..559c6918 100644 --- a/spec/models/reports/payroll_spec.rb +++ b/spec/models/reports/payroll_spec.rb @@ -21,26 +21,26 @@ module Reports end end - describe "#csv" do + describe "#generate" do let(:progress) { build(:application_progress, banking_approval_completed_at: Time.zone.now) } it "returns applicants who have banking details approved" do app = create(:application, application_progress: progress) - expect(report.csv).to include(app.applicant.family_name) + expect(report.generate).to include(app.applicant.family_name) end it "does not return applicants who have not the bankind details approved" do progress.banking_approval_completed_at = nil app = create(:application, application_progress: progress) - expect(report.csv).not_to include(app.urn) + expect(report.generate).not_to include(app.urn) end it "returns the data in CSV format" do application = create(:application, application_progress: progress) - expect(report.csv).to include([ + expect(report.generate).to include([ "", # No. "Prof", # TITLE application.applicant.given_name, # FORENAME @@ -116,12 +116,12 @@ module Reports RIGHT_TO_WORK_CONFIRM_STATUS ].join(",") - expect(report.csv).to include(expected_header) + expect(report.generate).to include(expected_header) end context "includes applications from the csv before invoking `post_generation_hook`" do let(:app) { create(:application, application_progress: progress) } - let(:csv) { report.csv } + let(:csv) { report.generate } before { app } diff --git a/spec/models/reports/qa_report_spec.rb b/spec/models/reports/qa_report_spec.rb index 1fd0a83f..8385354e 100644 --- a/spec/models/reports/qa_report_spec.rb +++ b/spec/models/reports/qa_report_spec.rb @@ -25,7 +25,7 @@ module Reports it "returns the data in CSV format" do application = create(:application) - expect(report.csv).to include([ + expect(report.generate).to include([ application.urn, application.applicant.full_name, application.applicant.date_of_birth.strftime("%d/%m/%Y"), @@ -50,7 +50,7 @@ module Reports it "returns the data including rejection reasons in CSV format" do application = create(:application, application_progress: build(:application_progress, rejection_completed_at: Time.zone.now, status: :rejected, rejection_reason: :request_to_re_submit, rejection_details: "Some details")) - expect(report.csv).to include([ + expect(report.generate).to include([ application.urn, application.applicant.full_name, application.applicant.date_of_birth.strftime("%d/%m/%Y"), @@ -85,7 +85,7 @@ module Reports ] expected_header += ["Rejection Reason", "Rejection Details"] if status == "rejected" - expect(report.csv).to include(expected_header.join(",")) + expect(report.generate).to include(expected_header.join(",")) end end end diff --git a/spec/models/reports/standing_data_spec.rb b/spec/models/reports/standing_data_spec.rb index c4cd9f6b..48cffccf 100644 --- a/spec/models/reports/standing_data_spec.rb +++ b/spec/models/reports/standing_data_spec.rb @@ -20,41 +20,41 @@ module Reports end end - describe "#csv" do + describe "#generate" do let(:progress) { build(:application_progress, school_checks_completed_at: Time.zone.now, banking_approval_completed_at: nil) } it "returns applicants who have completed initial checks but not home office checks" do app = create(:application, application_progress: progress) - expect(report.csv).to include(app.urn) + expect(report.generate).to include(app.urn) end it "does not return rejected applicants" do progress.rejection_completed_at = Time.zone.now app = create(:application, application_progress: progress) - expect(report.csv).not_to include(app.urn) + expect(report.generate).not_to include(app.urn) end it "does not return applicants who have not completed initial checks" do progress.school_checks_completed_at = nil app = create(:application, application_progress: progress) - expect(report.csv).not_to include(app.urn) + expect(report.generate).not_to include(app.urn) end it "does not return applicants who have completed home office checks" do progress.banking_approval_completed_at = Time.zone.now app = create(:application, application_progress: progress) - expect(report.csv).not_to include(app.urn) + expect(report.generate).not_to include(app.urn) end # rubocop:disable RSpec/ExampleLength it "returns the data in CSV format" do application = create(:application, application_progress: progress) - expect(report.csv).to include([ + expect(report.generate).to include([ application.urn, application.applicant.given_name, application.applicant.middle_name, @@ -80,12 +80,12 @@ module Reports Student_loan ].join(",") - expect(report.csv).to include(expected_header) + expect(report.generate).to include(expected_header) end context "includes applications from the csv before invoking `post_generation_hook`" do let(:app) { create(:application, application_progress: progress) } - let(:csv) { report.csv } + let(:csv) { report.generate } before { app } diff --git a/spec/services/report_spec.rb b/spec/services/report_spec.rb index 8f61a0b6..96d94aeb 100644 --- a/spec/services/report_spec.rb +++ b/spec/services/report_spec.rb @@ -56,7 +56,7 @@ end it { expect(Reports::QaReport).to have_received(:new).with(status:) } - it { expect(report).to have_received(:csv) } + it { expect(report).to have_received(:generate) } end context "other report data" do @@ -69,7 +69,7 @@ end it { expect(Reports::HomeOffice).to have_received(:new) } - it { expect(report).to have_received(:csv) } + it { expect(report).to have_received(:generate) } end # rubocop:enable RSpec/VerifiedDoubles end