Skip to content

Commit 97ec054

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 e228ded commit 97ec054

23 files changed

+219
-127
lines changed

app/components/app_patient_session_search_result_card_component.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ def initialize(patient_session, context:, programmes: [])
9797
def can_register_attendance?
9898
session_attendance =
9999
SessionAttendance.new(
100-
patient_session:,
101-
session_date: SessionDate.new(value: Date.current)
100+
patient:,
101+
session_date: SessionDate.new(session:, value: Date.current)
102102
)
103103

104104
policy(session_attendance).new?

app/controllers/patient_sessions/session_attendances_controller.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ def update
4343

4444
def set_session_attendance
4545
@session_attendance =
46-
authorize @patient_session
47-
.session_attendances
48-
.includes(:patient, :session_date)
49-
.find_or_initialize_by(session_date: @session_date)
46+
authorize(
47+
@patient
48+
.session_attendances
49+
.includes(:patient, session_date: { session: :programmes })
50+
.find_or_initialize_by(session_date: @session_date)
51+
)
5052
end
5153

5254
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/lib/status_updater.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def update_registration_statuses!
5959
.where(patient_session_id: patient_sessions.select(:id))
6060
.includes(
6161
:session_attendances,
62+
:session_date,
6263
:vaccination_records,
6364
patient_session: {
6465
session: :programmes

app/models/patient.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Patient < ApplicationRecord
6565
has_many :consent_notifications
6666
has_many :consent_statuses
6767
has_many :consents
68+
has_many :gillick_assessments
6869
has_many :notes
6970
has_many :notify_log_entries
7071
has_many :parent_relationships, -> { order(:created_at) }
@@ -73,17 +74,16 @@ class Patient < ApplicationRecord
7374
has_many :pre_screenings
7475
has_many :school_move_log_entries
7576
has_many :school_moves
77+
has_many :session_attendances
7678
has_many :session_notifications
7779
has_many :triage_statuses
7880
has_many :triages
7981
has_many :vaccination_records, -> { kept }
8082
has_many :vaccination_statuses
8183
has_many :patient_specific_directions
8284

83-
has_many :gillick_assessments
8485
has_many :parents, through: :parent_relationships
8586
has_many :patient_specific_directions
86-
has_many :session_attendances, through: :patient_sessions
8787
has_many :sessions, through: :patient_sessions
8888
has_many :teams, -> { distinct }, through: :sessions
8989

app/models/patient_session.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class PatientSession < ApplicationRecord
4444
belongs_to :patient
4545
belongs_to :session
4646

47-
has_many :session_attendances, dependent: :destroy
4847
has_one :registration_status
4948

5049
has_one :location, through: :session
@@ -67,6 +66,10 @@ class PatientSession < ApplicationRecord
6766
-> { where(patient_id: it.patient_id) },
6867
through: :session
6968

69+
has_many :session_attendances,
70+
-> { where(patient_id: it.patient_id) },
71+
through: :session
72+
7073
has_many :session_notifications,
7174
-> { where(session_id: it.session_id) },
7275
through: :patient
@@ -315,9 +318,10 @@ def programmes = session.programmes_for(patient:, academic_year:)
315318

316319
def todays_attendance
317320
if (session_date = session.session_dates.today.first)
318-
session_attendances.includes(:session_date).find_or_initialize_by(
319-
session_date:
320-
)
321+
patient
322+
.session_attendances
323+
.includes(:patient, session_date: { session: :programmes })
324+
.find_or_initialize_by(session_date:)
321325
end
322326
end
323327

app/models/patient_session/registration_status.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,24 @@ class PatientSession::RegistrationStatus < ApplicationRecord
2121
belongs_to :patient_session
2222

2323
has_one :patient, through: :patient_session
24+
has_one :session, through: :patient_session
2425

2526
has_many :vaccination_records,
2627
-> { kept.order(performed_at: :desc) },
2728
through: :patient
2829

29-
has_many :session_attendances,
30-
-> { includes(:session_date) },
31-
through: :patient_session
30+
has_one :session_date, -> { today }, through: :session, source: :session_dates
31+
32+
has_many :session_attendances, through: :patient
3233

3334
enum :status,
3435
{ unknown: 0, attending: 1, not_attending: 2, completed: 3 },
3536
default: :unknown,
3637
validate: true
3738

38-
def session_attendance = session_attendances.find(&:today?)
39+
def session_attendance
40+
session_attendances.find { it.session_date_id == session_date.id }
41+
end
3942

4043
def assign_status
4144
self.status = generator.status

app/models/session.rb

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

6061
has_many :location_programme_year_groups,

app/models/session_attendance.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,30 @@
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) }

app/policies/session_attendance_policy.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@ def update?
1111

1212
private
1313

14-
delegate :patient_session, :session_date, to: :record
15-
16-
def academic_year = patient_session.session.academic_year
14+
delegate :patient, :session_date, to: :record
15+
delegate :session, to: :session_date
16+
delegate :academic_year, to: :session
1717

1818
def already_vaccinated?
19-
patient_session.programmes.all? do |programme|
20-
patient_session
21-
.patient
22-
.vaccination_status(programme:, academic_year:)
23-
.vaccinated?
24-
end
19+
session
20+
.programmes_for(patient:, academic_year:)
21+
.all? do |programme|
22+
patient.vaccination_status(programme:, academic_year:).vaccinated?
23+
end
2524
end
2625

2726
def was_seen_by_nurse?
2827
VaccinationRecord.kept.exists?(
29-
patient_id: patient_session.patient_id,
30-
session_id: patient_session.session_id,
28+
patient:,
29+
session:,
3130
performed_at: session_date.value.all_day
3231
)
3332
end

0 commit comments

Comments
 (0)