|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../careplus/client" |
| 4 | + |
| 5 | +module MavisCLI |
| 6 | + module Reports |
| 7 | + class SendToCareplus < Dry::CLI::Command |
| 8 | + desc "Send a CarePlus CSV file to the CarePlus endpoint" |
| 9 | + |
| 10 | + example [ |
| 11 | + "--input=tmp/automated_export.csv", |
| 12 | + "--input=tmp/automated_export.csv --ods_code=ABC123" |
| 13 | + ] |
| 14 | + |
| 15 | + FALLBACK_NAMESPACE = "MOCK" |
| 16 | + FALLBACK_USERNAME = "mavis_user" |
| 17 | + FALLBACK_PASSWORD = "mavis_password" |
| 18 | + |
| 19 | + option :input, required: true, desc: "Path to the CSV file to send" |
| 20 | + option :ods_code, |
| 21 | + desc: "ODS code of the organisation (to use team credentials)" |
| 22 | + option :workgroup, |
| 23 | + desc: |
| 24 | + "Team workgroup (required if the organisation has multiple teams)" |
| 25 | + |
| 26 | + def call(input:, ods_code: nil, workgroup: nil, **) |
| 27 | + MavisCLI.load_rails |
| 28 | + |
| 29 | + unless File.exist?(input) |
| 30 | + warn "File not found: '#{input}'" |
| 31 | + return |
| 32 | + end |
| 33 | + |
| 34 | + username, password, namespace = |
| 35 | + resolve_credentials(ods_code:, workgroup:) |
| 36 | + return if username.nil? |
| 37 | + |
| 38 | + csv_payload = File.read(input) |
| 39 | + |
| 40 | + response = |
| 41 | + Careplus::Client.send_csv( |
| 42 | + username:, |
| 43 | + password:, |
| 44 | + namespace:, |
| 45 | + payload: csv_payload |
| 46 | + ) |
| 47 | + |
| 48 | + if response.is_a?(Net::HTTPSuccess) |
| 49 | + puts "Success (HTTP #{response.code})" |
| 50 | + puts response.body |
| 51 | + else |
| 52 | + warn "Request failed with HTTP #{response.code}: #{response.message}" |
| 53 | + warn response.body |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + private |
| 58 | + |
| 59 | + def resolve_credentials(ods_code:, workgroup:) |
| 60 | + if ods_code.nil? |
| 61 | + return FALLBACK_USERNAME, FALLBACK_PASSWORD, FALLBACK_NAMESPACE |
| 62 | + end |
| 63 | + |
| 64 | + organisation = Organisation.find_by(ods_code:) |
| 65 | + if organisation.nil? |
| 66 | + warn "Could not find organisation with ODS code '#{ods_code}'" |
| 67 | + return nil, nil |
| 68 | + end |
| 69 | + |
| 70 | + teams = organisation.teams |
| 71 | + teams = teams.where(workgroup:) if workgroup |
| 72 | + |
| 73 | + if teams.empty? |
| 74 | + warn( |
| 75 | + if workgroup |
| 76 | + "Could not find team '#{workgroup}' for organisation '#{ods_code}'" |
| 77 | + else |
| 78 | + "Organisation '#{ods_code}' has no teams." |
| 79 | + end |
| 80 | + ) |
| 81 | + return nil, nil, nil |
| 82 | + end |
| 83 | + |
| 84 | + if workgroup.nil? && teams.many? |
| 85 | + warn "Organisation '#{ods_code}' has multiple teams. Specify --workgroup." |
| 86 | + return nil, nil, nil |
| 87 | + end |
| 88 | + |
| 89 | + team = teams.sole |
| 90 | + |
| 91 | + unless team.careplus_username.present? && |
| 92 | + team.careplus_password.present? |
| 93 | + warn "Team '#{team.name}' does not have CarePlus credentials configured." |
| 94 | + return nil, nil, nil |
| 95 | + end |
| 96 | + |
| 97 | + [ |
| 98 | + team.careplus_username, |
| 99 | + team.careplus_password, |
| 100 | + team.careplus_namespace |
| 101 | + ] |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + register "reports" do |prefix| |
| 107 | + prefix.register "send-to-careplus", Reports::SendToCareplus |
| 108 | + end |
| 109 | +end |
0 commit comments