Class: DebtManagementCenter::DebtsService

Inherits:
BaseService show all
Includes:
RedisCaching
Defined in:
lib/debt_management_center/debts_service.rb

Defined Under Namespace

Classes: DebtNotFound

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.dmc'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#handle_client_error, #handle_error, #init_file_number, #save_error_details, #with_monitoring_and_error_handling

Methods included from Common::Client::Concerns::Monitoring

#increment, #increment_failure, #increment_total, #with_monitoring

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Methods included from SentryLogging

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

Constructor Details

#initialize(user) ⇒ DebtsService

Returns a new instance of DebtsService.



17
18
19
20
21
22
23
24
# File 'lib/debt_management_center/debts_service.rb', line 17

def initialize(user)
  super(user)
  @debts = if Flipper.enabled?(:debts_cache_dmc_empty_response)
             init_cached_debts
           else
             init_debts
           end
end

Instance Attribute Details

#file_numberObject (readonly)

Returns the value of attribute file_number.



10
11
12
# File 'lib/debt_management_center/debts_service.rb', line 10

def file_number
  @file_number
end

Instance Method Details

#add_debts_to_redisObject (private)



110
111
112
113
114
115
# File 'lib/debt_management_center/debts_service.rb', line 110

def add_debts_to_redis
  debts = @debts.map { |d| d['id'] = SecureRandom.uuid }
  debt_params = { REDIS_CONFIG[:debt][:namespace] => user.uuid }
  debt_store = DebtManagementCenter::DebtStore.new(debt_params)
  debt_store.update(uuid: user.uuid, debts:)
end

#build_composite_debt_id(debt) ⇒ Object (private)



66
67
68
# File 'lib/debt_management_center/debts_service.rb', line 66

def build_composite_debt_id(debt)
  "#{debt['deductionCode']}#{debt['originalAR'].to_i}"
end

#debts_with_sorted_historiesObject (private)



58
59
60
61
62
63
64
# File 'lib/debt_management_center/debts_service.rb', line 58

def debts_with_sorted_histories
  @debts.select do |debt|
    debt['debtHistory'] = sort_by_date(debt['debtHistory'])
    debt['compositeDebtId'] = build_composite_debt_id(debt)
    debt['payeeNumber'] == '00'
  end
end

#get_debt_by_id(id) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/debt_management_center/debts_service.rb', line 39

def get_debt_by_id(id)
  debt_store = DebtManagementCenter::DebtStore.find(@user.uuid)

  raise DebtNotFound if debt_store.blank?

  debt = debt_store.get_debt(id)
  StatsD.increment("#{STATSD_KEY_PREFIX}.get_debt.success")
  debt
rescue => e
  StatsD.increment("#{STATSD_KEY_PREFIX}.get_debt.failure")
  raise e
end

#get_debtsObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/debt_management_center/debts_service.rb', line 26

def get_debts
  has_dependent_debts = veteran_has_dependent_debts?
  debts = debts_with_sorted_histories
  StatsD.increment("#{STATSD_KEY_PREFIX}.get_debts.success")
  {
    has_dependent_debts:,
    debts:
  }
rescue => e
  StatsD.increment("#{STATSD_KEY_PREFIX}.get_debts.failure")
  raise e
end

#init_cached_debtsObject (private)



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/debt_management_center/debts_service.rb', line 83

def init_cached_debts
  StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_debts.fired")

  with_monitoring_and_error_handling do
    cache_key = "debts_data_#{@user.uuid}"
    cached_response = Rails.cache.read(cache_key)

    if cached_response
      StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_debts.cached_response_returned")
      return DebtManagementCenter::DebtsResponse.new(cached_response).debts
    end

    options = { timeout: 30 }
    response = perform(
      :post, Settings.dmc.debts_endpoint, { fileNumber: @file_number }, nil, options
    ).body

    if response.is_a?(Array) && response.empty?
      # DMC refreshes DB at 5am every morning
      Rails.cache.write(cache_key, response, expires_in: self.class.time_until_5am_utc)
      StatsD.increment("#{STATSD_KEY_PREFIX}.init_cached_debts.empty_response_cached")
    end

    DebtManagementCenter::DebtsResponse.new(response).debts
  end
end

#init_debtsObject (private)

Provided the cached version of this continues to work as intended, this method is not needed 8/5/24



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/debt_management_center/debts_service.rb', line 71

def init_debts
  with_monitoring_and_error_handling do
    Rails.logger.info('DebtManagement - DebtService#init_debts')
    options = { timeout: 30 }
    DebtManagementCenter::DebtsResponse.new(
      perform(
        :post, Settings.dmc.debts_endpoint, { fileNumber: @file_number }, nil, options
      ).body
    ).debts
  end
end

#sort_by_date(debt_history) ⇒ Object (private)



117
118
119
# File 'lib/debt_management_center/debts_service.rb', line 117

def sort_by_date(debt_history)
  debt_history.sort_by { |d| Date.strptime(d['date'], '%m/%d/%Y') }.reverse
end

#veteran_has_dependent_debts?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/debt_management_center/debts_service.rb', line 52

def veteran_has_dependent_debts?
  @debts.any? { |debt| debt['payeeNumber'] != '00' }
end