diff --git a/app/models/immunisation_import_row.rb b/app/models/immunisation_import_row.rb index f92d144df6..3ea6102189 100644 --- a/app/models/immunisation_import_row.rb +++ b/app/models/immunisation_import_row.rb @@ -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 @@ -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 @@ -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 @@ -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? diff --git a/config/locales/en.yml b/config/locales/en.yml index 0e4c81fc72..f25dd5c397 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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: diff --git a/spec/features/import_vaccination_records_spec.rb b/spec/features/import_vaccination_records_spec.rb index 2888792a71..544e11197a 100644 --- a/spec/features/import_vaccination_records_spec.rb +++ b/spec/features/import_vaccination_records_spec.rb @@ -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 diff --git a/spec/models/immunisation_import_row_spec.rb b/spec/models/immunisation_import_row_spec.rb index f1b66c1966..f044b90edf 100644 --- a/spec/models/immunisation_import_row_spec.rb +++ b/spec/models/immunisation_import_row_spec.rb @@ -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" } } @@ -528,7 +548,7 @@ context "without data" do let(:data) { {} } - it { should be_nil } + it { should eq("Unknown") } end context "with a school" do