Skip to content

Commit 816b3ea

Browse files
committed
Replace session attendance patient session foreign key
This replaces the foreign key association between session attendances and patient sessions to instead link directly to the patient. This is needed as we eventually want to replace the PatientSession model and to do that we need to make sure all foreign keys to it have been replaced. The functionality should be the same before and after. Jira-Issue: MAV-1820
1 parent 620f195 commit 816b3ea

17 files changed

+187
-90
lines changed

app/controllers/patient_sessions/session_attendances_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ def set_session_date
4747

4848
def set_session_attendance
4949
@session_attendance =
50-
authorize @patient_session
50+
authorize(@patient
5151
.session_attendances
5252
.includes(:patient, :session_date)
5353
.find_or_initialize_by(session_date: @session_date)
54+
.tap { it.patient_session = @patient_session })
5455
end
5556

5657
def session_attendance_params

app/lib/generate/vaccination_records.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ def create_vaccinations
2323
vaccination_records = []
2424

2525
random_patient_sessions.each do |patient_session|
26-
patient_session_id = patient_session.id
27-
session_date_ids = patient_session.session.session_dates.pluck(:id)
28-
29-
unless SessionAttendance.exists?(
30-
patient_session_id:,
31-
session_date_id: session_date_ids
26+
patient = patient_session.patient
27+
session = patient_session.session
28+
29+
unless SessionAttendance.joins(:session_date).exists?(
30+
patient:,
31+
session_date: {
32+
session:
33+
}
3234
)
3335
session_attendances << FactoryBot.build(
3436
:session_attendance,
3537
:present,
36-
patient_session:
38+
patient:,
39+
session:
3740
)
3841
end
3942

app/models/patient.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Patient < ApplicationRecord
7171
has_many :patient_sessions
7272
has_many :school_move_log_entries
7373
has_many :school_moves
74+
has_many :session_attendances
7475
has_many :session_notifications
7576
has_many :triage_statuses
7677
has_many :triages
@@ -81,7 +82,6 @@ class Patient < ApplicationRecord
8182
has_many :parents, through: :parent_relationships
8283
has_many :patient_specific_directions
8384
has_many :pre_screenings, through: :patient_sessions
84-
has_many :session_attendances, through: :patient_sessions
8585
has_many :sessions, through: :patient_sessions
8686
has_many :teams, -> { distinct }, through: :sessions
8787

app/models/patient_session.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class PatientSession < ApplicationRecord
4646

4747
has_many :gillick_assessments
4848
has_many :pre_screenings
49-
has_many :session_attendances, dependent: :destroy
5049
has_one :registration_status
5150

5251
has_one :location, through: :session
@@ -61,6 +60,10 @@ class PatientSession < ApplicationRecord
6160
through: :patient,
6261
source: :notes
6362

63+
has_many :session_attendances,
64+
-> { where(patient_id: it.patient_id) },
65+
through: :session
66+
6467
has_many :session_notifications,
6568
-> { where(session_id: it.session_id) },
6669
through: :patient
@@ -280,9 +283,11 @@ def programmes = session.programmes_for(patient:, academic_year:)
280283

281284
def todays_attendance
282285
if (session_date = session.session_dates.today.first)
283-
session_attendances.includes(:session_date).find_or_initialize_by(
284-
session_date:
285-
)
286+
patient
287+
.session_attendances
288+
.includes(:session_date)
289+
.find_or_initialize_by(session_date:)
290+
.tap { it.patient_session = self }
286291
end
287292
end
288293

app/models/patient_session/registration_status.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ class PatientSession::RegistrationStatus < ApplicationRecord
2727
through: :patient
2828

2929
has_one :session_attendance,
30-
-> { today },
31-
through: :patient_session,
30+
-> do
31+
today.where(
32+
session_dates: {
33+
session_id: it.patient_session.session_id
34+
}
35+
)
36+
end,
37+
through: :patient,
3238
source: :session_attendances
3339

3440
enum :status,

app/models/session.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Session < ApplicationRecord
5454
has_many :programmes, through: :session_programmes
5555
has_many :gillick_assessments, through: :patient_sessions
5656
has_many :patients, through: :patient_sessions
57+
has_many :session_attendances, through: :session_dates
5758
has_many :vaccines, through: :programmes
5859

5960
has_many :location_programme_year_groups,

app/models/session_attendance.rb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,33 @@
44
#
55
# Table name: session_attendances
66
#
7-
# id :bigint not null, primary key
8-
# attending :boolean not null
9-
# created_at :datetime not null
10-
# updated_at :datetime not null
11-
# patient_session_id :bigint not null
12-
# session_date_id :bigint not null
7+
# id :bigint not null, primary key
8+
# attending :boolean not null
9+
# created_at :datetime not null
10+
# updated_at :datetime not null
11+
# patient_id :bigint not null
12+
# session_date_id :bigint not null
1313
#
1414
# Indexes
1515
#
16-
# idx_on_patient_session_id_session_date_id_be8bd21ddf (patient_session_id,session_date_id) UNIQUE
17-
# index_session_attendances_on_session_date_id (session_date_id)
16+
# index_session_attendances_on_patient_id (patient_id)
17+
# index_session_attendances_on_session_date_id (session_date_id)
1818
#
1919
# Foreign Keys
2020
#
21-
# fk_rails_... (patient_session_id => patient_sessions.id)
21+
# fk_rails_... (patient_id => patients.id)
2222
# fk_rails_... (session_date_id => session_dates.id)
2323
#
2424
class SessionAttendance < ApplicationRecord
25-
audited associated_with: :patient_session
25+
audited associated_with: :patient
2626

27-
belongs_to :patient_session
27+
belongs_to :patient
2828
belongs_to :session_date
2929

30-
has_one :session, through: :patient_session
31-
has_one :patient, through: :patient_session
30+
has_one :session, through: :session_date
3231
has_one :location, through: :session
3332

3433
scope :today, -> { joins(:session_date).merge(SessionDate.today) }
34+
35+
attr_accessor :patient_session
3536
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
class RemovePatientSessionFromSessionAttendances < ActiveRecord::Migration[8.0]
4+
def up
5+
change_table :session_attendances, bulk: true do |t|
6+
t.references :patient, foreign_key: true
7+
end
8+
9+
SessionAttendance.find_each do |session_attendance|
10+
patient_session =
11+
PatientSession.find(session_attendance.patient_session_id)
12+
patient_id = patient_session.patient_id
13+
session_attendance.update_column(:patient_id, patient_id)
14+
end
15+
16+
change_table :session_attendances, bulk: true do |t|
17+
t.change_null :patient_id, false
18+
t.remove_references :patient_session
19+
end
20+
end
21+
22+
def down
23+
add_reference :session_attendances, :patient_session
24+
25+
SessionAttendance.find_each do |session_attendance|
26+
session_id =
27+
SessionDate.find(session_attendance.session_date_id).session_id
28+
patient_session =
29+
PatientSession.find_by!(
30+
patient_id: session_attendance.patient_id,
31+
session_id:
32+
)
33+
session_attendance.update_column(:patient_session_id, patient_session.id)
34+
end
35+
36+
change_table :gillick_assessments, bulk: true do |t|
37+
t.change_null :patient_session_id, false
38+
t.remove_references :patient
39+
end
40+
end
41+
end

db/schema.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2025_08_21_073434) do
13+
ActiveRecord::Schema[8.0].define(version: 2025_08_21_135954) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pg_catalog.plpgsql"
1616
enable_extension "pg_trgm"
@@ -753,12 +753,12 @@
753753
end
754754

