diff --git a/app/models/saved_claim/dependency_claim.rb b/app/models/saved_claim/dependency_claim.rb index 0f92b9a0dae..58fd14c3fdf 100644 --- a/app/models/saved_claim/dependency_claim.rb +++ b/app/models/saved_claim/dependency_claim.rb @@ -6,6 +6,7 @@ class SavedClaim::DependencyClaim < CentralMailClaim FORM = '686C-674' STUDENT_ATTENDING_COLLEGE_KEYS = %w[ + student_information student_name_and_ssn student_address_marriage_tuition last_term_school_information @@ -49,16 +50,26 @@ def upload_pdf(form_id, doc_type: '148') uploaded_forms ||= [] return if uploaded_forms.include? form_id - upload_to_vbms(path: process_pdf(to_pdf(form_id:), created_at, form_id), doc_type:) - uploaded_forms << form_id - save + processed_pdfs = [] + if form_id == '21-674-V2' + parsed_form['dependents_application']['student_information'].each_with_index do |student, index| + processed_pdfs << process_pdf(to_pdf(form_id:, student:), created_at, form_id, index) + end + else + processed_pdfs << process_pdf(to_pdf(form_id:), created_at, form_id) + end + processed_pdfs.each do |processed_pdf| + upload_to_vbms(path: processed_pdf, doc_type:) + uploaded_forms << form_id + save + end rescue => e Rails.logger.debug('DependencyClaim: Issue Uploading to VBMS in upload_pdf method', { saved_claim_id: id, form_id:, error: e }) raise e end - def process_pdf(pdf_path, timestamp = nil, form_id = nil) + def process_pdf(pdf_path, timestamp = nil, form_id = nil, iterator = nil) processed_pdf = PDFUtilities::DatestampPdf.new(pdf_path).run( text: 'Application Submitted on site', x: form_id == '686C-674' ? 400 : 300, @@ -69,7 +80,7 @@ def process_pdf(pdf_path, timestamp = nil, form_id = nil) template: "lib/pdf_fill/forms/pdfs/#{form_id}.pdf", multistamp: true ) - renamed_path = "tmp/pdfs/#{form_id}_#{id}_final.pdf" + renamed_path = iterator.present? ? "tmp/pdfs/#{form_id}_#{id}_#{iterator}_final.pdf" : "tmp/pdfs/#{form_id}_#{id}_final.pdf" # rubocop:disable Layout/LineLength File.rename(processed_pdf, renamed_path) # rename for vbms upload renamed_path # return the renamed path end @@ -147,17 +158,11 @@ def upload_to_vbms(path:, doc_type: '148') # end # end - def to_pdf(form_id: FORM) + def to_pdf(form_id: FORM, student: nil) self.form_id = form_id - - PdfFill::Filler.fill_form(self, nil, { created_at: }) + PdfFill::Filler.fill_form(self, nil, { created_at:, student: }) end - # this failure email is not the ideal way to handle the Notification Emails as - # part of the ZSF work, but with the initial timeline it handles the email as intended. - # Future work will be integrating into the Va Notify common lib: - # https://github.com/department-of-veterans-affairs/vets-api/blob/master/lib/veteran_facing_services/notification_email.rb - def send_failure_email(email) # rubocop:disable Metrics/MethodLength # if the claim is both a 686c and a 674, send a combination email. # otherwise, check to see which individual type it is and send the corresponding email. diff --git a/app/sidekiq/lighthouse/benefits_intake/submit_central_form686c_job.rb b/app/sidekiq/lighthouse/benefits_intake/submit_central_form686c_job.rb index f6ad45c0157..02b81d49857 100644 --- a/app/sidekiq/lighthouse/benefits_intake/submit_central_form686c_job.rb +++ b/app/sidekiq/lighthouse/benefits_intake/submit_central_form686c_job.rb @@ -15,7 +15,9 @@ class SubmitCentralForm686cJob FOREIGN_POSTALCODE = '00000' FORM_ID = '686C-674' + FORM_ID_V2 = '686C-674-V2' FORM_ID_674 = '21-674' + FORM_ID_674_V2 = '21-674-V2' STATSD_KEY_PREFIX = 'worker.submit_686c_674_backup_submission' ZSF_DD_TAG_FUNCTION = 'submit_686c_674_backup_submission' # retry for 2d 1h 47m 12s @@ -81,9 +83,15 @@ def upload_to_lh end def create_form_submission_attempt(intake_uuid) + v2 = Flipper.enabled?(:va_dependents_v2) + form_type = if v2 + claim.submittable_686? ? FORM_ID_V2 : FORM_ID_674_V2 + else + claim.submittable_686? ? FORM_ID : FORM_ID_674 + end FormSubmissionAttempt.transaction do form_submission = FormSubmission.create( - form_type: claim.submittable_686? ? FORM_ID : FORM_ID_674, + form_type:, saved_claim: claim, user_account: UserAccount.find_by(icn: claim.parsed_form['veteran_information']['icn']) ) @@ -93,12 +101,29 @@ def create_form_submission_attempt(intake_uuid) def get_files_from_claim # process the main pdf record and the attachments as we would for a vbms submission - form_674_path = process_pdf(claim.to_pdf(form_id: FORM_ID_674), claim.created_at, FORM_ID_674) if claim.submittable_674? # rubocop:disable Layout/LineLength - form_686c_path = process_pdf(claim.to_pdf(form_id: FORM_ID), claim.created_at, FORM_ID) if claim.submittable_686? # rubocop:disable Layout/LineLength - @form_path = form_686c_path || form_674_path + v2 = Flipper.enabled?(:va_dependents_v2) + if claim.submittable_674? + form_674_paths = [] + if v2 + claim.parsed_form['dependents_application']['student_information'].each do |student| + form_674_paths << process_pdf(claim.to_pdf(form_id: FORM_ID_674_V2, student:), claim.created_at, FORM_ID_674_V2) # rubocop:disable Layout/LineLength + end + else + form_674_paths << process_pdf(claim.to_pdf(form_id: FORM_ID_674), claim.created_at, FORM_ID_674) + end + end + form_id = v2 ? FORM_ID_V2 : FORM_ID + form_686c_path = process_pdf(claim.to_pdf(form_id:), claim.created_at, form_id) if claim.submittable_686? + # set main form_path to be first 674 in array if needed + @form_path = form_686c_path || form_674_paths.first + @attachment_paths = claim.persistent_attachments.map { |pa| process_pdf(pa.to_pdf, claim.created_at) } # Treat 674 as first attachment - attachment_paths.insert(0, form_674_path) if form_686c_path.present? && form_674_path.present? + if form_686c_path.present? && form_674_paths.present? + attachment_paths.unshift(*form_674_paths) + elsif form_674_paths.present? && form_674_paths.size > 1 + attachment_paths.unshift(*form_674_paths.drop(1)) + end end def cleanup_file_paths @@ -173,13 +198,15 @@ def generate_metadata # rubocop:disable Metrics/MethodLength form_pdf_metadata = get_hash_and_pages(form_path) address = form['veteran_contact_information']['veteran_address'] is_usa = address['country_name'] == 'USA' + v2 = Flipper.enabled?(:va_dependents_v2) + zip_code = v2 ? address['postal_code'] : address['zip_code'] metadata = { 'veteranFirstName' => veteran_information['full_name']['first'], 'veteranLastName' => veteran_information['full_name']['last'], 'fileNumber' => veteran_information['file_number'] || veteran_information['ssn'], 'receiveDt' => claim.created_at.in_time_zone('Central Time (US & Canada)').strftime('%Y-%m-%d %H:%M:%S'), 'uuid' => claim.guid, - 'zipCode' => is_usa ? address['zip_code'] : FOREIGN_POSTALCODE, + 'zipCode' => is_usa ? zip_code : FOREIGN_POSTALCODE, 'source' => 'va.gov', 'hashV' => form_pdf_metadata[:hash], 'numberAttachments' => attachment_paths.size, @@ -228,11 +255,12 @@ def generate_attachment_metadata(attachment_paths) def send_confirmation_email(user) return if user.va_profile_email.blank? + form_id = Flipper.enabled?(:va_dependents_v2) ? FORM_ID_V2 : FORM_ID VANotify::ConfirmationEmail.send( email_address: user.va_profile_email, template_id: Settings.vanotify.services.va_gov.template_id.form686c_confirmation_email, first_name: user&.first_name&.upcase, - user_uuid_and_form_id: "#{user.uuid}_#{FORM_ID}" + user_uuid_and_form_id: "#{user.uuid}_#{form_id}" ) end @@ -252,11 +280,11 @@ def self.trigger_failure_events(msg) def stamped_pdf_with_form(form_id, path, timestamp) PDFUtilities::DatestampPdf.new(path).run( text: 'Application Submitted on va.gov', - x: form_id == '686C-674' ? 400 : 300, - y: form_id == '686C-674' ? 675 : 775, + x: %w[686C-674 686C-674-V2].include?(form_id) ? 400 : 300, + y: %w[686C-674 686C-674-V2].include?(form_id) ? 675 : 775, text_only: true, # passing as text only because we override how the date is stamped in this instance timestamp:, - page_number: form_id == '686C-674' ? 6 : 0, + page_number: %w[686C-674 686C-674-V2].include?(form_id) ? 6 : 0, template: "lib/pdf_fill/forms/pdfs/#{form_id}.pdf", multistamp: true ) diff --git a/lib/pdf_fill/forms/va21674v2.rb b/lib/pdf_fill/forms/va21674v2.rb index 07cbe06ff7e..4a1c16beb24 100644 --- a/lib/pdf_fill/forms/va21674v2.rb +++ b/lib/pdf_fill/forms/va21674v2.rb @@ -816,6 +816,8 @@ class Va21674v2 < FormBase def merge_fields(options = {}) created_at = options[:created_at] if options[:created_at].present? + student = options[:student] + @form_data['dependents_application']['student_information'] = [student] expand_signature(@form_data['veteran_information']['full_name'], created_at&.to_date || Time.zone.today) @form_data['signature_date'] = split_date(@form_data['signatureDate']) veteran_contact_information = @form_data['dependents_application']['veteran_contact_information'] diff --git a/spec/models/saved_claim/dependency_claim_spec.rb b/spec/models/saved_claim/dependency_claim_spec.rb index 1e842930a1b..fe9e65daa3c 100644 --- a/spec/models/saved_claim/dependency_claim_spec.rb +++ b/spec/models/saved_claim/dependency_claim_spec.rb @@ -5,11 +5,17 @@ RSpec.describe SavedClaim::DependencyClaim do subject { create(:dependency_claim) } + let(:subject_v2) { create(:dependency_claim_v2) } + let(:all_flows_payload) { build(:form_686c_674_kitchen_sink) } + let(:all_flows_payload_v2) { build(:form686c_674_v2) } let(:adopted_child) { build(:adopted_child_lives_with_veteran) } + let(:adopted_child_v2) { build(:adopted_child_lives_with_veteran_v2) } let(:form_674_only) { build(:form_674_only) } + let(:form_674_only_v2) { build(:form_674_only_v2) } let(:doc_type) { '148' } let(:va_file_number) { subject.parsed_form['veteran_information']['va_file_number'] } + let(:va_file_number_v2) { subject_v2.parsed_form['veteran_information']['va_file_number'] } let(:va_file_number_with_payload) do { 'veteran_information' => { @@ -22,8 +28,21 @@ } } end + let(:va_file_number_with_payload_v2) do + { + 'veteran_information' => { + 'birth_date' => '1809-02-12', + 'full_name' => { + 'first' => 'WESLEY', 'last' => 'FORD', 'middle' => nil + }, + 'ssn' => va_file_number_v2, + 'va_file_number' => va_file_number_v2 + } + } + end let(:file_path) { "tmp/pdfs/686C-674_#{subject.id}_final.pdf" } + let(:file_path_v2) { "tmp/pdfs/686C-674-V2_#{subject_v2.id}_final.pdf" } describe '#upload_pdf' do context 'when :va_dependents_v2 is disabled' do @@ -39,7 +58,6 @@ doc_type: ).and_return(uploader) expect(uploader).to receive(:upload!) - subject.upload_pdf('686C-674') end end @@ -52,13 +70,25 @@ it 'when :va_dependents_v2 is enabled' do uploader = double(ClaimsApi::VBMSUploader) expect(ClaimsApi::VBMSUploader).to receive(:new).with( - filepath: file_path, - file_number: va_file_number, + filepath: file_path_v2, + file_number: va_file_number_v2, doc_type: ).and_return(uploader) expect(uploader).to receive(:upload!) - subject.upload_pdf('686C-674') + subject_v2.upload_pdf('686C-674-V2') + end + + it 'when :va_dependents_v2 is enabled upload 674' do + uploader = double(ClaimsApi::VBMSUploader) + expect(ClaimsApi::VBMSUploader).to receive(:new).with( + filepath: "tmp/pdfs/21-674-V2_#{subject_v2.id}_0_final.pdf", + file_number: va_file_number_v2, + doc_type: + ).and_return(uploader) + expect(uploader).to receive(:upload!) + + subject_v2.upload_pdf('21-674-V2') end end end @@ -100,7 +130,7 @@ end context 'va_dependents_v2 is enabled' do - subject { described_class.new(form: all_flows_payload.to_json) } + subject { described_class.new(form: all_flows_payload_v2.to_json) } before do allow(Flipper).to receive(:enabled?).with(:va_dependents_v2).and_return(true) @@ -108,16 +138,16 @@ describe '#formatted_686_data' do it 'returns all data for 686 submissions' do - formatted_data = subject.formatted_686_data(va_file_number_with_payload) + formatted_data = subject.formatted_686_data(va_file_number_with_payload_v2) expect(formatted_data).to include(:veteran_information) end end describe '#formatted_674_data' do it 'returns all data for 674 submissions' do - formatted_data = subject.formatted_674_data(va_file_number_with_payload) + formatted_data = subject.formatted_674_data(va_file_number_with_payload_v2) expect(formatted_data).to include(:dependents_application) - expect(formatted_data[:dependents_application]).to include(:student_name_and_ssn) + expect(formatted_data[:dependents_application]).to include(:student_information) end end @@ -151,7 +181,7 @@ end context 'va_dependents_v2 is enabled' do - subject { described_class.new(form: form_674_only.to_json) } + subject { described_class.new(form: form_674_only_v2.to_json) } before do allow(Flipper).to receive(:enabled?).with(:va_dependents_v2).and_return(true) @@ -187,7 +217,7 @@ end context 'va_dependents_v2 is enabled' do - subject { described_class.new(form: adopted_child.to_json) } + subject { described_class.new(form: adopted_child_v2.to_json) } before do allow(Flipper).to receive(:enabled?).with(:va_dependents_v2).and_return(true) @@ -208,7 +238,7 @@ end context 'v2 form' do - subject { described_class.new(form: all_flows_payload.to_json, use_v2: true) } + subject { described_class.new(form: all_flows_payload_v2.to_json, use_v2: true) } before do allow(Flipper).to receive(:enabled?).with(:va_dependents_v2).and_return(true) diff --git a/spec/sidekiq/lighthouse/benefits_intake/submit_central_form686c_job_spec.rb b/spec/sidekiq/lighthouse/benefits_intake/submit_central_form686c_job_spec.rb index df7760c6b81..afb9a538b9d 100644 --- a/spec/sidekiq/lighthouse/benefits_intake/submit_central_form686c_job_spec.rb +++ b/spec/sidekiq/lighthouse/benefits_intake/submit_central_form686c_job_spec.rb @@ -8,7 +8,9 @@ let(:user) { create(:evss_user, :loa3) } let(:claim) { create(:dependency_claim) } + let(:claim_v2) { create(:dependency_claim_v2) } let(:all_flows_payload) { build(:form_686c_674_kitchen_sink) } + let(:all_flows_payload_v2) { build(:form686c_674_v2) } let(:birth_date) { '1809-02-12' } let(:vet_info) do { @@ -30,6 +32,7 @@ end let(:encrypted_vet_info) { KmsEncrypted::Box.new.encrypt(vet_info.to_json) } let(:central_mail_submission) { claim.central_mail_submission } + let(:central_mail_submission_v2) { claim_v2.central_mail_submission } let(:user_struct) do OpenStruct.new( @@ -57,135 +60,130 @@ } end - describe '#perform' do - let(:success) { true } - let(:path) { 'tmp/pdf_path' } - - let(:lighthouse_mock) { double(:lighthouse_service) } - + context 'with va_dependents_v2 disabled' do before do - expect(BenefitsIntakeService::Service).to receive(:new) - .with(with_upload_location: true) - .and_return(lighthouse_mock) - expect(lighthouse_mock).to receive(:uuid).and_return('uuid') - datestamp_double1 = double - datestamp_double2 = double - datestamp_double3 = double - timestamp = claim.created_at - - expect(SavedClaim::DependencyClaim).to receive(:find).with(claim.id).and_return(claim) - expect(claim).to receive(:to_pdf).and_return('path1') - expect(PDFUtilities::DatestampPdf).to receive(:new).with('path1').and_return(datestamp_double1) - expect(datestamp_double1).to receive(:run).with(text: 'VA.GOV', x: 5, y: 5, timestamp:).and_return('path2') - expect(PDFUtilities::DatestampPdf).to receive(:new).with('path2').and_return(datestamp_double2) - expect(datestamp_double2).to receive(:run).with( - text: 'FDC Reviewed - va.gov Submission', - x: 400, - y: 770, - text_only: true - ).and_return('path3') - expect(PDFUtilities::DatestampPdf).to receive(:new).with('path3').and_return(datestamp_double3) - expect(datestamp_double3).to receive(:run).with( - text: 'Application Submitted on va.gov', - x: 400, - y: 675, - text_only: true, - timestamp:, - page_number: 6, - template: 'lib/pdf_fill/forms/pdfs/686C-674.pdf', - multistamp: true - ).and_return(path) - - data = JSON.parse('{"id":"6d8433c1-cd55-4c24-affd-f592287a7572","type":"document_upload"}') - expect(lighthouse_mock).to receive(:upload_form).with( - main_document: { file: path, file_name: 'pdf_path' }, - attachments: [], - form_metadata: hash_including(file_number: '796104437') - ).and_return(OpenStruct.new(success?: success, data:)) - - expect(Common::FileHelpers).to receive(:delete_file_if_exists).with(path) - - expect(FormSubmission).to receive(:create).with( - form_type: '686C-674', - saved_claim: claim, - user_account: nil - ).and_return(FormSubmission.new) - expect(FormSubmissionAttempt).to receive(:create).with(form_submission: an_instance_of(FormSubmission), - benefits_intake_uuid: 'uuid') + allow(Flipper).to receive(:enabled?).with(:va_dependents_v2).and_return(false) end - context 'with an response error' do - let(:success) { false } + describe '#perform' do + let(:success) { true } + let(:path) { 'tmp/pdf_path' } - it 'raises BenefitsIntakeResponseError and updates submission to failed' do - mailer_double = double('Mail::Message') - allow(mailer_double).to receive(:deliver_now) - expect(claim).to receive(:submittable_686?).and_return(true).exactly(:twice) - expect(claim).to receive(:submittable_674?).and_return(false) - expect { subject.perform(claim.id, encrypted_vet_info, encrypted_user_struct) }.to raise_error(Lighthouse::BenefitsIntake::SubmitCentralForm686cJob::BenefitsIntakeResponseError) # rubocop:disable Layout/LineLength + let(:lighthouse_mock) { double(:lighthouse_service) } - expect(central_mail_submission.reload.state).to eq('failed') + before do + expect(BenefitsIntakeService::Service).to receive(:new) + .with(with_upload_location: true) + .and_return(lighthouse_mock) + expect(lighthouse_mock).to receive(:uuid).and_return('uuid') + datestamp_double1 = double + datestamp_double2 = double + datestamp_double3 = double + timestamp = claim.created_at + + expect(SavedClaim::DependencyClaim).to receive(:find).with(claim.id).and_return(claim) + expect(claim).to receive(:to_pdf).and_return('path1') + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path1').and_return(datestamp_double1) + expect(datestamp_double1).to receive(:run).with(text: 'VA.GOV', x: 5, y: 5, timestamp:).and_return('path2') + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path2').and_return(datestamp_double2) + expect(datestamp_double2).to receive(:run).with( + text: 'FDC Reviewed - va.gov Submission', + x: 400, + y: 770, + text_only: true + ).and_return('path3') + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path3').and_return(datestamp_double3) + expect(datestamp_double3).to receive(:run).with( + text: 'Application Submitted on va.gov', + x: 400, + y: 675, + text_only: true, + timestamp:, + page_number: 6, + template: 'lib/pdf_fill/forms/pdfs/686C-674.pdf', + multistamp: true + ).and_return(path) + + data = JSON.parse('{"id":"6d8433c1-cd55-4c24-affd-f592287a7572","type":"document_upload"}') + expect(lighthouse_mock).to receive(:upload_form).with( + main_document: { file: path, file_name: 'pdf_path' }, + attachments: [], + form_metadata: hash_including(file_number: '796104437') + ).and_return(OpenStruct.new(success?: success, data:)) + + expect(Common::FileHelpers).to receive(:delete_file_if_exists).with(path) + + expect(FormSubmission).to receive(:create).with( + form_type: '686C-674', + saved_claim: claim, + user_account: nil + ).and_return(FormSubmission.new) + expect(FormSubmissionAttempt).to receive(:create).with(form_submission: an_instance_of(FormSubmission), + benefits_intake_uuid: 'uuid') end - end - it 'submits the saved claim and updates submission to success' do - expect(VANotify::EmailJob).to receive(:perform_async).with( - user_struct.va_profile_email, - 'fake_template_id', - { - 'date' => Time.now.in_time_zone('Eastern Time (US & Canada)').strftime('%B %d, %Y'), - 'first_name' => 'MARK' - } - ) - expect(claim).to receive(:submittable_686?).and_return(true).exactly(:twice) - expect(claim).to receive(:submittable_674?).and_return(false) - subject.perform(claim.id, encrypted_vet_info, encrypted_user_struct) - expect(central_mail_submission.reload.state).to eq('success') - end - end + context 'with an response error' do + let(:success) { false } - describe '#to_faraday_upload' do - it 'converts a file to faraday upload object' do - file_path = 'tmp/foo' - expect(Faraday::UploadIO).to receive(:new).with( - file_path, - 'application/pdf' - ) - described_class.new.to_faraday_upload(file_path) + it 'raises BenefitsIntakeResponseError and updates submission to failed' do + mailer_double = double('Mail::Message') + allow(mailer_double).to receive(:deliver_now) + expect(claim).to receive(:submittable_686?).and_return(true).exactly(:twice) + expect(claim).to receive(:submittable_674?).and_return(false) + expect { subject.perform(claim.id, encrypted_vet_info, encrypted_user_struct) }.to raise_error(Lighthouse::BenefitsIntake::SubmitCentralForm686cJob::BenefitsIntakeResponseError) # rubocop:disable Layout/LineLength + + expect(central_mail_submission.reload.state).to eq('failed') + end + end + + it 'submits the saved claim and updates submission to success' do + expect(VANotify::EmailJob).to receive(:perform_async).with( + user_struct.va_profile_email, + 'fake_template_id', + { + 'date' => Time.now.in_time_zone('Eastern Time (US & Canada)').strftime('%B %d, %Y'), + 'first_name' => 'MARK' + } + ) + expect(claim).to receive(:submittable_686?).and_return(true).exactly(:twice) + expect(claim).to receive(:submittable_674?).and_return(false) + subject.perform(claim.id, encrypted_vet_info, encrypted_user_struct) + expect(central_mail_submission.reload.state).to eq('success') + end end - end - describe '#process_pdf' do - timestamp = Time.zone.now - subject { job.process_pdf('path1', timestamp, '686C-674') } - - it 'processes a record and add stamps' do - datestamp_double1 = double - datestamp_double2 = double - datestamp_double3 = double - - expect(PDFUtilities::DatestampPdf).to receive(:new).with('path1').and_return(datestamp_double1) - expect(datestamp_double1).to receive(:run).with(text: 'VA.GOV', x: 5, y: 5, timestamp:).and_return('path2') - expect(PDFUtilities::DatestampPdf).to receive(:new).with('path2').and_return(datestamp_double2) - expect(datestamp_double2).to receive(:run).with( - text: 'FDC Reviewed - va.gov Submission', - x: 400, - y: 770, - text_only: true - ).and_return('path3') - expect(PDFUtilities::DatestampPdf).to receive(:new).with('path3').and_return(datestamp_double3) - expect(datestamp_double3).to receive(:run).with( - text: 'Application Submitted on va.gov', - x: 400, - y: 675, - text_only: true, - timestamp:, - page_number: 6, - template: 'lib/pdf_fill/forms/pdfs/686C-674.pdf', - multistamp: true - ).and_return('path4') - - expect(subject).to eq('path4') + describe '#process_pdf' do + timestamp = Time.zone.now + subject { job.process_pdf('path1', timestamp, '686C-674') } + + it 'processes a record and add stamps' do + datestamp_double1 = double + datestamp_double2 = double + datestamp_double3 = double + + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path1').and_return(datestamp_double1) + expect(datestamp_double1).to receive(:run).with(text: 'VA.GOV', x: 5, y: 5, timestamp:).and_return('path2') + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path2').and_return(datestamp_double2) + expect(datestamp_double2).to receive(:run).with( + text: 'FDC Reviewed - va.gov Submission', + x: 400, + y: 770, + text_only: true + ).and_return('path3') + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path3').and_return(datestamp_double3) + expect(datestamp_double3).to receive(:run).with( + text: 'Application Submitted on va.gov', + x: 400, + y: 675, + text_only: true, + timestamp:, + page_number: 6, + template: 'lib/pdf_fill/forms/pdfs/686C-674.pdf', + multistamp: true + ).and_return('path4') + + expect(subject).to eq('path4') + end end describe '#get_hash_and_pages' do @@ -260,6 +258,225 @@ end end + context 'with va_dependents_v2 enabled' do + before do + allow(Flipper).to receive(:enabled?).with(:va_dependents_v2).and_return(true) + end + + describe '#perform' do + let(:success) { true } + let(:path) { 'tmp/pdf_path' } + + let(:lighthouse_mock) { double(:lighthouse_service) } + + before do + expect(BenefitsIntakeService::Service).to receive(:new) + .with(with_upload_location: true) + .and_return(lighthouse_mock) + expect(lighthouse_mock).to receive(:uuid).and_return('uuid') + datestamp_double1 = double + datestamp_double2 = double + datestamp_double3 = double + timestamp = claim_v2.created_at + + expect(SavedClaim::DependencyClaim).to receive(:find).with(claim_v2.id).and_return(claim_v2) + expect(claim_v2).to receive(:to_pdf).and_return('path1') + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path1').and_return(datestamp_double1) + expect(datestamp_double1).to receive(:run).with(text: 'VA.GOV', x: 5, y: 5, timestamp:).and_return('path2') + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path2').and_return(datestamp_double2) + expect(datestamp_double2).to receive(:run).with( + text: 'FDC Reviewed - va.gov Submission', + x: 400, + y: 770, + text_only: true + ).and_return('path3') + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path3').and_return(datestamp_double3) + expect(datestamp_double3).to receive(:run).with( + text: 'Application Submitted on va.gov', + x: 400, + y: 675, + text_only: true, + timestamp:, + page_number: 6, + template: 'lib/pdf_fill/forms/pdfs/686C-674-V2.pdf', + multistamp: true + ).and_return(path) + + data = JSON.parse('{"id":"6d8433c1-cd55-4c24-affd-f592287a7572","type":"document_upload"}') + expect(lighthouse_mock).to receive(:upload_form).with( + main_document: { file: path, file_name: 'pdf_path' }, + attachments: [], + form_metadata: hash_including(file_number: '796104437') + ).and_return(OpenStruct.new(success?: success, data:)) + + expect(Common::FileHelpers).to receive(:delete_file_if_exists).with(path) + + expect(FormSubmission).to receive(:create).with( + form_type: '686C-674-V2', + saved_claim: claim_v2, + user_account: nil + ).and_return(FormSubmission.new) + expect(FormSubmissionAttempt).to receive(:create).with(form_submission: an_instance_of(FormSubmission), + benefits_intake_uuid: 'uuid') + end + + context 'with an response error' do + let(:success) { false } + + it 'raises BenefitsIntakeResponseError and updates submission to failed' do + mailer_double = double('Mail::Message') + allow(mailer_double).to receive(:deliver_now) + expect(claim_v2).to receive(:submittable_686?).and_return(true).exactly(:twice) + expect(claim_v2).to receive(:submittable_674?).and_return(false) + expect { subject.perform(claim_v2.id, encrypted_vet_info, encrypted_user_struct) }.to raise_error(Lighthouse::BenefitsIntake::SubmitCentralForm686cJob::BenefitsIntakeResponseError) # rubocop:disable Layout/LineLength + + expect(central_mail_submission_v2.reload.state).to eq('failed') + end + end + + it 'submits the saved claim and updates submission to success' do + expect(VANotify::EmailJob).to receive(:perform_async).with( + user_struct.va_profile_email, + 'fake_template_id', + { + 'date' => Time.now.in_time_zone('Eastern Time (US & Canada)').strftime('%B %d, %Y'), + 'first_name' => 'MARK' + } + ) + expect(claim_v2).to receive(:submittable_686?).and_return(true).exactly(:twice) + expect(claim_v2).to receive(:submittable_674?).and_return(false) + subject.perform(claim_v2.id, encrypted_vet_info, encrypted_user_struct) + expect(central_mail_submission_v2.reload.state).to eq('success') + end + end + + describe 'get files from claim' do + subject { job.get_files_from_claim } + + it 'compiles 686 and 674 files and returns attachments array with the generated 674' do + job.instance_variable_set('@claim', claim_v2) + expect(subject).to be_an_instance_of(Array) + expect(subject.size).to eq(1) + end + end + + describe '#process_pdf' do + timestamp = Time.zone.now + subject { job.process_pdf('path1', timestamp, '686C-674-V2') } + + it 'processes a record and add stamps' do + datestamp_double1 = double + datestamp_double2 = double + datestamp_double3 = double + + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path1').and_return(datestamp_double1) + expect(datestamp_double1).to receive(:run).with(text: 'VA.GOV', x: 5, y: 5, timestamp:).and_return('path2') + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path2').and_return(datestamp_double2) + expect(datestamp_double2).to receive(:run).with( + text: 'FDC Reviewed - va.gov Submission', + x: 400, + y: 770, + text_only: true + ).and_return('path3') + expect(PDFUtilities::DatestampPdf).to receive(:new).with('path3').and_return(datestamp_double3) + expect(datestamp_double3).to receive(:run).with( + text: 'Application Submitted on va.gov', + x: 400, + y: 675, + text_only: true, + timestamp:, + page_number: 6, + template: 'lib/pdf_fill/forms/pdfs/686C-674-V2.pdf', + multistamp: true + ).and_return('path4') + + expect(subject).to eq('path4') + end + end + + describe '#get_hash_and_pages' do + it 'gets sha and number of pages' do + expect(Digest::SHA256).to receive(:file).with('path').and_return( + OpenStruct.new(hexdigest: 'hexdigest') + ) + expect(PdfInfo::Metadata).to receive(:read).with('path').and_return( + OpenStruct.new(pages: 2) + ) + + expect(described_class.new.get_hash_and_pages('path')).to eq( + hash: 'hexdigest', + pages: 2 + ) + end + end + + describe '#generate_metadata' do + subject { job.generate_metadata } + + before do + job.instance_variable_set('@claim', claim_v2) + job.instance_variable_set('@form_path', 'pdf_path') + job.instance_variable_set('@attachment_paths', ['attachment_path']) + + expect(Digest::SHA256).to receive(:file).with('pdf_path').and_return( + OpenStruct.new(hexdigest: 'hash1') + ) + expect(PdfInfo::Metadata).to receive(:read).with('pdf_path').and_return( + OpenStruct.new(pages: 1) + ) + + expect(Digest::SHA256).to receive(:file).with('attachment_path').and_return( + OpenStruct.new(hexdigest: 'hash2') + ) + expect(PdfInfo::Metadata).to receive(:read).with('attachment_path').and_return( + OpenStruct.new(pages: 2) + ) + end + + context 'with a non us address' do + before do + form = JSON.parse(claim_v2.form) + form['dependents_application']['veteran_contact_information']['veteran_address']['country_name'] = 'AGO' + claim_v2.form = form.to_json + claim_v2.send(:remove_instance_variable, :@parsed_form) + end + + it 'generates metadata with 00000 for zipcode' do + expect(subject['zipCode']).to eq('00000') + end + end + + it 'generates the metadata', run_at: '2017-01-04 03:00:00 EDT' do + expect(subject).to eq( + 'veteranFirstName' => vet_info['veteran_information']['full_name']['first'], + 'veteranLastName' => vet_info['veteran_information']['full_name']['last'], + 'fileNumber' => claim_v2.parsed_form['veteran_information']['va_file_number'], + 'receiveDt' => '2017-01-04 01:00:00', + 'zipCode' => '00000', + 'uuid' => claim_v2.guid, + 'source' => 'va.gov', + 'hashV' => 'hash1', + 'numberAttachments' => 1, + 'docType' => '686C-674-V2', + 'numberPages' => 1, + 'ahash1' => 'hash2', + 'numberPages1' => 2 + ) + end + end + end + + describe '#to_faraday_upload' do + it 'converts a file to faraday upload object' do + file_path = 'tmp/foo' + expect(Faraday::UploadIO).to receive(:new).with( + file_path, + 'application/pdf' + ) + described_class.new.to_faraday_upload(file_path) + end + end + describe 'sidekiq_retries_exhausted block with dependents_trigger_action_needed_email flipper on' do before do allow(SavedClaim::DependencyClaim).to receive(:find).and_return(claim)