diff --git a/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/file_utilities.rb b/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/file_utilities.rb index 37f167a4a43..88e5fcc7866 100644 --- a/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/file_utilities.rb +++ b/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/file_utilities.rb @@ -78,12 +78,12 @@ def write_file(dir_path, file_name, content) File.write(File.join(dir_path, file_name), content) end - def unique_file_name(form_number, id) - "#{Time.zone.today.strftime('%-m.%d.%y')}_form_#{form_number}_vagov_#{id}" + def unique_file_name(form_number, id, date = Time.zone.today) + "#{date.strftime('%-m.%d.%y')}_form_#{form_number}_vagov_#{id}" end - def dated_directory_name(form_number) - "#{Time.zone.today.strftime('%-m.%d.%y')}-Form#{form_number}" + def dated_directory_name(form_number, date = Time.zone.today) + "#{date.strftime('%-m.%d.%y')}-Form#{form_number}" end def write_manifest(row, path) diff --git a/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/s3_client.rb b/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/s3_client.rb index da60515398f..d991b3cc40f 100644 --- a/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/s3_client.rb +++ b/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/s3_client.rb @@ -10,8 +10,8 @@ class S3Client include FileUtilities class << self - def fetch_presigned_url(id, type: :submission) - new(id:).generate_presigned_url(type:) + def fetch_presigned_url(id, config:, type: :submission) + new(id:, config:, type:).retrieve_presigned_url end end @@ -41,6 +41,12 @@ def upload config.handle_error("Failed #{upload_type} upload: #{id}", e) end + def retrieve_presigned_url + archive = config.submission_archive_class.new(config:, id:, type: upload_type) + @archive_path, @manifest_row = archive.retrieval_data + generate_presigned_url(type: upload_type) + end + private attr_reader :archive_path, :config, :id, :manifest_row, :parent_dir, :temp_directory_path, :upload_type @@ -72,8 +78,7 @@ def update_manifest temp_dir = Rails.root.join("tmp/#{SecureRandom.hex}-manifest/").to_s create_directory!(temp_dir) begin - form_number = manifest_row[1] - s3_path = build_s3_manifest_path(form_number) + s3_path = build_s3_manifest_path local_path = download_manifest(temp_dir, s3_path) write_and_upload_manifest(local_path) if config.include_manifest ensure @@ -83,8 +88,8 @@ def update_manifest config.handle_error('Failed to update manifest', e) end - def build_s3_manifest_path(form_number) - path = build_path(:file, s3_directory_path, "manifest_#{dated_directory_name(form_number)}", ext: '.csv') + def build_s3_manifest_path + path = build_path(:file, s3_directory_path, "manifest_#{container_directory}", ext: '.csv') path.sub(%r{^/}, '') end @@ -105,7 +110,13 @@ def s3_uploader end def s3_directory_path - @s3_directory_path ||= build_path(:dir, parent_dir, upload_type.to_s, dated_directory_name(manifest_row[1])) + @s3_directory_path ||= build_path(:dir, parent_dir, upload_type.to_s, container_directory) + end + + # /path/to/parent_dir//-Form + def container_directory + date = upload_type == :submission ? manifest_row[0] : Time.zone.today + dated_directory_name(manifest_row[1], date) end def generate_presigned_url(type: upload_type) diff --git a/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/submission_archive.rb b/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/submission_archive.rb index b04042c6d33..361856f22c7 100644 --- a/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/submission_archive.rb +++ b/modules/simple_forms_api/app/services/simple_forms_api/form_remediation/submission_archive.rb @@ -37,6 +37,12 @@ def build! config.handle_error("Failed building submission: #{id}", e) end + def retrieval_data + extension = archive_type == :submission ? 'pdf' : 'zip' + final_path = "#{temp_directory_path}#{submission_file_name}.#{extension}" + [final_path, manifest_entry] + end + private attr_reader :archive_type, :attachments, :config, :file_path, :id, :metadata, :pdf_already_exists, :submission, diff --git a/modules/simple_forms_api/spec/services/form_remediation/s3_client_spec.rb b/modules/simple_forms_api/spec/services/form_remediation/s3_client_spec.rb index 34463ed0a30..ea5431a193b 100644 --- a/modules/simple_forms_api/spec/services/form_remediation/s3_client_spec.rb +++ b/modules/simple_forms_api/spec/services/form_remediation/s3_client_spec.rb @@ -71,7 +71,10 @@ allow(CarrierWave::SanitizedFile).to receive(:new).with(file_double).and_return(carrier_wave_file) allow(CSV).to receive(:open).and_return(true) allow(SimpleFormsApi::FormRemediation::SubmissionArchive).to(receive(:new).and_return(submission_archive_double)) - allow(submission_archive_double).to receive(:build!).and_return([local_archive_path, manifest_entry]) + allow(submission_archive_double).to receive_messages( + build!: [local_archive_path, manifest_entry], + retrieval_data: [local_archive_path, manifest_entry] + ) allow(SimpleFormsApi::FormRemediation::Uploader).to receive_messages(new: uploader) allow(uploader).to receive(:get_s3_link).with(s3_archive_path).and_return('/s3_url/stuff.pdf') allow(uploader).to receive_messages(get_s3_file: s3_file, store!: carrier_wave_file) @@ -84,6 +87,11 @@ let(:type) { archive_type } describe '.fetch_presigned_url' do + subject(:fetch_presigned_url) { described_class.fetch_presigned_url(benefits_intake_uuid, config:, type:) } + + it 'returns the s3 link' do + expect(fetch_presigned_url).to eq('/s3_url/stuff.pdf') + end end describe '#initialize' do diff --git a/modules/simple_forms_api/spec/services/form_remediation/submission_archive_spec.rb b/modules/simple_forms_api/spec/services/form_remediation/submission_archive_spec.rb index a0c9103c458..1c0bfe13e44 100644 --- a/modules/simple_forms_api/spec/services/form_remediation/submission_archive_spec.rb +++ b/modules/simple_forms_api/spec/services/form_remediation/submission_archive_spec.rb @@ -192,6 +192,27 @@ include_examples 'successfully built submission archive' end end + + describe '#retrieval_data' do + subject(:retrieval_data) { archive_instance.retrieval_data } + + let(:archive_instance) { described_class.new(**hydrated_submission_args) } + let(:file_name) { "#{temp_file_path}/#{submission_file_path}.#{type == :remediation ? 'zip' : 'pdf'}" } + + it 'returns the correct archive path and manifest row' do + expect(retrieval_data.first).to include(file_name) + expect(retrieval_data.second).to eq( + [ + submission.created_at, + form_type, + benefits_intake_uuid, + metadata['fileNumber'], + metadata['veteranFirstName'], + metadata['veteranLastName'] + ] + ) + end + end end end end