diff --git a/modules/veteran/app/models/veteran/service/representative.rb b/modules/veteran/app/models/veteran/service/representative.rb index 7a6086b0cd3..43562b166e3 100644 --- a/modules/veteran/app/models/veteran/service/representative.rb +++ b/modules/veteran/app/models/veteran/service/representative.rb @@ -29,10 +29,11 @@ class Representative < ApplicationRecord def self.all_for_user(first_name:, last_name:, middle_initial: nil, poa_code: nil) return [] if first_name.nil? || last_name.nil? - representatives = where('lower(first_name) = ? AND lower(last_name) = ?', first_name&.downcase, - last_name&.downcase) - representatives = representatives.where('? = ANY(poa_codes)', poa_code) if poa_code - representatives.select { |rep| matching_middle_initial(rep, middle_initial) } + representatives = get_representatives(first_name, last_name) + + representatives = representatives&.where('? = ANY(poa_codes)', poa_code) if poa_code + + representatives&.select { |rep| matching_middle_initial(rep, middle_initial) } end # @@ -111,6 +112,29 @@ def diff(rep_data) end end + def self.get_suffixes(first_name, last_name) + first_suffix = first_name.split.last if first_name.split.count > 1 + last_suffix = last_name.split.last if last_name.split.count > 1 + [first_suffix, last_suffix] + end + + def self.get_representatives(first_name, last_name) + suffixes = get_suffixes(first_name, last_name) + + representatives = where('lower(first_name) = ? AND lower(last_name) = ?', first_name&.downcase, + last_name&.downcase) + + if representatives.blank? && suffixes.any? + # check without suffix + first_name = first_name.delete(suffixes[0]).strip if suffixes[0].present? + last_name = last_name.delete(suffixes[1]).strip if suffixes[1].present? + + representatives = where('lower(first_name) = ? AND lower(last_name) = ?', first_name&.downcase, + last_name&.downcase) + end + representatives + end + private # diff --git a/modules/veteran/spec/models/veteran/service/representative_spec.rb b/modules/veteran/spec/models/veteran/service/representative_spec.rb index 8e21010c430..4865a35d7b4 100644 --- a/modules/veteran/spec/models/veteran/service/representative_spec.rb +++ b/modules/veteran/spec/models/veteran/service/representative_spec.rb @@ -69,6 +69,22 @@ def basic_attributes poa_code: '016' )).to eq([]) end + + it 'can find a user with a suffix' do + expect(Veteran::Service::Representative.all_for_user( + first_name: identity.first_name, + last_name: "#{identity.last_name} III", + poa_code: 'A1Q' + ).first.poa_codes).to include('A1Q') + end + + it 'can find a user with a suffix on the first name' do + expect(Veteran::Service::Representative.all_for_user( + first_name: "#{identity.first_name} Esq", + last_name: identity.last_name, + poa_code: 'A1Q' + ).first.poa_codes).to include('A1Q') + end end end