@@ -23,9 +23,7 @@ class Form526Submission < ApplicationRecord
2323 :submit_flashes ,
2424 :poll_form526_pdf ,
2525 :cleanup ,
26- additional_class_logs : {
27- action : 'Begin as anciliary 526 submission'
28- } ,
26+ additional_class_logs : { action : 'Begin as anciliary 526 submission' } ,
2927 additional_instance_logs : {
3028 saved_claim_id : %i[ saved_claim id ] ,
3129 user_uuid : %i[ user_uuid ]
@@ -54,13 +52,11 @@ class Form526Submission < ApplicationRecord
5452 has_kms_key
5553 has_encrypted :auth_headers_json , :birls_ids_tried , :form_json , key : :kms_key , **lockbox_options
5654
57- belongs_to :saved_claim ,
58- class_name : 'SavedClaim::DisabilityCompensation' ,
59- inverse_of : false
55+ belongs_to :saved_claim , class_name : 'SavedClaim::DisabilityCompensation' , inverse_of : false
6056
6157 has_many :form526_job_statuses , dependent : :destroy
6258 has_many :form526_submission_remediations , dependent : :destroy
63- belongs_to :user_account , dependent : nil , optional : true
59+ belongs_to :user_account , dependent : nil
6460
6561 validates ( :auth_headers_json , presence : true )
6662 enum :backup_submitted_claim_status , { accepted : 0 , rejected : 1 , paranoid_success : 2 }
@@ -163,9 +159,7 @@ def submit_with_birls_id_that_hasnt_been_tried_yet!(
163159
164160 # Note that the User record is cached in Redis -- `User.redis_namespace_ttl`
165161 def get_first_name
166- user = User . find ( user_uuid )
167- user &.first_name &.upcase . presence ||
168- auth_headers &.dig ( 'va_eauth_firstName' ) &.upcase
162+ user &.first_name &.upcase . presence || auth_headers &.dig ( 'va_eauth_firstName' ) &.upcase
169163 end
170164
171165 # Checks against the User record first, and then resorts to checking the auth_headers
@@ -174,15 +168,13 @@ def get_first_name
174168 # @return [Hash] of the user's full name (first, middle, last, suffix)
175169 #
176170 def full_name
177- name_hash = User . find ( user_uuid ) &.full_name_normalized
171+ name_hash = user &.full_name_normalized
178172 return name_hash if name_hash &.[]( :first ) . present?
179173
180- {
181- first : auth_headers &.dig ( 'va_eauth_firstName' ) &.capitalize ,
174+ { first : auth_headers &.dig ( 'va_eauth_firstName' ) &.capitalize ,
182175 middle : nil ,
183176 last : auth_headers &.dig ( 'va_eauth_lastName' ) &.capitalize ,
184- suffix : nil
185- }
177+ suffix : nil }
186178 end
187179
188180 # form_json is memoized here so call invalidate_form_hash after updating form_json
@@ -468,22 +460,11 @@ def last_remediation
468460 end
469461
470462 def account
471- # first, check for an ICN on the UserAccount associated to the submission, return it if found
472- account = user_account
473- return account if account &.icn . present?
474-
475- # next, check past submissions for different UserAccounts that might have ICNs
476- past_submissions = get_past_submissions
477- account = find_user_account_with_icn ( past_submissions , 'past submissions' )
478- return account if account . present? && account . icn . present?
479-
480- # next, check for any historical UserAccounts for that user which might have an ICN
481- user_verifications = get_user_verifications
482- account = find_user_account_with_icn ( user_verifications , 'user verifications' )
483- return account if account . present? && account . icn . present?
463+ return user_account if user_account &.icn . present?
484464
485- # failing all the above, default to an Account lookup
486- Account . lookup_by_user_uuid ( user_uuid )
465+ Rails . logger . info ( 'Form526Submission::account - no UserAccount ICN found' , log_payload )
466+ # query MPI by EDIPI first & attributes second for user ICN, return in OpenStruct
467+ get_icn_from_mpi
487468 end
488469
489470 # Send the Submitted Email - when the Veteran has clicked the "submit" button in va.gov
@@ -493,8 +474,7 @@ def account
493474 # @param invoker: string where the Received Email trigger is being called from
494475 def send_submitted_email ( invoker )
495476 if Flipper . enabled? ( :disability_526_send_form526_submitted_email )
496- Rails . logger . info ( "Form526SubmittedEmailJob called for user #{ user_uuid } ,
497- submission: #{ id } from #{ invoker } " )
477+ Rails . logger . info ( "Form526SubmittedEmailJob called for user #{ user_uuid } , submission: #{ id } from #{ invoker } " )
498478 first_name = get_first_name
499479 params = personalization_parameters ( first_name )
500480 Form526SubmittedEmailJob . perform_async ( params )
@@ -506,8 +486,7 @@ def send_submitted_email(invoker)
506486 # Backup Path: when Form526StatusPollingJob reaches "paranoid_success" status
507487 # @param invoker: string where the Received Email trigger is being called from
508488 def send_received_email ( invoker )
509- Rails . logger . info ( "Form526ConfirmationEmailJob called for user #{ user_uuid } ,
510- submission: #{ id } from #{ invoker } " )
489+ Rails . logger . info ( "Form526ConfirmationEmailJob called for user #{ user_uuid } , submission: #{ id } from #{ invoker } " )
511490 first_name = get_first_name
512491 params = personalization_parameters ( first_name )
513492 Form526ConfirmationEmailJob . perform_async ( params )
@@ -612,7 +591,6 @@ def submit_form_8940
612591 end
613592
614593 def submit_flashes
615- user = User . find ( user_uuid )
616594 # Note that the User record is cached in Redis -- `User.redis_namespace_ttl`
617595 # If this method runs after the TTL, then the flashes will not be applied -- a possible bug.
618596 BGS ::FlashUpdater . perform_async ( id ) if user && Flipper . enabled? ( :disability_compensation_flashes , user )
@@ -630,27 +608,51 @@ def cleanup
630608 EVSS ::DisabilityCompensationForm ::SubmitForm526Cleanup . perform_async ( id )
631609 end
632610
633- def find_user_account_with_icn ( records , record_type )
634- records . pluck ( :user_account_id ) . uniq . each do |user_account_id |
635- user_account = UserAccount . find ( user_account_id )
636- next if user_account &.icn . blank?
637-
638- Rails . logger . info ( "ICN not found on submission #{ id } , " \
639- "using ICN for user account #{ user_account_id } instead (based on #{ record_type } )" )
640- return user_account
611+ def get_icn_from_mpi
612+ edipi_response_profile = edipi_mpi_profile_query ( auth_headers [ 'va_eauth_dodedipnid' ] )
613+ if edipi_response_profile &.icn . present?
614+ OpenStruct . new ( icn : edipi_response_profile . icn )
615+ else
616+ Rails . logger . info ( 'Form526Submission::account - unable to look up MPI profile with EDIPI' , log_payload )
617+ attributes_response_profile = attributes_mpi_profile_query ( auth_headers )
618+ if attributes_response_profile &.icn . present?
619+ OpenStruct . new ( icn : attributes_response_profile . icn )
620+ else
621+ Rails . logger . info ( 'Form526Submission::account - no ICN present' , log_payload )
622+ OpenStruct . new ( icn : nil )
623+ end
641624 end
642625 end
643626
644- def get_past_submissions
645- Form526Submission . where ( user_uuid :) . where . not ( user_account_id :)
627+ def edipi_mpi_profile_query ( edipi )
628+ return unless edipi
629+
630+ edipi_response = mpi_service . find_profile_by_edipi ( edipi :)
631+ edipi_response . profile if edipi_response . ok? && edipi_response . profile . icn . present?
632+ end
633+
634+ def attributes_mpi_profile_query ( auth_headers )
635+ required_attributes = %w[ va_eauth_firstName va_eauth_lastName va_eauth_birthdate va_eauth_pnid ]
636+ return unless required_attributes . all? { |attr | auth_headers [ attr ] . present? }
637+
638+ attributes_response = mpi_service . find_profile_by_attributes (
639+ first_name : auth_headers [ 'va_eauth_firstName' ] ,
640+ last_name : auth_headers [ 'va_eauth_lastName' ] ,
641+ birth_date : auth_headers [ 'va_eauth_birthdate' ] &.to_date . to_s ,
642+ ssn : auth_headers [ 'va_eauth_pnid' ]
643+ )
644+ attributes_response . profile if attributes_response . ok? && attributes_response . profile . icn . present?
645+ end
646+
647+ def log_payload
648+ @log_payload ||= { user_uuid :, submission_id : id }
649+ end
650+
651+ def mpi_service
652+ @mpi_service ||= MPI ::Service . new
646653 end
647654
648- def get_user_verifications
649- UserVerification . where ( idme_uuid : user_uuid )
650- . or ( UserVerification . where ( backing_idme_uuid : user_uuid ) )
651- . or ( UserVerification . where ( logingov_uuid : user_uuid ) )
652- . or ( UserVerification . where ( mhv_uuid : user_uuid ) )
653- . or ( UserVerification . where ( dslogon_uuid : user_uuid ) )
654- . where . not ( user_account_id :)
655+ def user
656+ @user ||= User . find ( user_uuid )
655657 end
656658end
0 commit comments