Skip to content

Commit 654507a

Browse files
authored
Merge pull request #3359 from nhsuk/remove-patient-organisation
Simplify patient-organisation relationship
2 parents 74b38e9 + 225198f commit 654507a

37 files changed

+191
-243
lines changed

app/components/app_patient_cohort_table_component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<% row.with_cell do %>
1919
<span class="nhsuk-table-responsive__heading">Actions</span>
2020
<%= form_with model: @patient, builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
21-
<%= f.hidden_field :organisation_id, value: "" %>
21+
<%= f.hidden_field :organisation_id, value: organisation.id %>
2222
<%= f.govuk_submit "Remove from cohort", class: "app-button--secondary-warning app-button--small" %>
2323
<% end %>
2424
<% end %>
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
# frozen_string_literal: true
22

33
class AppPatientCohortTableComponent < ViewComponent::Base
4-
def initialize(patient)
4+
def initialize(patient, current_user:)
55
super
66

77
@patient = patient
8+
@current_user = current_user
89
end
910

1011
private
1112

12-
attr_reader :patient
13+
attr_reader :patient, :current_user
1314

14-
delegate :organisation, :year_group, to: :patient
15+
delegate :year_group, to: :patient
16+
17+
def organisation
18+
@organisation ||=
19+
if current_user.selected_organisation.patients.include?(patient)
20+
current_user.selected_organisation
21+
end
22+
end
1523
end

app/controllers/api/organisations_controller.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def destroy
2424
log_destroy(SessionNotification.where(session: sessions))
2525
log_destroy(VaccinationRecord.where(session: sessions))
2626

27+
patient_ids = organisation.patients.pluck(:id)
28+
2729
patient_sessions = PatientSession.where(session: sessions)
2830
log_destroy(GillickAssessment.where(patient_session: patient_sessions))
2931
log_destroy(PreScreening.where(patient_session: patient_sessions))
@@ -32,22 +34,25 @@ def destroy
3234
log_destroy(SessionDate.where(session: sessions))
3335
log_destroy(sessions)
3436

35-
patients = organisation.patients
37+
organisation.patients
3638

37-
log_destroy(SchoolMove.where(patient: patients))
39+
log_destroy(SchoolMove.where(patient_id: patient_ids))
3840
log_destroy(SchoolMove.where(organisation:))
39-
log_destroy(SchoolMoveLogEntry.where(patient: patients))
40-
log_destroy(AccessLogEntry.where(patient: patients))
41-
log_destroy(NotifyLogEntry.where(patient: patients))
41+
log_destroy(SchoolMoveLogEntry.where(patient_id: patient_ids))
42+
log_destroy(AccessLogEntry.where(patient_id: patient_ids))
43+
log_destroy(NotifyLogEntry.where(patient_id: patient_ids))
4244
# In local dev we can end up with NotifyLogEntries without a patient
4345
log_destroy(NotifyLogEntry.where(patient_id: nil))
44-
log_destroy(VaccinationRecord.where(patient: patients))
46+
log_destroy(VaccinationRecord.where(patient_id: patient_ids))
4547

4648
log_destroy(ConsentForm.where(organisation:))
4749
log_destroy(Consent.where(organisation:))
4850
log_destroy(Triage.where(organisation:))
4951

50-
patients.includes(:parents).in_batches { log_destroy(it) }
52+
Patient
53+
.where(id: patient_ids)
54+
.includes(:parents)
55+
.in_batches { log_destroy(it) }
5156

5257
batches = Batch.where(organisation:)
5358
log_destroy(VaccinationRecord.where(batch: batches))

app/controllers/cohorts_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def index
1111
birth_academic_years = @programme.birth_academic_years
1212

1313
@patient_count_by_birth_academic_year =
14-
patients_in_cohort
14+
patients_in_organisation
1515
.where(birth_academic_year: birth_academic_years)
1616
.group(:birth_academic_year)
1717
.count
@@ -24,7 +24,7 @@ def show
2424
@birth_academic_year = Integer(params[:id])
2525

2626
patients =
27-
patients_in_cohort
27+
patients_in_organisation
2828
.where(birth_academic_year: @birth_academic_year)
2929
.not_deceased
3030
.includes(:school)
@@ -39,7 +39,7 @@ def set_programme
3939
@programme = policy_scope(Programme).find_by!(type: params[:programme_type])
4040
end
4141

42-
def patients_in_cohort
43-
Patient.where(organisation: current_user.selected_organisation)
42+
def patients_in_organisation
43+
current_user.selected_organisation.patients
4444
end
4545
end

app/controllers/patients/edit_controller.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ def set_patient
4343
def existing_patient
4444
@existing_patient ||=
4545
if nhs_number.present?
46-
policy_scope(Patient)
47-
.or(Patient.where(organisation: nil))
48-
.includes(parent_relationships: :parent)
49-
.find_by(nhs_number:)
46+
policy_scope(Patient).includes(parent_relationships: :parent).find_by(
47+
nhs_number:
48+
) ||
49+
Patient
50+
.where
51+
.missing(:patient_sessions)
52+
.includes(parent_relationships: :parent)
53+
.find_by(nhs_number:)
5054
end
5155
end
5256

app/controllers/patients_controller.rb

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,14 @@ def edit
3232
end
3333

