Skip to content

Commit ea351d5

Browse files
Merge pull request #5500 from nhsuk/handle_nil_pds_patient_lookups_gracefully
Gracefully handle nil values from NHS::PDS patient lookups
2 parents 932fc0e + d293b07 commit ea351d5

6 files changed

Lines changed: 113 additions & 64 deletions

File tree

app/jobs/patient_update_from_pds_job.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def perform(patient, search_results = [])
1919
return unless unique_nhs_number
2020

2121
pds_patient = PDS::Patient.find(unique_nhs_number)
22+
return unless pds_patient
2223

2324
if pds_patient.nhs_number != patient.nhs_number
2425
if (

app/lib/mavis_cli/pds/get.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#frozen_string_literal: true
1+
# frozen_string_literal: true
22

33
module MavisCLI
44
module PDS
@@ -12,12 +12,16 @@ def call(nhs_number:)
1212

1313
response = NHS::PDS.get_patient(nhs_number)
1414

15-
puts response.status unless response.status == 200
16-
puts response.env.url
17-
puts ""
18-
puts(response.headers.map { "#{_1}: #{_2}" })
19-
puts ""
20-
puts JSON.pretty_generate(response.body)
15+
if response.nil?
16+
puts "Response from NHS::PDS is `nil`"
17+
else
18+
puts response.status unless response.status == 200
19+
puts response.env.url
20+
puts ""
21+
puts(response.headers.map { "#{_1}: #{_2}" })
22+
puts ""
23+
puts JSON.pretty_generate(response.body)
24+
end
2125
end
2226
end
2327
end

app/lib/mavis_cli/pds/search.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,16 @@ def call(
6868

6969
response = NHS::PDS.search_patients(query)
7070

71-
puts response.status unless response.status == 200
72-
puts response.env.url
73-
puts ""
74-
puts(response.headers.map { "#{_1}: #{_2}" })
75-
puts ""
76-
puts JSON.pretty_generate(response.body)
71+
if response.nil?
72+
puts "Response from NHS::PDS is `nil`"
73+
else
74+
puts response.status unless response.status == 200
75+
puts response.env.url
76+
puts ""
77+
puts(response.headers.map { "#{_1}: #{_2}" })
78+
puts ""
79+
puts JSON.pretty_generate(response.body)
80+
end
7781
end
7882
end
7983
end

app/models/pds/patient.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class PDS::Patient
1313
class << self
1414
def find(nhs_number)
1515
response = NHS::PDS.get_patient(nhs_number)
16+
return unless response
17+
1618
from_pds_fhir_response(response.body)
1719
end
1820

@@ -33,8 +35,10 @@ def search(
3335
"_fuzzy-match" => fuzzy
3436
}.compact_blank
3537

36-
results = NHS::PDS.search_patients(query).body
38+
response = NHS::PDS.search_patients(query)
39+
return unless response
3740

41+
results = response.body
3842
return if results["total"].zero?
3943

4044
from_pds_fhir_response(results["entry"].first["resource"])

spec/lib/nhs/pds_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
describe "#get_patient" do
55
subject(:get_patient) { described_class.get_patient("9000000009") }
66

7+
context "with pds.enabled is false" do
8+
before { Settings.pds.enabled = false }
9+
10+
after { Settings.reload! }
11+
12+
it { should be_nil }
13+
end
14+
715
context "with a successful response" do
816
before do
917
stub_request(
@@ -90,6 +98,14 @@
9098
)
9199
end
92100

101+
context "with pds.enabled is false" do
102+
before { Settings.pds.enabled = false }
103+
104+
after { Settings.reload! }
105+
106+
it { should be_nil }
107+
end
108+
93109
context "with a successful response" do
94110
before do
95111
stub_request(

spec/models/pds/patient_spec.rb

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,40 @@
88
file_fixture("pds/get-patient-response-deceased.json").read
99
end
1010

11-
before do
12-
allow(NHS::PDS).to receive(:get_patient).and_return(
13-
instance_double(
14-
Faraday::Response,
15-
status: 200,
16-
body: JSON.parse(json_response)
17-
)
18-
)
19-
end
11+
context "with pds.enabled is false" do
12+
before { Settings.pds.enabled = false }
2013

21-
it "calls get_patient on PDS library" do
22-
find
23-
expect(NHS::PDS).to have_received(:get_patient).with("9000000009")
14+
after { Settings.reload! }
15+
16+
it { should be_nil }
2417
end
2518

26-
it "parses the patient information" do
27-
expect(find).to have_attributes(
28-
nhs_number: "9000000009",
29-
family_name: "Smith",
30-
date_of_birth: Date.new(2010, 10, 22),
31-
date_of_death: Date.new(2010, 10, 22),
32-
restricted: true,
33-
gp_ods_code: "Y12345"
34-
)
19+
context "when the patient exists" do
20+
before do
21+
allow(NHS::PDS).to receive(:get_patient).and_return(
22+
instance_double(
23+
Faraday::Response,
24+
status: 200,
25+
body: JSON.parse(json_response)
26+
)
27+
)
28+
end
29+
30+
it "calls get_patient on PDS library" do
31+
find
32+
expect(NHS::PDS).to have_received(:get_patient).with("9000000009")
33+
end
34+
35+
it "parses the patient information" do
36+
expect(find).to have_attributes(
37+
nhs_number: "9000000009",
38+
family_name: "Smith",
39+
date_of_birth: Date.new(2010, 10, 22),
40+
date_of_death: Date.new(2010, 10, 22),
41+
restricted: true,
42+
gp_ods_code: "Y12345"
43+
)
44+
end
3545
end
3646
end
3747

@@ -49,38 +59,48 @@
4959
file_fixture("pds/search-patients-response.json").read
5060
end
5161

52-
before do
53-
allow(NHS::PDS).to receive(:search_patients).and_return(
54-
instance_double(
55-
Faraday::Response,
56-
status: 200,
57-
body: JSON.parse(json_response)
58-
)
59-
)
60-
end
62+
context "with pds.enabled is false" do
63+
before { Settings.pds.enabled = false }
6164

62-
it "calls find on PDS library" do
63-
search
64-
expect(NHS::PDS).to have_received(:search_patients).with(
65-
{
66-
"_history" => true,
67-
"address-postalcode" => "SW11 1AA",
68-
"birthdate" => "eq2020-01-01",
69-
"family" => "Smith",
70-
"given" => "John"
71-
}
72-
)
65+
after { Settings.reload! }
66+
67+
it { should be_nil }
7368
end
7469

75-
it "parses the patient information" do
76-
expect(search).to have_attributes(
77-
nhs_number: "9449306168",
78-
family_name: "LAWMAN",
79-
date_of_birth: Date.new(1939, 1, 9),
80-
date_of_death: nil,
81-
restricted: false,
82-
gp_ods_code: "H81109"
83-
)
70+
context "when the patient exists" do
71+
before do
72+
allow(NHS::PDS).to receive(:search_patients).and_return(
73+
instance_double(
74+
Faraday::Response,
75+
status: 200,
76+
body: JSON.parse(json_response)
77+
)
78+
)
79+
end
80+
81+
it "calls find on PDS library" do
82+
search
83+
expect(NHS::PDS).to have_received(:search_patients).with(
84+
{
85+
"_history" => true,
86+
"address-postalcode" => "SW11 1AA",
87+
"birthdate" => "eq2020-01-01",
88+
"family" => "Smith",
89+
"given" => "John"
90+
}
91+
)
92+
end
93+
94+
it "parses the patient information" do
95+
expect(search).to have_attributes(
96+
nhs_number: "9449306168",
97+
family_name: "LAWMAN",
98+
date_of_birth: Date.new(1939, 1, 9),
99+
date_of_death: nil,
100+
restricted: false,
101+
gp_ods_code: "H81109"
102+
)
103+
end
84104
end
85105
end
86106
end

0 commit comments

Comments
 (0)