Class: BGS::PowerOfAttorneyVerifier

Inherits:
Object
  • Object
show all
Defined in:
lib/bgs/power_of_attorney_verifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ PowerOfAttorneyVerifier

Returns a new instance of PowerOfAttorneyVerifier.



5
6
7
8
# File 'lib/bgs/power_of_attorney_verifier.rb', line 5

def initialize(user)
  @user = user
  @veteran = Veteran::User.new(@user)
end

Instance Method Details

#current_poaObject



10
11
12
# File 'lib/bgs/power_of_attorney_verifier.rb', line 10

def current_poa
  @current_poa ||= @veteran.power_of_attorney
end

#current_poa_codeObject



14
15
16
# File 'lib/bgs/power_of_attorney_verifier.rb', line 14

def current_poa_code
  current_poa.try(:code)
end

#matches(veteran_poa_code, representative) ⇒ Object



49
50
51
# File 'lib/bgs/power_of_attorney_verifier.rb', line 49

def matches(veteran_poa_code, representative)
  representative.poa_codes.include?(veteran_poa_code)
end

#previous_poa_codeObject



18
19
20
# File 'lib/bgs/power_of_attorney_verifier.rb', line 18

def previous_poa_code
  @previous_poa_code ||= @veteran.previous_power_of_attorney.try(:code)
end

#verify(user) ⇒ Object

rubocop:disable Metrics/MethodLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bgs/power_of_attorney_verifier.rb', line 22

def verify(user) # rubocop:disable Metrics/MethodLength
  reps = Veteran::Service::Representative.all_for_user(first_name: user.first_name,
                                                       last_name: user.last_name)
  raise ::Common::Exceptions::Unauthorized, detail: 'VSO Representative Not Found' if reps.blank?

  if reps.count > 1
    if user.middle_name.blank?
      raise ::Common::Exceptions::Unauthorized, detail: 'Ambiguous VSO Representative Results'
    else
      middle_initial = user.middle_name[0]
      reps = Veteran::Service::Representative.all_for_user(first_name: user.first_name,
                                                           last_name: user.last_name,
                                                           middle_initial:)

      raise ::Common::Exceptions::Unauthorized, detail: 'VSO Representative Not Found' if reps.blank?
      raise ::Common::Exceptions::Unauthorized, detail: 'Ambiguous VSO Representative Results' if reps.count > 1
    end
  end

  rep = reps.first
  veteran_poa_code = current_poa_code
  unless matches(veteran_poa_code, rep)
    Rails.logger.info("POA code of #{rep.poa_codes.join(', ')} not valid for veteran code #{veteran_poa_code}")
    raise ::Common::Exceptions::Unauthorized, detail: "Power of Attorney code doesn't match Veteran's"
  end
end