Skip to content

Commit 4d51071

Browse files
authored
Merge pull request #4383 from nhsuk/fix-wildcard-search
Skip wildcard searches for names ≤ 3 characters
2 parents db71c25 + e99fbad commit 4d51071

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

app/jobs/process_patient_changesets_job.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ def steps
9292
no_matches: :no_fuzzy_with_wildcard_family_name,
9393
one_match: :no_fuzzy_with_wildcard_family_name,
9494
too_many_matches: :no_fuzzy_with_wildcard_family_name,
95+
skip_step: :no_fuzzy_with_wildcard_family_name,
9596
format_query: ->(query) { query[:given_name][3..] = "*" }
9697
},
9798
no_fuzzy_with_wildcard_family_name: {
9899
no_matches: :fuzzy_without_history,
99100
one_match: :fuzzy_without_history,
100101
too_many_matches: :fuzzy_without_history,
102+
skip_step: :fuzzy_without_history,
101103
format_query: ->(query) { query[:family_name][3..] = "*" }
102104
},
103105
fuzzy_without_history: {
@@ -123,6 +125,14 @@ def steps
123125

124126
def search_for_patient(attrs, step_name)
125127
return :no_postcode, nil if attrs["address_postcode"].blank?
128+
129+
case step_name
130+
when :no_fuzzy_with_wildcard_given_name
131+
return :skip_step, nil if attrs["given_name"].length <= 3
132+
when :no_fuzzy_with_wildcard_family_name
133+
return :skip_step, nil if attrs["family_name"].length <= 3
134+
end
135+
126136
query = {
127137
family_name: attrs["family_name"].dup,
128138
given_name: attrs["given_name"].dup,

app/models/pds_search_result.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class PDSSearchResult < ApplicationRecord
4242
validate: true
4343

4444
enum :result,
45-
{ no_matches: 0, one_match: 1, too_many_matches: 2, error: 3 },
45+
{
46+
no_matches: 0,
47+
one_match: 1,
48+
too_many_matches: 2,
49+
error: 3,
50+
skip_step: 4,
51+
no_postcode: 5
52+
},
4653
validate: true
4754
end

spec/jobs/process_patient_changesets_job_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,47 @@
107107
end
108108
end
109109

110+
context "when given name is too short for wildcard search" do
111+
let(:step) { :no_fuzzy_with_wildcard_given_name }
112+
113+
before do
114+
patient_changeset.child_attributes["given_name"] = "Al"
115+
allow(described_class).to receive(:perform_now).and_call_original
116+
allow(PDS::Patient).to receive(:search).and_return(nil)
117+
end
118+
119+
it "skips to the next appropriate step" do
120+
perform
121+
122+
expect(described_class).to have_received(:perform_now).with(
123+
patient_changeset,
124+
:no_fuzzy_with_wildcard_family_name
125+
)
126+
127+
expect(patient_changeset).to be_processed
128+
given_name_result =
129+
patient_changeset.search_results.find do |result|
130+
result["step"] == "no_fuzzy_with_wildcard_given_name"
131+
end
132+
expect(given_name_result["result"]).to eq("skip_step")
133+
end
134+
end
135+
136+
context "when patient has no postcode" do
137+
before { patient_changeset.child_attributes["address_postcode"] = "" }
138+
139+
it "finishes processing without performing search" do
140+
expect(PDS::Patient).not_to receive(:search)
141+
142+
perform
143+
144+
expect(patient_changeset).to be_processed
145+
expect(patient_changeset.search_results.last["result"]).to eq(
146+
"no_postcode"
147+
)
148+
end
149+
end
150+
110151
context "when import is slow" do
111152
before do
112153
allow(patient_changeset.import).to receive(:slow?).and_return(true)

0 commit comments

Comments
 (0)