Skip to content

Commit 41822d6

Browse files
Add read_immunisation
This makes the API call to read a vaccination record in the API. It returns a `FHIR::Immunization` object
1 parent f0950a7 commit 41822d6

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

app/lib/nhs/immunisations_api.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,54 @@ def create_immunisation(vaccination_record)
7575
end
7676
end
7777

78+
def read_immunisation(vaccination_record)
79+
unless Flipper.enabled?(:imms_api_integration)
80+
Rails.logger.info(
81+
"Not reading vaccination record from immunisations API as the" \
82+
" feature flag is disabled: #{vaccination_record.id}"
83+
)
84+
return
85+
end
86+
87+
if vaccination_record.nhs_immunisations_api_id.blank?
88+
raise "Vaccination record #{vaccination_record.id} missing NHS Immunisation ID"
89+
end
90+
91+
Rails.logger.info(
92+
"Reading vaccination record from immunisations API:" \
93+
" #{vaccination_record.id}"
94+
)
95+
96+
nhs_id = vaccination_record.nhs_immunisations_api_id
97+
response, duration =
98+
execute_and_time do
99+
NHS::API.connection.get(
100+
"/immunisation-fhir-api/FHIR/R4/Immunization/#{nhs_id}",
101+
nil,
102+
{ "Accept" => "application/fhir+json" }
103+
)
104+
end
105+
106+
Rails.logger.info(
107+
"Read response returned with status #{response.status} in #{duration}s"
108+
)
109+
110+
if response.status == 200
111+
FHIR.from_contents(response.body.to_json)
112+
else
113+
raise "Error reading vaccination record #{vaccination_record.id} from" \
114+
" Immunisations API: unexpected response status" \
115+
" #{response.status}"
116+
end
117+
rescue Faraday::ClientError => e
118+
if (diagnostics = extract_error_diagnostics(e&.response)).present?
119+
raise "Error reading vaccination record #{vaccination_record.id} from" \
120+
" Immunisations API: #{diagnostics}"
121+
else
122+
raise
123+
end
124+
end
125+
78126
def update_immunisation(vaccination_record)
79127
unless Flipper.enabled?(:imms_api_integration)
80128
Rails.logger.info(

spec/lib/nhs/immunisations_api_spec.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,83 @@
301301
include_examples "an imms_api_integration feature flag check"
302302
end
303303

304+
describe "read immunisation" do
305+
subject(:perform_request) do
306+
described_class.read_immunisation(vaccination_record)
307+
end
308+
309+
let(:status) { 200 }
310+
let(:body) { file_fixture("fhir/fhir_record_full.json").read }
311+
let(:headers) { { "content-type" => "application/fhir+json" } }
312+
313+
let!(:request_stub) do
314+
stub_request(
315+
:get,
316+
"https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization/ffff1111-eeee-2222-dddd-3333eeee4444"
317+
).to_return(status:, body:, headers:)
318+
end
319+
320+
before do
321+
vaccination_record.update(
322+
nhs_immunisations_api_id: "ffff1111-eeee-2222-dddd-3333eeee4444"
323+
)
324+
end
325+
326+
include_examples "an imms_api_integration feature flag check"
327+
328+
it "sends the correct request" do
329+
request_stub.with do |request|
330+
expect(request.headers).to include(
331+
{ "Accept" => "application/fhir+json" }
332+
)
333+
end
334+
335+
perform_request
336+
337+
expect(request_stub).to have_been_made
338+
end
339+
340+
it "returns the FHIR record" do
341+
expect(perform_request).to be_a FHIR::Immunization
342+
end
343+
344+
context "an error is returned by the api" do
345+
let(:code) { nil }
346+
let(:diagnostics) { nil }
347+
348+
let(:body) do
349+
{
350+
resourceType: "OperationOutcome",
351+
id: "bc2c3c82-4392-4314-9d6b-a7345f82d923",
352+
meta: {
353+
profile: [
354+
"https://simplifier.net/guide/UKCoreDevelopment2/ProfileUKCore-OperationOutcome"
355+
]
356+
},
357+
issue: [
358+
{
359+
severity: "error",
360+
code: "invalid",
361+
details: {
362+
coding: [
363+
{
364+
system: "https://fhir.nhs.uk/Codesystem/http-error-codes",
365+
code:
366+
}
367+
]
368+
},
369+
diagnostics:
370+
}
371+
]
372+
}.to_json
373+
end
374+
375+
include_examples "unexpected response status", 201, "reading"
376+
include_examples "client error (4XX) handling", "reading"
377+
include_examples "generic error handling"
378+
end
379+
end
380+
304381
describe "update immunisations" do
305382
subject(:perform_request) do
306383
described_class.update_immunisation(vaccination_record)

0 commit comments

Comments
 (0)