Skip to content

Commit 41d5831

Browse files
authored
Merge pull request #4299 from nhsuk/fix-patient-changeset-bugs
Fix PatientChangeset bugs in importing parents and parent_relationships. Only perform PDS search if postcode present in upload.
2 parents 95ee0b8 + 795defc commit 41d5831

File tree

4 files changed

+252
-17
lines changed

4 files changed

+252
-17
lines changed

app/jobs/process_patient_changesets_job.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def perform(patient_changeset)
2929
private
3030

3131
def search_for_patient(attrs)
32+
return nil if attrs["address_postcode"].blank?
3233
PDS::Patient.search(
3334
family_name: attrs["family_name"],
3435
given_name: attrs["given_name"],

app/models/patient_changeset.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ def parents
102102
parent =
103103
Parent.match_existing(
104104
patient: existing_patients.first,
105-
email: attrs[:email],
106-
phone: attrs[:phone],
107-
full_name: attrs[:full_name]
105+
email: attrs["email"],
106+
phone: attrs["phone"],
107+
full_name: attrs["full_name"]
108108
) || Parent.new
109109

110-
parent.email = attrs[:email] if attrs[:email]
111-
parent.full_name = attrs[:full_name] if attrs[:full_name]
112-
parent.phone = attrs[:phone] if attrs[:phone]
110+
parent.email = attrs["email"] if attrs["email"]
111+
parent.full_name = attrs["full_name"] if attrs["full_name"]
112+
parent.phone = attrs["phone"] if attrs["phone"]
113113
parent.phone_receive_updates = false if parent.phone.blank?
114114

115115
parent
@@ -121,8 +121,7 @@ def parent_relationships
121121
[
122122
parent_1_attributes["relationship"],
123123
parent_2_attributes["relationship"]
124-
].compact
125-
.map { parent_relationship_attributes(it) }
124+
].map { parent_relationship_attributes(it) }
126125
.zip(parents)
127126
.map do |relationship, parent|
128127
ParentRelationship

spec/features/import_child_pds_lookup_extravaganza_spec.rb

Lines changed: 238 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,50 @@
1414
when_i_visit_the_import_page
1515
and_i_upload_import_file("pds_extravaganza.csv")
1616
then_i_should_see_the_import_page
17-
and_i_should_see_one_new_patient_created
17+
and_i_should_see_correct_patient_counts
1818

1919
# Case 1: Patient with existing NHS number (Albert) - nothing should happen
2020
and_i_see_the_patient_uploaded_with_nhs_number
21+
and_parents_are_created_for_albert
2122

2223
# Case 2: Existing patient without NHS number (Betty) - should not show duplicate review
2324
and_i_do_not_see_an_import_review_for_the_first_patient_uploaded_without_nhs_number
2425
when_i_click_on_the_patient_without_review
2526
then_i_see_the_new_patient_has_an_nhs_number
27+
and_betty_has_correct_parent_relationships
2628

2729
# Case 3: Existing patient with NHS number (Catherine) - should show duplicate review
2830
when_i_go_back_to_the_import_page
2931
then_i_see_an_import_review_for_the_second_patient_uploaded_without_nhs_number
3032
when_i_click_review
3133
then_i_see_both_records_have_an_nhs_number
34+
and_i_see_address_differences_for_review
3235
when_i_use_duplicate_record_during_merge
3336
then_the_existing_patient_has_an_nhs_number_in_mavis
37+
and_catherine_parents_are_handled_correctly
3438

3539
# Case 4: New patient without NHS number (Charlie) - should be created with NHS number from PDS
3640
when_i_go_back_to_the_import_page
3741
when_i_click_on_new_patient_uploaded_without_an_nhs_number
3842
then_i_see_the_new_patient_now_has_an_nhs_number
43+
and_charlie_has_no_parents_as_expected
44+
45+
# Case 5: Home educated patient (Emma) - test school move handling
46+
when_i_go_back_to_the_import_page
47+
when_i_click_on_home_educated_patient
48+
then_i_see_home_educated_patient_details
49+
and_emma_has_correct_parent_data
50+
51+
# Case 6: Patient with parent but no relationship specified (Oliver)
52+
when_i_go_back_to_the_import_page
53+
when_i_click_on_patient_with_unknown_relationship
54+
then_i_see_patient_with_unknown_relationship_details
55+
and_oliver_has_unknown_relationship_parent
56+
57+
then_school_moves_are_created_appropriately
58+
59+
and_all_parent_relationships_are_established
60+
and_import_counts_are_correct
3961
end
4062

