Skip to content

Commit

Permalink
Api 35333 update document filenames (#16257)
Browse files Browse the repository at this point in the history
* Updates to set file name for BD uploads

* Rubocop adjustments

* Update to remove filename params, modify calls to BD

* Add handling for supporting doc filename

* Remove check for blank filename for v1 form 526 uploads

* Disable Rubocop method length

* Update tests for filename handling

* Update test for filename handling
  • Loading branch information
mchristiansonVA committed Apr 23, 2024
1 parent 8afe93c commit 13bbc44
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
15 changes: 8 additions & 7 deletions modules/claims_api/app/sidekiq/claims_api/claim_uploader.rb
Expand Up @@ -7,7 +7,7 @@ module ClaimsApi
class ClaimUploader < ClaimsApi::ServiceBase
sidekiq_options retry: true, unique_until: :success

def perform(uuid)
def perform(uuid) # rubocop:disable Metrics/MethodLength
claim_object = ClaimsApi::SupportingDocument.find_by(id: uuid) ||
ClaimsApi::AutoEstablishedClaim.find_by(id: uuid)

Expand All @@ -21,11 +21,12 @@ def perform(uuid)
else
auth_headers = auto_claim.auth_headers
uploader = claim_object.uploader
uploader.retrieve_from_store!(claim_object.file_data['filename'])
original_filename = claim_object.file_data['filename']
uploader.retrieve_from_store!(original_filename)
file_body = uploader.read
ClaimsApi::Logger.log('lighthouse_claim_uploader', claim_id: auto_claim.id, attachment_id: uuid)
if Flipper.enabled? :claims_claim_uploader_use_bd
bd_upload_body(auto_claim:, file_body:, doc_type:)
bd_upload_body(auto_claim:, file_body:, doc_type:, original_filename:)
else
EVSS::DocumentsService.new(auth_headers).upload(file_body, claim_upload_document(claim_object))
end
Expand All @@ -34,19 +35,19 @@ def perform(uuid)

private

def bd_upload_body(auto_claim:, file_body:, doc_type:)
def bd_upload_body(auto_claim:, file_body:, doc_type:, original_filename:)
fh = Tempfile.new(['pdf_path', '.pdf'], binmode: true)
begin
fh.write(file_body)
fh.close
claim_bd_upload_document(auto_claim, doc_type, fh.path)
claim_bd_upload_document(auto_claim, doc_type, fh.path, original_filename)
ensure
fh.unlink
end
end

def claim_bd_upload_document(claim, doc_type, pdf_path) # rubocop:disable Metrics/MethodLength
ClaimsApi::BD.new.upload(claim:, doc_type:, pdf_path:)
def claim_bd_upload_document(claim, doc_type, pdf_path, original_filename) # rubocop:disable Metrics/MethodLength
ClaimsApi::BD.new.upload(claim:, doc_type:, pdf_path:, original_filename:)
# Temporary errors (returning HTML, connection timeout), retry call
rescue Faraday::ParsingError, Faraday::TimeoutError => e
message = get_error_message(e)
Expand Down
28 changes: 24 additions & 4 deletions modules/claims_api/lib/bd/bd.rb
Expand Up @@ -33,15 +33,15 @@ def search(claim_id, file_number)
# Upload document of mapped claim
#
# @return success or failure
def upload(claim:, pdf_path:, doc_type: 'L122', file_number: nil)
def upload(claim:, pdf_path:, doc_type: 'L122', file_number: nil, original_filename: nil)
unless File.exist? pdf_path
ClaimsApi::Logger.log('benefits_documents', detail: "Error uploading doc to BD: #{pdf_path} doesn't exist",
claim_id: claim&.id)
raise Errno::ENOENT, pdf_path
end

@multipart = true
body = generate_upload_body(claim:, doc_type:, pdf_path:, file_number:)
body = generate_upload_body(claim:, doc_type:, pdf_path:, file_number:, original_filename:)
res = client.post('documents', body)&.body&.deep_symbolize_keys
request_id = res&.dig(:data, :requestId)
ClaimsApi::Logger.log(
Expand All @@ -62,10 +62,11 @@ def upload(claim:, pdf_path:, doc_type: 'L122', file_number: nil)
# Generate form body to upload a document
#
# @return {parameters, file}
def generate_upload_body(claim:, doc_type:, pdf_path:, file_number: nil)
def generate_upload_body(claim:, doc_type:, pdf_path:, file_number: nil, original_filename: nil)
payload = {}
veteran_name = "#{claim.auth_headers['va_eauth_firstName']}_#{claim.auth_headers['va_eauth_lastName']}"
file_name = "526EZ_#{veteran_name}_#{claim.evss_id}.pdf"
file_name = generate_file_name(doc_type:, veteran_name:, claim_id: claim.evss_id, original_filename:)

data = {
data: {
systemName: 'VA.gov',
Expand All @@ -83,6 +84,25 @@ def generate_upload_body(claim:, doc_type:, pdf_path:, file_number: nil)
payload
end

def generate_file_name(doc_type:, veteran_name:, claim_id:, original_filename:)
if doc_type == 'L122'
"#{veteran_name}_#{claim_id}_526EZ.pdf"
else
filename = get_original_supporting_doc_file_name(original_filename)
"#{veteran_name}_#{claim_id}_#{filename}.pdf"
end
end

##
# DisabilityCompensationDocuments method create_unique_filename adds a random 11 digit
# hex string to the original filename, so we remove that to yield the user-submitted
# filename to use as part of the filename uploaded to the BD service.
def get_original_supporting_doc_file_name(original_filename)
file_extension = File.extname(original_filename)
base_filename = File.basename(original_filename, file_extension)
base_filename[0...-12]
end

##
# Configure Faraday base class (and do auth)
#
Expand Down
3 changes: 2 additions & 1 deletion modules/claims_api/spec/lib/claims_api/bd_spec.rb
Expand Up @@ -24,7 +24,8 @@
end

it 'uploads an attachment to BD' do
result = subject.send(:generate_upload_body, claim:, doc_type: 'L023', pdf_path:)
result = subject.send(:generate_upload_body, claim:, doc_type: 'L023', original_filename: '21-526EZ.pdf',
pdf_path:)
js = JSON.parse(result[:parameters].read)
expect(js['data']['docType']).to eq 'L023'
end
Expand Down
10 changes: 7 additions & 3 deletions modules/claims_api/spec/sidekiq/claim_uploader_spec.rb
Expand Up @@ -56,6 +56,8 @@
claim
end

let(:original_filename) { 'extras' }

it 'submits successfully' do
expect do
subject.perform_async(supporting_document.id)
Expand Down Expand Up @@ -134,7 +136,7 @@
allow(Tempfile).to receive(:new).and_return tf
allow(Flipper).to receive(:enabled?).with(:claims_claim_uploader_use_bd).and_return true

args = { claim: auto_claim, doc_type: 'L122', pdf_path: tf.path }
args = { claim: auto_claim, doc_type: 'L122', original_filename: 'extras.pdf', pdf_path: tf.path }
expect_any_instance_of(ClaimsApi::BD).to receive(:upload).with(args).and_return true
subject.new.perform(auto_claim.id)
end
Expand All @@ -144,7 +146,8 @@
allow(Tempfile).to receive(:new).and_return tf
allow(Flipper).to receive(:enabled?).with(:claims_claim_uploader_use_bd).and_return true

args = { claim: supporting_document.auto_established_claim, doc_type: 'L023', pdf_path: tf.path }
args = { claim: supporting_document.auto_established_claim, doc_type: 'L023',
original_filename: 'extras.pdf', pdf_path: tf.path }
expect_any_instance_of(ClaimsApi::BD).to receive(:upload).with(args).and_return true
subject.new.perform(supporting_document.id)
end
Expand All @@ -161,7 +164,8 @@
text: 'Error calling external service to upload claim document.' }
]
}
args = { claim: supporting_document.auto_established_claim, doc_type: 'L023', pdf_path: tf.path }
args = { claim: supporting_document.auto_established_claim, doc_type: 'L023',
original_filename: 'extras.pdf', pdf_path: tf.path }
allow_any_instance_of(ClaimsApi::BD).to(
receive(:upload).with(args).and_raise(Common::Exceptions::BackendServiceException.new(
'', {}, 500, body
Expand Down

0 comments on commit 13bbc44

Please sign in to comment.