Skip to content

Commit fc20fe8

Browse files
committed
Replace pre-screening patient session foreign key
This replaces the foreign key association between pre-screenings and patient sessions to instead link directly to the patient and the session (via the date). 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, actually slightly improved as pre-screening is only supposed to exist on the day it happened, so now we link directly to the session date rather than the overall session. Jira-Issue: MAV-1819
1 parent fe65298 commit fc20fe8

22 files changed

+153
-58
lines changed

app/components/app_patient_session_record_component.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def render?
2222
(
2323
patient_session.registration_status&.attending? ||
2424
patient_session.registration_status&.completed? ||
25-
!patient_session.session.requires_registration?
25+
!session.requires_registration?
2626
)
2727
end
2828

@@ -35,8 +35,14 @@ def render?
3535

3636
def default_vaccinate_form
3737
pre_screening_confirmed = patient.pre_screenings.today.exists?(programme:)
38+
session_date = session.session_dates.today.first
3839

39-
VaccinateForm.new(patient_session:, programme:, pre_screening_confirmed:)
40+
VaccinateForm.new(
41+
patient:,
42+
session_date:,
43+
programme:,
44+
pre_screening_confirmed:
45+
)
4046
end
4147

4248
def heading

app/components/app_vaccinate_form_component.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ def initialize(vaccinate_form)
1111

1212
attr_reader :vaccinate_form
1313

14-
delegate :patient_session, :programme, to: :vaccinate_form
15-
delegate :patient, :session, to: :patient_session
14+
delegate :patient, :session, :programme, to: :vaccinate_form
1615
delegate :academic_year, to: :session
1716

1817
def url

app/controllers/api/testing/teams_controller.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ def destroy
2727

2828
patient_ids = team.patients.pluck(:id)
2929

30-
patient_sessions = PatientSession.where(session: sessions)
31-
log_destroy(PreScreening.where(patient_session: patient_sessions))
32-
patient_sessions.in_batches { log_destroy(it) }
33-
34-
log_destroy(SessionDate.where(session: sessions))
30+
log_destroy(PatientSession.where(session: sessions))
3531

3632
log_destroy(AccessLogEntry.where(patient_id: patient_ids))
3733
log_destroy(ArchiveReason.where(patient_id: patient_ids))
@@ -41,11 +37,14 @@ def destroy
4137
# In local dev we can end up with NotifyLogEntries without a patient
4238
log_destroy(NotifyLogEntry.where(patient_id: nil))
4339
log_destroy(NotifyLogEntry.where(patient_id: patient_ids))
40+
log_destroy(PreScreening.where(patient_id: patient_ids))
4441
log_destroy(SchoolMove.where(patient_id: patient_ids))
4542
log_destroy(SchoolMove.where(team:))
4643
log_destroy(SchoolMoveLogEntry.where(patient_id: patient_ids))
4744
log_destroy(VaccinationRecord.where(patient_id: patient_ids))
4845

46+
log_destroy(SessionDate.where(session: sessions))
47+
4948
log_destroy(ConsentForm.where(team:))
5049
log_destroy(Consent.where(team:))
5150
log_destroy(Triage.where(team:))

app/controllers/patient_sessions/base_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def set_session
1919
)
2020
end
2121

22+
def set_session_date
23+
@session_date = @session.session_dates.find_by!(value: Date.current)
24+
end
25+
2226
def set_academic_year
2327
@academic_year = @session.academic_year
2428
end

app/controllers/patient_sessions/session_attendances_controller.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ def update
4141

4242
private
4343

44-
def set_session_date
45-
@session_date = @session.session_dates.find_by!(value: Date.current)
46-
end
47-
4844
def set_session_attendance
4945
@session_attendance =
5046
authorize @patient_session

app/controllers/patient_sessions/vaccinations_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class PatientSessions::VaccinationsController < PatientSessions::BaseController
55
include VaccinationMailerConcern
66

77
before_action :set_todays_batch
8+
before_action :set_session_date
89

910
after_action :verify_authorized
1011