4163
def given_i_am_signed_in
@@ -52,9 +74,12 @@ def given_i_am_signed_in
5274

5375
def and_an_hpv_programme_is_underway
5476
@school = create(:school, urn: "123456", team: @team)
77+
@clinic = create(:generic_clinic, team: @team)
5578

5679
@session =
5780
create(:session, team: @team, location: @school, programmes: [@programme])
81+
@clinic_session =
82+
create(:session, team: @team, location: @clinic, programmes: [@programme])
5883
end
5984

6085
def and_an_existing_patient_record_exists
@@ -91,7 +116,16 @@ def and_an_existing_patient_record_exists
91116
session: @session
92117
)
93118

94-
# Catherine - will have different address, causing review
119+
@different_school = create(:school, urn: "456789", team: @team)
120+
@different_school_session =
121+
create(
122+
:session,
123+
team: @team,
124+
location: @different_school,
125+
programmes: [@programme]
126+
)
127+
128+
# Catherine - will have different address and school, causing review, and school move
95129
@existing_patient_duplicate_review =
96130
create(
97131
:patient,
@@ -104,11 +138,26 @@ def and_an_existing_patient_record_exists
104138
address_line_2: "",
105139
address_town: "Birmingham", # Different from CSV
106140
address_postcode: "B1 1AA", # Different from CSV
107-
school: nil,
108-
session: @session
141+
school: @different_school,
142+
session: @different_school_session
109143
)
110144

145+
@existing_parent =
146+
create(
147+
:parent,
148+
full_name: "John Tweedle",
149+
email: "john.tweedle@email.com"
150+
)
151+
create(
152+
:parent_relationship,
153+
parent: @existing_parent,
154+
patient: @existing_patient_uploaded_with_nhs_number,
155+
type: "unknown"
156+
)
157+
111158
expect(Patient.count).to eq(3)
159+
expect(ParentRelationship.count).to eq(1)
160+
expect(Parent.count).to eq(1)
112161
end
113162

114163
def and_pds_lookup_during_import_is_enabled
@@ -137,6 +186,14 @@ def and_pds_lookup_during_import_is_enabled
137186
"birthdate" => "eq2011-03-15",
138187
"address-postalcode" => "SW2 2BB"
139188
)
189+
190+
stub_pds_search_to_return_a_patient(
191+
"9435783309",
192+
"family" => "Homeschool",
193+
"given" => "Emma",
194+
"birthdate" => "eq2010-06-01",
195+
"address-postalcode" => "SW3 3AA"
196+
)
140197
end
141198

142199
def when_i_visit_the_import_page
@@ -215,11 +272,187 @@ def then_i_see_the_new_patient_now_has_an_nhs_number
215272
end
216273

