Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions app/models/immunisation_import_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ImmunisationImportRow
validates :time_of_vaccination,
presence: true,
if: -> { @data["TIME_OF_VACCINATION"]&.strip.present? }
validate :session_date_exists

CARE_SETTING_SCHOOL = 1
CARE_SETTING_COMMUNITY = 2
Expand Down Expand Up @@ -323,8 +324,6 @@ def administered_at
end

def location
return unless valid?

@location ||=
if school && (care_setting.nil? || care_setting == CARE_SETTING_SCHOOL)
school
Expand All @@ -334,12 +333,10 @@ def location
end

def school
return unless valid?

@school ||=
if school_urn != SCHOOL_URN_HOME_EDUCATED &&
school_urn != SCHOOL_URN_UNKNOWN
Location.find_by!(urn: school_urn)
Location.find_by(urn: school_urn)
end
end

Expand Down Expand Up @@ -407,6 +404,19 @@ def date_of_birth_in_a_valid_year_group
end
end

def session_date_exists
return if date_of_vaccination.nil? || location.nil?
return if academic_year != Date.current.academic_year

unless Session.has_date(date_of_vaccination).exists?(
organisation:,
location:,
academic_year:
)
errors.add(:date_of_vaccination, :inclusion)
end
end

def existing_patients
if patient_first_name.blank? || patient_last_name.blank? ||
patient_date_of_birth.nil? || patient_postcode.blank?
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ en:
blank: Enter a date in the correct format.
greater_than_or_equal_to: The vaccination date is outside the programme. Enter a date after the programme started.
less_than_or_equal_to: The vaccination date is outside the programme. Enter a date before today.
inclusion: Enter a date for a current session
time_of_vaccination:
blank: Enter a time in the correct format.
vaccine_given:
Expand Down
2 changes: 1 addition & 1 deletion spec/features/import_vaccination_records_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

describe "Immunisation imports" do
around { |example| travel_to(Date.new(2024, 5, 20)) { example.run } }
around { |example| travel_to(Date.new(2025, 5, 20)) { example.run } }

scenario "User uploads a file, views cohort and vaccination records" do
given_i_am_signed_in
Expand Down
22 changes: 21 additions & 1 deletion spec/models/immunisation_import_row_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@
end
end

context "when date doesn't match an existing session" do
subject(:errors) { immunisation_import_row.errors[:date_of_vaccination] }

before { immunisation_import_row.valid? }

context "when importing for the current academic year" do
let(:data) do
{ "DATE_OF_VACCINATION" => "#{Date.current.academic_year}0901" }
end

it { should include(/current session/) }
end

context "when importing for a different academic year" do
let(:data) { { "DATE_OF_VACCINATION" => "20220101" } }

it { should be_empty }
end
end

context "with an invalid time of vaccination" do
let(:data) { { "TIME_OF_VACCINATION" => "abc" } }

Expand Down Expand Up @@ -528,7 +548,7 @@
context "without data" do
let(:data) { {} }

it { should be_nil }
it { should eq("Unknown") }
end

context "with a school" do
Expand Down
Loading