Skip to content

Commit

Permalink
Adds the entry date form
Browse files Browse the repository at this point in the history
As in claim the entry date form writes to the date_of_entry attribute.
We also reset the date of entry if the start date changes as the
eligibility calculation for date of entry depends on the start date.

Of note there doesn't seem to be a validation in claim that the date of
entry isn't _after_ the start date of the contract. I'm not sure if
that's a business rule we want to enforce or not. There is a validation
that the date of entry isn't in the future.
  • Loading branch information
rjlynch committed Jun 26, 2024
1 parent 3041304 commit a92f8ef
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Journeys
module GetATeacherRelocationPayment
class EntryDateForm < Form
include ActiveRecord::AttributeAssignment

attribute :date_of_entry, :date

validates :date_of_entry, presence: {
message: i18n_error_message(:presence)
}

validates :date_of_entry,
comparison: {
less_than: ->(_) { Date.tomorrow },
message: i18n_error_message(:date_not_in_future)
}, if: :date_of_entry

def initialize(journey_session:, journey:, params:)
super

# Handle setting date from multi part params see
# ActiveRecord::AttributeAssignment
_assign_attributes(permitted_params)
rescue ActiveRecord::MultiparameterAssignmentErrors
# Invalid date was entered
self.date_of_entry = nil
end

def save
return false unless valid?

journey_session.answers.assign_attributes(date_of_entry: date_of_entry)

journey_session.save!
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ def initialize(journey_session:, journey:, params:)
def save
return false unless valid?

if start_date_changed?
journey_session.answers.assign_attributes(date_of_entry: nil)
end

journey_session.answers.assign_attributes(start_date: start_date)

journey_session.save!
end

private

def start_date_changed?
journey_session.answers.start_date != start_date
end
end
end
end
3 changes: 2 additions & 1 deletion app/models/journeys/get_a_teacher_relocation_payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module GetATeacherRelocationPayment
"contract-details" => ContractDetailsForm,
"start-date" => StartDateForm,
"subject" => SubjectForm,
"visa" => VisaForm
"visa" => VisaForm,
"entry-date" => EntryDateForm
}
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def eligibility_answers
a << start_date_details
a << subject_details
a << visa_details
a << entry_date
end.compact
end

Expand Down Expand Up @@ -63,6 +64,14 @@ def visa_details
"visa"
]
end

def entry_date
[
t("get_a_teacher_relocation_payment.forms.entry_date.question"),
answers.date_of_entry.strftime("%d-%m-%Y"),
"entry-date"
]
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SessionAnswers < Journeys::SessionAnswers
attribute :start_date, :date
attribute :subject, :string
attribute :visa_type, :string
attribute :date_of_entry, :date
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class SlugSequence
"start-date",
"subject",
"visa",
"entry-date",
"check-your-answers-part-one"
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ def ineligible_reason
"taught subject not accepted"
in visa_type: "Other"
"visa not accepted"
in date_of_entry: Date, start_date: Date unless date_of_entry_eligible?
"cannot enter the UK more than 3 months before your contract start date"
else
nil
end
end

def date_of_entry_eligible?
return false unless answers.date_of_entry && answers.start_date

answers.date_of_entry >= answers.start_date - 3.months
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<% content_for(
:page_title,
page_title(
t("get_a_teacher_relocation_payment.forms.entry_date.question"),
journey: current_journey_routing_name,
show_error: @form.errors.any?
)
) %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_for(
@form,
url: claim_path(current_journey_routing_name),
builder: GOVUKDesignSystemFormBuilder::FormBuilder
) do |f| %>
<% if f.object.errors.any? %>
<%= render("shared/error_summary", instance: f.object) %>
<% end %>

<%= f.govuk_date_field(
:date_of_entry,
legend: {
text: t("get_a_teacher_relocation_payment.forms.entry_date.question")
},
) %>

<%= f.govuk_submit %>
<% end %>
</div>
</div>
1 change: 1 addition & 0 deletions config/analytics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ shared:
- start_date
- subject
- visa_type
- date_of_entry
:schools:
- id
- urn
Expand Down
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,12 @@ en:
question: "Select the visa you used to move to England"
errors:
inclusion: "Choose your visa type"
entry_date:
question: "Enter the date you moved to England to start your teaching job"
errors:
presence: "Enter your entry date"
date_not_in_future: "Date of entry cannot be in the future"


