|
| 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