Skip to content

Commit 871784b

Browse files
committed
Populate school and location at same time
In the previous commit we added a `school_id` column which will eventually replace the `location_id` column. This change ensures that when saving or updating patient locations, we set the `school_id` column matching the value of the `location_id` column. Once we're confident both columns are being written to we will stop writing to `location_id` and eventually remove it. Jira-Issue: MAV-7114 Jira-Issue: MAV-7113
1 parent 434d998 commit 871784b

5 files changed

Lines changed: 29 additions & 7 deletions

File tree

app/lib/mavis_cli/schools/move_patients.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def call(source_urn:, target_urn:)
7070
PatientLocation.where(
7171
academic_year:,
7272
location_id: old_loc.id
73-
).update_all(location_id: new_loc.id)
73+
).update_all(location_id: new_loc.id, school_id: new_loc.id)
7474
ConsentForm.where(team_location_id: old_team_location.id).update_all(
7575
team_location_id: new_team_location.id
7676
)

app/lib/team_merger.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ def migrate_generic_locations(merged_team)
380380
.where.not(id: patient_location_ids_to_keep)
381381
.delete_all
382382
PatientLocation.where(id: patient_location_ids_to_keep).update_all(
383-
location_id: merged_loc.id
383+
location_id: merged_loc.id,
384+
school_id: merged_loc.id
384385
)
385386
end
386387

app/models/patient_location.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class PatientLocation < ApplicationRecord
3737

3838
belongs_to :patient
3939
belongs_to :location
40+
belongs_to :school, class_name: "Location", optional: true
4041

4142
has_and_belongs_to_many :immunisation_imports
4243

@@ -84,6 +85,16 @@ class PatientLocation < ApplicationRecord
8485
)
8586
end
8687

88+
def location=(location)
89+
super
90+
self.school = location
91+
end
92+
93+
def location_id=(location_id)
94+
super
95+
self.school_id = location_id
96+
end
97+
8798
def begin_date
8899
value = date_range.begin
89100
return nil if value.nil? || value == -Float::INFINITY

spec/jobs/enqueue_vaccinations_search_in_nhs_job_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,12 @@ def setup_feature_flag
357357

358358
Patient.import(build_list(:patient, 50, team:, school:, session:))
359359
PatientLocation.import(
360-
Patient.all.map do
361-
{
362-
patient_id: it.id,
363-
location_id: location.id,
360+
Patient.all.map do |patient|
361+
PatientLocation.new(
362+
patient:,
363+
location:,
364364
academic_year: AcademicYear.pending
365-
}
365+
)
366366
end
367367
)
368368
PatientTeamUpdater.call

spec/models/patient_location_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
let(:programme) { Programme.sample }
3838
let(:session) { create(:session, programmes: [programme]) }
3939

40+
describe "associations" do
41+
it { should belong_to(:patient) }
42+
43+
it do
44+
expect(patient_location).to belong_to(:school).class_name(
45+
"Location"
46+
).optional(true)
47+
end
48+
end
49+
4050
describe "scopes" do
4151
describe "#appear_in_programmes" do
4252
subject(:scope) do

0 commit comments

Comments
 (0)