Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise NHS::PDS::TooManyMatches #2758

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Down Expand Up @@ -54,10 +57,17 @@ 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
rescue Faraday::BadRequestError => e
add_sentry_breadcrumb(e)
raise
Expand All @@ -75,10 +85,17 @@ def add_sentry_breadcrumb(error)
Sentry.add_breadcrumb(crumb)
end

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
Loading