Skip to content

Commit 6e6f695

Browse files
committed
Ensure reliable NHS number retrieval from search results
Previously, `nhs_number` within `patient_changeset.search_results` was accessed using symbol keys (e.g., `sr[:nhs_number]`). However, when `search_results` (stored in a `jsonb` column) are deserialized from the database, they become standard Ruby `Hash` objects with string keys, causing symbol access to fail if `with_indifferent_access` was not effectively preserved. This commit updates the `unique_nhs_numbers` method to consistently use string keys (`sr["nhs_number"]`) for accessing the NHS number.
1 parent 0cc0d2e commit 6e6f695

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

app/jobs/process_patient_changesets_job.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ def perform(patient_changeset, step_name = nil)
5151
private
5252

5353
def unique_nhs_numbers(patient_changeset)
54-
patient_changeset.search_results.pluck(:nhs_number).compact.uniq
54+
patient_changeset
55+
.search_results
56+
.map { |sr| sr["nhs_number"] || sr[:nhs_number] }
57+
.compact
58+
.uniq
5559
end
5660

5761
def get_unique_nhs_number(patient_changeset)

spec/features/import_child_pds_lookup_extravaganza_spec.rb

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,15 @@ def and_pds_lookup_during_import_is_enabled
343343
address_postcode: "W2 3PE"
344344
)
345345

346-
stub_all_searches_to_return_no_patient(
346+
stub_pds_cascading_search(
347347
family_name: "Richard",
348348
given_name: "Caroline",
349349
birthdate: "eq2010-05-15",
350-
address_postcode: "B1 1AA"
350+
address_postcode: "B1 1AA",
351+
steps: {
352+
wildcard_postcode: "1111111111",
353+
fuzzy: "9435726097"
354+
}
351355
)
352356
end
353357

@@ -386,17 +390,57 @@ def stub_all_searches_to_return_no_patient(
386390
"given" => given_name,
387391
"birthdate" => birthdate,
388392
"address-postalcode" => address_postcode,
389-
"_fuzzy-match" => "true",
390-
"_history" => "false"
393+
"_fuzzy-match" => "true"
391394
)
395+
end
396+
397+
def stub_pds_cascading_search(
398+
family_name:,
399+
given_name:,
400+
birthdate:,
401+
address_postcode:,
402+
steps: {}
403+
)
392404
stub_pds_search_to_return_no_patients(
393405
"family" => family_name,
394406
"given" => given_name,
395407
"birthdate" => birthdate,
396-
"address-postalcode" => address_postcode,
397-
"_fuzzy-match" => "true",
398-
"_history" => "true"
408+
"address-postalcode" => address_postcode
399409
)
410+
411+
{
412+
wildcard_postcode: {
413+
"family" => family_name,
414+
"given" => given_name,
415+
"birthdate" => birthdate,
416+
"address-postalcode" => "#{address_postcode[0..1]}*"
417+
},
418+
wildcard_given_name: {
419+
"family" => family_name,
420+
"given" => "#{given_name[0..2]}*",
421+
"birthdate" => birthdate,
422+
"address-postalcode" => address_postcode
423+
},
424+
wildcard_family_name: {
425+
"family" => "#{family_name[0..2]}*",
426+
"given" => given_name,
427+
"birthdate" => birthdate,
428+
"address-postalcode" => address_postcode
429+
},
430+
fuzzy: {
431+
"family" => family_name,
432+
"given" => given_name,
433+
"birthdate" => birthdate,
434+
"address-postalcode" => address_postcode,
435+
"_fuzzy-match" => "true"
436+
}
437+
}.each do |step, query|
438+
if steps[step]
439+
stub_pds_search_to_return_a_patient(steps[step], **query)
440+
else
441+
stub_pds_search_to_return_no_patients(**query)
442+
end
443+
end
400444
end
401445

402446
def when_i_visit_the_import_page

spec/support/pds_helper.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
module PDSHelper
44
def stub_pds_search_to_return_no_patients(**query)
5-
query["_history"] ||= "true"
6-
query.delete("_history") if query["_history"] == "false"
5+
query["_history"] = "true" unless query["_fuzzy-match"] == "true"
76

87
stub_request(
98
:get,
@@ -19,7 +18,7 @@ def stub_pds_search_to_return_no_patients(**query)
1918
end
2019

2120
def stub_pds_search_to_return_a_patient(nhs_number = "9449306168", **query)
22-
query["_history"] ||= "true"
21+
query["_history"] = "true" unless query["_fuzzy-match"] == "true"
2322

2423
response_data =
2524
JSON.parse(file_fixture("pds/search-patients-response.json").read)

0 commit comments

Comments
 (0)