diff --git a/app/models/saved_claim/dependency_claim.rb b/app/models/saved_claim/dependency_claim.rb index 0f92b9a0dae..38a415469da 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,10 +158,9 @@ 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 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..931e5f313d5 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 @@ -93,12 +95,28 @@ 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_686c_path = process_pdf(claim.to_pdf(form_id: v2 ? FORM_ID_V2 : FORM_ID), claim.created_at, FORM_ID) if claim.submittable_686? # rubocop:disable Layout/LineLength + # 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 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..22b0e183ebf 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 { @@ -57,90 +59,96 @@ } end - describe '#perform' do - let(:success) { true } - let(:path) { 'tmp/pdf_path' } + context 'with va_dependents_v2 disabled' do + before do + allow(Flipper).to receive(:enabled?).with(:va_dependents_v2).and_return(false) + end - let(:lighthouse_mock) { double(:lighthouse_service) } + describe '#perform' do + let(:success) { true } + let(:path) { 'tmp/pdf_path' } - 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 + let(:lighthouse_mock) { double(:lighthouse_service) } - 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 + 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 - context 'with an response error' do - let(:success) { false } + 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).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 + 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') + expect(central_mail_submission.reload.state).to eq('failed') + end 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') + 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