Skip to content

Commit 4ed9374

Browse files
committed
Corrections to vaccination record date range validations
This issue came up during testing of MAV-2848. It should be possible to record a `performed_at` date before the current academic year unless the programme type is `flu`. Summary of validation rules: - `performed_at` should not be earlier than the patient's date of birth - `performed_at` should not be later than the current datetime - If the programme is Flu and this is a national reporting record `performed_at` should not be earlier than the start of the current academic year. Jira-Issue: MAV-2848 Jira-Issue: MAV-3393
1 parent 17284c8 commit 4ed9374

2 files changed

Lines changed: 83 additions & 10 deletions

File tree

app/models/draft_vaccination_record.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,11 @@ def batch
455455
end
456456

457457
def earliest_possible_date
458-
academic_year.to_academic_year_date_range.first
458+
if programme&.flu? || national_reporting_user_and_record?
459+
academic_year.to_academic_year_date_range.first
460+
else
461+
patient.date_of_birth
462+
end
459463
end
460464

461465
def latest_possible_date

spec/models/draft_vaccination_record_spec.rb

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
let(:programme) { Programme.hpv }
1414
let(:session) { create(:session, team:, programmes: [programme]) }
15-
let(:patient) { create(:patient, :in_attendance, session:) }
15+
let(:patient) { create(:patient, :in_attendance, session:, date_of_birth: Date.new(2020, 8, 31)) }
16+
1617
let(:vaccine) { programme.vaccines.first }
1718
let(:batch) { create(:batch, team:, vaccine:, expiry: Date.new(2026, 11, 1)) }
1819

@@ -64,23 +65,91 @@
6465
end
6566
end
6667

67-
context "when performed_at is before the start of the academic year" do
68+
context "when the programme is flu" do
6869
let(:attributes) do
6970
valid_administered_attributes.merge(
70-
performed_at: Time.zone.local(2023, 8, 31, 12)
71+
performed_at: Time.zone.local(2023, 8, 31, 12),
72+
programme_type: Programme.flu.type
7173
)
7274
end
7375

74-
around { |example| travel_to(Date.new(2025, 8, 31)) { example.run } }
76+
context "when performed_at is before the start of the academic year" do
77+
around { |example| travel_to(Date.new(2025, 8, 31)) { example.run } }
7578

76-
before { draft_vaccination_record.wizard_step = :date_and_time }
79+
before { draft_vaccination_record.wizard_step = :date_and_time }
7780

78-
it "has an error" do
79-
expect(draft_vaccination_record.save(context: :update)).to be(false)
80-
expect(draft_vaccination_record.errors[:performed_at_date]).to include(
81-
"The vaccination cannot take place before 1 September 2024"
81+
it "has an error" do
82+
expect(draft_vaccination_record.save(context: :update)).to be(false)
83+
expect(draft_vaccination_record.errors[:performed_at_date]).to include(
84+
"The vaccination cannot take place before 1 September 2024"
85+
)
86+
end
87+
end
88+
end
89+
90+
context "when the record is national reporting" do
91+
let(:team) { create(:team, :national_reporting, :with_one_nurse, programmes: [programme]) }
92+
let(:attributes) do
93+
valid_administered_attributes.merge(
94+
performed_at: Time.zone.local(2023, 8, 31, 12),
95+
programme_type: Programme.mmr.type,
96+
source: "national_reporting"
8297
)
8398
end
99+
100+
context "when performed_at is before the start of the academic year" do
101+
around { |example| travel_to(Date.new(2025, 8, 31)) { example.run } }
102+
103+
before { draft_vaccination_record.wizard_step = :date_and_time }
104+
105+
it "has an error" do
106+
expect(draft_vaccination_record.save(context: :update)).to be(false)
107+
expect(draft_vaccination_record.errors[:performed_at_date]).to include(
108+
"The vaccination cannot take place before 1 September 2024"
109+
)
110+
end
111+
end
112+
end
113+
114+
context "when the programme is not flu" do
115+
let(:attributes) do
116+
valid_administered_attributes.merge(
117+
performed_at: Time.zone.local(2023, 8, 31, 12),
118+
programme_type: Programme.mmr.type
119+
)
120+
end
121+
122+
context "when performed_at is before the start of the academic year for an already vaccinated patient" do
123+
around { |example| travel_to(Date.new(2025, 8, 31)) { example.run } }
124+
125+
before { draft_vaccination_record.wizard_step = :date_and_time }
126+
127+
it "is valid" do
128+
expect(draft_vaccination_record.save(context: :update)).to be(true)
129+
expect(draft_vaccination_record.errors[:performed_at_date]).to be_empty
130+
end
131+
end
132+
133+
context "when performed_at is before patient date of birth" do
134+
let(:patient) { create(:patient, :in_attendance, session:, date_of_birth: Date.new(2020, 8, 31)) }
135+
let(:attributes) do
136+
valid_administered_attributes.merge(
137+
performed_at: Time.zone.local(2020, 8, 30, 12),
138+
outcome: "already_had"
139+
)
140+
end
141+
142+
around { |example| travel_to(Date.new(2025, 8, 31)) { example.run } }
143+
144+
before { draft_vaccination_record.wizard_step = :date_and_time }
145+
146+
it "has an error" do
147+
expect(draft_vaccination_record.save(context: :update)).to be(false)
148+
expect(draft_vaccination_record.errors[:performed_at_date]).to include(
149+
"The vaccination cannot take place before 31 August 2020"
150+
)
151+
end
152+
end
84153
end
85154

86155
context "when performed_at is after the end of a previous academic year session" do

0 commit comments

Comments
 (0)