Skip to content

Commit bc8a814

Browse files
committed
Add locations to vaccination records
Currently the location of a vaccination record is determined from the session it was recorded in. In most cases, this will be the same, however there are scenarios where this is not the case: - If the session is for the generic clinic, the location needs to be set to one of the community clinics. - If a nurse is marking a patient as already having had the vaccine, this happens in a session, but the vaccination would have occurred elsewhere. The first case is already handled by the `location_name` column which is a free-text field and can contain anything. This works, but is unstructured, ideally we'd like to move to a place where we can link these records with the locations in the database for the community clinic. This commit gets us closer to that point. The second case is not handled currently. Instead, when recording a patient as having already had the vaccine, the location of the vaccination ends up being the session that the nurse happened to use to mark the patient as vaccinated already. In some cases this might be correct, but in most it won't be. This commit allows us to fix the second case, which will be done in a followup commit to this one.
1 parent e8b8f3b commit bc8a814

16 files changed

+70
-18
lines changed

app/controllers/patient_sessions/programmes_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def record_already_vaccinated
2020
draft_vaccination_record.reset!
2121
draft_vaccination_record.update!(
2222
first_active_wizard_step: :confirm,
23+
location: @session.location,
2324
location_name: @session.clinic? ? "Unknown" : nil,
2425
outcome: :already_had,
2526
patient: @patient,

app/forms/vaccinate_form.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def save(draft_vaccination_record:)
8888
identity_check_confirmed_by_other_relationship
8989
draft_vaccination_record.identity_check_confirmed_by_patient =
9090
identity_check_confirmed_by_patient
91+
draft_vaccination_record.location_id =
92+
session.location_id unless session.generic_clinic?
9193
draft_vaccination_record.patient_id = patient_session.patient_id
9294
draft_vaccination_record.performed_at = Time.current
9395
draft_vaccination_record.performed_by_user = current_user
@@ -100,7 +102,7 @@ def save(draft_vaccination_record:)
100102

101103
private
102104

103-
delegate :organisation, to: :patient_session
105+
delegate :organisation, :session, to: :patient_session
104106

105107
def administered? = vaccine_method != "none"
106108

app/lib/reports/export_formatters.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ def school_urn(location:, patient:)
1414
end
1515

1616
def school_name(location:, patient:)
17-
location.school? ? location.name : patient.school&.name || ""
17+
location&.school? ? location.name : patient.school&.name || ""
1818
end
1919

2020
def care_setting(location:)
21-
location.school? ? "1" : "2"
21+
location&.school? ? "1" : "2"
2222
end
2323

2424
def clinic_name(location:, vaccination_record:)
25-
location.school? ? "" : vaccination_record.location_name
25+
location&.school? ? "" : vaccination_record.location_name
2626
end
2727

2828
def consent_status(patient:, programme:, academic_year:)

app/lib/reports/programme_vaccinations_exporter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def vaccination_records
9393
:batch,
9494
:location,
9595
:performed_by_user,
96+
:session,
9697
:vaccine,
9798
patient: %i[consent_statuses gp_practice school]
9899
)

app/lib/reports/systm_one_exporter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def row(vaccination_record:)
150150

151151
# TODO: Needs support for community and generic clinics.
152152
def practice_code(vaccination_record)
153-
location = vaccination_record.session.location
153+
location = vaccination_record.location
154154

155155
location.school? ? location.urn : location.ods_code
156156
end

app/models/draft_vaccination_record.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class DraftVaccinationRecord
1616
attribute :identity_check_confirmed_by_other_name, :string
1717
attribute :identity_check_confirmed_by_other_relationship, :string
1818
attribute :identity_check_confirmed_by_patient, :boolean
19+
attribute :location_id, :integer
1920
attribute :location_name, :string
2021
attribute :notes, :string
2122
attribute :outcome, :string
@@ -49,7 +50,7 @@ def wizard_steps
4950
(:delivery if administered?),
5051
(:dose if administered? && can_be_half_dose?),
5152
(:batch if administered?),
52-
(:location if location&.generic_clinic?),
53+
(:location if session&.generic_clinic?),
5354
:confirm
5455
].compact
5556
end
@@ -141,6 +142,15 @@ def batch=(value)
141142
self.batch_id = value.id
142143
end
143144

