From d38a360bcbe800e19e81965b6f3a00a7440eca18 Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Thu, 9 Jan 2025 16:53:44 +0000 Subject: [PATCH] New helper method changes --- app/helpers/collection_deadline_helper.rb | 85 +++++++++++++++++++ app/helpers/collection_time_helper.rb | 61 ------------- app/helpers/home_helper.rb | 9 -- app/models/form.rb | 15 ---- spec/features/home_page_spec.rb | 1 + .../collection_deadline_helper_spec.rb | 29 +++++++ spec/helpers/collection_time_helper_spec.rb | 23 ----- 7 files changed, 115 insertions(+), 108 deletions(-) create mode 100644 app/helpers/collection_deadline_helper.rb delete mode 100644 app/helpers/home_helper.rb create mode 100644 spec/helpers/collection_deadline_helper_spec.rb diff --git a/app/helpers/collection_deadline_helper.rb b/app/helpers/collection_deadline_helper.rb new file mode 100644 index 000000000..231ae8a66 --- /dev/null +++ b/app/helpers/collection_deadline_helper.rb @@ -0,0 +1,85 @@ +module CollectionDeadlineHelper + include CollectionTimeHelper + + QUARTERLY_DEADLINES = { + 2024 => { + first_quarter_deadline: Time.zone.local(2024, 7, 12), + second_quarter_deadline: Time.zone.local(2024, 10, 11), + third_quarter_deadline: Time.zone.local(2025, 1, 10), + fourth_quarter_deadline: Time.zone.local(2025, 6, 6), # Same as submission deadline + }, + 2025 => { + first_quarter_deadline: Time.zone.local(2025, 7, 11), + second_quarter_deadline: Time.zone.local(2025, 10, 10), + third_quarter_deadline: Time.zone.local(2026, 1, 16), + fourth_quarter_deadline: Time.zone.local(2026, 6, 5), # Same as submission deadline + }, + }.freeze + + def quarterly_cutoff_date(quarter, year) + send("#{quarter}_quarter", year)[:cutoff_date].strftime("%A %-d %B %Y") + end + + def formatted_deadline + quarterly_cutoff_date("fourth", current_collection_start_year) + end + + def quarter_deadlines_for_year(year) + QUARTERLY_DEADLINES[year] + end + + def first_quarter(year) + { + cutoff_date: quarter_deadlines_for_year(year)[:first_quarter_deadline], + start_date: Time.zone.local(year, 4, 1), + end_date: Time.zone.local(year, 6, 30), + } + end + + def second_quarter(year) + { + cutoff_date: quarter_deadlines_for_year(year)[:second_quarter_deadline], + start_date: Time.zone.local(year, 7, 1), + end_date: Time.zone.local(year, 9, 30), + } + end + + def third_quarter(year) + { + cutoff_date: quarter_deadlines_for_year(year)[:third_quarter_deadline], + start_date: Time.zone.local(year, 10, 1), + end_date: Time.zone.local(year, 12, 31), + } + end + + def fourth_quarter(year) + { + cutoff_date: quarter_deadlines_for_year(year)[:fourth_quarter_deadline], + start_date: Time.zone.local(year + 1, 1, 1), + end_date: Time.zone.local(year + 1, 3, 31), + } + end + + def quarter_dates(year) + [ + first_quarter(year).merge(quarter: "Q1"), + second_quarter(year).merge(quarter: "Q2"), + third_quarter(year).merge(quarter: "Q3"), + ] + end + + def quarter_for_date(date: Time.zone.now) + quarters = quarter_dates(current_collection_start_year) + + quarter = quarters.find { |q| date.between?(q[:start_date], q[:cutoff_date] + 1.day) } + + return unless quarter + + OpenStruct.new( + quarter: quarter[:quarter], + cutoff_date: quarter[:cutoff_date], + quarter_start_date: quarter[:start_date], + quarter_end_date: quarter[:end_date], + ) + end +end diff --git a/app/helpers/collection_time_helper.rb b/app/helpers/collection_time_helper.rb index 05f834d86..7c90bb5f1 100644 --- a/app/helpers/collection_time_helper.rb +++ b/app/helpers/collection_time_helper.rb @@ -54,66 +54,5 @@ module CollectionTimeHelper current_collection_start_year - 2 end - def quarter_deadlines - Form::QUARTERLY_DEADLINES - end - - def quarter_deadlines_for_year(year) - quarter_deadlines[year] - end - - def first_quarter(year) - { - cutoff_date: quarter_deadlines_for_year(year)[:first_quarter_deadline], - start_date: Time.zone.local(year, 4, 1), - end_date: Time.zone.local(year, 6, 30), - } - end - - def second_quarter(year) - { - cutoff_date: quarter_deadlines_for_year(year)[:second_quarter_deadline], - start_date: Time.zone.local(year, 7, 1), - end_date: Time.zone.local(year, 9, 30), - } - end - - def third_quarter(year) - { - cutoff_date: quarter_deadlines_for_year(year)[:third_quarter_deadline], - start_date: Time.zone.local(year, 10, 1), - end_date: Time.zone.local(year, 12, 31), - } - end - def fourth_quarter(year) - { - cutoff_date: quarter_deadlines_for_year(year)[:fourth_quarter_deadline], - start_date: Time.zone.local(year + 1, 1, 1), - end_date: Time.zone.local(year + 1, 3, 31), - } - end - - def quarter_dates(year) - [ - first_quarter(year).merge(quarter: "Q1"), - second_quarter(year).merge(quarter: "Q2"), - third_quarter(year).merge(quarter: "Q3"), - ] - end - - def quarter_for_date(date: Time.zone.now) - quarters = quarter_dates(current_collection_start_year) - - quarter = quarters.find { |q| date.between?(q[:start_date], q[:cutoff_date] + 1.day) } - - return unless quarter - - OpenStruct.new( - quarter: quarter[:quarter], - cutoff_date: quarter[:cutoff_date], - quarter_start_date: quarter[:start_date], - quarter_end_date: quarter[:end_date], - ) - end end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb deleted file mode 100644 index 79b314fff..000000000 --- a/app/helpers/home_helper.rb +++ /dev/null @@ -1,9 +0,0 @@ -module HomeHelper - def quarterly_cutoff_date(quarter, year) - send("#{quarter}_quarter", year)[:cutoff_date].strftime("%A %-d %B %Y") - end - - def formatted_deadline - quarterly_cutoff_date("fourth", current_collection_start_year) - end -end diff --git a/app/models/form.rb b/app/models/form.rb index f75159f25..ce77378ca 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -24,21 +24,6 @@ class Form }, }.freeze - QUARTERLY_DEADLINES = { - 2024 => { - first_quarter_deadline: Time.zone.local(2024, 7, 12), - second_quarter_deadline: Time.zone.local(2024, 10, 11), - third_quarter_deadline: Time.zone.local(2025, 1, 10), - fourth_quarter_deadline: Time.zone.local(2025, 6, 6), # Same as submission deadline, can be refactored - }, - 2025 => { - first_quarter_deadline: Time.zone.local(2025, 7, 11), - second_quarter_deadline: Time.zone.local(2025, 10, 10), - third_quarter_deadline: Time.zone.local(2026, 1, 16), - fourth_quarter_deadline: Time.zone.local(2026, 6, 5), # Same as submission deadline, can be refactored - }, - }.freeze - def initialize(form_path, start_year = "", sections_in_form = [], type = "lettings") if sales_or_start_year_after_2022?(type, start_year) @start_date = Time.zone.local(start_year, 4, 1) diff --git a/spec/features/home_page_spec.rb b/spec/features/home_page_spec.rb index 0871f715d..e5117b558 100644 --- a/spec/features/home_page_spec.rb +++ b/spec/features/home_page_spec.rb @@ -78,6 +78,7 @@ RSpec.describe "Home Page Features" do scenario "displays correct text for quarters" do Timecop.freeze(Time.zone.local(next_collection_year, 4, 1)) do visit root_path + save_and_open_page find("span.govuk-details__summary-text", text: "Quarterly cut-off dates for 2025 to 2026").click expect(page).to have_content("Q1 - Friday 11 July 2025") expect(page).to have_content("Q2 - Friday 10 October 2025") diff --git a/spec/helpers/collection_deadline_helper_spec.rb b/spec/helpers/collection_deadline_helper_spec.rb new file mode 100644 index 000000000..95f430fd6 --- /dev/null +++ b/spec/helpers/collection_deadline_helper_spec.rb @@ -0,0 +1,29 @@ +require "rails_helper" + +RSpec.describe CollectionDeadlineHelper do + let(:current_user) { create(:user, :data_coordinator) } + let(:user) { create(:user, :data_coordinator) } + + describe "#quarter_for_date" do + it "returns correct cutoff date for the first quarter of 2024/25" do + quarter = quarter_for_date(date: Time.zone.local(2024, 4, 1)) + expect(quarter.cutoff_date).to eq(Time.zone.local(2024, 7, 12)) + expect(quarter.quarter_start_date).to eq(Time.zone.local(2024, 4, 1)) + expect(quarter.quarter_end_date).to eq(Time.zone.local(2024, 6, 30)) + end + + it "returns correct cutoff date for the second quarter of 2024/25" do + quarter = quarter_for_date(date: Time.zone.local(2024, 9, 30)) + expect(quarter.cutoff_date).to eq(Time.zone.local(2024, 10, 11)) + expect(quarter.quarter_start_date).to eq(Time.zone.local(2024, 7, 1)) + expect(quarter.quarter_end_date).to eq(Time.zone.local(2024, 9, 30)) + end + + it "returns correct cutoff date for the third quarter of 2024/25" do + quarter = quarter_for_date(date: Time.zone.local(2024, 10, 25)) + expect(quarter.cutoff_date).to eq(Time.zone.local(2025, 1, 10)) + expect(quarter.quarter_start_date).to eq(Time.zone.local(2024, 10, 1)) + expect(quarter.quarter_end_date).to eq(Time.zone.local(2024, 12, 31)) + end + end +end diff --git a/spec/helpers/collection_time_helper_spec.rb b/spec/helpers/collection_time_helper_spec.rb index bffc492c6..a23fc905d 100644 --- a/spec/helpers/collection_time_helper_spec.rb +++ b/spec/helpers/collection_time_helper_spec.rb @@ -106,27 +106,4 @@ RSpec.describe CollectionTimeHelper do end end end - - describe "#quarter_for_date" do - it "returns correct cutoff date for the first quarter of 2024/25" do - quarter = quarter_for_date(date: Time.zone.local(2024, 4, 1)) - expect(quarter.cutoff_date).to eq(Time.zone.local(2024, 7, 12)) - expect(quarter.quarter_start_date).to eq(Time.zone.local(2024, 4, 1)) - expect(quarter.quarter_end_date).to eq(Time.zone.local(2024, 6, 30)) - end - - it "returns correct cutoff date for the second quarter of 2024/25" do - quarter = quarter_for_date(date: Time.zone.local(2024, 9, 30)) - expect(quarter.cutoff_date).to eq(Time.zone.local(2024, 10, 11)) - expect(quarter.quarter_start_date).to eq(Time.zone.local(2024, 7, 1)) - expect(quarter.quarter_end_date).to eq(Time.zone.local(2024, 9, 30)) - end - - it "returns correct cutoff date for the third quarter of 2024/25" do - quarter = quarter_for_date(date: Time.zone.local(2024, 10, 25)) - expect(quarter.cutoff_date).to eq(Time.zone.local(2025, 1, 10)) - expect(quarter.quarter_start_date).to eq(Time.zone.local(2024, 10, 1)) - expect(quarter.quarter_end_date).to eq(Time.zone.local(2024, 12, 31)) - end - end end