Skip to content

Commit

Permalink
Fix how we query MPI Profile for first name (#18752)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrillberg authored Oct 4, 2024
1 parent bfd5aad commit ae6c512
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def enqueue_email(at, template_id, data)
# async job and we have a UserAccount
if user_account
data[:personalization]['first_name'] = get_first_name
return if data[:personalization]['first_name'].blank?

VANotify::UserAccountJob.perform_at(
at,
user_account.id,
Expand All @@ -107,7 +109,7 @@ def enqueue_email(at, template_id, data)
)
# async job and we don't have a UserAccount but form data should include email
else
return if data[:email].blank?
return if data[:email].blank? || data[:personalization]['first_name'].blank?

VANotify::EmailJob.perform_at(
at,
Expand All @@ -121,14 +123,16 @@ def enqueue_email(at, template_id, data)
def send_email_now(template_id, data)
# sync job and we have a User
if user
return if data[:personalization]['first_name'].blank?

VANotify::EmailJob.perform_async(
user.va_profile_email,
template_id,
data[:personalization]
)
# sync job and form data should include email
else
return if data[:email].blank?
return if data[:email].blank? || data[:personalization]['first_name'].blank?

VANotify::EmailJob.perform_async(
data[:email],
Expand All @@ -140,17 +144,21 @@ def send_email_now(template_id, data)

def get_first_name
if user_account
mpi_profile = MPI::Service.new.find_profile_by_identifier(identifier_type: 'ICN', identifier: user_account.icn)
if mpi_profile
raise mpi_profile.error if mpi_profile.error
raise 'First name not found in MPI profile' unless mpi_profile.first_name
mpi_response = MPI::Service.new.find_profile_by_identifier(identifier_type: 'ICN', identifier: user_account.icn)
if mpi_response
error = mpi_response.error
Rails.logger.error('MPI response error', { error: }) if error

first_name = mpi_response.profile&.given_names&.first
Rails.logger.error('MPI profile missing first_name') unless first_name

mpi_profile.first_name
first_name
end
elsif user
raise 'First name not found in user profile' unless user.first_name
first_name = user.first_name
Rails.logger.error('First name not found in user profile') unless first_name

user.first_name
first_name
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@

it 'sends the email at the specified time' do
time = double
mpi_profile = double(first_name: double, error: nil)
profile = double(given_names: [double])
mpi_profile = double(profile:, error: nil)
allow(VANotify::UserAccountJob).to receive(:perform_at)
allow_any_instance_of(MPI::Service).to receive(:find_profile_by_identifier).and_return(mpi_profile)
subject = described_class.new(config, notification_type:, user_account:)
Expand Down

0 comments on commit ae6c512

Please sign in to comment.