@@ -5,8 +5,8 @@ class << self
55 def record_immunisation ( vaccination_record )
66 unless Flipper . enabled? ( :immunisations_fhir_api_integration )
77 Rails . logger . info (
8- "Not syncing vaccination record to immunisations API as the feature " \
9- " flag is disabled: #{ vaccination_record . id } "
8+ "Not recording vaccination record to immunisations API as the" \
9+ " feature flag is disabled: #{ vaccination_record . id } "
1010 )
1111 return
1212 end
@@ -28,13 +28,71 @@ def record_immunisation(vaccination_record)
2828 nhs_immunisations_api_etag : 1
2929 )
3030 else
31- raise "Error syncing vaccination record #{ vaccination_record . id } to" \
31+ raise "Error recording vaccination record #{ vaccination_record . id } to" \
3232 " Immunisations API: unexpected response status" \
3333 " #{ response . status } "
3434 end
3535 rescue Faraday ::ClientError => e
3636 if ( diagnostics = extract_error_diagnostics ( e &.response ) ) . present?
37- raise "Error syncing vaccination record #{ vaccination_record . id } to" \
37+ raise "Error recording vaccination record #{ vaccination_record . id } to" \
38+ " Immunisations API: #{ diagnostics } "
39+ else
40+ raise
41+ end
42+ end
43+
44+ def update_immunisation ( vaccination_record )
45+ unless Flipper . enabled? ( :immunisations_fhir_api_integration )
46+ Rails . logger . info (
47+ "Not updating vaccination record to immunisations API as the" \
48+ " feature flag is disabled: #{ vaccination_record . id } "
49+ )
50+ return
51+ end
52+
53+ if vaccination_record . nhs_immunisations_api_id . blank?
54+ raise "Vaccination record #{ vaccination_record . id } missing NHS Immunisation ID"
55+ end
56+
57+ if vaccination_record . nhs_immunisations_api_etag . blank?
58+ raise "Vaccination record #{ vaccination_record . id } missing ETag"
59+ end
60+
61+ nhs_id = vaccination_record . nhs_immunisations_api_id
62+ response =
63+ NHS ::API . connection . put (
64+ "/immunisation-fhir-api/FHIR/R4/Immunization/#{ nhs_id } " ,
65+ vaccination_record . fhir_record . to_json ,
66+ {
67+ "Content-Type" => "application/fhir+json" ,
68+ "E-Tag" => vaccination_record . nhs_immunisations_api_etag
69+ }
70+ )
71+
72+ if response . status == 200
73+ vaccination_record . update! (
74+ nhs_immunisations_api_synced_at : Time . current ,
75+ # This simplistic approach is based on the assumption that the NHS
76+ # Immunisations API will always simply increment the ETag. Alternative
77+ # approaches are to perform a GET after this POST to get the ETag, or
78+ # to update our record before we do this POST, which would mean we
79+ # don't need to store the ETag at all.
80+ #
81+ # However at this time it's our understanding that the API will always
82+ # increment the ETag, and, practically speaking, it's very unlikely
83+ # that our record would be updated by someone else. Hence this simple
84+ # approach.
85+ nhs_immunisations_api_etag :
86+ vaccination_record . nhs_immunisations_api_etag . to_i + 1
87+ )
88+ else
89+ raise "Error updating vaccination record #{ vaccination_record . id } to" \
90+ " Immunisations API: unexpected response status" \
91+ " #{ response . status } "
92+ end
93+ rescue Faraday ::ClientError => e
94+ if ( diagnostics = extract_error_diagnostics ( e &.response ) ) . present?
95+ raise "Error updating vaccination record #{ vaccination_record . id } to" \
3896 " Immunisations API: #{ diagnostics } "
3997 else
4098 raise
0 commit comments