diff --git a/modules/claims_api/app/sidekiq/claims_api/claim_uploader.rb b/modules/claims_api/app/sidekiq/claims_api/claim_uploader.rb index bc6bb6cacc0..e0a87d2c5eb 100644 --- a/modules/claims_api/app/sidekiq/claims_api/claim_uploader.rb +++ b/modules/claims_api/app/sidekiq/claims_api/claim_uploader.rb @@ -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) @@ -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 @@ -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) diff --git a/modules/claims_api/lib/bd/bd.rb b/modules/claims_api/lib/bd/bd.rb index a435e1bf067..dc72ecb6625 100644 --- a/modules/claims_api/lib/bd/bd.rb +++ b/modules/claims_api/lib/bd/bd.rb @@ -33,7 +33,7 @@ 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) @@ -41,7 +41,7 @@ def upload(claim:, pdf_path:, doc_type: 'L122', file_number: nil) 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( @@ -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', @@ -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) # diff --git a/modules/claims_api/spec/lib/claims_api/bd_spec.rb b/modules/claims_api/spec/lib/claims_api/bd_spec.rb index 67b2cb70d8c..33fa48aaf72 100644 --- a/modules/claims_api/spec/lib/claims_api/bd_spec.rb +++ b/modules/claims_api/spec/lib/claims_api/bd_spec.rb @@ -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 diff --git a/modules/claims_api/spec/sidekiq/claim_uploader_spec.rb b/modules/claims_api/spec/sidekiq/claim_uploader_spec.rb index 099cfb01737..0c2b94d2ef9 100644 --- a/modules/claims_api/spec/sidekiq/claim_uploader_spec.rb +++ b/modules/claims_api/spec/sidekiq/claim_uploader_spec.rb @@ -56,6 +56,8 @@ claim end + let(:original_filename) { 'extras' } + it 'submits successfully' do expect do subject.perform_async(supporting_document.id) @@ -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 @@ -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 @@ -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