Skip to content

Commit

Permalink
reverse fillper
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudmagic80 committed Dec 1, 2024
1 parent 4333497 commit 1f43fc3
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 385 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,37 +93,19 @@ def supporting_document_ids(parsed_form_data)
parsed_form_data['supporting_docs']&.pluck('claim_id')&.compact.presence || []
end

# rubocop:disable Metrics/MethodLength
# rubocop:disable Style/IdenticalConditionalBranches
def get_file_paths_and_metadata(parsed_form_data)
if Flipper.enabled?(:champva_unique_temp_file_names, @user)
attachment_ids, form = get_attachment_ids_and_form(parsed_form_data)
filler = IvcChampva::PdfFiller.new(form_number: form.form_id, form:, uuid: form.uuid)
file_path = if @current_user
filler.generate(@current_user.loa[:current])
else
filler.generate
end
metadata = IvcChampva::MetadataValidator.validate(form.metadata)
file_paths = form.handle_attachments(file_path)

[file_paths, metadata.merge({ 'attachment_ids' => attachment_ids })]
else
attachment_ids, form = get_attachment_ids_and_form(parsed_form_data)
filler = IvcChampva::PdfFiller.new(form_number: form.form_id, form:)
file_path = if @current_user
filler.generate(@current_user.loa[:current])
else
filler.generate
end
metadata = IvcChampva::MetadataValidator.validate(form.metadata)
file_paths = form.handle_attachments(file_path)

[file_paths, metadata.merge({ 'attachment_ids' => attachment_ids })]
end
attachment_ids, form = get_attachment_ids_and_form(parsed_form_data)
filler = IvcChampva::PdfFiller.new(form_number: form.form_id, form:, uuid: form.uuid)
file_path = if @current_user
filler.generate(@current_user.loa[:current])
else
filler.generate
end
metadata = IvcChampva::MetadataValidator.validate(form.metadata)
file_paths = form.handle_attachments(file_path)

[file_paths, metadata.merge({ 'attachment_ids' => attachment_ids })]
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Style/IdenticalConditionalBranches

def get_form_id
form_number = params[:form_number]
Expand Down
51 changes: 13 additions & 38 deletions modules/ivc_champva/app/services/ivc_champva/attachments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,24 @@ module IvcChampva
module Attachments
attr_accessor :form_id, :uuid, :data

# rubocop:disable Metrics/MethodLength
def handle_attachments(file_path)
if Flipper.enabled?(:champva_unique_temp_file_names, @user)
file_paths = [file_path]
attachments = get_attachments

attachments.each_with_index do |attachment, index|
new_file_name = if attachment.include?('_additional_')
"#{uuid}_#{File.basename(attachment,
'.*')}.pdf"
else
"#{uuid}_#{form_id}_supporting_doc-#{index}.pdf"
end
new_file_path = File.join(File.dirname(attachment), new_file_name)
File.rename(attachment, new_file_path)
file_paths << new_file_path
end
else
file_path_uuid = file_path.gsub("#{form_id}-tmp", "#{uuid}_#{form_id}-tmp")
File.rename(file_path, file_path_uuid)
attachments = get_attachments
file_paths = [file_path_uuid]

if attachments.count.positive?
supporting_doc_index = 0
attachments.each do |attachment|
new_file_name =
if attachment.include?('_additional_')
"#{uuid}_#{File.basename(attachment, '.*')}.pdf"
else
"#{uuid}_#{form_id}_supporting_doc-#{supporting_doc_index}.pdf".tap { supporting_doc_index += 1 }
end

new_file_path = File.join(File.dirname(attachment), new_file_name)
File.rename(attachment, new_file_path)
file_paths << new_file_path
end
end
file_paths = [file_path]
attachments = get_attachments

attachments.each_with_index do |attachment, index|
new_file_name = if attachment.include?('_additional_')
"#{uuid}_#{File.basename(attachment,
'.*')}.pdf"
else
"#{uuid}_#{form_id}_supporting_doc-#{index}.pdf"
end
new_file_path = File.join(File.dirname(attachment), new_file_name)
File.rename(attachment, new_file_path)
file_paths << new_file_path
end

file_paths
end
# rubocop:enable Metrics/MethodLength

private

Expand Down
33 changes: 11 additions & 22 deletions modules/ivc_champva/app/services/ivc_champva/pdf_filler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@ def initialize(form_number:, form:, name: nil, uuid: nil)
@uuid = uuid if Flipper.enabled?(:champva_unique_temp_file_names, @user)
end

# rubocop:disable Metrics/MethodLength
def generate(current_loa = nil)
if Flipper.enabled?(:champva_unique_temp_file_names, @user)
generated_form_path = Rails.root.join("tmp/#{@uuid}_#{name}-tmp.pdf").to_s
stamped_template_path = Rails.root.join("tmp/#{@uuid}_#{name}-stamped.pdf").to_s
generated_form_path = Rails.root.join("tmp/#{@uuid}_#{name}-tmp.pdf").to_s
stamped_template_path = Rails.root.join("tmp/#{@uuid}_#{name}-stamped.pdf").to_s

tempfile = create_tempfile
FileUtils.touch(tempfile)
FileUtils.copy_file(tempfile.path, stamped_template_path)
else
template_form_path = "#{TEMPLATE_BASE}/#{form_number}.pdf"
generated_form_path = "tmp/#{name}-tmp.pdf"
stamped_template_path = "tmp/#{name}-stamped.pdf"
FileUtils.copy(template_form_path, stamped_template_path)
end
tempfile = create_tempfile
FileUtils.touch(tempfile)
FileUtils.copy_file(tempfile.path, stamped_template_path)

