Skip to content

Commit

Permalink
Add reset method on report classes
Browse files Browse the repository at this point in the history
This method will be invoked when a report will need resetting in case
of download error or lost report file.
  • Loading branch information
fumimowdan committed Dec 5, 2023
1 parent f41004d commit 72fd9bc
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/models/reports/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions app/models/reports/home_office_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/models/reports/home_office_excel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/models/reports/payroll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/reports/qa_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/models/reports/standing_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/models/reports/home_office_excel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 20 additions & 1 deletion spec/models/reports/payroll_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions spec/models/reports/qa_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions spec/models/reports/standing_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 72fd9bc

Please sign in to comment.