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] feat: add logic to handle multiple attachments for 20-10207 #18308

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -12,21 +12,6 @@ class UploadsController < ApplicationController
before_action :load_user, if: :skip_authentication?
skip_after_action :set_csrf_header

FORM_NUMBER_MAP = {
'21-0966' => 'vba_21_0966',
'21-0972' => 'vba_21_0972',
'21-0845' => 'vba_21_0845',
'21-10210' => 'vba_21_10210',
'21-4138' => 'vba_21_4138',
'21-4142' => 'vba_21_4142',
'21P-0847' => 'vba_21p_0847',
'26-4555' => 'vba_26_4555',
'40-0247' => 'vba_40_0247',
'20-10206' => 'vba_20_10206',
'40-10007' => 'vba_40_10007',
'20-10207' => 'vba_20_10207'
}.freeze

UNAUTHENTICATED_FORMS = %w[40-0247 21-10210 21P-0847 40-10007].freeze

def submit
Expand All @@ -37,7 +22,8 @@ def submit
elsif form_is264555_and_should_use_lgy_api
handle264555
else
submit_form_to_benefits_intake
benefits_intake_client = SimpleFormsApi::BenefitsIntakeClient.new(@current_user, params)
benefits_intake_client.submit_form
end

clear_saved_form(params[:form_number])
Expand Down Expand Up @@ -92,7 +78,7 @@ def handle_210966_authenticated

if Flipper.enabled?(:simple_forms_email_confirmations)
SimpleFormsApi::ConfirmationEmail.new(
form_data: parsed_form_data, form_number: get_form_id, confirmation_number:, user: @current_user
form_data: parsed_form_data, form_number: fetch_form_id, confirmation_number:, user: @current_user
).send
end

Expand All @@ -118,105 +104,6 @@ def handle264555
{ json: { reference_number:, status: }, status: lgy_response.status }
end

def submit_form_to_benefits_intake
form_id = get_form_id
parsed_form_data = JSON.parse(params.to_json)
file_path, metadata, form = get_file_paths_and_metadata(parsed_form_data)

if Flipper.enabled?(:simple_forms_lighthouse_benefits_intake_service)
status, confirmation_number = upload_pdf(file_path, metadata, form)
else
status, confirmation_number = SimpleFormsApi::PdfUploader.new(file_path, metadata,
form).upload_to_benefits_intake(params)
end

form.track_user_identity(confirmation_number)

Rails.logger.info(
'Simple forms api - sent to benefits intake',
{ form_number: params[:form_number], status:, uuid: confirmation_number }
)

if status == 200 && Flipper.enabled?(:simple_forms_email_confirmations)
SimpleFormsApi::ConfirmationEmail.new(
form_data: parsed_form_data, form_number: form_id, confirmation_number:, user: @current_user
).send
end

{ json: get_json(confirmation_number || nil, form_id), status: }
end

def get_file_paths_and_metadata(parsed_form_data)
form_id = get_form_id
form = "SimpleFormsApi::#{form_id.titleize.gsub(' ', '')}".constantize.new(parsed_form_data)
# This path can come about if the user is authenticated and, for some reason, doesn't have a participant_id
if form_id == 'vba_21_0966' && params[:preparer_identification] == 'VETERAN' && @current_user
form = form.populate_veteran_data(@current_user)
end
filler = SimpleFormsApi::PdfFiller.new(form_number: form_id, form:)

file_path = if @current_user
filler.generate(@current_user.loa[:current])
else
filler.generate
end
metadata = SimpleFormsApiSubmission::MetadataValidator.validate(form.metadata,
zip_code_is_us_based: form.zip_code_is_us_based)

form.handle_attachments(file_path) if %w[vba_40_0247 vba_20_10207 vba_40_10007].include? form_id

[file_path, metadata, form]
end

def upload_pdf(file_path, metadata, form)
location, uuid = prepare_for_upload(form)
log_upload_details(location, uuid)
response = perform_pdf_upload(location, file_path, metadata)

[response.status, uuid]
end

def prepare_for_upload(form)
location, uuid = lighthouse_service.request_upload
stamp_pdf_with_uuid(form, uuid)
create_form_submission_attempt(uuid)

[location, uuid]
end

