Skip to content

Commit fad2ee4

Browse files
committed
Create notices when patient moves teams
When a patient is mvoed between teams we need to ensure important notices are created for the new team to see too. MAV-2309
1 parent 24cd61b commit fad2ee4

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

app/models/concerns/contributes_to_patient_teams.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,19 @@ def insert_patient_teams_relationships
222222
end
223223

224224
def sync_patient_teams_table_on_patient_ids(pk_ids)
225+
affected_patient_ids = []
225226
transaction do
226227
contributing_subqueries.each do |key, subquery|
227228
patient_id_source =
228229
connection.quote_string(subquery[:patient_id_source])
229230
sterile_key = connection.quote(PatientTeam.sources.fetch(key.to_s))
231+
232+
affected_patient_ids |=
233+
select("#{subquery[:patient_id_source]} as patient_id")
234+
.where("#{table_name}.id = ANY(ARRAY[?]::bigint[])", pk_ids)
235+
.distinct
236+
.pluck(:patient_id)
237+
230238
patient_relationships_to_remove =
231239
select("#{patient_id_source} as patient_id")
232240
.where("#{table_name}.id = ANY(ARRAY[?]::bigint[])", pk_ids)
@@ -247,6 +255,10 @@ def sync_patient_teams_table_on_patient_ids(pk_ids)
247255

248256
PatientTeam.missing_sources.delete_all
249257
end
258+
259+
if affected_patient_ids.any?
260+
ImportantNoticeGeneratorJob.perform_later(affected_patient_ids)
261+
end
250262
end
251263

252264
def add_patient_team_relationships

app/models/patient_team.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class PatientTeam < ApplicationRecord
3939
school_move_school: 5
4040
}
4141

42+
after_create_commit :generate_important_notices
43+
4244
def add_source!(source)
4345
update!(sources: Array(sources) | [source.to_s])
4446
end
@@ -47,4 +49,10 @@ def remove_source!(source)
4749
self.sources = sources.reject { it == source.to_s }
4850
sources.empty? ? delete : save!
4951
end
52+
53+
private
54+
55+
def generate_important_notices
56+
ImportantNoticeGeneratorJob.perform_later([patient_id])
57+
end
5058
end

spec/factories/vaccination_records.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@
121121

122122
notify_parents { true }
123123

124+
after(:create) do |vaccination_record|
125+
ImportantNoticeGeneratorJob.perform_now([vaccination_record.patient_id])
126+
end
127+
124128
trait :sourced_from_nhs_immunisations_api do
125129
source { "nhs_immunisations_api" }
126130
nhs_immunisations_api_id { SecureRandom.uuid }

spec/features/manage_children_spec.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@
174174
then_i_see_the_notice_of_sensitive
175175
end
176176

177+
scenario "Important notices for patient added to new team" do
178+
given_another_team_exists
179+
and_a_patient_with_all_notices_exists
180+
181+
when_the_patient_is_added_to_the_new_team
182+
when_i_go_to_the_imports_page_as_a_superuser_in_the_new_team
183+
and_i_click_on_notices
184+
then_i_see_all_patient_notices
185+
and_i_do_not_see_gillick_no_notify_notices
186+
end
187+
177188
def given_my_team_exists
178189
@hpv = Programme.hpv
179190
@flu = Programme.flu
@@ -188,6 +199,18 @@ def given_my_team_exists
188199
TeamSessionsFactory.call(@team, academic_year: AcademicYear.current)
189200
end
190201

202+
def given_another_team_exists
203+
@new_team =
204+
create(
205+
:team,
206+
:with_generic_clinic,
207+
:with_one_nurse,
208+
programmes: [@hpv, @flu]
209+
)
210+
211+
TeamSessionsFactory.call(@new_team, academic_year: AcademicYear.current)
212+
end
213+
191214
def given_patients_exist
192215
school = create(:school, team: @team)
193216

@@ -288,6 +311,20 @@ def when_a_restricted_patient_exists
288311
@restricted_patient = create(:patient, :restricted, session:)
289312
end
290313

314+
def and_a_patient_with_all_notices_exists
315+
session = create(:session, team: @team, programmes: [@hpv])
316+
@patient_all_notices =
317+
create(:patient, :deceased, :invalidated, :restricted, session:)
318+
create(
319+
:vaccination_record,
320+
notify_parents: false,
321+
patient: @patient_all_notices,
322+
programme: @hpv,
323+
session:
324+
)
325+
expect(@patient_all_notices.important_notices.count).to eq(4)
326+
end
327+
291328
def when_i_click_on_children
292329
sign_in @team.users.first
293330

@@ -462,6 +499,12 @@ def when_i_go_to_the_imports_page_as_a_superuser
462499
visit "/imports"
463500
end
464501

502+
def when_i_go_to_the_imports_page_as_a_superuser_in_the_new_team
503+
sign_in @new_team.users.first, superuser: true
504+
505+
visit "/imports"
506+
end
507+
465508
def when_i_wait_for_the_sync_to_complete
466509
Sidekiq::Job.drain_all
467510
end
@@ -484,6 +527,21 @@ def when_i_click_on_notices
484527

485528
alias_method :and_i_click_on_notices, :when_i_click_on_notices
486529

530+
def when_the_patient_is_added_to_the_new_team
531+
SchoolMove.new(
532+
academic_year: AcademicYear.current,
533+
home_educated: false,
534+
patient: @patient_all_notices,
535+
team: @new_team
536+
).confirm!
537+
538+
expect(@patient_all_notices.teams).to include(@new_team)
539+
expect(@patient_all_notices.teams).to include(@team)
540+
541+
perform_enqueued_jobs_while_exists(only: SyncPatientTeamJob)
542+
perform_enqueued_jobs_while_exists(only: ImportantNoticeGeneratorJob)
543+
end
544+
487545
def then_i_see_no_notices
488546
expect(page).to have_content("There are currently no important notices.")
489547
end
@@ -506,6 +564,22 @@ def then_i_see_the_notice_of_sensitive
506564
expect(page).to have_content("Record flagged as sensitive")
507565
end
508566

567+
def then_i_see_all_patient_notices
568+
expect(page).to have_content("Important notices (3)")
569+
expect(page).to have_content(@patient_all_notices.full_name).exactly(
570+
3
571+
).times
572+
expect(page).to have_content("Record updated with child’s date of death")
573+
expect(page).to have_content("Record flagged as invalid")
574+
expect(page).to have_content("Record flagged as sensitive")
575+
end
576+
577+
def and_i_do_not_see_gillick_no_notify_notices
578+
expect(page).not_to have_content(
579+
"does not want their parents to be notified"
580+
)
581+
end
582+
509583
def then_the_vaccination_record_is_created_with_the_nhs
510584
expect(@stubbed_post_request).to have_been_requested
511585
end

0 commit comments

Comments
 (0)