check_your_answers:
part_one:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDateOfEntryToInternationalRelocationPaymentsEligibilities < ActiveRecord::Migration[7.0]
def change
add_column :international_relocation_payments_eligibilities, :date_of_entry, :date
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_06_21_153517) do
ActiveRecord::Schema[7.0].define(version: 2024_06_21_200501) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "pgcrypto"
Expand Down Expand Up @@ -194,6 +194,7 @@
t.date "start_date"
t.string "subject"
t.string "visa_type"
t.date "date_of_entry"
end

create_table "journey_configurations", primary_key: "routing_name", id: :string, force: :cascade do |t|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
visa_type { "British National (Overseas) visa" }
end

trait :with_entry_date do
with_start_date
date_of_entry { start_date - 1.week }
end

trait :with_email_details do
email_address { generate(:email_address) }
email_verified { true }
Expand All @@ -59,6 +64,7 @@
with_one_year_contract
with_start_date
with_visa
with_entry_date
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@
then_i_see_the_ineligible_page
end
end

context "ineligible - entry date" do
it "shows the ineligible page" do
when_i_start_the_form
and_i_complete_application_route_question_with(
option: "I am employed as a teacher in a school in England"
)
and_i_complete_the_state_funded_secondary_school_step_with(option: "Yes")
and_i_complete_the_contract_details_step_with(option: "Yes")
and_i_complete_the_contract_start_date_step_with(
date: contract_start_date
)
and_i_complete_the_subject_step_with(option: "Physics")
and_i_complete_the_visa_screen_with(option: "British National (Overseas) visa")
and_i_complete_the_entry_date_page_with(date: contract_start_date - 4.months)
then_i_see_the_ineligible_page
end
end
end

def then_i_see_the_ineligible_page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
Date.tomorrow
end

let(:entry_date) do
contract_start_date - 1.week
end

before do
journey_configuration
end
Expand All @@ -28,6 +32,7 @@
)
and_i_complete_the_subject_step_with(option: "Physics")
and_i_complete_the_visa_screen_with(option: "British National (Overseas) visa")
and_i_complete_the_entry_date_page_with(date: entry_date)
then_the_check_your_answers_part_one_page_shows_my_answers
and_i_dont_change_my_answers
and_the_personal_details_section_has_been_temporarily_stubbed
Expand Down Expand Up @@ -62,5 +67,9 @@ def then_the_check_your_answers_part_one_page_shows_my_answers
expect(page).to have_text(
"Select the visa you used to move to England British National (Overseas) visa"
)

expect(page).to have_text(
"Enter the date you moved to England to start your teaching job #{entry_date.strftime("%d-%m-%Y")}"
)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
require "rails_helper"

RSpec.describe Journeys::GetATeacherRelocationPayment::EntryDateForm, type: :model do
let(:journey_session) { create(:get_a_teacher_relocation_payment_session) }

let(:params) do
ActionController::Parameters.new(
claim: multi_part_date_parms(option)
)
end

let(:option) { nil }

def multi_part_date_parms(date)
return {} unless date.present?

{
"date_of_entry(1i)" => date.year.to_s,
"date_of_entry(2i)" => date.month.to_s,
"date_of_entry(3i)" => date.day.to_s
}
end

let(:form) do
described_class.new(
journey_session: journey_session,
journey: Journeys::GetATeacherRelocationPayment,
params: params
)
end

describe "validations" do
subject { form }

context "with an invalid date" do
it { is_expected.not_to be_valid }
end

context "with a date in the future" do
it do
is_expected.not_to(
allow_value(Date.tomorrow)
.for(:date_of_entry)
.with_message("Date of entry cannot be in the future")
)
end
end

context "with a date in the present" do
it { is_expected.to allow_value(Date.today).for(:date_of_entry) }
end

context "with a date in the past" do
it { is_expected.to allow_value(Date.yesterday).for(:date_of_entry) }
end
end

describe "#date_of_entry" do
subject { form.date_of_entry }

before do
journey_session.answers.assign_attributes(date_of_entry: Date.tomorrow)
end

context "when date is not present in the params" do
let(:option) { nil }

it { is_expected.to eq(journey_session.answers.date_of_entry) }
end

context "when date is persent in the params" do
let(:option) { Date.yesterday }

it { is_expected.to eq(option) }
end

context "when date is invalid" do
let(:params) do
ActionController::Parameters.new(
claim: {
"date_of_entry(1i)" => "01",
"date_of_entry(2i)" => "00",
"date_of_entry(3i)" => "2024"
}
)
end

it { is_expected.to be_nil }
end
end

describe "#save" do
let(:option) { Date.yesterday }

it "updates the journey session" do
expect { expect(form.save).to be(true) }.to(
change { journey_session.reload.answers.date_of_entry }
.to(option)
)
end
end
end
Loading

0 comments on commit a92f8ef

Please sign in to comment.