Skip to content

Commit b1a9976

Browse files
committed
Record info when syncing vaccination record
This info will be used for performing updates and deletes later. Jira-Issue: MAV-1477
1 parent 119557d commit b1a9976

3 files changed

Lines changed: 140 additions & 47 deletions

File tree

app/lib/nhs/immunisations_api.rb

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,27 @@
33
module NHS::ImmunisationsAPI
44
class << self
55
def record_immunisation(vaccination_record)
6-
NHS::API.connection.post(
7-
"/immunisation-fhir-api/FHIR/R4/Immunization",
8-
vaccination_record.fhir_record.to_json,
9-
"Content-Type" => "application/fhir+json"
10-
)
6+
response =
7+
NHS::API.connection.post(
8+
"/immunisation-fhir-api/FHIR/R4/Immunization",
9+
vaccination_record.fhir_record.to_json,
10+
"Content-Type" => "application/fhir+json"
11+
)
12+
13+
if response.status == 201
14+
vaccination_record.update!(
15+
nhs_immunisations_api_id:
16+
extract_nhs_id(response.headers.fetch("location")),
17+
nhs_immunisations_api_synced_at: Time.current,
18+
# We would normally retrieve this from the API response, but the NHS
19+
# Immunisations API does not return this to us, yet.
20+
nhs_immunisations_api_etag: 1
21+
)
22+
else
23+
raise "Error syncing vaccination record #{vaccination_record.id} to" \
24+
" Immunisations API: unexpected response status" \
25+
" #{response.status}"
26+
end
1127
rescue Faraday::ClientError => e
1228
if (diagnostics = extract_error_diagnostics(e&.response)).present?
1329
raise "Error syncing vaccination #{vaccination_record.id} record to" \
@@ -32,5 +48,13 @@ def extract_error_diagnostics(response)
3248
nil
3349
end
3450
end
51+
52+
def extract_nhs_id(location)
53+
if (match = location.match(%r{Immunization/([a-f0-9-]+)}))
54+
match[1]
55+
else
56+
raise UnrecognisedLocation, location
57+
end
58+
end
3559
end
3660
end

spec/features/cli_vaccination_records_sync_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ def and_the_nhs_api_is_available
7070
"Content-Type" => "application/fhir+json",
7171
"Accept" => "application/fhir+json"
7272
}
73-
).to_return(status: 200, body: "", headers: {})
73+
).to_return(
74+
status: 201,
75+
body: "",
76+
headers: {
77+
location:
78+
"https://sandbox.api.service.nhs.uk/immunisation-fhir-api/Immunization/11112222-3333-4444-5555-666677778888"
79+
}
80+
)
7481
end
7582

7683
def when_i_run_the_sync_command

spec/lib/nhs/immunisations_api_spec.rb

Lines changed: 103 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -49,66 +49,128 @@
4949
end
5050

5151
describe "record_immunisation" do
52+
before do
53+
stub_request(
54+
:post,
55+
"https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization"
56+
).to_return(
57+
status: 201,
58+
body: "",
59+
headers: {
60+
location:
61+
"https://sandbox.api.service.nhs.uk/immunisation-fhir-api/Immunization/ffff1111-eeee-2222-dddd-3333eeee4444"
62+
}
63+
)
64+
end
65+
5266
it "sends the correct JSON payload" do
5367
expected_body =
5468
File.read(Rails.root.join("spec/fixtures/fhir/immunisation.json")).chomp
5569

5670
# stree-ignore
5771
stubbed_request =
5872
stub_request(
59-
:post,
60-
"https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization"
73+
:post, "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization"
6174
)
6275
.with { |request|
63-
expect(request.headers["Accept"]).to eq "application/fhir+json"
64-
expect(
65-
request.headers["Content-Type"]
66-
).to eq "application/fhir+json"
67-
expect(request.body).to eq expected_body
68-
true
69-
}
70-
.to_return(status: 200, body: "", headers: {})
76+
expect(request.headers["Accept"]).to eq "application/fhir+json"
77+
expect(
78+
request.headers["Content-Type"]
79+
).to eq "application/fhir+json"
80+
expect(request.body).to eq expected_body
81+
true
82+
}
83+
.to_return(status: 201,
84+
body: "",
85+
headers: {
86+
location:
87+
"https://sandbox.api.service.nhs.uk/immunisation-fhir-api/Immunization/ffff1111-eeee-2222-dddd-3333eeee4444"
88+
})
7189

