Skip to content

Commit 6e2c11a

Browse files
authored
Merge pull request #6010 from nhsuk/dismiss-important-notice-when-patient-moves-back-into-team-nil-school
Fix bugs with school moves across teams
2 parents 4dd42fb + 1796c0d commit 6e2c11a

22 files changed

Lines changed: 1019 additions & 235 deletions

app/components/app_import_review_school_moves_summary_component.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@
3737
<%= destination_school_name(changeset) %>
3838
</span>
3939
<% if school_move_across_teams?(changeset) %>
40+
<% source_team = changeset.patient.teams_via_patient_locations.first %>
4041
<div class="nhsuk-u-margin-top-1">
4142
<%= render(
4243
AppStatusComponent.new(
43-
text: "This child is moving in from #{changeset.patient.school.teams.first.name}'s area",
44+
text: "This child is moving in from #{source_team.name}'s area",
4445
small: true,
4546
classes: "nhsuk-u-margin-top-1 nhsuk-u-margin-bottom-0",
4647
)

app/components/app_import_review_school_moves_summary_component.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ def destination_school(changeset)
3030
end
3131

3232
def school_move_across_teams?(changeset)
33-
patient = changeset.patient
3433
dest_school = destination_school(changeset)
3534

36-
dest_school && patient.school && patient.school.teams.any? &&
37-
(dest_school.teams & patient.school.teams).empty?
35+
current_teams = changeset.patient.teams_via_patient_locations
36+
return false if current_teams.empty?
37+
38+
new_teams = dest_school&.teams || [changeset.import.team]
39+
40+
(new_teams & current_teams).empty?
3841
end
3942
end

app/components/app_patient_card_component.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,13 @@ def gillick_no_notify_notices
9292
end
9393

9494
def team_changed_notices
95-
return unless patient.school
96-
9795
valid_notices =
9896
patient
9997
.important_notices
10098
.team_changed
10199
.includes(:school_move_log_entry)
102100
.where(team: current_team)
103-
.where.not(team: patient.school.teams)
101+
.where.not(team: patient.teams_via_patient_locations)
104102

105103
valid_notices.map(&:message)
106104
end

app/jobs/important_notice_generator_job.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,13 @@ def collect_notices_for_patient(
107107

108108
team_changed_notices = patient.important_notices.team_changed
109109
if team_changed_notices.any?
110-
notice_ids_to_dismiss.concat(
111-
team_changed_notices.where(team: patient.school&.teams).ids
112-
)
110+
current_teams = patient.teams_via_patient_locations
111+
112+
if current_teams.any?
113+
notice_ids_to_dismiss.concat(
114+
team_changed_notices.where(team_id: current_teams).ids
115+
)
116+
end
113117
end
114118

115119
unless patient.invalidated?

app/models/concerns/patient_import_concern.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def increment_column_counts!(import, counts, changesets)
128128
end
129129

130130
def has_auto_confirmable_school_move?(school_move, import)
131+
return false if school_move.from_another_team?
132+
131133
patient = school_move.patient
132134
team = import.team
133135

app/models/patient.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,18 @@ def not_in_team?(team:, academic_year:)
710710
end
711711
end
712712

713+
def teams_via_patient_locations
714+
Team
715+
.joins(team_locations: { location: :patient_locations })
716+
.merge(
717+
PatientLocation.active.where(
718+
patient_id: id,
719+
academic_year: AcademicYear.current
720+
)
721+
)
722+
.distinct
723+
end
724+
713725
def dup_for_pending_changes
714726
dup.tap do |new_patient|
715727
new_patient.nhs_number = nil

app/models/patient_location.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class PatientLocation < ApplicationRecord
3737
scope :current, -> { where(academic_year: AcademicYear.current) }
3838
scope :pending, -> { where(academic_year: AcademicYear.pending) }
3939

40+
scope :active, -> { where("date_range @> ?::date", Date.current) }
41+
4042
scope :joins_team_locations, -> { references(:teams_locations).joins(<<-SQL) }
4143
INNER JOIN team_locations
4244
ON team_locations.location_id = patient_locations.location_id

app/models/school_move.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ def confirm!(user: nil)
8787
update_locations!
8888

8989
log_entry = create_log_entry!(user:)
90-
create_important_notice!(log_entry) if move_across_teams
9190

9291
update_patient_teams!
9392
update_patient_statuses!
93+
if move_across_teams
94+
create_important_notice!(log_entry)
95+
update_important_notices!
96+
end
9497

9598
destroy! if persisted?
9699
end
@@ -101,12 +104,7 @@ def ignore!
101104
end
102105

103106
def from_another_team?
104-
current_teams =
105-
patient
106-
.patient_teams
107-
.includes(:team)
108-
.select { it.sources.include?("patient_location") }
109-
.map(&:team)
107+
current_teams = patient.teams_via_patient_locations
110108

111109
return false if current_teams.empty?
112110

@@ -231,4 +229,8 @@ def update_patient_teams!
231229
def update_patient_statuses!
232230
PatientStatusUpdater.call(patient:)
233231
end
232+
233+
def update_important_notices!
234+
ImportantNoticeGeneratorJob.perform_later([patient.id])
235+
end
234236
end

app/views/school_moves/show.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
<% card.with_heading(level: 2) { "#{@school_move.human_enum_name(:source)} record" } %>
2020
<% if @school_move.from_another_team? %>
2121
<div class="nhsuk-u-margin-top-1">
22+
<% source_team = @school_move.patient.teams_via_patient_locations.first %>
2223
<%= render AppStatusComponent.new(text: "Confirming this school move will bring #{@school_move.patient.given_name} " \
23-
"into your area from #{@school_move.patient.school.teams.first.name}") %>
24+
"into your area from #{source_team.name}") %>
2425
</div>
2526
<% end %>
2627
<%= render AppChildSummaryComponent.new(@patient_with_changes) %>

spec/components/app_import_review_component_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@
124124
]
125125
end
126126

127+
before do
128+
create(
129+
:patient_location,
130+
patient: inter_team_patient,
131+
location: other_location
132+
)
133+
end
134+
127135
include_examples "section with details",
128136
title:
129137
"Children moving from another SAIS team's area - resolve after import",
@@ -155,6 +163,14 @@
155163
]
156164
end
157165

166+
before do
167+
create(
168+
:patient_location,
169+
patient: inter_team_patient,
170+
location: other_location
171+
)
172+
end
173+
158174
it "renders separate expander for close matches" do
159175
expect(rendered).to have_css(
160176
".nhsuk-details__summary-text",

0 commit comments

Comments
 (0)