Skip to content

Commit

Permalink
72706: Update logic tto appease Rubocop and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
balexandr committed Mar 26, 2024
1 parent 8f28fe5 commit 2494184
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module IvcChampva
module V1
class UploadsController < ApplicationController
skip_before_action :authenticate
before_action :authenticate, if: :should_authenticate
skip_after_action :set_csrf_header

FORM_NUMBER_MAP = {
Expand All @@ -13,18 +15,21 @@ class UploadsController < ApplicationController
'10-7959F-2' => 'vha_10_7959f_2'
}.freeze

UNAUTHENTICATED_FORMS = %w[].freeze

def submit
Datadog::Tracing.active_trace&.set_tag('form_id', params[:form_number])
form_id = get_form_id
parsed_form_data = JSON.parse(params.to_json)
file_path, file_paths, metadata, form = get_file_paths_and_metadata(parsed_form_data)

file_paths, metadata = get_file_paths_and_metadata(parsed_form_data)
status, error_message = handle_uploads(form_id, metadata, file_paths)

render json: get_json(form_id, error_message || nil)

rescue => e
raise Exceptions::ScrubbedUploadsSubmitError.new(params), e
render json: {
error_message:,
status:
}
rescue
puts 'A default error occurred while uploading a document.'
end

def submit_supporting_documents
Expand All @@ -38,8 +43,34 @@ def submit_supporting_documents
end
end

def authenticate
super
rescue Common::Exceptions::Unauthorized
Rails.logger.info(
'IVC Champva - unauthenticated user submitting form',
{ form_number: params[:form_number] }
)
end

private

def get_file_paths_and_metadata(parsed_form_data)
form_id = get_form_id
form = "IvcChampva::#{form_id.titleize.gsub(' ', '')}".constantize.new(parsed_form_data)
filler = IvcChampva::PdfFiller.new(form_number: form_id, form:)

file_path = if @current_user
filler.generate(@current_user.loa[:current])
else
filler.generate
end

metadata = IvcChampva::MetadataValidator.validate(form.metadata)
file_paths = form.handle_attachments(file_path)

[file_paths, metadata]
end

def handle_uploads(form_id, metadata, pdf_file_paths)
meta_file_name = "#{form_id}_metadata.json"
meta_file_path = "tmp/#{meta_file_name}"
Expand Down Expand Up @@ -67,29 +98,6 @@ def handle_uploads(form_id, metadata, pdf_file_paths)
end
end

def get_file_paths_and_metadata(parsed_form_data)
form_id = get_form_id
form = "IvcChampva::#{form_id.titleize.gsub(' ', '')}".constantize.new(parsed_form_data)
filler = IvcChampva::PdfFiller.new(form_number: form_id, form:)

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

maybe_add_file_paths =
case form_id
when 'vba_40_0247', 'vba_20_10207', 'vha_10_10d', 'vba_40_10007'
form.handle_attachments(file_path)
else
[file_path]
end

[file_path, maybe_add_file_paths, metadata, form]
end

def upload_to_ivc_s3(file_name, file_path, metadata = {})
case ivc_s3_client.put_object(file_name, file_path, metadata)
in { success: true }
Expand All @@ -108,12 +116,8 @@ def get_form_id
FORM_NUMBER_MAP[form_number]
end

def get_json(confirmation_number, form_id, error_message)
json = { confirmation_number: }
json[:expiration_date] = 1.year.from_now if form_id == 'vba_21_0966'
json[:error_message] = error_message

json
def should_authenticate
true unless UNAUTHENTICATED_FORMS.include? params[:form_number]
end

def ivc_s3_client
Expand Down
8 changes: 7 additions & 1 deletion modules/ivc_champva/app/models/ivc_champva/vha_10_10d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ def submission_date_config
{ should_stamp_date?: false }
end

def track_user_identity(confirmation_number); end
def method_missing(_, *args, _)
args
end

def respond_to_missing?(method)
method == :handle_attachments || super
end

private

Expand Down
8 changes: 8 additions & 0 deletions modules/ivc_champva/app/models/ivc_champva/vha_10_7959c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,13 @@ def submission_date_config
text_coords: [440, 670]
}
end

def method_missing(_, *args, _)
args
end

def respond_to_missing?(method)
method == :handle_attachments || super
end
end
end
8 changes: 7 additions & 1 deletion modules/ivc_champva/app/models/ivc_champva/vha_10_7959f_1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def submission_date_config
{ should_stamp_date?: false }
end

def track_user_identity(confirmation_number); end
def method_missing(_, *args, _)
args
end

def respond_to_missing?(method)
method == :handle_attachments || super
end
end
end
8 changes: 7 additions & 1 deletion modules/ivc_champva/app/models/ivc_champva/vha_10_7959f_2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def submission_date_config
{ should_stamp_date?: false }
end

def track_user_identity(confirmation_number); end
def method_missing(_, *args, _)
args
end

def respond_to_missing?(method)
method == :handle_attachments || super
end
end
end
141 changes: 1 addition & 140 deletions modules/ivc_champva/app/services/ivc_champva/pdf_stamper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module IvcChampva
class PdfStamper
FORM_REQUIRES_STAMP = %w[26-4555 21-4142 21-10210 21-0845 21P-0847 21-0966 21-0972 20-10207 10-7959F-1].freeze
FORM_REQUIRES_STAMP = %w[10-7959F-1].freeze
SUBMISSION_TEXT = 'Signed electronically and submitted via VA.gov at '
SUBMISSION_DATE_TITLE = 'Application Submitted:'

