Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SIMPLE-FORMS] refactor: intent to file logic #19955

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def submit
Datadog::Tracing.active_trace&.set_tag('form_id', params[:form_number])

response = if intent_service.use_intent_api?
handle_210966_authenticated
SimpleFormsApi::IntentToFile::Submission.new(@current_user, params).submit
elsif params[:form_number] == '26-4555'
handle264555
else
Expand Down Expand Up @@ -92,27 +92,7 @@ def skip_authentication?
end

def intent_service
@intent_service ||= SimpleFormsApi::IntentToFile.new(@current_user, params)
end

def handle_210966_authenticated
parsed_form_data = JSON.parse(params.to_json)
form = SimpleFormsApi::VBA210966.new(parsed_form_data)
existing_intents = intent_service.existing_intents
confirmation_number, expiration_date = intent_service.submit
form.track_user_identity(confirmation_number)

if confirmation_number && Flipper.enabled?(:simple_forms_email_confirmations)
send_intent_received_email(parsed_form_data, confirmation_number, expiration_date)
end

json_for210966(confirmation_number, expiration_date, existing_intents)
rescue Common::Exceptions::UnprocessableEntity, Exceptions::BenefitsClaimsApiDownError => e
# Common::Exceptions::UnprocessableEntity: There is an authentication issue with the Intent to File API
# Exceptions::BenefitsClaimsApiDownError: The Intent to File API is down or timed out
# In either case, we revert to sending a PDF to Central Mail through the Benefits Intake API
prepare_params_for_benefits_intake_and_log_error(e)
submit_form_to_benefits_intake
@intent_service ||= SimpleFormsApi::SupportingForms::IntentToFile.new(@current_user, params)
end

def handle264555
Expand Down Expand Up @@ -268,34 +248,6 @@ def get_json(confirmation_number, pdf_url)
end
end

def prepare_params_for_benefits_intake_and_log_error(e)
params['veteran_full_name'] ||= {
'first' => params['full_name']['first'],
'last' => params['full_name']['last']
}
params['veteran_id'] ||= { 'ssn' => params['ssn'] }
params['veteran_mailing_address'] ||= { 'postal_code' => @current_user.address[:postal_code] || '00000' }
Rails.logger.info(
'Simple forms api - 21-0966 Benefits Claims Intent to File API error,' \
'reverting to filling a PDF and sending it to Benefits Intake API',
{
error: e,
is_current_user_participant_id_present: @current_user.participant_id ? true : false,
current_user_account_uuid: @current_user.user_account_uuid
}
)
end

def json_for210966(confirmation_number, expiration_date, existing_intents)
{ json: {
confirmation_number:,
expiration_date:,
compensation_intent: existing_intents['compensation'],
pension_intent: existing_intents['pension'],
survivor_intent: existing_intents['survivor']
} }
end

def send_confirmation_email(parsed_form_data, confirmation_number)
config = {
form_data: parsed_form_data,
Expand All @@ -310,22 +262,6 @@ def send_confirmation_email(parsed_form_data, confirmation_number)
)
notification_email.send
end

def send_intent_received_email(parsed_form_data, confirmation_number, expiration_date)
config = {
form_data: parsed_form_data,
form_number: 'vba_21_0966_intent_api',
confirmation_number:,
date_submitted: Time.zone.today.strftime('%B %d, %Y'),
expiration_date:
}
notification_email = SimpleFormsApi::NotificationEmail.new(
config,
notification_type: :received,
user: @current_user
)
notification_email.send
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# frozen_string_literal: true

module SimpleFormsApi
module IntentToFile
class Submission
def initialize(current_user, params)
@current_user = current_user
@params = params
end

def submit
parsed_form_data = JSON.parse(@params.to_json)
confirmation_number, expiration_date = intent_service.submit

track_user_identity(confirmation_number)

if confirmation_number && email_confirmation_enabled?
send_confirmation_email(parsed_form_data, confirmation_number, expiration_date)
end

build_response(confirmation_number, expiration_date)
rescue Common::Exceptions::UnprocessableEntity, Exceptions::BenefitsClaimsApiDownError => e
handle_submission_error(e)
end

private

attr_reader :current_user, :params

def intent_service
@intent_service ||= SimpleFormsApi::SupportingForms::IntentToFile.new(@current_user, params)
end

def track_user_identity(confirmation_number)
return unless confirmation_number

SimpleFormsApi::VBA210966.new(parsed_form_data).track_user_identity(confirmation_number)
end

def email_confirmation_enabled?
Flipper.enabled?(:simple_forms_email_confirmations)
end

def build_response(confirmation_number, expiration_date)
{
json: {
confirmation_number: confirmation_number,
expiration_date: expiration_date,
compensation_intent: intent_service.existing_intents['compensation'],
pension_intent: intent_service.existing_intents['pension'],
survivor_intent: intent_service.existing_intents['survivor']
}
}
end

def handle_submission_error(error)
prepare_params_for_benefits_intake
log_error(error)
SimpleFormsApi::BenefitsIntake::Submission.new(current_user, params).submit
end

def prepare_params_for_benefits_intake
params['veteran_full_name'] ||= {
'first' => params['full_name']['first'],
'last' => params['full_name']['last']
}
params['veteran_id'] ||= { 'ssn' => params['ssn'] }
params['veteran_mailing_address'] ||= { 'postal_code' => current_user.address[:postal_code] || '00000' }
end

def log_error(error)
Rails.logger.info(
'Simple forms api - 21-0966 Benefits Claims Intent to File API error, reverting to filling a PDF and sending it to Benefits Intake API',

Check failure on line 73 in modules/simple_forms_api/app/services/simple_forms_api/intent_to_file/submission.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/LineLength: Line is too long. [146/120]
{
error: error,
is_current_user_participant_id_present: current_user.participant_id.present?,
current_user_account_uuid: current_user.user_account_uuid
}
)
end

def send_confirmation_email(parsed_form_data, confirmation_number, expiration_date)
config = {
form_data: parsed_form_data,
form_number: 'vba_21_0966_intent_api',
confirmation_number: confirmation_number,
date_submitted: Time.zone.today.strftime('%B %d, %Y'),
expiration_date: expiration_date
}
notification_email = SimpleFormsApi::NotificationEmail.new(
config,
notification_type: :received,
user: current_user
)
notification_email.send
end
end
end
end
Loading