From fd06d7b071bfca4cb1651d190bddb93b8129a97a Mon Sep 17 00:00:00 2001 From: stevenjcumming <134282106+stevenjcumming@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:57:52 -0400 Subject: [PATCH] resolve EVSS::DisabilityCompensationForm::SubmitForm526 Metrics/MethodLength --- .../submit_form526.rb | 114 +++++++++--------- 1 file changed, 59 insertions(+), 55 deletions(-) diff --git a/app/sidekiq/evss/disability_compensation_form/submit_form526.rb b/app/sidekiq/evss/disability_compensation_form/submit_form526.rb index 27a9cf8ac4b..35b3cc14336 100644 --- a/app/sidekiq/evss/disability_compensation_form/submit_form526.rb +++ b/app/sidekiq/evss/disability_compensation_form/submit_form526.rb @@ -76,23 +76,11 @@ class SubmitForm526 < Job # # @param submission_id [Integer] The {Form526Submission} id # - def perform(submission_id) # rubocop:disable Metrics/MethodLength - send_notifications = true - @submission_id = submission_id - + def perform(submission_id) Sentry.set_tags(source: '526EZ-all-claims') super(submission_id) - if Flipper.enabled?(:disability_compensation_fail_submission, - OpenStruct.new({ flipper_id: submission.user_uuid })) - with_tracking('Form526 Submission', submission.saved_claim_id, submission.id, submission.bdd?) do - Rails.logger.info("disability_compensation_fail_submission enabled for submission #{submission.id}") - throw StandardError - rescue => e - handle_errors(submission, e) - return - end - end + return if fail_submission_feature_enabled?(submission) # This instantiates the service as defined by the inheriting object # TODO: this meaningless variable assignment is required for the specs to pass, which @@ -103,62 +91,73 @@ def perform(submission_id) # rubocop:disable Metrics/MethodLength with_tracking('Form526 Submission', submission.saved_claim_id, submission.id, submission.bdd?) do submission.mark_birls_id_as_tried! - begin - submission.prepare_for_evss! - rescue => e - handle_errors(submission, e) - return - end - - user_account = UserAccount.find_by(id: submission.user_account_id) || - Account.lookup_by_user_uuid(submission.user_uuid) + return unless successfully_prepare_submission_for_evss?(submission) begin # send submission data to either EVSS or Lighthouse (LH) response = if submission.claims_api? # not needed once fully migrated to LH - # submit 526 through LH API - # 1. get user's ICN - icn = user_account.icn - # 2. transform submission data to LH format - transform_service = EVSS::DisabilityCompensationForm::Form526ToLighthouseTransform.new - body = transform_service.transform(submission.form['form526']) - # 3. send transformed submission data to LH endpoint - service = BenefitsClaims::Service.new(icn) - raw_response = service.submit526(body) - raw_response_body = if raw_response.body.is_a? String - JSON.parse(raw_response.body) - else - raw_response.body - end - # 4. convert LH raw response to a FormSubmitResponse for further processing (claim_id, status) - # parse claimId from LH response - submitted_claim_id = raw_response_body.dig('data', 'attributes', 'claimId').to_i - raw_response_struct = OpenStruct.new({ - body: { claim_id: submitted_claim_id }, - status: raw_response.status - }) - EVSS::DisabilityCompensationForm::FormSubmitResponse - .new(raw_response_struct.status, raw_response_struct) + send_submission_data_to_lighthouse(submission, submission_account(submission).icn) else service.submit_form526(submission.form_to_json(Form526Submission::FORM_526)) end response_handler(response) + send_post_evss_notifications(submission, true) rescue => e - send_notifications = false + send_post_evss_notifications(submission, false) handle_errors(submission, e) end + end + end - if Flipper.enabled?(:disability_compensation_production_tester, - OpenStruct.new({ flipper_id: submission.user_uuid })) - Rails.logger.info("send_post_evss_notifications call skipped for submission #{submission.id}") - elsif send_notifications - send_post_evss_notifications(submission) + private + + def fail_submission_feature_enabled?(submission) + if Flipper.enabled?(:disability_compensation_fail_submission, + OpenStruct.new({ flipper_id: submission.user_uuid })) + with_tracking('Form526 Submission', submission.saved_claim_id, submission.id, submission.bdd?) do + Rails.logger.info("disability_compensation_fail_submission enabled for submission #{submission.id}") + throw StandardError + rescue => e + handle_errors(submission, e) + true end end end - private + def successfully_prepare_submission_for_evss?(submission) + submission.prepare_for_evss! + true + rescue => e + handle_errors(submission, e) + false + end + + def submission_account(submission) + UserAccount.find_by(id: submission.user_account_id) || Account.lookup_by_user_uuid(submission.user_uuid) + end + + def send_submission_data_to_lighthouse(submission, icn) + # 1. transform submission data to LH format + transform_service = EVSS::DisabilityCompensationForm::Form526ToLighthouseTransform.new + body = transform_service.transform(submission.form['form526']) + # 2. send transformed submission data to LH endpoint + benefits_claims_service = BenefitsClaims::Service.new(icn) + raw_response = benefits_claims_service.submit526(body) + raw_response_body = if raw_response.body.is_a? String + JSON.parse(raw_response.body) + else + raw_response.body + end + # 3. convert LH raw response to a FormSubmitResponse for further processing (claim_id, status) + # parse claimId from LH response + submitted_claim_id = raw_response_body.dig('data', 'attributes', 'claimId').to_i + raw_response_struct = OpenStruct.new({ + body: { claim_id: submitted_claim_id }, + status: raw_response.status + }) + EVSS::DisabilityCompensationForm::FormSubmitResponse.new(raw_response_struct.status, raw_response_struct) + end def submit_complete_form service.submit_form526(submission.form_to_json(Form526Submission::FORM_526)) @@ -169,8 +168,13 @@ def response_handler(response) submission.save end - def send_post_evss_notifications(submission) - submission.send_post_evss_notifications! + def send_post_evss_notifications(submission, send_notifications) + actor = OpenStruct.new({ flipper_id: submission.user_uuid }) + if Flipper.enabled?(:disability_compensation_production_tester, actor) + Rails.logger.info("send_post_evss_notifications call skipped for submission #{submission.id}") + elsif send_notifications + submission.send_post_evss_notifications! + end rescue => e handle_errors(submission, e) end