Skip to content

Commit cc620c1

Browse files
committed
Fix invalid vaccination dates in already vaccinated flow
The "record as already vaccinated" flow from a child programme page creates a session-less `DraftVaccinationRecord`. In that path, draft date validation was not being enforced consistently, so invalid vaccination dates could reach the check-and-confirm step and then fail later when `VaccinationRecord` validation ran on save. Update `DraftVaccinationRecord` so `performed_at_date` range validation runs during draft updates for session-less manual-report flows as well as session-backed flows. Jira-Issue: MAV-7121
1 parent e12832d commit cc620c1

3 files changed

Lines changed: 89 additions & 3 deletions

File tree

app/models/draft_vaccination_record.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def wizard_steps
9292

9393
on_wizard_step :date_and_time, exact: true do
9494
validates :performed_at_date, presence: true
95-
validate :performed_at_date_within_range
9695
end
9796

9897
on_wizard_step :outcome, exact: true do
@@ -188,6 +187,8 @@ def wizard_steps
188187
validates :source, inclusion: { in: VaccinationRecord.sources.keys }
189188
end
190189

190+
validate :performed_at_date_within_range, on: :update
191+
191192
def created_at = Time.current
192193

193194
def administered?
@@ -478,7 +479,7 @@ def reset_unused_attributes
478479
end
479480
end
480481

481-
def academic_year = session&.academic_year
482+
def academic_year = session&.academic_year || AcademicYear.pending
482483

483484
def batch
484485
return nil if batch_id.nil?
@@ -498,7 +499,7 @@ def latest_possible_date
498499
end
499500

500501
def performed_at_date_within_range
501-
return if performed_at_date.nil? || session.nil?
502+
return if performed_at_date.nil?
502503

503504
if performed_at_date < earliest_possible_date
504505
errors.add(
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
describe "Record already vaccinated" do
4+
around { |example| travel_to(Date.new(2026, 5, 1)) { example.run } }
5+
6+
scenario "Flu rejects a future date" do
7+
given_i_am_signed_in
8+
9+
when_i_go_to_a_patient
10+
and_i_click_on_the_programme
11+
then_i_see_the_patient_page
12+
13+
when_i_click_record_already_vaccinated
14+
and_i_choose_a_future_date
15+
then_i_see_an_error_for_the_future_date
16+
end
17+
18+
def given_i_am_signed_in
19+
programmes = [Programme.flu]
20+
21+
team = create(:team, :with_one_nurse, programmes:)
22+
school = create(:gias_school, :primary, team:, programmes:)
23+
24+
@patient = create(:patient, :consent_no_response, school:, programmes:)
25+
26+
sign_in team.users.first
27+
end
28+
29+
def when_i_go_to_a_patient
30+
visit patient_path(@patient)
31+
end
32+
33+
def and_i_click_on_the_programme
34+
within(".app-secondary-navigation") { click_on "Flu" }
35+
end
36+
37+
def then_i_see_the_patient_page
38+
expect(page).to have_content(@patient.full_name)
39+
end
40+
41+
def when_i_click_record_already_vaccinated
42+
click_on "Record as already vaccinated"
43+
end
44+
45+
def and_i_choose_a_future_date
46+
fill_in "Day", with: "2"
47+
fill_in "Month", with: "5"
48+
fill_in "Year", with: "2026"
49+
click_on "Continue"
50+
end
51+
52+
def then_i_see_an_error_for_the_future_date
53+
expect(page).to have_content(
54+
"The vaccination cannot take place after 1 May 2026"
55+
)
56+
expect(page).not_to have_content("Check and confirm")
57+
end
58+
end

spec/models/draft_vaccination_record_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,33 @@
7272
end
7373
end
7474

75+
context "when recording recording already vaccinated" do
76+
context "when performed_at is in the future on the date step" do
77+
let(:attributes) do
78+
valid_administered_attributes.merge(
79+
patient_id: patient.id,
80+
performed_at: 1.day.from_now,
81+
programme_type: programme.type,
82+
session_id: nil,
83+
source: "manual_report"
84+
)
85+
end
86+
87+
around { |example| freeze_time { example.run } }
88+
89+
before { draft_vaccination_record.wizard_step = :date_and_time }
90+
91+
it "has an error" do
92+
expect(draft_vaccination_record.save(context: :update)).to be(false)
93+
expect(
94+
draft_vaccination_record.errors[:performed_at_date]
95+
).to include(
96+
"The vaccination cannot take place after #{Date.current.to_fs(:long)}"
97+
)
98+
end
99+
end
100+
end
101+
75102
context "when the programme is flu" do
76103
let(:attributes) do
77104
valid_administered_attributes.merge(

0 commit comments

Comments
 (0)