From fd1785007402e8385eaf1047e518630aaf75abaa Mon Sep 17 00:00:00 2001 From: eddieleeper Date: Wed, 20 Dec 2023 12:59:59 +0000 Subject: [PATCH 1/3] Update contract start date rules (#265) --- README.md | 6 ++ app/models/form/eligibility_check.rb | 25 +++-- config/locales/en.yml | 1 + spec/factories/forms.rb | 8 +- spec/models/form/eligibility_check_spec.rb | 112 ++++++++++++++++++++- spec/support/time_helpers.rb | 3 + 6 files changed, 141 insertions(+), 14 deletions(-) create mode 100644 spec/support/time_helpers.rb diff --git a/README.md b/README.md index b7e9eb20..fa4daf17 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,12 @@ When adding / removing or editing along side the code changes you will need to u available environments. Run the following command `make edit-app-secrets` +#### Contract Start Date +The environment variable `CONTRACT_START_MONTHS_LIMIT` can be set to `5` to override +the default of six months prior to the current service start date. +`AppSettings.current.service_start_date`. +This can be set to either `5` or `6` anything else will default to `6`. + ### SSH access Access a deploy with the command `make ssh`. diff --git a/app/models/form/eligibility_check.rb b/app/models/form/eligibility_check.rb index d50965db..3cf7fc69 100644 --- a/app/models/form/eligibility_check.rb +++ b/app/models/form/eligibility_check.rb @@ -31,7 +31,7 @@ def failure_reason in visa_type: "Other" "visa not accepted" in start_date: Date unless contract_start_date_eligible?(form.start_date) - "contract must start after the first monday of July of this year" + I18n.t("contract_must_start_within_months", count: months_limit_in_words) in date_of_entry: Date, start_date: Date unless date_of_entry_eligible?(form.date_of_entry, form.start_date) "cannot enter the UK more than 3 months before your contract start date" else @@ -46,11 +46,24 @@ def date_of_entry_eligible?(date_of_entry, start_date) end def contract_start_date_eligible?(start_date) - current_year = Date.current.year - first_monday_in_july = Date.new(current_year, 7, 1) - .beginning_of_month - .next_occurring(:monday) + months_before_service_start = AppSettings.current.service_start_date.months_ago(months_limit).beginning_of_month + start_date >= months_before_service_start + end + +private + + # default to 6 and only allow 5 or 6. anything else results in 6. + def months_limit + limit = ENV.fetch("CONTRACT_START_MONTHS_LIMIT", 6).to_i + [5, 6].include?(limit) ? limit : 6 + end - start_date >= first_monday_in_july + def months_limit_in_words + case months_limit + when 5 + "five" + else + "six" + end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 15578818..c01aea5b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -61,3 +61,4 @@ en: ho_missing_header_mappings: "config.header_mappings must be present" ho_missing_worksheet_name: "config.worksheet_name must be present" ho_invalid_worksheet_name: "config.worksheet_name not present in file" + contract_must_start_within_months: contract must start within the last %{count} months diff --git a/spec/factories/forms.rb b/spec/factories/forms.rb index 6e77c3d9..eeda10b1 100644 --- a/spec/factories/forms.rb +++ b/spec/factories/forms.rb @@ -63,8 +63,8 @@ "Youth Mobility Scheme", ].sample end - start_date { Date.new(Date.current.year, 9, 1) } - date_of_entry { Date.new(Date.current.year, 9, 1) } + start_date { 1.month.ago } + date_of_entry { 1.month.ago } subject { "physics" } end @@ -72,8 +72,8 @@ state_funded_secondary_school { true } one_year { true } visa_type { "British National (Overseas) visa" } - start_date { Date.new(Date.current.year, 9, 1) } - date_of_entry { Date.new(Date.current.year, 9, 1) } + start_date { 1.month.ago } + date_of_entry { 1.month.ago } subject { "physics" } date_of_birth { rand(18..90).years.ago.to_date } email_address { Faker::Internet.email } diff --git a/spec/models/form/eligibility_check_spec.rb b/spec/models/form/eligibility_check_spec.rb index 900861cc..4d7654ee 100644 --- a/spec/models/form/eligibility_check_spec.rb +++ b/spec/models/form/eligibility_check_spec.rb @@ -12,6 +12,110 @@ it { expect(check.failure_reason).to be_nil } end + context "in the Jan/Feb 2024 window" do + before do + travel_to Time.zone.local(2024, 1, 2) + AppSettings.current.update!( + service_start_date: Time.zone.today, + service_end_date: 1.month.from_now.end_of_month, + ) + travel_to Time.zone.local(2024, 2, 29) + end + + let(:form) { build(:form, :eligible) } + + context "when the contract start date is September 2023" do + let(:form) do + build(:form, :eligible, + start_date: Date.new(2023, 9, 2), + date_of_entry: Date.new(2023, 9, 2)) + end + + it { expect(check.failure_reason).to be_nil } + end + + context "when the contract start date is Jan 2024" do + let(:form) do + build(:form, :eligible, + start_date: Date.new(2024, 1, 1), + date_of_entry: Date.new(2024, 1, 1)) + end + + it { expect(check.failure_reason).to be_nil } + end + + context "when the contract start date is July 2023" do + let(:form) do + build(:form, :eligible, + start_date: Date.new(2023, 7, 1), + date_of_entry: Date.new(2023, 7, 1)) + end + + it { expect(check.failure_reason).to be_nil } + end + + context "when the contract start date is June 2023" do + let(:form) do + build(:form, :eligible, + start_date: Date.new(2023, 6, 30), + date_of_entry: Date.new(2023, 6, 30)) + end + let(:expected) { "contract must start within the last six months" } + + it { expect(check.failure_reason).to eq(expected) } + end + end + + context "in the Apr/May 2024 window" do + before do + travel_to Time.zone.local(2024, 4, 1) + AppSettings.current.update!( + service_start_date: Time.zone.today, + service_end_date: 1.month.from_now.end_of_month, + ) + travel_to Time.zone.local(2024, 5, 31) + ENV["CONTRACT_START_MONTHS_LIMIT"] = "5" + end + + after do + ENV.delete("CONTRACT_START_MONTHS_LIMIT") + end + + let(:form) { build(:form, :eligible) } + + context "when the contract start date is September 2023" do + let(:form) do + build(:form, :eligible, + start_date: Date.new(2023, 9, 30), + date_of_entry: Date.new(2023, 9, 30)) + end + let(:expected) { "contract must start within the last five months" } + + it { expect(check.failure_reason).to eq(expected) } + end + + context "when the contract start date is October 2023" do + let(:form) do + build(:form, :eligible, + start_date: Date.new(2023, 10, 31), + date_of_entry: Date.new(2023, 10, 31)) + end + let(:expected) { "contract must start within the last five months"} + + it { expect(check.failure_reason).to eq(expected) } + end + + context "when the contract start date is Jan 2024" do + let(:form) do + build(:form, :eligible, + start_date: Date.new(2024, 1, 1), + date_of_entry: Date.new(2024, 1, 1)) + end + + it { expect(check.failure_reason).to be_nil } + end + end + context "when ineligible" do context "because of chosen application_route" do let(:form) { build(:form, application_route: "other") } @@ -49,8 +153,8 @@ end context "because of start date too early" do - let(:form) { build(:form, start_date: Date.new(Date.current.year, 6, 30)) } - let(:expected) { "contract must start after the first monday of July of this year" } + let(:form) { build(:form, start_date: 8.months.ago) } + let(:expected) { "contract must start within the last six months" } it { expect(check.failure_reason).to eq(expected) } end @@ -59,8 +163,8 @@ let(:form) do build( :form, - start_date: Date.new(Date.current.year, 8, 30), - date_of_entry: Date.new(Date.current.year, 5, 29), + start_date: 1.month.ago, + date_of_entry: 5.months.ago, ) end let(:expected) { "cannot enter the UK more than 3 months before your contract start date" } diff --git a/spec/support/time_helpers.rb b/spec/support/time_helpers.rb new file mode 100644 index 00000000..33bd51e7 --- /dev/null +++ b/spec/support/time_helpers.rb @@ -0,0 +1,3 @@ +RSpec.configure do |config| + config.include ActiveSupport::Testing::TimeHelpers +end From 1e2be8321424a0aa6bba5401ffe91045e110ccf1 Mon Sep 17 00:00:00 2001 From: eddieleeper Date: Wed, 20 Dec 2023 15:07:48 +0000 Subject: [PATCH 2/3] Update contract start date rules; amendments following review (#265) --- README.md | 4 ++-- app/models/form/eligibility_check.rb | 9 ++------- config/application.rb | 1 + spec/models/form/eligibility_check_spec.rb | 4 ++-- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index fa4daf17..8d40c63c 100644 --- a/README.md +++ b/README.md @@ -105,10 +105,10 @@ available environments. Run the following command `make edit-app-secrets` #### Contract Start Date -The environment variable `CONTRACT_START_MONTHS_LIMIT` can be set to `5` to override +The custom configuration entry `config.x.form_eligibility.contract_start_months_limit` can be set to `5` to override the default of six months prior to the current service start date. `AppSettings.current.service_start_date`. -This can be set to either `5` or `6` anything else will default to `6`. +This should be set to either `5` or `6` anything else will default to `6`. ### SSH access diff --git a/app/models/form/eligibility_check.rb b/app/models/form/eligibility_check.rb index 3cf7fc69..9137b797 100644 --- a/app/models/form/eligibility_check.rb +++ b/app/models/form/eligibility_check.rb @@ -54,16 +54,11 @@ def contract_start_date_eligible?(start_date) # default to 6 and only allow 5 or 6. anything else results in 6. def months_limit - limit = ENV.fetch("CONTRACT_START_MONTHS_LIMIT", 6).to_i + limit = Rails.configuration.x.form_eligibility.contract_start_months_limit.to_i [5, 6].include?(limit) ? limit : 6 end def months_limit_in_words - case months_limit - when 5 - "five" - else - "six" - end + months_limit == 5 ? "five" : "six" end end diff --git a/config/application.rb b/config/application.rb index 2948f757..1122ac69 100644 --- a/config/application.rb +++ b/config/application.rb @@ -49,5 +49,6 @@ class Application < Rails::Application config.x.govuk_notify.generic_email_template_id = ENV.fetch("GOVUK_NOTIFY_GENERIC_EMAIL_TEMPLATE_ID") config.x.events.filtered_attributes = YAML.load_file(Rails.root.join("config/events/filtered_attributes.yml")) + config.x.form_eligibility.contract_start_months_limit = 6 end end diff --git a/spec/models/form/eligibility_check_spec.rb b/spec/models/form/eligibility_check_spec.rb index 4d7654ee..bfea7001 100644 --- a/spec/models/form/eligibility_check_spec.rb +++ b/spec/models/form/eligibility_check_spec.rb @@ -74,11 +74,11 @@ service_end_date: 1.month.from_now.end_of_month, ) travel_to Time.zone.local(2024, 5, 31) - ENV["CONTRACT_START_MONTHS_LIMIT"] = "5" + Rails.configuration.x.form_eligibility.contract_start_months_limit = 5 end after do - ENV.delete("CONTRACT_START_MONTHS_LIMIT") + Rails.configuration.x.form_eligibility.contract_start_months_limit = 6 end let(:form) { build(:form, :eligible) } From a3c581a5a85169ff8ab3d59873fdf569c79346fc Mon Sep 17 00:00:00 2001 From: eddieleeper Date: Wed, 20 Dec 2023 15:17:36 +0000 Subject: [PATCH 3/3] Update contract start date rules; appease rubocop (#265) --- spec/models/form/eligibility_check_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/form/eligibility_check_spec.rb b/spec/models/form/eligibility_check_spec.rb index bfea7001..c59c0459 100644 --- a/spec/models/form/eligibility_check_spec.rb +++ b/spec/models/form/eligibility_check_spec.rb @@ -100,7 +100,7 @@ start_date: Date.new(2023, 10, 31), date_of_entry: Date.new(2023, 10, 31)) end - let(:expected) { "contract must start within the last five months"} + let(:expected) { "contract must start within the last five months" } it { expect(check.failure_reason).to eq(expected) } end