diff --git a/app/models/reports/base.rb b/app/models/reports/base.rb index f2949099..d6d19203 100644 --- a/app/models/reports/base.rb +++ b/app/models/reports/base.rb @@ -26,5 +26,20 @@ def filename def generate; end def post_generation_hook; end + + def reset; end + + def timestamp + kwargs&.fetch(:timestamp, nil) + end + + private + + def timestamp_range + return unless timestamp + + t = Time.zone.parse(timestamp) + Range.new(t - 1.second, t + 1.second) + end end end diff --git a/app/models/reports/home_office_csv.rb b/app/models/reports/home_office_csv.rb index 571808b6..1e2a04d5 100644 --- a/app/models/reports/home_office_csv.rb +++ b/app/models/reports/home_office_csv.rb @@ -13,6 +13,12 @@ def post_generation_hook applications.update_all(home_office_csv_downloaded_at: Time.zone.now) # rubocop:disable Rails/SkipsModelValidations end + def reset + Application + .where(home_office_csv_downloaded_at: timestamp_range) + .update_all(home_office_csv_downloaded_at: nil) # rubocop:disable Rails/SkipsModelValidations + end + private def rows diff --git a/app/models/reports/home_office_excel.rb b/app/models/reports/home_office_excel.rb index d7e2b3a8..d71ee1b4 100644 --- a/app/models/reports/home_office_excel.rb +++ b/app/models/reports/home_office_excel.rb @@ -16,6 +16,12 @@ def post_generation_hook base_query.update_all(home_office_csv_downloaded_at: Time.zone.now) # rubocop:disable Rails/SkipsModelValidations end + def reset + Application + .where(home_office_csv_downloaded_at: timestamp_range) + .update_all(home_office_csv_downloaded_at: nil) # rubocop:disable Rails/SkipsModelValidations + end + private def workbook diff --git a/app/models/reports/payroll.rb b/app/models/reports/payroll.rb index c1b35d38..db96ead6 100644 --- a/app/models/reports/payroll.rb +++ b/app/models/reports/payroll.rb @@ -13,6 +13,12 @@ def post_generation_hook applications.update_all(payroll_csv_downloaded_at: Time.zone.now) # rubocop:disable Rails/SkipsModelValidations end + def reset + Application + .where(payroll_csv_downloaded_at: timestamp_range) + .update_all(payroll_csv_downloaded_at: nil) # rubocop:disable Rails/SkipsModelValidations + end + private def rows diff --git a/app/models/reports/qa_report.rb b/app/models/reports/qa_report.rb index 14451b87..8b3ff8a9 100644 --- a/app/models/reports/qa_report.rb +++ b/app/models/reports/qa_report.rb @@ -20,6 +20,10 @@ def status kwargs.fetch(:status) end + def reset + QaStatus.where(status: status, date: timestamp_range).delete_all + end + private def applications diff --git a/app/models/reports/standing_data.rb b/app/models/reports/standing_data.rb index c4a90c6b..87cb5e46 100644 --- a/app/models/reports/standing_data.rb +++ b/app/models/reports/standing_data.rb @@ -13,6 +13,12 @@ def post_generation_hook applications.update_all(standing_data_csv_downloaded_at: Time.zone.now) # rubocop:disable Rails/SkipsModelValidations end + def reset + Application + .where(standing_data_csv_downloaded_at: timestamp_range) + .update_all(standing_data_csv_downloaded_at: nil) # rubocop:disable Rails/SkipsModelValidations + end + private def rows diff --git a/spec/models/reports/home_office_excel_spec.rb b/spec/models/reports/home_office_excel_spec.rb index 9370146c..3d93e61c 100644 --- a/spec/models/reports/home_office_excel_spec.rb +++ b/spec/models/reports/home_office_excel_spec.rb @@ -104,5 +104,25 @@ module Reports end end end + + describe "reset" do + let(:app) { create(:application, application_progress: build(:application_progress, :home_office_pending)) } + + before { app } + + it "allows previously downloaded applications in a report to be downloaded again" do + travel_to(Time.zone.local(2023, 7, 17, 12, 30, 45)) do + first_dataset = report.send(:dataset) + expect(first_dataset.first).to include(app.urn) + + report.post_generation_hook + + described_class.new(timestamp: Time.zone.now.to_s).reset + + second_dataset = report.send(:dataset) + expect(second_dataset.first).to include(app.urn) + end + end + end end end diff --git a/spec/models/reports/payroll_spec.rb b/spec/models/reports/payroll_spec.rb index 559c6918..26f7a951 100644 --- a/spec/models/reports/payroll_spec.rb +++ b/spec/models/reports/payroll_spec.rb @@ -130,7 +130,26 @@ module Reports context "excludes applications from the csv after invoking `post_generation_hook`" do before { report.post_generation_hook } - it { expect(csv).not_to include(app.urn) } + it { expect(csv).not_to include(app.applicant.email_address) } + end + end + end + + describe "reset" do + let(:progress) { build(:application_progress, banking_approval_completed_at: Time.zone.now) } + let(:app) { create(:application, application_progress: progress) } + + before { app } + + it "allows previously downloaded applications in a report to be downloaded again" do + travel_to(Time.zone.local(2023, 7, 17, 12, 30, 45)) do + expect(report.generate).to include(app.applicant.email_address) + + report.post_generation_hook + + described_class.new(timestamp: Time.zone.now.to_s).reset + + expect(report.generate).to include(app.applicant.email_address) end end end diff --git a/spec/models/reports/qa_report_spec.rb b/spec/models/reports/qa_report_spec.rb index 5313c452..54db65aa 100644 --- a/spec/models/reports/qa_report_spec.rb +++ b/spec/models/reports/qa_report_spec.rb @@ -88,6 +88,24 @@ module Reports expect(report.generate).to include(expected_header.join(",")) end end + + describe "reset" do + let(:app) { create(:application) } + + before { app } + + it "allows previously downloaded applications in a report to be downloaded again" do + travel_to(Time.zone.local(2023, 7, 17, 12, 30, 45)) do + expect(report.generate).to include(app.urn) + + report.post_generation_hook + + described_class.new(status: status, timestamp: Time.zone.now.to_s).reset + + expect(report.generate).to include(app.urn) + end + end + end end end # rubocop:enable Metrics/ExampleLength diff --git a/spec/models/reports/standing_data_spec.rb b/spec/models/reports/standing_data_spec.rb index 48cffccf..61fecb36 100644 --- a/spec/models/reports/standing_data_spec.rb +++ b/spec/models/reports/standing_data_spec.rb @@ -8,6 +8,8 @@ module Reports subject(:report) { described_class.new } + let(:progress) { build(:application_progress, school_checks_completed_at: Time.zone.now, banking_approval_completed_at: nil) } + it "returns the filename of the Report" do frozen_time = Time.zone.local(2023, 7, 17, 12, 30, 45) travel_to frozen_time do @@ -99,5 +101,23 @@ module Reports end end # rubocop:enable RSpec/ExampleLength + + describe "reset" do + let(:app) { create(:application, application_progress: progress) } + + before { app } + + it "allows previously downloaded applications in a report to be downloaded again" do + travel_to(Time.zone.local(2023, 7, 17, 12, 30, 45)) do + expect(report.generate).to include(app.urn) + + report.post_generation_hook + + described_class.new(timestamp: Time.zone.now.to_s).reset + + expect(report.generate).to include(app.urn) + end + end + end end end