Skip to content

Commit 2db880d

Browse files
authored
Merge pull request #5997 from nhsuk/fix-parent-relationships-move-or-remove-on-duplicate-resolution
Fix parent contacts being attached to the wrong patient after resolving import duplicates
2 parents e6924f7 + ac90b7b commit 2db880d

3 files changed

Lines changed: 129 additions & 10 deletions

File tree

app/forms/import_duplicate_form.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def apply_pending_changes!
5858
end
5959

6060
def discard_pending_changes!
61+
remove_imported_parent_relationships_if_needed!
62+
6163
object.patient.discard_pending_changes! if object.respond_to?(:patient)
6264

6365
object.discard_pending_changes!
@@ -70,4 +72,18 @@ def keep_both_changes!
7072
def reset_count!
7173
TeamCachedCounts.new(current_team).reset_import_issues!
7274
end
75+
76+
def remove_imported_parent_relationships_if_needed!
77+
return unless object.is_a?(Patient)
78+
79+
changeset = object.changesets.includes(:import).order(:created_at).last
80+
return if changeset.nil?
81+
82+
changeset
83+
.import
84+
.parent_relationships
85+
.includes(:patient)
86+
.where(patient: object)
87+
.find_each(&:destroy!)
88+
end
7389
end

app/models/patient.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,26 @@ def dup_for_pending_changes
757757
end
758758
end
759759

760+
def apply_pending_changes_to_new_record!
761+
new_record = nil
762+
changeset = changesets.includes(:import).order(:created_at).last
763+
764+
ActiveRecord::Base.transaction do
765+
new_record = super
766+
767+
if changeset
768+
changeset.update!(patient_id: new_record.id)
769+
changeset
770+
.import
771+
.parent_relationships
772+
.where(patient_id: id)
773+
.update_all(patient_id: new_record.id)
774+
end
775+
end
776+
777+
new_record
778+
end
779+
760780
def clear_pending_sessions!(team: nil)
761781
scope = patient_locations.pending
762782

spec/forms/import_duplicate_form_spec.rb

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,101 @@
44
let(:programme) { Programme.sample }
55

66
describe "#save" do
7-
subject do
8-
described_class.new(
9-
apply_changes: "apply",
10-
object: vaccination_record,
11-
current_team: team
12-
).save
13-
end
7+
context "resolving a vaccination record" do
8+
subject do
9+
described_class.new(
10+
apply_changes: "apply",
11+
object: vaccination_record,
12+
current_team: team
13+
).save
14+
end
1415

15-
let(:team) { create(:team, programmes: [programme]) }
16-
let(:vaccination_record) { create(:vaccination_record, programme:, team:) }
16+
let(:team) { create(:team, programmes: [programme]) }
17+
let(:vaccination_record) do
18+
create(:vaccination_record, programme:, team:)
19+
end
1720

18-
it_behaves_like "a method that updates team cached counts"
21+
it_behaves_like "a method that updates team cached counts"
22+
end
23+
24+
context "resolving a patient record" do
25+
context "when a patient import issue includes parent relationships" do
26+
let(:team) { create(:team, programmes: [programme]) }
27+
let(:session) { create(:session, team:, programmes: [programme]) }
28+
let(:class_import) { create(:class_import, session:) }
29+
let(:existing_patient) { create(:patient) }
30+
31+
let(:import_parent) { create(:parent) }
32+
let(:existing_parent) { create(:parent) }
33+
34+
let(:import_relationship) do
35+
create(
36+
:parent_relationship,
37+
patient: existing_patient,
38+
parent: import_parent,
39+
type: "father"
40+
)
41+
end
42+
43+
let!(:existing_relationship) do
44+
create(
45+
:parent_relationship,
46+
patient: existing_patient,
47+
parent: existing_parent,
48+
type: "mother"
49+
)
50+
end
51+
52+
let!(:changeset) do
53+
create(
54+
:patient_changeset,
55+
:class_import,
56+
:import_issue,
57+
import: class_import,
58+
patient: existing_patient
59+
)
60+
end
61+
62+
before do
63+
existing_patient.update!(pending_changes: { "given_name" => "Twin" })
64+
class_import.parent_relationships << import_relationship
65+
end
66+
67+
it "moves imported parent relationships to the new patient when keeping both" do
68+
form =
69+
described_class.new(
70+
apply_changes: "keep_both",
71+
object: existing_patient,
72+
current_team: team
73+
)
74+
75+
expect(form.save).to be(true)
76+
77+
new_patient = changeset.reload.patient
78+
79+
expect(new_patient).not_to eq(existing_patient)
80+
expect(import_relationship.reload.patient).to eq(new_patient)
81+
expect(existing_relationship.reload.patient).to eq(existing_patient)
82+
end
83+
84+
it "removes imported parent relationships when discarding changes" do
85+
form =
86+
described_class.new(
87+
apply_changes: "discard",
88+
object: existing_patient,
89+
current_team: team
90+
)
91+
92+
expect(form.save).to be(true)
93+
94+
expect { import_relationship.reload }.to raise_error(
95+
ActiveRecord::RecordNotFound
96+
)
97+
expect(existing_relationship.reload.patient).to eq(existing_patient)
98+
expect(changeset.reload.patient).to eq(existing_patient)
99+
end
100+
end
101+
end
19102
end
20103

21104
describe "#can_apply?" do

0 commit comments

Comments
 (0)