Class: DebtManagementCenter::DebtLetterDownloader

Inherits:
Object
  • Object
show all
Includes:
SentryLogging
Defined in:
lib/debt_management_center/debt_letter_downloader.rb

Constant Summary collapse

DEBTS_DOCUMENT_TYPES =
%w[
  193
  194
  1213
  1214
  1215
  1216
  1217
  1287
  1334
].freeze

Instance Method Summary collapse

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) ⇒ DebtLetterDownloader

Returns a new instance of DebtLetterDownloader.



21
22
23
24
25
26
27
# File 'lib/debt_management_center/debt_letter_downloader.rb', line 21

def initialize(user)
  @user = user
  @client = VBMS::Client.from_env_vars(env_name: Settings.vbms.env)
  @service = debts_service
  @vbms_documents = get_vbms_documents
  verify_no_dependent_debts
end

Instance Method Details

#debts_serviceObject (private)



67
68
69
# File 'lib/debt_management_center/debt_letter_downloader.rb', line 67

def debts_service
  DebtManagementCenter::DebtsService.new(@user)
end

#file_name(document_id) ⇒ Object



37
38
39
40
41
42
# File 'lib/debt_management_center/debt_letter_downloader.rb', line 37

def file_name(document_id)
  verify_letter_in_folder(document_id)

  document = @vbms_documents.detect { |doc| doc.document_id == document_id }
  "#{document.type_description} #{document.upload_date.to_formatted_s(:long)}"
end

#get_letter(document_id) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/debt_management_center/debt_letter_downloader.rb', line 29

def get_letter(document_id)
  verify_letter_in_folder(document_id)

  @client.send_request(
    VBMS::Requests::GetDocumentContent.new(document_id)
  ).content
end

#get_vbms_documentsObject (private)



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

def get_vbms_documents
  @client.send_request(
    VBMS::Requests::FindDocumentVersionReference.new(@service.file_number)
  )
rescue => e
  log_exception_to_sentry(e)
  []
end

#list_lettersObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/debt_management_center/debt_letter_downloader.rb', line 44

def list_letters
  debts_records = @vbms_documents.find_all do |record|
    DEBTS_DOCUMENT_TYPES.include?(record.doc_type)
  end

  debts_records.map do |debts_record|
    debts_record.marshal_dump.slice(
      :document_id, :doc_type, :type_description, :received_at
    )
  end
end

#verify_letter_in_folder(document_id) ⇒ Object (private)



75
76
77
78
79
# File 'lib/debt_management_center/debt_letter_downloader.rb', line 75

def verify_letter_in_folder(document_id)
  raise Common::Exceptions::Unauthorized unless list_letters.any? do |letter|
    letter[:document_id] == document_id
  end
end

#verify_no_dependent_debtsObject (private)



71
72
73
# File 'lib/debt_management_center/debt_letter_downloader.rb', line 71

def verify_no_dependent_debts
  raise Common::Exceptions::Unauthorized if @service.veteran_has_dependent_debts?
end