Skip to content

Commit

Permalink
1134: Validate pending induction submission start_date against future…
Browse files Browse the repository at this point in the history
… IPs
  • Loading branch information
edujackedu committed Jan 31, 2025
1 parent 14e423b commit 89b380c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
9 changes: 9 additions & 0 deletions app/models/concerns/shared_induction_period_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ def ensure_start_date_after_qts_date(qts_award_date)

errors.add(:started_on, "Start date cannot be before QTS award date (#{qts_award_date.to_fs(:govuk)})")
end

def ensure_no_future_induction_periods(teacher)
teacher.induction_periods.order(:started_on).each do |period|
if started_on < period.started_on
errors.add(:started_on, "Enter a start date after the last induction period finished (#{period.started_on.to_fs(:govuk)})")
break
end
end
end
end
10 changes: 10 additions & 0 deletions app/models/pending_induction_submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,21 @@ class PendingInductionSubmission < ApplicationRecord

validate :start_date_after_qts_date, on: :register_ect

validate :no_future_induction_periods, if: -> { started_on.present? }

private

def start_date_after_qts_date
return if trs_qts_awarded_on.blank?

ensure_start_date_after_qts_date(trs_qts_awarded_on)
end

def no_future_induction_periods
teacher = Teacher.find_by(trn:)

return if teacher.blank?

ensure_no_future_induction_periods(teacher)
end
end
14 changes: 14 additions & 0 deletions spec/models/pending_induction_submission_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,19 @@
end
end
end

describe "#no_future_induction_periods" do
it "validates that there are no future induction periods" do
teacher = FactoryBot.create(:teacher)
started_on = Date.current - 1.day
FactoryBot.create(:induction_period, teacher:, started_on:, finished_on: Date.current)

pending_induction_submission = FactoryBot.build(:pending_induction_submission, trn: teacher.trn, started_on: Date.current - 3.days, finished_on: Date.current - 1.day)

pending_induction_submission.valid?(:register_ect)

expect(pending_induction_submission.errors[:started_on]).to include("Enter a start date after the last induction period finished (#{started_on.to_fs(:govuk)})")
end
end
end
end
22 changes: 16 additions & 6 deletions spec/services/appropriate_bodies/claim_an_ect/register_ect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,24 @@
end
end

xcontext "when the teacher already has an induction period" do
context "when trying to register a teacher with a start_date before future IPs" do
let!(:existing_teacher) { FactoryBot.create(:teacher, trn: "1234567") }
let!(:existing_induction_period) { FactoryBot.create(:induction_period, teacher: existing_teacher) }
let(:started_on) { 2.days.ago }
let!(:existing_induction_period) { FactoryBot.create(:induction_period, teacher: existing_teacher, started_on:, finished_on: 1.day.ago) }

it "raises TeacherAlreadyClaimedError" do
expect {
subject.register(pending_induction_submission_params)
}.to raise_error(AppropriateBodies::Errors::TeacherAlreadyClaimedError, "Teacher already claimed")
let(:pending_induction_submission_params) do
{
induction_programme: "fip",
started_on: 3.days.ago.to_date,
trn: "1234567",
trs_first_name: "John",
trs_last_name: "Doe",
trs_qts_awarded_on:
}
end
it "fails because invalid" do
expect(subject.register(pending_induction_submission_params)).to be_falsey
expect(subject.pending_induction_submission.errors.key?(:started_on)).to be true
end
end

Expand Down

0 comments on commit 89b380c

Please sign in to comment.