Skip to content

Commit 6bbdf80

Browse files
authored
Ensure vaccination records without vaccines are handled correctly (#3218)
A number of components currently break when they try to render a vaccination record that doesn't have a vaccine. This is a valid state for a historical vaccination record that was imported and performed outside of the service, so it needs to be handled correctly.
2 parents bc79cd1 + 5529341 commit 6bbdf80

13 files changed

+114
-30
lines changed

app/components/app_activity_log_component.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ def vaccination_events
207207
vaccination_records.flat_map do |vaccination_record|
208208
title =
209209
if vaccination_record.administered?
210-
"Vaccinated with #{vaccination_record.vaccine.brand}"
210+
if (vaccine = vaccination_record.vaccine)
211+
"Vaccinated with #{vaccine.brand}"
212+
else
213+
"Vaccinated"
214+
end
211215
else
212216
"Vaccination not given: #{vaccination_record.human_enum_name(:outcome)}"
213217
end

app/components/app_outcome_banner_component.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,19 @@ def show_location?
6868

6969
def vaccine_summary
7070
type = vaccination_record.programme.name
71-
brand = vaccination_record.vaccine.brand
72-
batch = vaccination_record.batch.name
73-
"#{type} (#{brand}, #{batch})"
71+
batch = vaccination_record.batch&.name
72+
brand =
73+
(vaccination_record.vaccine || vaccination_record.batch&.vaccine)&.brand
74+
75+
if brand.present? && batch.present?
76+
"#{type} (#{brand}, #{batch})"
77+
elsif brand.present?
78+
"#{type} (#{brand})"
79+
elsif batch.present?
80+
"#{type} (#{batch})"
81+
else
82+
type
83+
end
7484
end
7585

7686
def reason_do_not_vaccinate

app/components/app_patient_vaccination_table_component.html.erb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
<% body.with_row do |row| %>
1616
<% row.with_cell do %>
1717
<span class="nhsuk-table-responsive__heading">Vaccine</span>
18-
<% label = "#{vaccination_record.vaccine.brand} (#{vaccination_record.programme.name})" %>
18+
<% label = if (vaccine = vaccination_record.vaccine)
19+
"#{vaccine.brand} (#{vaccination_record.programme.name})"
20+
else
21+
vaccination_record.programme.name
22+
end %>
23+
1924
<%= link_to label, programme_vaccination_record_path(vaccination_record.programme, vaccination_record) %>
2025
<% end %>
2126
<% row.with_cell do %>

app/jobs/mesh_dps_export_job.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ def perform
77
return unless Flipper.enabled? :mesh_jobs
88

99
Programme.find_each do |programme|
10-
if programme.vaccination_records.administered.unexported.any?
10+
if programme
11+
.vaccination_records
12+
.administered
13+
.unexported
14+
.recorded_in_service
15+
.any?
1116
dps_export = DPSExport.create!(programme:)
1217
response =
1318
MESH.send_file(data: dps_export.csv, to: Settings.mesh.dps_mailbox)

app/models/dps_export.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ def set_filename
5959

6060
def set_vaccination_records
6161
self.vaccination_records =
62-
programme.vaccination_records.administered.unexported
62+
programme.vaccination_records.administered.unexported.recorded_in_service
6363
end
6464
end

app/models/vaccination_record.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class VaccinationRecord < ApplicationRecord
9292
has_one :organisation, through: :session
9393
has_one :team, through: :session
9494

95+
scope :recorded_in_service, -> { where.not(session: nil) }
9596
scope :unexported, -> { where.missing(:dps_exports) }
9697

9798
scope :with_pending_changes,

spec/components/app_activity_log_component_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,23 @@
409409
by: "JOY, Nurse",
410410
programme: "HPV"
411411
end
412+
413+
describe "vaccination records" do
414+
context "without a vaccine" do
415+
before do
416+
create(
417+
:vaccination_record,
418+
patient:,
419+
programme: programmes.first,
420+
vaccine: nil,
421+
performed_at: Time.zone.parse("2024-05-31 13:00")
422+
)
423+
end
424+
425+
include_examples "card",
426+
title: "Vaccinated",
427+
date: "31 May 2024 at 1:00pm",
428+
programme: "HPV"
429+
end
430+
end
412431
end

spec/components/app_outcome_banner_component_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@
112112

113113
it { should have_text("Vaccinator\nUnknown") }
114114
end
115+
116+
context "when the vaccination is historical" do
117+
before do
118+
vaccination_record.update!(session: nil, location_name: "Unknown")
119+
end
120+
121+
it { should have_text("Vaccinated") }
122+
end
115123
end
116124

117125
context "state is triaged_do_not_vaccinate" do

spec/components/app_patient_vaccination_table_component_spec.rb

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,38 @@
3434
)
3535
end
3636
let(:session) { create(:session, location:, programmes: [programme]) }
37-
let(:vaccination_record) do
38-
create(
39-
:vaccination_record,
40-
patient:,
41-
programme:,
42-
session:,
43-
performed_at: Time.zone.local(2024, 1, 1)
44-
)
37+
38+
context "with a vaccine" do
39+
before do
40+
create(
41+
:vaccination_record,
42+
patient:,
43+
programme:,
44+
session:,
45+
performed_at: Time.zone.local(2024, 1, 1)
46+
)
47+
end
48+
49+
it { should have_link("Gardasil 9 (HPV)") }
50+
it { should have_content("Test School, Waterloo Road, London, SE1 8TY") }
51+
it { should have_content("1 January 2024") }
4552
end
4653

47-
before { vaccination_record }
54+
context "without a vaccine" do
55+
before do
56+
create(
57+
:vaccination_record,
58+
patient:,
59+
programme:,
60+
session:,
61+
performed_at: Time.zone.local(2024, 1, 1),
62+
vaccine: nil
63+
)
64+
end
4865

49-
it { should have_link("Gardasil 9 (HPV)") }
50-
it { should have_content("Test School, Waterloo Road, London, SE1 8TY") }
51-
it { should have_content("1 January 2024") }
66+
it { should have_link("HPV") }
67+
it { should have_content("Test School, Waterloo Road, London, SE1 8TY") }
68+
it { should have_content("1 January 2024") }
69+
end
5270
end
5371
end

spec/factories/vaccination_records.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@
7070
delivery_method { "intramuscular" }
7171

7272
vaccine do
73-
programme.vaccines.active.first || association(:vaccine, programme:)
73+
if session
74+
programme.vaccines.active.first || association(:vaccine, programme:)
75+
end
7476
end
7577

7678
batch do
77-
association :batch, organisation:, vaccine:, strategy: :create if vaccine
79+
association(:batch, organisation:, vaccine:, strategy: :create) if vaccine
7880
end
7981

8082
performed_by

0 commit comments

Comments
 (0)