Skip to content

Commit 51294d9

Browse files
Make read_immunisation a thin wrapper around read_immunisation_by_nhs_immunisations_api_id
This reduces code duplication a lot, while maintaining the same functionality. The only downside with this is that the error and log messages will be slightly different ("reading by ID" vs "reading record #{ID}"). Given that we only expect these functions to be run by operational staff, this doesn't seem like too much of a problem.
1 parent 1212813 commit 51294d9

File tree

2 files changed

+2
-81
lines changed

2 files changed

+2
-81
lines changed

app/lib/nhs/immunisations_api.rb

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -123,51 +123,8 @@ def read_immunisation_by_nhs_immunisations_api_id(nhs_immunisations_api_id)
123123
end
124124

125125
def read_immunisation(vaccination_record)
126-
unless Flipper.enabled?(:imms_api_integration)
127-
Rails.logger.info(
128-
"Not reading vaccination record from immunisations API as the" \
129-
" feature flag is disabled: #{vaccination_record.id}"
130-
)
131-
return
132-
end
133-
134-
if vaccination_record.nhs_immunisations_api_id.blank?
135-
raise "Vaccination record #{vaccination_record.id} missing NHS Immunisation ID"
136-
end
137-
138-
Rails.logger.info(
139-
"Reading vaccination record from immunisations API:" \
140-
" #{vaccination_record.id}"
141-
)
142-
143-
nhs_id = vaccination_record.nhs_immunisations_api_id
144-
response, duration =
145-
execute_and_time do
146-
NHS::API.connection.get(
147-
"/immunisation-fhir-api/FHIR/R4/Immunization/#{nhs_id}",
148-
nil,
149-
{ "Accept" => "application/fhir+json" }
150-
)
151-
end
152-
153-
Rails.logger.info(
154-
"Read response returned with status #{response.status} in #{duration}s"
155-
)
156-
157-
if response.status == 200
158-
FHIR.from_contents(response.body.to_json)
159-
else
160-
raise "Error reading vaccination record #{vaccination_record.id} from" \
161-
" Immunisations API: unexpected response status" \
162-
" #{response.status}"
163-
end
164-
rescue Faraday::ClientError => e
165-
if (diagnostics = extract_error_diagnostics(e&.response)).present?
166-
raise "Error reading vaccination record #{vaccination_record.id} from" \
167-
" Immunisations API: #{diagnostics}"
168-
else
169-
raise
170-
end
126+
nhs_immunisations_api_id = vaccination_record.nhs_immunisations_api_id
127+
read_immunisation_by_nhs_immunisations_api_id(nhs_immunisations_api_id)
171128
end
172129

173130
def update_immunisation(vaccination_record)

spec/lib/nhs/immunisations_api_spec.rb

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -428,42 +428,6 @@
428428
it "returns the FHIR record" do
429429
expect(perform_request).to be_a FHIR::Immunization
430430
end
431-
432-
context "an error is returned by the api" do
433-
let(:code) { nil }
434-
let(:diagnostics) { nil }
435-
436-
let(:body) do
437-
{
438-
resourceType: "OperationOutcome",
439-
id: "bc2c3c82-4392-4314-9d6b-a7345f82d923",
440-
meta: {
441-
profile: [
442-
"https://simplifier.net/guide/UKCoreDevelopment2/ProfileUKCore-OperationOutcome"
443-
]
444-
},
445-
issue: [
446-
{
447-
severity: "error",
448-
code: "invalid",
449-
details: {
450-
coding: [
451-
{
452-
system: "https://fhir.nhs.uk/Codesystem/http-error-codes",
453-
code:
454-
}
455-
]
456-
},
457-
diagnostics:
458-
}
459-
]
460-
}.to_json
461-
end
462-
463-
include_examples "unexpected response status", 201, "reading"
464-
include_examples "client error (4XX) handling", "reading"
465-
include_examples "generic error handling"
466-
end
467431
end
468432

469433
describe "update immunisations" do

0 commit comments

Comments
 (0)