217274
def then_the_existing_patient_has_an_nhs_number_in_mavis
218-
expect(Patient.count).to eq(4)
275+
expect(Patient.count).to eq(6)
219276
patient = Patient.where(given_name: "Catherine").first
220277
expect(patient.nhs_number).to eq("9876543210")
221278
expect(patient.address_line_1).to eq("456 New Street")
222279
expect(patient.address_town).to eq("London")
223280
expect(patient.address_postcode).to eq("SW2 2BB")
224281
end
282+
283+
def and_i_should_see_correct_patient_counts
284+
perform_enqueued_jobs
285+
expect(Patient.count).to eq(6)
286+
end
287+
288+
def and_parents_are_created_for_albert
289+
albert = Patient.find_by(given_name: "Albert", family_name: "Tweedle")
290+
expect(albert.parents.count).to eq(2)
291+
expect(albert.parents.map(&:full_name)).to contain_exactly(
292+
"John Tweedle",
293+
"Mary Tweedle"
294+
)
295+
expect(albert.parents.find_by(full_name: "John Tweedle").email).to eq(
296+
"john.tweedle@email.com"
297+
)
298+
end
299+
300+
def and_betty_has_correct_parent_relationships
301+
betty = Patient.find_by(given_name: "Betty", family_name: "Samson")
302+
expect(betty.parent_relationships.count).to eq(2)
303+
304+
dad_relationship =
305+
betty
306+
.parent_relationships
307+
.joins(:parent)
308+
.find_by(parents: { full_name: "Robert Samson" })
309+
expect(dad_relationship.type).to eq("father")
310+
311+
mum_relationship =
312+
betty
313+
.parent_relationships
314+
.joins(:parent)
315+
.find_by(parents: { full_name: "Linda Samson" })
316+
expect(mum_relationship.type).to eq("mother")
317+
318+
linda = Parent.find_by(full_name: "Linda Samson")
319+
expect(linda.phone).to be_blank
320+
expect(linda.phone_receive_updates).to be false
321+
end
322+
323+
def and_i_see_address_differences_for_review
324+
expect(page).to have_content("999 Old Street") # Original address
325+
expect(page).to have_content("456 New Street") # New address from CSV
326+
expect(page).to have_content("Birmingham") # Original town
327+
expect(page).to have_content("London") # New town from CSV
328+
end
329+
330+
def and_catherine_parents_are_handled_correctly
331+
catherine =
332+
Patient.find_by(given_name: "Catherine", family_name: "Williams")
333+
expect(catherine.parents.count).to eq(2)
334+
335+
guardian = catherine.parents.find_by(full_name: "David Williams")
336+
guardian_relationship =
337+
catherine.parent_relationships.find_by(parent: guardian)
338+
expect(guardian_relationship.type).to eq("guardian")
339+
340+
mother = catherine.parents.find_by(full_name: "Sarah Williams")
341+
expect(mother.phone).to be_present
342+
end
343+
344+
def and_charlie_has_no_parents_as_expected
345+
charlie = Patient.find_by(given_name: "Charlie", family_name: "Brown")
346+
expect(charlie.parents.count).to eq(0)
347+
expect(charlie.parent_relationships.count).to eq(0)
348+
end
349+
350+
def when_i_click_on_home_educated_patient
351+
click_link "HOMESCHOOL, Emma"
352+
end
353+
354+
def then_i_see_home_educated_patient_details
355+
expect(page).to have_content("HOMESCHOOL, Emma")
356+
expect(page).to have_content("1 June 2010")
357+
end
358+
359+
def and_emma_has_correct_parent_data
360+
emma = Patient.find_by(given_name: "Emma", family_name: "Homeschool")
361+
expect(emma.parents.count).to eq(1)
362+
363+
father = emma.parents.first
364+
expect(father.full_name).to eq("Mike HomeDad")
365+
expect(father.email).to eq("mike@home.com")
366+
367+
relationship = emma.parent_relationships.first
368+
expect(relationship.type).to eq("father")
369+
end
370+
371+
def then_school_moves_are_created_appropriately
372+
perform_enqueued_jobs
373+
374+
charlie = Patient.find_by(given_name: "Charlie")
375+
charlie_move = SchoolMoveLogEntry.find_by(patient: charlie)
376+
expect(charlie_move).to be_present
377+
expect(charlie_move.school).to eq(@school)
378+
379+
emma = Patient.find_by(given_name: "Emma")
380+
emma_move = SchoolMoveLogEntry.find_by(patient: emma)
381+
expect(emma_move).to be_present
382+
expect(emma_move.home_educated).to be true
383+
expect(emma_move.school).to be_nil
384+
385+
catherine =
386+
Patient.find_by(given_name: "Catherine", family_name: "Williams")
387+
catherine_move = SchoolMove.find_by(patient: catherine, school: @school)
388+
expect(catherine_move).to be_present
389+
catherine_log_entry = SchoolMoveLogEntry.find_by(patient: catherine)
390+
expect(catherine_log_entry).to be_nil
391+
expect(catherine.school).to eq(@different_school)
392+
end
393+
394+
def and_all_parent_relationships_are_established
395+
expect(Parent.count).to eq(8)
396+
expect(ParentRelationship.count).to eq(8)
397+
398+
father_relationships = ParentRelationship.where(type: "father")
399+
expect(father_relationships.count).to eq(3) # John Tweedle, Mike HomeDad, Robert Samson
400+
401+
mother_relationships = ParentRelationship.where(type: "mother")
402+
expect(mother_relationships.count).to eq(3) # Mary Tweedle, Linda Samson, Sarah Williams
403+
404+
guardian_relationships = ParentRelationship.where(type: "guardian")
405+
expect(guardian_relationships.count).to eq(1) # David Williams
406+
407+
unknown_relationships = ParentRelationship.where(type: "unknown")
408+
expect(unknown_relationships.count).to eq(1) # Jane Doe
409+
end
410+
411+
def and_import_counts_are_correct
412+
import = CohortImport.last
413+
expect(import.patients.count).to eq(6)
414+
end
415+
416+
def when_i_click_on_patient_with_unknown_relationship
417+
click_link "GREEN, Oliver"
418+
end
419+
420+
def then_i_see_patient_with_unknown_relationship_details
421+
expect(page).to have_content("GREEN, Oliver")
422+
expect(page).to have_content("15 August 2010")
423+
end
424+
425+
def and_oliver_has_unknown_relationship_parent
426+
oliver = Patient.find_by(given_name: "Oliver", family_name: "Green")
427+
expect(oliver.parents.count).to eq(1)
428+
429+
parent = oliver.parents.first
430+
expect(parent.full_name).to eq("Jane Doe")
431+
expect(parent.email).to be_blank
432+
433+
relationship = oliver.parent_relationships.first
434+
expect(relationship.type).to eq("unknown")
435+
expect(relationship.label).to eq("Unknown")
436+
end
437+
def when_i_click_on_patient_with_unknown_relationship
438+
click_link "GREEN, Oliver"
439+
end
440+
441+
def then_i_see_patient_with_unknown_relationship_details
442+
expect(page).to have_content("GREEN, Oliver")
443+
expect(page).to have_content("15 August 2010")
444+
end
445+
446+
def and_oliver_has_unknown_relationship_parent
447+
oliver = Patient.find_by(given_name: "Oliver", family_name: "Green")
448+
expect(oliver.parents.count).to eq(1)
449+
450+
parent = oliver.parents.first
451+
expect(parent.full_name).to eq("Jane Doe")
452+
expect(parent.email).to be_blank
453+
454+
relationship = oliver.parent_relationships.first
455+
expect(relationship.type).to eq("unknown")
456+
expect(relationship.label).to eq("Unknown")
457+
end
225458
end
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
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,,,,,,,,,Albert,Tweedle,,2009-12-29,8,38A Battersea Rise,,London,SW11 1EH,9999075320
3-
123456,,,,,,,,,Betty,Samson,Beth,2010-01-01,8,123 High Street,,London,SW1A 1AA,
4-
123456,,,,,,,,,Catherine,Williams,,2009-05-15,8,456 New Street,,London,SW2 2BB,
5-
123456,,,,,,,,,Charlie,Brown,,2011-03-15,9,42 Wallaby Way,,London,SW2 2BB,
2+
123456,John Tweedle,Father,john.tweedle@email.com,01234567890,Mary Tweedle,Mother,mary.tweedle@email.com,01234567891,Albert,Tweedle,,2009-12-29,8,38A Battersea Rise,,London,SW11 1EH,9999075320
3+
123456,Robert Samson,Dad,bob.samson@email.com,01234567892,Linda Samson,Mum,linda.samson@email.com,,Betty,Samson,Beth,2010-01-01,8,123 High Street,,London,SW1A 1AA,
4+
123456,David Williams,Guardian,david.w@email.com,,Sarah Williams,Mother,sarah.w@email.com,01234567894,Catherine,Williams,,2009-05-15,8,456 New Street,,London,SW2 2BB,
5+
123456,,,,,,,,,Charlie,Brown,,2011-03-15,9,42 Wallaby Way,,London,SW2 2BB,
6+
999999,Mike HomeDad,Father,mike@home.com,01234567895,,,,,Emma,Homeschool,,2010-06-01,8,123 Home Street,,London,SW3 3AA,
7+
123456,Jane Doe,,,01234567896,,,,,Oliver,Green,,2010-08-15,8,789 Silent Street,,London,SW1W 8JL,9435753868

0 commit comments

Comments
 (0)