145+
def location
146+
return nil if location_id.nil?
147+
Location.find(location_id)
148+
end
149+
150+
def location=(value)
151+
self.location_id = value&.id
152+
end
153+
144154
def patient
145155
return nil if patient_id.nil?
146156

@@ -151,8 +161,6 @@ def patient=(value)
151161
self.patient_id = value.id
152162
end
153163

154-
delegate :location, to: :session, allow_nil: true
155-
156164
def performed_by_user
157165
return nil if performed_by_user_id.nil?
158166

@@ -266,6 +274,7 @@ def writable_attribute_names
266274
full_dose
267275
protocol
268276
identity_check
277+
location_id
269278
location_name
270279
notes
271280
outcome

app/models/immunisation_import_row.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def to_vaccination_record
9898
attributes = {
9999
dose_sequence: dose_sequence_value,
100100
full_dose: true,
101+
location:,
101102
location_name:,
102103
outcome:,
103104
patient_id: patient.id,
@@ -225,8 +226,12 @@ def vaccine_name = @data[:vaccine_given]
225226

226227
delegate :organisation, to: :team
227228

229+
def location
230+
session&.location unless session&.generic_clinic?
231+
end
232+
228233
def location_name
229-
return unless session.nil? || session.location.generic_clinic?
234+
return unless location.nil?
230235

231236
if is_school_setting? || (is_unknown_setting? && clinic_name.blank?)
232237
school&.name || school_name&.to_s || "Unknown"

app/models/session.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class Session < ApplicationRecord
166166

167167
before_create :set_slug
168168

169-
delegate :clinic?, :school?, to: :location
169+
delegate :clinic?, :generic_clinic?, :school?, to: :location
170170

171171
def to_param
172172
slug

app/models/vaccination_record.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
# created_at :datetime not null
2929
# updated_at :datetime not null
3030
# batch_id :bigint
31+
# location_id :bigint
3132
# nhs_immunisations_api_id :string
3233
# patient_id :bigint
3334
# performed_by_user_id :bigint
@@ -39,6 +40,7 @@
3940
#
4041
# index_vaccination_records_on_batch_id (batch_id)
4142
# index_vaccination_records_on_discarded_at (discarded_at)
43+
# index_vaccination_records_on_location_id (location_id)
4244
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
4345
# index_vaccination_records_on_patient_id (patient_id)
4446
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
@@ -90,14 +92,14 @@ class VaccinationRecord < ApplicationRecord
9092

9193
has_and_belongs_to_many :immunisation_imports
9294

95+
belongs_to :location, optional: true
9396
belongs_to :patient
9497
belongs_to :session, optional: true
9598

9699
has_one :identity_check, autosave: true, dependent: :destroy
97-
has_one :location, through: :session
98100
has_one :organisation, through: :session
99-
has_one :team, through: :session
100101
has_one :subteam, through: :session
102+
has_one :team, through: :session
101103

102104
scope :for_academic_year,
103105
->(academic_year) do
@@ -214,9 +216,7 @@ def snomed_procedure_code = vaccine&.snomed_procedure_code(dose_sequence:)
214216

215217
private
216218

217-
def requires_location_name?
218-
session.nil? || location&.generic_clinic?
219-
end
219+
def requires_location_name? = location.nil?
220220

221221
delegate :maximum_dose_sequence, to: :programme
222222

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
class AddLocationToVaccinationRecords < ActiveRecord::Migration[8.0]
4+
def change
5+
add_reference :vaccination_records, :location
6+
7+
reversible do |dir|
8+
dir.up do
9+
VaccinationRecord
10+
.where.not(session_id: nil)
11+
.eager_load(:session)
12+
.find_each { it.update_column(:location_id, it.session.location_id) }
13+
end
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)