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



45
46
47
# File 'lib/bgs/power_of_attorney_verifier.rb', line 45

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

#representatives_with_middle_names_for_user(user) ⇒ Object (private)



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bgs/power_of_attorney_verifier.rb', line 51

def representatives_with_middle_names_for_user(user)
  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:)

  if reps.blank? || reps.count > 1
    reps = Veteran::Service::Representative.all_for_user(first_name: user.first_name,
                                                         last_name: user.last_name,
                                                         poa_code: current_poa_code)
  end
  reps
end

#verify(user) ⇒ Object



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

def verify(user)
  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
      reps = representatives_with_middle_names_for_user(user)
      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