Skip to content

Commit 655abe4

Browse files
authored
Merge pull request #5962 from nhsuk/fix-associate-changeset-to-correct-patient-in-duplicate-flow
Fix: changesets and search results are attributed to the wrong patient in "Keep both records" flow
2 parents 2db880d + ec76f89 commit 655abe4

4 files changed

Lines changed: 101 additions & 3 deletions

File tree

app/forms/import_duplicate_form.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ def discard_pending_changes!
6666
end
6767

6868
def keep_both_changes!
69-
object.apply_pending_changes_to_new_record! if can_keep_both? && can_apply?
69+
if can_keep_both? && can_apply?
70+
changeset = object.changesets.includes(:import).order(:created_at).last
71+
object.apply_pending_changes_to_new_record!(changeset:)
72+
end
7073
end
7174

7275
def reset_count!

app/models/concerns/pending_changes_concern.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,21 @@ def discard_pending_changes!
4444
save!
4545
end
4646

47-
def apply_pending_changes_to_new_record!
47+
def apply_pending_changes_to_new_record!(changeset:)
48+
new_record = nil
49+
4850
ActiveRecord::Base.transaction do
4951
new_record = dup_for_pending_changes.tap(&:apply_pending_changes!)
52+
53+
changeset.update!(patient: new_record)
54+
55+
PDSSearchResult.where(import: changeset.import, patient: self).update_all(
56+
patient_id: new_record.id
57+
)
5058
discard_pending_changes!
51-
new_record
5259
end
60+
61+
new_record
5362
end
5463

5564
private

spec/features/import_child_records_spec.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,26 @@
121121
and_i_go_to_the_import_page
122122
then_i_should_see_import_issues_with_the_count
123123
end
124+
125+
scenario "Keep both records assigns changesets and PDS search results to the new patient" do
126+
given_the_app_is_setup
127+
and_import_review_screen_is_enabled
128+
and_pds_lookup_during_import_is_enabled
129+
stub_pds_search_to_return_no_patients
130+
and_an_existing_patient_record_exists_with_same_name_and_dob
131+
132+
when_i_visit_the_import_page
133+
and_i_choose_to_import_child_records
134+
and_i_upload_a_file_with_duplicate_except_for_postcode
135+
136+
when_i_review_the_duplicate_record
137+
and_i_choose_to_keep_both_records
138+
and_i_confirm_my_selection
139+
then_i_should_see_a_success_message
140+
141+
then_the_changeset_should_belong_to_the_new_patient
142+
and_the_pds_search_results_should_belong_to_the_new_patient
143+
end
124144
end
125145

126146
def given_the_app_is_setup
@@ -363,4 +383,68 @@ def then_i_should_see_import_issues_with_the_count
363383
expect(page).to have_link("Issues")
364384
expect(page).to have_selector(".app-count", text: "(1)").twice
365385
end
386+
387+
def and_an_existing_patient_record_exists_with_same_name_and_dob
388+
@existing_patient =
389+
create(
390+
:patient,
391+
given_name: "Taylor",
392+
family_name: "Reed",
393+
nhs_number: nil,
394+
date_of_birth: Date.new(2010, 1, 1),
395+
address_line_1: "10 Downing Street",
396+
address_line_2: "",
397+
address_town: "London",
398+
address_postcode: "SW1A 1AA",
399+
school: Location.find_by!(urn: "123456")
400+
)
401+
end
402+
403+
def and_i_upload_a_file_with_duplicate_except_for_postcode
404+
attach_file(
405+
"cohort_import[csv]",
406+
"spec/fixtures/cohort_import/duplicate_except_postcode.csv"
407+
)
408+
click_on "Continue"
409+
wait_for_import_to_complete(CohortImport)
410+
end
411+
412+
def when_i_review_the_duplicate_record
413+
find(".nhsuk-details__summary", text: /1 upload issue/).click
414+
click_on "Review"
415+
end
416+
417+
def and_i_choose_to_keep_both_records
418+
choose "Keep both child records"
419+
end
420+
421+
def and_i_confirm_my_selection
422+
click_on "Resolve duplicate"
423+
end
424+
425+
def then_i_should_see_a_success_message
426+
expect(page).to have_content("Record updated")
427+
end
428+
429+
def then_the_changeset_should_belong_to_the_new_patient
430+
import = CohortImport.order(:created_at).last
431+
changeset = import.changesets.from_file.sole
432+
expect(changeset.reload.patient_id).to eq(new_patient.id)
433+
end
434+
435+
def and_the_pds_search_results_should_belong_to_the_new_patient
436+
import = CohortImport.order(:created_at).last
437+
438+
results =
439+
PDSSearchResult.where(import_type: "CohortImport", import_id: import.id)
440+
441+
expect(results.pluck(:patient_id).uniq).to eq([new_patient.id])
442+
end
443+
444+
def new_patient
445+
Patient
446+
.where(given_name: "Taylor", family_name: "Reed")
447+
.where.not(id: @existing_patient.id)
448+
.sole
449+
end
366450
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CHILD_SCHOOL_URN,PARENT_1_NAME,PARENT_1_RELATIONSHIP,PARENT_1_EMAIL,PARENT_1_PHONE,PARENT_2_NAME,PARENT_2_RELATIONSHIP,PARENT_2_EMAIL,PARENT_2_PHONE,CHILD_FIRST_NAME,CHILD_LAST_NAME,CHILD_PREFERRED_GIVEN_NAME,CHILD_DATE_OF_BIRTH,CHILD_YEAR_GROUP,CHILD_ADDRESS_LINE_1,CHILD_ADDRESS_LINE_2,CHILD_TOWN,CHILD_POSTCODE,CHILD_NHS_NUMBER
2+
123456,,,,,,,,,Taylor,Reed,,2010-01-01,9,10 Downing Street,,London,SW1A 1AB,

0 commit comments

Comments
 (0)