3434
def update
35-
old_organisation = @patient.organisation
36-
3735
organisation_id = params.dig(:patient, :organisation_id).presence
3836

3937
ActiveRecord::Base.transaction do
40-
@patient.update!(organisation_id:)
41-
42-
if organisation_id.nil?
43-
@patient
44-
.patient_sessions
45-
.where(session: old_organisation.sessions)
46-
.destroy_all_if_safe
47-
end
38+
@patient
39+
.patient_sessions
40+
.joins(:session)
41+
.where(session: { organisation_id: })
42+
.destroy_all_if_safe
4843
end
4944

5045
path =
@@ -68,7 +63,6 @@ def set_patient
6863
@patient =
6964
policy_scope(Patient).includes(
7065
:gp_practice,
71-
:organisation,
7266
:school,
7367
consents: %i[parent patient],
7468
parent_relationships: :parent,

app/lib/patient_merger.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,6 @@ def call
9595
patient_to_destroy.cohort_imports.clear
9696
patient_to_destroy.immunisation_imports.clear
9797

98-
# Add patient back to the cohort if the patient to destroy was in the cohort.
99-
if patient_to_keep.organisation_id.nil?
100-
patient_to_keep.update!(
101-
organisation_id: patient_to_destroy.organisation_id
102-
)
103-
end
104-
10598
patient_to_destroy.reload.destroy!
10699

107100
StatusUpdater.call(patient: patient_to_keep)

app/lib/reports/school_moves_exporter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def school_move_log_entries
5050
begin
5151
historical_patients =
5252
Patient
53-
.where.not(organisation:)
53+
.where.not(id: organisation.patients.select(:id))
5454
.where(
5555
SchoolMoveLogEntry
5656
.where("patient_id = patients.id")

app/models/organisation.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ class Organisation < ApplicationRecord
3939
has_many :locations
4040
has_many :organisation_programmes,
4141
-> { joins(:programme).order(:"programmes.type") }
42-
has_many :patients
4342
has_many :sessions
4443
has_many :teams
4544

4645
has_many :community_clinics, through: :teams
4746
has_many :locations, through: :teams
4847
has_many :patient_sessions, through: :sessions
48+
has_many :patients, through: :patient_sessions
4949
has_many :programmes, through: :organisation_programmes
5050
has_many :schools, through: :teams
51-
has_many :vaccines, through: :programmes
5251
has_many :vaccination_records, through: :sessions
52+
has_many :vaccines, through: :programmes
5353

5454
has_and_belongs_to_many :users
5555

app/models/patient.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
# created_at :datetime not null
2929
# updated_at :datetime not null
3030
# gp_practice_id :bigint
31-
# organisation_id :bigint
3231
# school_id :bigint
3332
#
3433
# Indexes
@@ -39,13 +38,11 @@
3938
# index_patients_on_names_family_first (family_name,given_name)
4039
# index_patients_on_names_given_first (given_name,family_name)
4140
# index_patients_on_nhs_number (nhs_number) UNIQUE
42-
# index_patients_on_organisation_id (organisation_id)
4341
# index_patients_on_school_id (school_id)
4442
#
4543
# Foreign Keys
4644
#
4745
# fk_rails_... (gp_practice_id => locations.id)
48-
# fk_rails_... (organisation_id => organisations.id)
4946
# fk_rails_... (school_id => locations.id)
5047
#
5148
class Patient < ApplicationRecord
@@ -56,11 +53,10 @@ class Patient < ApplicationRecord
5653
include PendingChangesConcern
5754
include Schoolable
5855

59-
audited associated_with: :organisation
56+
audited
6057
has_associated_audits
6158

6259
belongs_to :gp_practice, class_name: "Location", optional: true
63-
belongs_to :organisation, optional: true
6460

6561
has_many :access_log_entries
6662
has_many :consent_notifications
@@ -77,11 +73,12 @@ class Patient < ApplicationRecord
7773
has_many :vaccination_records, -> { kept }
7874
has_many :vaccination_statuses
7975

80-
has_many :parents, through: :parent_relationships
8176
has_many :gillick_assessments, through: :patient_sessions
77+
has_many :parents, through: :parent_relationships
8278
has_many :pre_screenings, through: :patient_sessions
8379
has_many :session_attendances, through: :patient_sessions
8480
has_many :sessions, through: :patient_sessions
81+
has_many :organisations, through: :sessions
8582

8683
has_many :sessions_for_current_academic_year,
8784
-> { for_current_academic_year },
@@ -354,6 +351,8 @@ def invalidate!
354351
update_column(:invalidated_at, Time.current)
355352
end
356353

354+
def not_in_organisation? = patient_sessions.empty?
355+
357356
def dup_for_pending_changes
358357
dup.tap do |new_patient|
359358
new_patient.nhs_number = nil
@@ -385,7 +384,6 @@ def self.from_consent_form(consent_form)
385384
given_name: consent_form.given_name,
386385
home_educated: consent_form.home_educated,
387386
nhs_number: consent_form.nhs_number,
388-
organisation: consent_form.organisation,
389387
preferred_family_name: consent_form.preferred_family_name,
390388
preferred_given_name: consent_form.preferred_given_name,
391389
school: consent_form.school

0 commit comments

Comments
 (0)