Skip to content

Commit 45c50e5

Browse files
Merge pull request #4336 from nhsuk/vaccination-from-fhir-record-cli
Add CLI command `create-from-fhir`
2 parents c0bf129 + 65d9ebe commit 45c50e5

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

app/lib/mavis_cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ def self.progress_bar(total)
5454
require_relative "mavis_cli/users/create"
5555
require_relative "mavis_cli/vaccination_records/generate_fhir"
5656
require_relative "mavis_cli/vaccination_records/sync"
57+
require_relative "mavis_cli/vaccination_records/create_from_fhir"
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# frozen_string_literal: true
2+
3+
module MavisCLI
4+
module VaccinationRecords
5+
class CreateFromFHIR < Dry::CLI::Command
6+
desc "Create a vaccination record from a FHIR Immunization JSON"
7+
8+
argument :patient_id,
9+
required: true,
10+
desc: "ID of the patient to attach the vaccination record to"
11+
argument :team_id,
12+
required: false,
13+
desc: "ID of the team (optional, used for batch association)"
14+
15+
option :file,
16+
type: :string,
17+
required: false,
18+
desc: "Path to a file containing the FHIR Immunization JSON"
19+
option :json,
20+
type: :string,
21+
required: false,
22+
desc: "Raw FHIR Immunization JSON string"
23+
24+
def call(patient_id:, team_id: nil, file: nil, json: nil, **)
25+
MavisCLI.load_rails
26+
27+
unless Flipper.enabled?(:immunisations_fhir_api_integration)
28+
puts "Error: Feature flag :immunisations_fhir_api_integration is not enabled"
29+
return
30+
end
31+
32+
unless Flipper.enabled?(:immunisations_fhir_api_integration_search)
33+
puts "Error: Feature flag :immunisations_fhir_api_integration_search is not enabled"
34+
return
35+
end
36+
37+
if file.blank? && json.blank?
38+
puts "Error: Provide either --file PATH or --json STRING"
39+
return
40+
end
41+
42+
if file.present? && json.present?
43+
puts "Error: Provide only one of --file or --json"
44+
return
45+
end
46+
47+
patient = Patient.find_by(id: patient_id)
48+
if patient.nil?
49+
puts "Error: Patient with ID #{patient_id} not found"
50+
return
51+
end
52+
53+
team = team_id.present? ? Team.find_by(id: team_id) : nil
54+
if team_id.present? && team.nil?
55+
puts "Error: Team with ID #{team_id} not found"
56+
return
57+
end
58+
59+
begin
60+
payload = file ? File.read(file) : json
61+
data = JSON.parse(payload)
62+
rescue Errno::ENOENT
63+
puts "Error: File not found: #{file}"
64+
return
65+
rescue JSON::ParserError => e
66+
puts "Error: Invalid JSON - #{e.message}"
67+
return
68+
end
69+
70+
begin
71+
fhir_record = FHIR::Immunization.new(data)
72+
rescue StandardError => e
73+
puts "Error: Could not build FHIR::Immunization - #{e.message}"
74+
return
75+
end
76+
77+
begin
78+
record =
79+
FHIRMapper::VaccinationRecord.from_fhir_record(
80+
fhir_record,
81+
patient: patient,
82+
team: team
83+
)
84+
85+
record.save!
86+
puts "Created vaccination record #{record.id}"
87+
rescue ActiveRecord::RecordInvalid => e
88+
puts "Error: Could not save VaccinationRecord - #{e.record.errors.full_messages.join(", ")}"
89+
rescue StandardError => e
90+
puts "Error: #{e.message}"
91+
end
92+
end
93+
end
94+
end
95+
96+
register "vaccination-records" do |prefix|
97+
prefix.register "create-from-fhir", VaccinationRecords::CreateFromFHIR
98+
end
99+
end

0 commit comments

Comments
 (0)