Skip to content

Commit 7577ff0

Browse files
Create read-imms-api CLI tool
This is a feature which will `read` a vaccination record in the Imms API, and either print out the resulting JSON, or save it to a file. It is possible to either search for an existing `VaccinationRecord` by ID, or by an Imms API ID
1 parent cc3d4a1 commit 7577ff0

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

app/lib/mavis_cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ def self.progress_bar(total)
5757
require_relative "mavis_cli/vaccination_records/sync"
5858
require_relative "mavis_cli/vaccination_records/create_from_fhir"
5959
require_relative "mavis_cli/vaccination_records/search_imms_api"
60+
require_relative "mavis_cli/vaccination_records/read_imms_api"
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# frozen_string_literal: true
2+
3+
module MavisCLI
4+
module VaccinationRecords
5+
class ReadImmsAPI < Dry::CLI::Command
6+
desc "Search vaccination records in NHS Immunisations API for a patient"
7+
8+
option :vaccination_record_id,
9+
required: false,
10+
desc: "Mavis ID of the vaccination record"
11+
option :imms_api_id,
12+
required: false,
13+
desc: "The Imms API ID of the vaccination record"
14+
option :output_file,
15+
required: false,
16+
desc: "File path to save JSON output"
17+
18+
def call(
19+
vaccination_record_id: nil,
20+
imms_api_id: nil,
21+
output_file: nil,
22+
**
23+
)
24+
MavisCLI.load_rails
25+
26+
unless Flipper.enabled?(:imms_api_integration)
27+
puts "Cannot search: Feature flag :imms_api_integration is not enabled"
28+
return
29+
end
30+
31+
if vaccination_record_id.blank? && imms_api_id.blank?
32+
puts "Error: Provide either --vaccination-record-id or --imms-api-id"
33+
return
34+
end
35+
36+
if vaccination_record_id.present? && imms_api_id.present?
37+
puts "Error: Provide only one of --vaccination-record-id or --imms-api-id"
38+
return
39+
end
40+
41+
if imms_api_id.present?
42+
fhir_record =
43+
NHS::ImmunisationsAPI.read_immunisation_by_nhs_immunisations_api_id(
44+
imms_api_id
45+
)
46+
else
47+
vaccination_record =
48+
VaccinationRecord.find_by(id: vaccination_record_id)
49+
if vaccination_record.nil?
50+
puts "Error: Vaccination record with ID #{vaccination_record_id} not found"
51+
return
52+
end
53+
54+
fhir_record =
55+
NHS::ImmunisationsAPI.read_immunisation(vaccination_record)
56+
end
57+
58+
if fhir_record.nil?
59+
puts "No result returned"
60+
return
61+
end
62+
63+
json = fhir_record.to_json
64+
pretty = JSON.pretty_generate(JSON.parse(json))
65+
66+
if output_file && !output_file.strip.empty?
67+
File.write(output_file, pretty)
68+
puts "Saved JSON to #{output_file}"
69+
else
70+
puts pretty
71+
end
72+
end
73+
end
74+
end
75+
76+
register "vaccination-records" do |prefix|
77+
prefix.register "read-imms-api", VaccinationRecords::ReadImmsAPI
78+
end
79+
end

0 commit comments

Comments
 (0)