7290
described_class.record_immunisation(vaccination_record)
7391

7492
expect(stubbed_request).to have_been_made
7593
end
7694

95+
it "stores the id from the response" do
96+
described_class.record_immunisation(vaccination_record)
97+
98+
expect(
99+
vaccination_record.nhs_immunisations_api_id
100+
).to eq "ffff1111-eeee-2222-dddd-3333eeee4444"
101+
end
102+
103+
it "stores the nhs_immunisations_api_synced_at from the response" do
104+
freeze_time do
105+
described_class.record_immunisation(vaccination_record)
106+
107+
expect(
108+
vaccination_record.nhs_immunisations_api_synced_at
109+
).to eq Time.current
110+
end
111+
end
112+
113+
it "initialises the etag to 1" do
114+
described_class.record_immunisation(vaccination_record)
115+
116+
expect(vaccination_record.nhs_immunisations_api_etag).to eq "1"
117+
end
118+
77119
context "an error is returned by the api" do
78-
context "4XX error" do
79-
let(:response) do
80-
{
81-
resourceType: "OperationOutcome",
82-
id: "bc2c3c82-4392-4314-9d6b-a7345f82d923",
83-
meta: {
84-
profile: [
85-
"https://simplifier.net/guide/UKCoreDevelopment2/ProfileUKCore-OperationOutcome"
86-
]
87-
},
88-
issue: [
89-
{
90-
severity: "error",
91-
code: "invalid",
92-
details: {
93-
coding: [
94-
{
95-
system: "https://fhir.nhs.uk/Codesystem/http-error-codes",
96-
code: "NOT-FOUND"
97-
}
98-
]
99-
},
100-
diagnostics: "Invalid patient ID"
101-
}
120+
before do
121+
stub_request(
122+
:post,
123+
"https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization"
124+
).to_return(status: status, body: response, headers: {})
125+
end
126+
127+
let(:status) { 201 }
128+
let(:code) { nil }
129+
let(:diagnostics) { nil }
130+
131+
let(:response) do
132+
{
133+
resourceType: "OperationOutcome",
134+
id: "bc2c3c82-4392-4314-9d6b-a7345f82d923",
135+
meta: {
136+
profile: [
137+
"https://simplifier.net/guide/UKCoreDevelopment2/ProfileUKCore-OperationOutcome"
102138
]
103-
}.to_json
104-
end
139+
},
140+
issue: [
141+
{
142+
severity: "error",
143+
code: "invalid",
144+
details: {
145+
coding: [
146+
{
147+
system: "https://fhir.nhs.uk/Codesystem/http-error-codes",
148+
code:
149+
}
150+
]
151+
},
152+
diagnostics:
153+
}
154+
]
155+
}.to_json
156+
end
105157

106-
before do
107-
stub_request(
108-
:post,
109-
"https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization"
110-
).to_return(status: 404, body: response, headers: {})
158+
context "unexpected response status" do
159+
let(:status) { 200 }
160+
let(:response) { "" }
161+
162+
it "raises an error saying the response is unexpected" do
163+
expect {
164+
described_class.record_immunisation(vaccination_record)
165+
}.to raise_error(
166+
"Error syncing vaccination record #{vaccination_record.id} to" \
167+
" Immunisations API: unexpected response status 200"
168+
)
111169
end
170+
end
171+
172+
context "4XX error" do
173+
let(:status) { 404 }
112174

113175
it "raises an error with the diagnostic message" do
114176
expect {

0 commit comments

Comments
 (0)