Skip to content

Commit 50b4f07

Browse files
committed
Remove session association from AttendanceRecord
This replaces the session association with one to the location instead, as we want registration status to apply across all sessions for a particular location, in preparation for the removal of the `PatientSession` model. Jira-Issue: MAV-1938
1 parent 9d4bbc3 commit 50b4f07

19 files changed

+151
-104
lines changed

app/components/app_patient_session_search_result_card_component.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@ def can_register_attendance?
9898
attendance_record =
9999
AttendanceRecord.new(
100100
patient:,
101-
session_date: SessionDate.new(session:, value: Date.current)
101+
location: session.location,
102+
date: Date.current
102103
)
103104

105+
attendance_record.session = session
106+
104107
policy(attendance_record).new?
105108
end
106109

app/controllers/patient_sessions/attendances_controller.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@ def update
4242
private
4343

4444
def set_attendance_record
45-
@attendance_record =
46-
authorize(
47-
@patient
48-
.attendance_records
49-
.includes(:patient, session_date: { session: :programmes })
50-
.find_or_initialize_by(session_date: @session_date)
45+
attendance_record =
46+
@patient.attendance_records.find_or_initialize_by(
47+
location: @session.location,
48+
date: @session_date.value
5149
)
50+
51+
attendance_record.session = @session
52+
53+
@attendance_record = authorize attendance_record
5254
end
5355

5456
def attendance_record_params
5557
params
5658
.expect(attendance_record: :attending)
57-
.tap { |p| p[:attending] = nil if p[:attending] == "not_registered" }
59+
.tap { it[:attending] = nil if it[:attending] == "not_registered" }
5860
end
5961
end

app/controllers/sessions/register_controller.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@ def show
2626

2727
def create
2828
attendance_record =
29-
ActiveRecord::Base.transaction do
30-
record =
31-
authorize @patient
32-
.attendance_records
33-
.includes(:session_date)
34-
.find_or_initialize_by(session_date: @session_date)
35-
record.update!(attending: params[:status] == "present")
36-
StatusUpdater.call(patient: @patient)
37-
record
38-
end
29+
@patient.attendance_records.find_or_initialize_by(
30+
location: @session.location,
31+
date: @session_date.value
32+
)
33+
34+
attendance_record.session = @session
35+
36+
authorize attendance_record
37+
38+
ActiveRecord::Base.transaction do
39+
attendance_record.update!(attending: params[:status] == "present")
40+
StatusUpdater.call(patient: @patient)
41+
end
3942

4043
name = @patient.full_name
4144

app/lib/generate/vaccination_records.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ def create_vaccinations
2626
patient = patient_session.patient
2727
session = patient_session.session
2828

29-
unless AttendanceRecord.joins(:session_date).exists?(
30-
patient:,
31-
session_date: {
32-
session:
33-
}
34-
)
29+
unless AttendanceRecord.exists?(patient:, location: session.location)
3530
attendance_records << FactoryBot.build(
3631
:attendance_record,
3732
:present,

app/lib/status_updater.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ def update_registration_statuses!
6363
.includes(
6464
:patient,
6565
:attendance_records,
66-
:session_date,
6766
:vaccination_records,
6867
session: :programmes
6968
)

app/models/attendance_record.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,35 @@
44
#
55
# Table name: attendance_records
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_id :bigint not null
12-
# session_date_id :bigint not null
7+
# id :bigint not null, primary key
8+
# attending :boolean not null
9+
# date :date not null
10+
# created_at :datetime not null
11+
# updated_at :datetime not null
12+
# location_id :bigint not null
13+
# patient_id :bigint not null
1314
#
1415
# Indexes
1516
#
16-
# index_attendance_records_on_patient_id (patient_id)
17-
# index_attendance_records_on_patient_id_and_session_date_id (patient_id,session_date_id) UNIQUE
18-
# index_attendance_records_on_session_date_id (session_date_id)
17+
# idx_on_patient_id_location_id_date_e5912f40c4 (patient_id,location_id,date) UNIQUE
18+
# index_attendance_records_on_location_id (location_id)
19+
# index_attendance_records_on_patient_id (patient_id)
1920
#
2021
# Foreign Keys
2122
#
23+
# fk_rails_... (location_id => locations.id)
2224
# fk_rails_... (patient_id => patients.id)
23-
# fk_rails_... (session_date_id => session_dates.id)
2425
#
2526
class AttendanceRecord < ApplicationRecord
2627
audited associated_with: :patient
2728

2829
belongs_to :patient
29-
belongs_to :session_date
30+
belongs_to :location
3031

31-
has_one :session, through: :session_date
32-
has_one :location, through: :session
32+
scope :today, -> { where(date: Date.current) }
3333

34-
scope :today, -> { joins(:session_date).merge(SessionDate.today) }
34+
delegate :today?, to: :date
3535

36-
delegate :today?, to: :session_date
36+
# This is needed to be able to pass a session to the policy.
37+
attr_accessor :session
3738
end

app/models/location.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class Location < ApplicationRecord
5353
primary_key: :gias_code,
5454
optional: true
5555

56+
has_many :attendance_records
5657
has_many :consent_forms
5758
has_many :location_programme_year_groups
5859
has_many :patients, foreign_key: :school_id

app/models/patient/registration_status.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class Patient::RegistrationStatus < ApplicationRecord
2929
-> { kept.order(performed_at: :desc) },
3030
through: :patient
3131

32-
has_one :session_date, -> { today }, through: :session, source: :session_dates
33-
3432
has_many :attendance_records, through: :patient
3533

3634
enum :status,
@@ -39,7 +37,9 @@ class Patient::RegistrationStatus < ApplicationRecord
3937
validate: true
4038

4139
def attendance_record
42-
attendance_records.find { it.session_date_id == session_date&.id }
40+
attendance_records.find do
41+
it.location_id == session.location_id && it.today?
42+
end
4343
end
4444

4545
def assign_status

app/models/patient_session.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class PatientSession < ApplicationRecord
7272

7373
has_many :attendance_records,
7474
-> { where(patient_id: it.patient_id) },
75-
through: :session
75+
through: :location
7676

7777
has_many :session_notifications,
7878
-> { where(session_id: it.session_id) },

app/models/session.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ 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 :attendance_records, through: :session_dates
5958
has_many :vaccines, through: :programmes
6059

6160
has_many :location_programme_year_groups,

0 commit comments

Comments
 (0)