@@ -17,7 +18,8 @@ def create
1718
@vaccinate_form =
1819
VaccinateForm.new(
1920
current_user:,
20-
patient_session: @patient_session,
21+
patient: @patient,
22+
session_date: @session_date,
2123
programme: @programme,
2224
todays_batch: @todays_batch,
2325
**vaccinate_form_params

app/forms/vaccinate_form.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ class VaccinateForm
44
include ActiveModel::Model
55
include ActiveModel::Attributes
66

7-
attr_accessor :patient_session, :programme, :current_user, :todays_batch
7+
attr_accessor :patient,
8+
:session_date,
9+
:programme,
10+
:current_user,
11+
:todays_batch
12+
13+
delegate :session, to: :session_date
814

915
attribute :identity_check_confirmed_by_other_name, :string
1016
attribute :identity_check_confirmed_by_other_relationship, :string
@@ -90,19 +96,19 @@ def save(draft_vaccination_record:)
9096
identity_check_confirmed_by_patient
9197
draft_vaccination_record.location_id =
9298
session.location_id unless session.generic_clinic?
93-
draft_vaccination_record.patient_id = patient_session.patient_id
99+
draft_vaccination_record.patient_id = patient.id
94100
draft_vaccination_record.performed_at = Time.current
95101
draft_vaccination_record.performed_by_user = current_user
96102
draft_vaccination_record.performed_ods_code = organisation.ods_code
97103
draft_vaccination_record.programme = programme
98-
draft_vaccination_record.session_id = patient_session.session_id
104+
draft_vaccination_record.session_id = session.id
99105

100106
draft_vaccination_record.save # rubocop:disable Rails/SaveBang
101107
end
102108

103109
private
104110

105-
delegate :organisation, :session, to: :patient_session
111+
delegate :organisation, to: :session
106112

107113
def administered? = vaccine_method != "none"
108114

@@ -131,10 +137,11 @@ def delivery_method
131137

132138
def pre_screening
133139
@pre_screening ||=
134-
patient_session.pre_screenings.build(
140+
patient.pre_screenings.build(
135141
notes: pre_screening_notes,
136142
performed_by: current_user,
137-
programme:
143+
programme:,
144+
session_date:
138145
)
139146
end
140147
end

app/lib/patient_merger.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def call
4040
patient_id: patient_to_keep.id
4141
)
4242

43+
patient_to_destroy.pre_screenings.update_all(
44+
patient_id: patient_to_keep.id
45+
)
46+
4347
patient_to_destroy.school_moves.find_each do |school_move|
4448
if patient_to_keep.school_moves.exists?(
4549
home_educated: school_move.home_educated,
@@ -78,18 +82,12 @@ def call
7882
end
7983

8084
patient_to_destroy.patient_sessions.each do |patient_session|
81-
if (
82-
existing_patient_session =
83-
patient_to_keep.patient_sessions.find_by(
84-
session_id: patient_session.session_id
85-
)
85+
if patient_to_keep.patient_sessions.exists?(
86+
session_id: patient_session.session_id
8687
)
87-
patient_session.pre_screenings.update_all(
88-
patient_session_id: existing_patient_session.id
89-
)
90-
else
91-
patient_session.update!(patient: patient_to_keep)
88+
next
9289
end
90+
patient_session.update!(patient: patient_to_keep)
9391
end
9492

9593
PatientSession.where(patient: patient_to_destroy).destroy_all

app/models/patient.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class Patient < ApplicationRecord
7070
has_many :parent_relationships, -> { order(:created_at) }
7171
has_many :patient_sessions
7272
has_many :pds_search_results
73+
has_many :pre_screenings
7374
has_many :school_move_log_entries
7475
has_many :school_moves
7576
has_many :session_notifications
@@ -81,7 +82,6 @@ class Patient < ApplicationRecord
8182
has_many :gillick_assessments
8283
has_many :parents, through: :parent_relationships
8384
has_many :patient_specific_directions
84-
has_many :pre_screenings, through: :patient_sessions
8585
has_many :session_attendances, through: :patient_sessions
8686
has_many :sessions, through: :patient_sessions
8787
has_many :teams, -> { distinct }, through: :sessions

app/models/patient_session.rb

Lines changed: 4 additions & 1 deletion
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 :pre_screenings
4847
has_many :session_attendances, dependent: :destroy
4948
has_one :registration_status
5049

@@ -64,6 +63,10 @@ class PatientSession < ApplicationRecord
6463
through: :patient,
6564
source: :notes
6665

66+
has_many :pre_screenings,
67+
-> { where(patient_id: it.patient_id) },
68+
through: :session
69+
6770
has_many :session_notifications,
6871
-> { where(session_id: it.session_id) },
6972
through: :patient

0 commit comments

Comments
 (0)