From ae6c51219a1c4ea27a3671b7655c144ebef87dbb Mon Sep 17 00:00:00 2001 From: Eric Tillberg Date: Fri, 4 Oct 2024 16:27:11 -0400 Subject: [PATCH] Fix how we query MPI Profile for first name (#18752) --- .../simple_forms_api/notification_email.rb | 26 ++++++++++++------- .../spec/services/notification_email_spec.rb | 3 ++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/modules/simple_forms_api/app/services/simple_forms_api/notification_email.rb b/modules/simple_forms_api/app/services/simple_forms_api/notification_email.rb index 2f140cd1d16..d75fe2b98c9 100644 --- a/modules/simple_forms_api/app/services/simple_forms_api/notification_email.rb +++ b/modules/simple_forms_api/app/services/simple_forms_api/notification_email.rb @@ -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, @@ -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, @@ -121,6 +123,8 @@ 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, @@ -128,7 +132,7 @@ def send_email_now(template_id, data) ) # 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], @@ -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 diff --git a/modules/simple_forms_api/spec/services/notification_email_spec.rb b/modules/simple_forms_api/spec/services/notification_email_spec.rb index 2658e2a2a6c..5e85dafbadb 100644 --- a/modules/simple_forms_api/spec/services/notification_email_spec.rb +++ b/modules/simple_forms_api/spec/services/notification_email_spec.rb @@ -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:)