Skip to content

Commit 3262a7c

Browse files
committed
Handle merging patients with PatientProgrammeVaccinationsSearch
This ensures that when merging two patients together, if the one to be destroyed has a PatientProgrammeVaccinationsSearch record, this is moved across to the other patient rather than preventing the merge from happening due to a foreign key constraint. We only keep the most recently searched record when duplicates exist for the same programme type.
1 parent 3fef3b3 commit 3262a7c

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

app/lib/patient_merger.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ def call
112112

113113
PatientLocation.where(patient: patient_to_destroy).destroy_all
114114

115+
patient_to_destroy
116+
.patient_programme_vaccinations_searches
117+
.find_each do |search|
118+
existing_search =
119+
patient_to_keep.patient_programme_vaccinations_searches.find_by(
120+
programme_type: search.programme_type
121+
)
122+
123+
if existing_search
124+
if existing_search.last_searched_at > search.last_searched_at
125+
search.last_searched_at = existing_search.last_searched_at
126+
end
127+
existing_search.destroy!
128+
end
129+
search.update!(patient_id: patient_to_keep.id)
130+
end
131+
115132
patient_to_destroy.changesets.update_all(patient_id: nil)
116133

117134
patient_to_destroy.class_imports.each do |import|

spec/lib/patient_merger_spec.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@
7676
patient: patient_to_destroy
7777
)
7878
end
79+
let(:patient_programme_vaccinations_search) do
80+
create(
81+
:patient_programme_vaccinations_search,
82+
patient: patient_to_destroy,
83+
programme: programme
84+
)
85+
end
7986
let(:pre_screening) { create(:pre_screening, patient: patient_to_destroy) }
8087
let(:school_move) do
8188
create(:school_move, :to_school, patient: patient_to_destroy)
@@ -185,6 +192,12 @@
185192
)
186193
end
187194

195+
it "moves patient programme vaccinations searches" do
196+
expect { call }.to change {
197+
patient_programme_vaccinations_search.reload.patient
198+
}.to(patient_to_keep)
199+
end
200+
188201
it "moves patient specific directions" do
189202
expect { call }.to change {
190203
patient_specific_direction.reload.patient
@@ -377,6 +390,78 @@
377390
end
378391
end
379392

393+
describe "vaccination searches" do
394+
context "when patients have overlapping and unique programme searches" do
395+
before do
396+
create(
397+
:patient_programme_vaccinations_search,
398+
patient: patient_to_keep,
399+
programme: Programme.hpv,
400+
last_searched_at: 1.day.ago
401+
)
402+
create(
403+
:patient_programme_vaccinations_search,
404+
patient: patient_to_destroy,
405+
programme: Programme.hpv,
406+
last_searched_at: 5.days.ago
407+
)
408+
409+
create(
410+
:patient_programme_vaccinations_search,
411+
patient: patient_to_destroy,
412+
programme: Programme.flu,
413+
last_searched_at: 3.days.ago
414+
)
415+
create(
416+
:patient_programme_vaccinations_search,
417+
patient: patient_to_keep,
418+
programme: Programme.flu,
419+
last_searched_at: 1.day.ago
420+
)
421+
422+
create(
423+
:patient_programme_vaccinations_search,
424+
patient: patient_to_keep,
425+
programme: Programme.menacwy,
426+
last_searched_at: 2.days.ago
427+
)
428+
429+
create(
430+
:patient_programme_vaccinations_search,
431+
patient: patient_to_destroy,
432+
programme: Programme.td_ipv,
433+
last_searched_at: 2.days.ago
434+
)
435+
end
436+
437+
it "merges searches correctly" do
438+
expect { call }.to change(
439+
PatientProgrammeVaccinationsSearch,
440+
:count
441+
).by(-2)
442+
443+
searches = patient_to_keep.patient_programme_vaccinations_searches
444+
expect(searches.count).to eq(4)
445+
446+
hpv_search = searches.find_by(programme_type: "hpv")
447+
expect(hpv_search.last_searched_at.to_date).to eq(1.day.ago.to_date)
448+
449+
flu_search = searches.find_by(programme_type: "flu")
450+
expect(flu_search.last_searched_at.to_date).to eq(1.day.ago.to_date)
451+
452+
tdipv_search = searches.find_by(programme_type: "td_ipv")
453+
expect(tdipv_search.last_searched_at.to_date).to eq(
454+
2.days.ago.to_date
455+
)
456+
457+
menacwy_search = searches.find_by(programme_type: "menacwy")
458+
expect(menacwy_search.last_searched_at.to_date).to eq(
459+
2.days.ago.to_date
460+
)
461+
end
462+
end
463+
end
464+
380465
context "when the patient to destroy has a changeset" do
381466
before do
382467
create(:patient_changeset, :class_import, patient: patient_to_destroy)

0 commit comments

Comments
 (0)