if File.exist? stamped_template_path
begin
Expand All @@ -49,17 +41,14 @@ def generate(current_loa = nil)
raise "stamped template file does not exist: #{stamped_template_path}"
end
end
# rubocop:enable Metrics/MethodLength

def create_tempfile
if Flipper.enabled?(:champva_unique_temp_file_names, @user)
# Tempfile workaround inspired by this:
# https://github.com/actions/runner-images/issues/4443#issuecomment-965391736
template_form_path = "#{TEMPLATE_BASE}/#{form_number}.pdf"
Tempfile.new(['', '.pdf'], Rails.root.join('tmp')).tap do |tmpfile|
IO.copy_stream(template_form_path, tmpfile)
tmpfile.close
end
# Tempfile workaround inspired by this:
# https://github.com/actions/runner-images/issues/4443#issuecomment-965391736
template_form_path = "#{TEMPLATE_BASE}/#{form_number}.pdf"
Tempfile.new(['', '.pdf'], Rails.root.join('tmp')).tap do |tmpfile|
IO.copy_stream(template_form_path, tmpfile)
tmpfile.close
end
end

Expand Down
30 changes: 5 additions & 25 deletions modules/ivc_champva/spec/models/vha_10_7959c_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,12 @@
end

describe '#handle_attachments' do
context 'Feature champva_unique_temp_file_names=true' do
before do
allow(Flipper).to receive(:enabled?).with(:champva_unique_temp_file_names, nil).and_return(true)
end

let(:file_path) { "#{uuid}_vha_10_7959c-tmp.pdf" }

it 'renames the file and returns the new file path' do
allow(File).to receive(:rename)
result = instance.handle_attachments(file_path)
expect(result).to eq(["#{uuid}_vha_10_7959c-tmp.pdf"])
end
end
let(:file_path) { "#{uuid}_vha_10_7959c-tmp.pdf" }

context 'Feature champva_unique_temp_file_names=false' do
before do
allow(Flipper).to receive(:enabled?).with(:champva_unique_temp_file_names, nil).and_return(false)
end

let(:file_path) { 'vha_10_7959c-tmp.pdf' }

it 'renames the file and returns the new file path' do
allow(File).to receive(:rename)
result = instance.handle_attachments(file_path)
expect(result).to eq(["#{uuid}_vha_10_7959c-tmp.pdf"])
end
it 'renames the file and returns the new file path' do
allow(File).to receive(:rename)
result = instance.handle_attachments(file_path)
expect(result).to eq(["#{uuid}_vha_10_7959c-tmp.pdf"])
end
end

Expand Down
30 changes: 5 additions & 25 deletions modules/ivc_champva/spec/models/vha_10_7959f_1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,12 @@
end

describe '#handle_attachments' do
context 'Feature champva_unique_temp_file_names=true' do
before do
allow(Flipper).to receive(:enabled?).with(:champva_unique_temp_file_names, nil).and_return(true)
end

let(:file_path) { "#{uuid}_vha_10_7959f_1-tmp.pdf" }

it 'renames the file and returns the new file path' do
allow(File).to receive(:rename)
result = instance.handle_attachments(file_path)
expect(result).to eq(["#{uuid}_vha_10_7959f_1-tmp.pdf"])
end
end
let(:file_path) { "#{uuid}_vha_10_7959f_1-tmp.pdf" }

context 'Feature champva_unique_temp_file_names=false' do
before do
allow(Flipper).to receive(:enabled?).with(:champva_unique_temp_file_names, nil).and_return(false)
end

let(:file_path) { 'vha_10_7959f_1-tmp.pdf' }

it 'renames the file and returns the new file path' do
allow(File).to receive(:rename)
result = instance.handle_attachments(file_path)
expect(result).to eq(["#{uuid}_vha_10_7959f_1-tmp.pdf"])
end
it 'renames the file and returns the new file path' do
allow(File).to receive(:rename)
result = instance.handle_attachments(file_path)
expect(result).to eq(["#{uuid}_vha_10_7959f_1-tmp.pdf"])
end
end

Expand Down
30 changes: 5 additions & 25 deletions modules/ivc_champva/spec/models/vha_10_7959f_2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,12 @@
end

describe '#handle_attachments' do
context 'Feature champva_unique_temp_file_names=true' do
before do
allow(Flipper).to receive(:enabled?).with(:champva_unique_temp_file_names, nil).and_return(true)
end

let(:file_path) { "#{uuid}_vha_10_7959f_2-tmp.pdf" }

it 'renames the file and returns the new file path' do
allow(File).to receive(:rename)
result = instance.handle_attachments(file_path)
expect(result).to eq(["#{uuid}_vha_10_7959f_2-tmp.pdf"])
end
end
let(:file_path) { "#{uuid}_vha_10_7959f_2-tmp.pdf" }

context 'Feature champva_unique_temp_file_names=false' do
before do
allow(Flipper).to receive(:enabled?).with(:champva_unique_temp_file_names, nil).and_return(false)
end

let(:file_path) { 'vha_10_7959f_2-tmp.pdf' }

it 'renames the file and returns the new file path' do
allow(File).to receive(:rename)
result = instance.handle_attachments(file_path)
expect(result).to eq(["#{uuid}_vha_10_7959f_2-tmp.pdf"])
end
it 'renames the file and returns the new file path' do
allow(File).to receive(:rename)
result = instance.handle_attachments(file_path)
expect(result).to eq(["#{uuid}_vha_10_7959f_2-tmp.pdf"])
end
end

Expand Down
Loading

0 comments on commit 1f43fc3

Please sign in to comment.