Class: Facilities::MentalHealthReloadJob

Inherits:
Object
  • Object
show all
Includes:
SentryLogging, Sidekiq::Job
Defined in:
app/sidekiq/facilities/mental_health_reload_job.rb

Instance Method Summary collapse

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger

Instance Method Details

#convert_extension(ext) ⇒ Object



39
40
41
42
43
44
45
# File 'app/sidekiq/facilities/mental_health_reload_job.rb', line 39

def convert_extension(ext)
  if %w[NULL 0].include?(ext)
    nil
  else
    ext
  end
end

#fetch_mental_health_dataObject



15
16
17
18
19
20
# File 'app/sidekiq/facilities/mental_health_reload_job.rb', line 15

def fetch_mental_health_data
  mental_file = Rails.root.join('lib', 'facilities', 'mental_health_data', 'mental_health_phone_numbers.csv')
  CSV.read(mental_file, headers: true, quote_char: '|')
rescue => e
  raise MentalHealthDownloadError, "Failed to download mental health data: #{e.cause}"
end

#performObject



60
61
62
# File 'app/sidekiq/facilities/mental_health_reload_job.rb', line 60

def perform
  update_mental_health_data
end

#remove_invalid(record_keys) ⇒ Object



47
48
49
50
# File 'app/sidekiq/facilities/mental_health_reload_job.rb', line 47

def remove_invalid(record_keys)
  invalid = FacilityMentalHealth.keys - record_keys
  invalid.each { |x| FacilityMentalHealth.delete(x) }
end

#update_cache(records) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/sidekiq/facilities/mental_health_reload_job.rb', line 22

def update_cache(records)
  records.each do |r|
    ext = convert_extension(r['Extension'])

    attrs = {
      station_number: r['StationNumber'],
      mh_phone: r['MHPhone'],
      mh_ext: ext,
      modified: r['Modified'],
      local_updated: Time.now.utc.iso8601
    }

    mental_health_record = FacilityMentalHealth.find_or_build(r['StationNumber'])
    mental_health_record.update(attrs)
  end
end

#update_mental_health_dataObject



52
53
54
55
56
57
58
# File 'app/sidekiq/facilities/mental_health_reload_job.rb', line 52

def update_mental_health_data
  records = fetch_mental_health_data
  update_cache(records)
  remove_invalid(records.map { |r| r['StationNumber'] })
rescue Facilities::MentalHealthDownloadError => e
  log_exception_to_sentry(e)
end