def stamp_pdf_with_uuid(form, uuid)
# Stamp uuid on 40-10007
pdf_stamper = SimpleFormsApi::PdfStamper.new(stamped_template_path: 'tmp/vba_40_10007-tmp.pdf', form:)
pdf_stamper.stamp_uuid(uuid)
end

def create_form_submission_attempt(uuid)
form_submission = create_form_submission(uuid)
FormSubmissionAttempt.create(form_submission:)
end

def create_form_submission(uuid)
FormSubmission.create(
form_type: params[:form_number],
benefits_intake_uuid: uuid,
form_data: params.to_json,
user_account: @current_user&.user_account
)
end

def log_upload_details(location, uuid)
Datadog::Tracing.active_trace&.set_tag('uuid', uuid)
Rails.logger.info('Simple forms api - preparing to upload PDF to benefits intake', { location:, uuid: })
end

def perform_pdf_upload(location, file_path, metadata)
lighthouse_service.perform_upload(
metadata: metadata.to_json,
document: file_path,
upload_url: location
)
end

def form_is264555_and_should_use_lgy_api
params[:form_number] == '26-4555' && icn
end
Expand All @@ -225,11 +112,11 @@ def icn
@current_user&.icn
end

def get_form_id
def fetch_form_id
form_number = params[:form_number]
raise 'missing form_number in params' unless form_number

FORM_NUMBER_MAP[form_number]
SimpleFormsApi::BenefitsIntakeClient::FORM_NUMBER_MAP[form_number]
end

def get_json(confirmation_number, form_id)
Expand Down Expand Up @@ -258,13 +145,15 @@ def prepare_params_for_benefits_intake_and_log_error(e)
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']
} }
{
json: {
confirmation_number:,
expiration_date:,
compensation_intent: existing_intents['compensation'],
pension_intent: existing_intents['pension'],
survivor_intent: existing_intents['survivor']
}
}
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def metadata
end

def zip_code_is_us_based
@data.dig('veteran_mailing_address',
'country') == 'USA' || @data.dig('non_veteran_mailing_address', 'country') == 'USA'
@data.dig('veteran_mailing_address', 'country') == 'USA' ||
@data.dig('non_veteran_mailing_address', 'country') == 'USA'
end

def handle_attachments(file_path)
Expand Down Expand Up @@ -135,8 +135,6 @@ def track_user_identity(confirmation_number)
living_situations:, other_reasons:)
end

private

def get_attachments
attachments = []

Expand Down Expand Up @@ -164,6 +162,8 @@ def get_attachments
attachments
end

private

