Skip to content

Commit

Permalink
API-41234-rep-validation-include-suffix (#19017)
Browse files Browse the repository at this point in the history
* Adds logic to check with  and without a suffix if it exists. Adds tests.

* WIP: made changes based on PR comments, and now it isn't working; switching branches.

* Adusts specs to changes in represenative.

* Cleanup naming, and check.
  • Loading branch information
stiehlrod authored Oct 24, 2024
1 parent a714a2b commit b0ac007
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
32 changes: 28 additions & 4 deletions modules/veteran/app/models/veteran/service/representative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand Down Expand Up @@ -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

#
Expand Down
16 changes: 16 additions & 0 deletions modules/veteran/spec/models/veteran/service/representative_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b0ac007

Please sign in to comment.