755755
create_table "session_attendances", force: :cascade do |t|
756-
t.bigint "patient_session_id", null: false
757756
t.bigint "session_date_id", null: false
758757
t.boolean "attending", null: false
759758
t.datetime "created_at", null: false
760759
t.datetime "updated_at", null: false
761-
t.index ["patient_session_id", "session_date_id"], name: "idx_on_patient_session_id_session_date_id_be8bd21ddf", unique: true
760+
t.bigint "patient_id", null: false
761+
t.index ["patient_id"], name: "index_session_attendances_on_patient_id"
762762
t.index ["session_date_id"], name: "index_session_attendances_on_session_date_id"
763763
end
764764

@@ -1055,7 +1055,7 @@
10551055
add_foreign_key "school_moves", "locations", column: "school_id"
10561056
add_foreign_key "school_moves", "patients"
10571057
add_foreign_key "school_moves", "teams"
1058-
add_foreign_key "session_attendances", "patient_sessions"
1058+
add_foreign_key "session_attendances", "patients"
10591059
add_foreign_key "session_attendances", "session_dates"
10601060
add_foreign_key "session_dates", "sessions"
10611061
add_foreign_key "session_notifications", "patients"

spec/factories/patient_sessions.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,22 @@
5252
end
5353

5454
trait :in_attendance do
55-
session_attendances do
56-
[association(:session_attendance, :present, patient_session: instance)]
57-
end
5855
registration_status do
5956
association(
6057
:patient_session_registration_status,
6158
:attending,
6259
patient_session: instance
6360
)
6461
end
62+
63+
after(:create) do |patient_session|
64+
create(
65+
:session_attendance,
66+
:present,
67+
patient: patient_session.patient,
68+
session: patient_session.session
69+
)
70+
end
6571
end
6672

6773
trait :added_to_session

0 commit comments

Comments
 (0)