Expand Down Expand Up @@ -37,145 +37,6 @@ def self.stamp107959f1(stamped_template_path, form)
stamp(desired_stamps, stamped_template_path, append_to_stamp)
end

def self.stamp264555(stamped_template_path, form)
desired_stamps = []
desired_stamps.append([73, 390, 'X']) unless form.data['previous_sah_application']['has_previous_sah_application']
desired_stamps.append([73, 355, 'X']) unless form.data['previous_hi_application']['has_previous_hi_application']
desired_stamps.append([73, 320, 'X']) unless form.data['living_situation']['is_in_care_facility']
append_to_stamp = false
stamp(desired_stamps, stamped_template_path, append_to_stamp)
end

def self.stamp214142(stamped_template_path, form)
desired_stamps = [[50, 560]]
signature_text = form.data['statement_of_truth_signature']
page_configuration = [
{ type: :new_page },
{ type: :text, position: desired_stamps[0] },
{ type: :new_page }
]

multistamp(stamped_template_path, signature_text, page_configuration)

# This is a one-off case where we need to stamp a date on the first page of 21-4142 when resubmitting
if form.data['in_progress_form_created_at']
date_title = 'Application Submitted:'
date_text = form.data['in_progress_form_created_at']
stamp214142_date_stamp_for_resubmission(stamped_template_path, date_title, date_text)
end
end

def self.stamp214142_date_stamp_for_resubmission(stamped_template_path, date_title, date_text)
date_title_stamp_position = [440, 710]
date_text_stamp_position = [440, 690]
page_configuration = [
{ type: :text, position: date_title_stamp_position },
{ type: :new_page },
{ type: :new_page }
]

multistamp(stamped_template_path, date_title, page_configuration, 12)

page_configuration = [
{ type: :text, position: date_text_stamp_position },
{ type: :new_page },
{ type: :new_page }
]

multistamp(stamped_template_path, date_text, page_configuration, 12)
end

def self.stamp2110210(stamped_template_path, form)
desired_stamps = [[50, 160]]
signature_text = form.data['statement_of_truth_signature']
page_configuration = [
{ type: :new_page },
{ type: :new_page },
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp210845(stamped_template_path, form)
desired_stamps = [[50, 240]]
signature_text = form.data['statement_of_truth_signature']
page_configuration = [
{ type: :new_page },
{ type: :new_page },
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp21p0847(stamped_template_path, form)
desired_stamps = [[50, 190]]
signature_text = form.data['statement_of_truth_signature']
page_configuration = [
{ type: :new_page },
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp210972(stamped_template_path, form)
desired_stamps = [[50, 465]]
signature_text = form.data['statement_of_truth_signature']
page_configuration = [
{ type: :new_page },
{ type: :new_page },
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp210966(stamped_template_path, form)
desired_stamps = [[50, 415]]
signature_text = form.data['statement_of_truth_signature']
page_configuration = [
{ type: :new_page },
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp2010207(stamped_template_path, form)
desired_stamps = if form.data['preparer_type'] == 'veteran'
[[50, 690]]
elsif form.data['third_party_type'] == 'power-of-attorney'
[[50, 445]]
elsif form.data['preparer_type'] == 'third-party-veteran' ||
form.data['preparer_type'] == 'third-party-non-veteran' ||
form.data['preparer_type'] == 'non-veteran'
[[50, 570]]
end
signature_text = form.data['statement_of_truth_signature']
page_configuration = [
{ type: :new_page },
{ type: :new_page },
{ type: :new_page },
{ type: :new_page },
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp4010007_uuid(uuid)
uuid = "UUID: #{uuid}"
stamped_template_path = 'tmp/vba_40_10007-tmp.pdf'
desired_stamps = [[410, 20]]
page_configuration = [
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, uuid, page_configuration, 7)
end

def self.multistamp(stamped_template_path, signature_text, page_configuration, font_size = 16)
stamp_path = Common::FileHelpers.random_file_path
Prawn::Document.generate(stamp_path, margin: [0, 0]) do |pdf|
Expand Down
20 changes: 19 additions & 1 deletion modules/ivc_champva/app/services/ivc_champva/s3.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# S3 Module for simple form submission
# S3 Module for ivc form submission
# Return
# { success: Boolean, [error_message: String] }
module IvcChampva
Expand All @@ -14,6 +14,24 @@ def initialize(region:, access_key_id:, secret_access_key:, bucket_name:)
@bucket_name = bucket_name
end

def put_object(key, file, metadata = {})
Datadog::Tracing.trace('S3 Put File(s)') do
# Convert nil values to empty strings in the metadata
metadata&.transform_values! { |value| value || '' }

client.put_object({
bucket: Settings.ivc_forms.s3.bucket,
key:,
body: File.read(file),
metadata:,
acl: 'public-read'
})
{ success: true }
rescue => e
{ success: false, error_message: "S3 PutObject failure for #{file}: #{e.message}" }
end
end

def upload_file(key, file)
obj = resource.bucket(bucket_name).object(key)
obj.upload_file(file)
Expand Down
2 changes: 1 addition & 1 deletion modules/ivc_champva/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
post '/forms', to: 'uploads#submit'
post '/forms/submit_supporting_documents', to: 'uploads#submit_supporting_documents'
end
end
end
Loading

0 comments on commit 2494184

Please sign in to comment.