Skip to content

Commit

Permalink
Make immunisation row validation stricter
Browse files Browse the repository at this point in the history
If the row is marked as not vaccinated, we should ensure that a batch,
vaccine, delivery site is not provided. Similarly, if a row is marked as
vaccinated, we should ensure that a reason not vaccinated is not provided.
  • Loading branch information
thomasleese committed Nov 18, 2024
1 parent 4324bb0 commit a0734e9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
31 changes: 20 additions & 11 deletions app/models/immunisation_import_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,39 @@ class ImmunisationImportRow
include ActiveModel::Model

validates :administered, inclusion: [true, false]
validates :batch_expiry_date, presence: true, if: :administered

with_options if: :administered do
validates :batch_expiry_date, presence: true
validates :batch_number, presence: true
validates :delivery_site, presence: true
validates :reason, absence: true
validates :vaccine_given, inclusion: { in: :valid_given_vaccines }
end

with_options unless: :administered do
validates :batch_expiry_date, absence: true
validates :batch_number, absence: true
validates :delivery_site, absence: true
validates :reason, presence: true
validates :vaccine_given, absence: true
end

validates :batch_expiry_date,
comparison: {
greater_than: -> { Date.new(Date.current.year - 15, 1, 1) },
less_than: -> { Date.new(Date.current.year + 15, 1, 1) }
},
if: -> { administered && batch_expiry_date.present? }
validates :batch_number, presence: true, if: :administered
validates :reason, presence: true, if: -> { administered == false }
validates :delivery_site, presence: true, if: :administered
if: :batch_expiry_date
validate :delivery_site_appropriate_for_vaccine,
if: -> { administered && delivery_site.present? && vaccine.present? }
validates :dose_sequence,
comparison: {
greater_than_or_equal_to: 1,
less_than_or_equal_to: :maximum_dose_sequence
},
if: -> { administered && vaccine.present? }
if: :vaccine

validates :organisation_code, comparison: { equal_to: :ods_code }
validates :vaccine_given,
inclusion: {
in: :valid_given_vaccines
},
if: :administered

SCHOOL_URN_HOME_EDUCATED = "999999"
SCHOOL_URN_UNKNOWN = "888888"
Expand Down
11 changes: 11 additions & 0 deletions spec/models/immunisation_import_row_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@
end
end

context "when vaccinated and a reason not given" do
let(:data) do
{ "VACCINATED" => "Y", "REASON_NOT_VACCINATED" => "unwell" }
end

it "has errors" do
expect(immunisation_import_row).to be_invalid
expect(immunisation_import_row.errors[:reason]).to eq(["must be blank"])
end
end

context "for a vaccination administered today, with no time provided" do
around { |example| freeze_time { example.run } }

Expand Down

0 comments on commit a0734e9

Please sign in to comment.