|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module MavisCLI |
| 4 | + module VaccinationRecords |
| 5 | + class SearchImmsAPI < Dry::CLI::Command |
| 6 | + desc "Search vaccination records in NHS Immunisations API for a patient" |
| 7 | + |
| 8 | + argument :patient_id, required: true, desc: "ID of the patient" |
| 9 | + argument :programme_types, |
| 10 | + type: :array, |
| 11 | + required: true, |
| 12 | + desc: "The programme types to search for" |
| 13 | + argument :date_from, required: false, desc: "Start date (YYYY-MM-DD)" |
| 14 | + argument :date_to, required: false, desc: "End date (YYYY-MM-DD)" |
| 15 | + argument :output_file, |
| 16 | + required: false, |
| 17 | + desc: "File path to save JSON output" |
| 18 | + |
| 19 | + def call( |
| 20 | + patient_id:, |
| 21 | + programme_types:, |
| 22 | + date_from: nil, |
| 23 | + date_to: nil, |
| 24 | + output_file: nil, |
| 25 | + ** |
| 26 | + ) |
| 27 | + MavisCLI.load_rails |
| 28 | + |
| 29 | + unless Flipper.enabled?(:immunisations_fhir_api_integration) && |
| 30 | + Flipper.enabled?(:immunisations_fhir_api_integration_search) |
| 31 | + puts "Cannot search: one of the feature flags is disabled " \ |
| 32 | + "(immunisations_fhir_api_integration, immunisations_fhir_api_integration_search)" |
| 33 | + return |
| 34 | + end |
| 35 | + |
| 36 | + patient = Patient.find_by(id: patient_id) |
| 37 | + if patient.nil? |
| 38 | + puts "Error: Patient with ID #{patient_id} not found" |
| 39 | + return |
| 40 | + end |
| 41 | + |
| 42 | + programmes = |
| 43 | + programme_types.map { |type| Programme.find_by(type: type.downcase) } |
| 44 | + if programmes.any?(&:nil?) |
| 45 | + puts "Error: One or more programmes not found in database; " \ |
| 46 | + "available types are: #{Programme.types.keys.join(", ")}" |
| 47 | + return |
| 48 | + end |
| 49 | + |
| 50 | + from_date = parse_date_opt(date_from, "DATE_FROM") |
| 51 | + return if from_date == :error |
| 52 | + to_date = parse_date_opt(date_to, "DATE_TO") |
| 53 | + return if to_date == :error |
| 54 | + |
| 55 | + if from_date && to_date && from_date > to_date |
| 56 | + puts "Error: DATE_FROM cannot be after DATE_TO" |
| 57 | + return |
| 58 | + end |
| 59 | + |
| 60 | + bundle = |
| 61 | + NHS::ImmunisationsAPI.search_immunisations( |
| 62 | + patient, |
| 63 | + programmes: programmes, |
| 64 | + date_from: from_date, |
| 65 | + date_to: to_date |
| 66 | + ) |
| 67 | + |
| 68 | + if bundle.nil? |
| 69 | + puts "No result returned" |
| 70 | + return |
| 71 | + end |
| 72 | + |
| 73 | + json = bundle.to_json |
| 74 | + pretty = JSON.pretty_generate(JSON.parse(json)) |
| 75 | + |
| 76 | + if output_file && !output_file.strip.empty? |
| 77 | + File.write(output_file, pretty) |
| 78 | + puts "Saved JSON to #{output_file}" |
| 79 | + else |
| 80 | + puts pretty |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + private |
| 85 | + |
| 86 | + def parse_date_opt(value, name) |
| 87 | + return nil if value&.strip.blank? |
| 88 | + Date.parse(value) |
| 89 | + rescue ArgumentError |
| 90 | + puts "Error: Invalid #{name} format '#{value}'. Expected YYYY-MM-DD." |
| 91 | + :error |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + register "vaccination-records" do |prefix| |
| 97 | + prefix.register "search-imms-api", VaccinationRecords::SearchImmsAPI |
| 98 | + end |
| 99 | +end |
0 commit comments