Skip to content

Commit

Permalink
Raise NHS::PDS::TooManyMatches
Browse files Browse the repository at this point in the history
This updates the PDS search client code to handle getting a too many
matches error response and raise a suitable exception.

This doesn't handle the error, as we need to decide how to handle this
case, but having its own error means we can easily filter these out in
Sentry.
  • Loading branch information
thomasleese committed Jan 8, 2025
1 parent 5427190 commit ce197e2
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 29 deletions.
31 changes: 24 additions & 7 deletions app/lib/nhs/pds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class InvalidNHSNumber < StandardError
class PatientNotFound < StandardError
end

class TooManyMatches < StandardError
end

class << self
def get_patient(nhs_number)
NHS::API.connection.get(
Expand All @@ -52,18 +55,32 @@ def search_patients(attributes)
raise "Unrecognised attributes: #{missing_attrs.join(", ")}"
end

NHS::API.connection.get(
"personal-demographics/FHIR/R4/Patient",
attributes
)
response =
NHS::API.connection.get(
"personal-demographics/FHIR/R4/Patient",
attributes
)

if is_error?(response, "TOO_MANY_MATCHES")
raise TooManyMatches
else
response
end
end

private

def is_error?(error, code)
response = JSON.parse(error.response_body)
def is_error?(error_or_response, code)
response =
if error_or_response.is_a?(Faraday::ClientError)
JSON.parse(error_or_response.response_body)
elsif error_or_response.is_a?(Faraday::Response)
error_or_response.body
end

return false if (issues = response["issue"]).blank?

response["issue"].any? do |issue|
issues.any? do |issue|
issue["details"]["coding"].any? { |coding| coding["code"] == code }
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/fixtures/files/pds/too-many-matches.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"issue": [
{
"code": "multiple-matches",
"details": {
"coding": [
{
"code": "TOO_MANY_MATCHES",
"display": "Too Many Matches",
"system": "https://fhir.nhs.uk/R4/CodeSystem/Spine-ErrorOrWarningCode",
"version": "1"
}
]
},
"severity": "information"
}
],
"resourceType": "OperationOutcome"
}
72 changes: 50 additions & 22 deletions spec/lib/nhs/pds_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,33 +82,61 @@
end

describe "#search_patients" do
before do
stub_request(
:get,
"https://sandbox.api.service.nhs.uk/personal-demographics/FHIR/R4/Patient"
).with(
query: {
birthdate: "eq1939-01-09",
family: "Lawman",
gender: "female"
}
).to_return(
body: file_fixture("pds/search-patients-response.json"),
headers: {
"Content-Type" => "application/fhir+json"
}
subject(:search_patients) do
described_class.search_patients(
family: "Lawman",
gender: "female",
birthdate: "eq1939-01-09"
)
end

it "sends a GET request to with the provided attributes" do
response =
described_class.search_patients(
family: "Lawman",
gender: "female",
birthdate: "eq1939-01-09"
context "with a successful response" do
before do
stub_request(
:get,
"https://sandbox.api.service.nhs.uk/personal-demographics/FHIR/R4/Patient"
).with(
query: {
birthdate: "eq1939-01-09",
family: "Lawman",
gender: "female"
}
).to_return(
body: file_fixture("pds/search-patients-response.json"),
headers: {
"Content-Type" => "application/fhir+json"
}
)
end

it "sends a GET request to with the provided attributes" do
response = search_patients
expect(response.body).to include("total" => 1)
end
end

expect(response.body).to include("total" => 1)
context "with a too many matches response" do
before do
stub_request(
:get,
"https://sandbox.api.service.nhs.uk/personal-demographics/FHIR/R4/Patient"
).with(
query: {
birthdate: "eq1939-01-09",
family: "Lawman",
gender: "female"
}
).to_return(
body: file_fixture("pds/too-many-matches.json"),
headers: {
"Content-Type" => "application/fhir+json"
}
)
end

it "raises an error" do
expect { search_patients }.to raise_error(NHS::PDS::TooManyMatches)
end
end

it "raises an error if an unrecognised attribute is provided" do
Expand Down

0 comments on commit ce197e2

Please sign in to comment.