def veteran_ssn
[
data.dig('veteran_id', 'ssn')&.[](0..2),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# frozen_string_literal: true

module SimpleFormsApi
class BenefitsIntakeClient
pennja marked this conversation as resolved.
Show resolved Hide resolved
pennja marked this conversation as resolved.
Show resolved Hide resolved
pennja marked this conversation as resolved.
Show resolved Hide resolved
pennja marked this conversation as resolved.
Show resolved Hide resolved
pennja marked this conversation as resolved.
Show resolved Hide resolved
FORM_NUMBER_MAP = {
'20-10206' => 'vba_20_10206',
'20-10207' => 'vba_20_10207',
'21-0845' => 'vba_21_0845',
'21-0966' => 'vba_21_0966',
'21-0972' => 'vba_21_0972',
'21-10210' => 'vba_21_10210',
'21-4138' => 'vba_21_4138',
'21-4142' => 'vba_21_4142',
'21P-0847' => 'vba_21p_0847',
'26-4555' => 'vba_26_4555',
'40-0247' => 'vba_40_0247',
'40-10007' => 'vba_40_10007'
}.freeze

def initialize(current_user, form_number:, **kwargs)
@current_user = current_user
@form_number = form_number
pennja marked this conversation as resolved.
Show resolved Hide resolved
@params = kwargs
@form_id = fetch_form_id
end

def submit_form
pennja marked this conversation as resolved.
Show resolved Hide resolved
parsed_form_data = parse_and_assign_form_data
status, confirmation_number = upload_pdf

form.track_user_identity(confirmation_number)
log_submission(status, confirmation_number)

send_confirmation_email(parsed_form_data, confirmation_number) if status == 200

generate_response(confirmation_number, status)
rescue => e
handle_submission_error(e)
end

private

attr_accessor :attachments, :file_path, :form_id, :form, :metadata, :params

def fetch_form_id
FORM_NUMBER_MAP.fetch(form_number) { raise ArgumentError, "Invalid form_number: #{form_number}" }
end

def parse_and_assign_form_data
parsed_form_data = JSON.parse(params.to_json)
assign_form_info(parsed_form_data)
parsed_form_data
end

def log_submission(status, uuid)
Rails.logger.info('PDF was successfully uploaded to benefits intake', { form_number: form_id, status:, uuid: })
end

def send_confirmation_email(parsed_form_data, confirmation_number, status)
return unless status == 200 && Flipper.enabled?(:simple_forms_email_confirmations)
pennja marked this conversation as resolved.
Show resolved Hide resolved

SimpleFormsApi::ConfirmationEmail.new(
form_data: parsed_form_data,
form_number: form_id,
confirmation_number:,
user: @current_user
).send
end

def generate_response(confirmation_number, status)
{ json: get_json(confirmation_number, form_id), status: }
end

def assign_form_info(parsed_form_data)
@form = initialize_form(parsed_form_data)
@file_path = generate_filled_form
@metadata = validate_metadata

handle_form_specific_logic
end

def initialize_form(parsed_form_data)
form_class = "SimpleFormsApi::#{form_id.titleize.gsub(' ', '')}".constantize
form = form_class.new(parsed_form_data)

# This path can come about if the user is authenticated and, for some reason, doesn't have a participant_id
form.populate_veteran_data(@current_user) if form_id == 'vba_21_0966' && preparer_is_veteran?
form
end

def generate_filled_form
filler = SimpleFormsApi::PdfFiller.new(form_number: form_id, form:)
@current_user ? filler.generate(@current_user.loa[:current]) : filler.generate
end

def validate_metadata
SimpleFormsApiSubmission::MetadataValidator.validate(
form.metadata,
zip_code_is_us_based: form.zip_code_is_us_based
)
end

def preparer_is_veteran?
params[:preparer_identification] == 'VETERAN' && @current_user
end

def handle_form_specific_logic
if form_id == 'vba_20_10207'
@attachments = form.get_attachments
elsif %w[vba_40_0247 vba_40_10007].include?(form_id)
form.handle_attachments(file_path)
end
end

def upload_pdf
location, uuid = prepare_for_upload
log_upload_details(location, uuid)

response = use_benefits_intake_service? ? perform_pdf_upload(location) : perform_document_upload(location)
[response.status, uuid]
end

def use_benefits_intake_service?
Flipper.enabled?(:simple_forms_lighthouse_benefits_intake_service)
end

def prepare_for_upload
location, uuid = lighthouse_service.request_upload
stamp_pdf_with_uuid(uuid)
create_form_submission_attempt(uuid)
[location, uuid]
end

def stamp_pdf_with_uuid(uuid)
# Stamp uuid on 40-10007
pdf_stamper = SimpleFormsApi::PdfStamper.new(stamped_template_path: 'tmp/vba_40_10007-tmp.pdf', form:)
pdf_stamper.stamp_uuid(uuid)
end

def create_form_submission_attempt(uuid)
form_submission = create_form_submission(uuid)
FormSubmissionAttempt.create(form_submission:)
end

def create_form_submission(uuid)
FormSubmission.create(
form_type: form_number,
benefits_intake_uuid: uuid,
form_data: params.to_json,
user_account: @current_user&.user_account
)
end

def log_upload_details(location, uuid)
Datadog::Tracing.active_trace&.set_tag('uuid', uuid)
Rails.logger.info('Preparing to upload PDF to benefits intake', { location:, uuid: })
end

def perform_pdf_upload(upload_url)
lighthouse_service.perform_upload(
metadata: metadata.to_json,
document: file_path,
upload_url:,
attachments:
)
end

def perform_document_upload(upload_url)
lighthouse_service.upload_doc(
metadata: metadata.to_json,
file: file_path,
upload_url:,
attachments:
)
end

def handle_submission_error(error)
Rails.logger.error('Form submission to benefits intake failed', { error: error.message, form_number: form_id })
end
end
end
Loading
Loading