Skip to content

Commit

Permalink
reverse flipper
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudmagic80 committed Nov 26, 2024
1 parent eff3d53 commit b7a1712
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 104 deletions.
3 changes: 0 additions & 3 deletions config/features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,6 @@ features:
champva_unique_temp_file_names:
actor_type: user
description: Enables unique temp file names for CHAMPVA PDF files
champva_file_recreate:
actor_type: user
description: Enhances file creation reliability by adding retry functionality in the vets-api tmp folder
champva_failure_email_job_enabled:
actor_type: user
description: Enables sending failure notification emails for IVC CHAMPVA form submissions that lack a Pega status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,25 @@ class UploadsController < ApplicationController
'10-7959A' => 'vha_10_7959a'
}.freeze

if Flipper.enabled?(:champva_file_recreate, @user)
def submit
Datadog::Tracing.trace('Start IVC File Submission') do
form_id = get_form_id
Datadog::Tracing.active_trace&.set_tag('form_id', form_id)
parsed_form_data = JSON.parse(params.to_json)
statuses, error_message = handle_file_uploads(form_id, parsed_form_data)
def submit
Datadog::Tracing.trace('Start IVC File Submission') do
form_id = get_form_id
Datadog::Tracing.active_trace&.set_tag('form_id', form_id)
parsed_form_data = JSON.parse(params.to_json)
statuses, error_message = handle_file_uploads(form_id, parsed_form_data)

response = build_json(Array(statuses), error_message)
response = build_json(Array(statuses), error_message)

if @current_user && response[:status] == 200
InProgressForm.form_for_user(params[:form_number], @current_user)&.destroy!
end

render json: response[:json], status: response[:status]
end
rescue => e
Rails.logger.error "Error: #{e.message}"
Rails.logger.error e.backtrace.join("\n")
render json: { error_message: "Error: #{e.message}" }, status: :internal_server_error
end
else
def submit
Datadog::Tracing.trace('Start IVC File Submission') do
form_id = get_form_id
Datadog::Tracing.active_trace&.set_tag('form_id', form_id)
parsed_form_data = JSON.parse(params.to_json)
file_paths, metadata = get_file_paths_and_metadata(parsed_form_data)
statuses, error_message = FileUploader.new(form_id, metadata, file_paths, true).handle_uploads
response = build_json(Array(statuses), error_message)

if @current_user && response[:status] == 200
InProgressForm.form_for_user(params[:form_number],
@current_user)&.destroy!
end

render json: response[:json], status: response[:status]
rescue => e
Rails.logger.error "Error: #{e.message}"
Rails.logger.error e.backtrace.join("\n")
render json: { error_message: "Error: #{e.message}" },
status: :internal_server_error
if @current_user && response[:status] == 200
InProgressForm.form_for_user(params[:form_number], @current_user)&.destroy!
end

render json: response[:json], status: response[:status]
end
rescue => e
Rails.logger.error "Error: #{e.message}"
Rails.logger.error e.backtrace.join("\n")
render json: { error_message: "Error: #{e.message}" }, status: :internal_server_error
end

def submit_supporting_documents
Expand All @@ -74,22 +49,20 @@ def submit_supporting_documents

private

if Flipper.enabled?(:champva_file_recreate, @user)
def handle_file_uploads(form_id, parsed_form_data)
def handle_file_uploads(form_id, parsed_form_data)
file_paths, metadata = get_file_paths_and_metadata(parsed_form_data)
statuses, error_message = FileUploader.new(form_id, metadata, file_paths, true).handle_uploads
statuses = Array(statuses)

# Retry attempt if specific error message is found
if statuses.any? do |status|
status.is_a?(String) && status.include?('No such file or directory @ rb_sysopen')
end
file_paths, metadata = get_file_paths_and_metadata(parsed_form_data)
statuses, error_message = FileUploader.new(form_id, metadata, file_paths, true).handle_uploads
statuses = Array(statuses)

# Retry attempt if specific error message is found
if statuses.any? do |status|
status.is_a?(String) && status.include?('No such file or directory @ rb_sysopen')
end
file_paths, metadata = get_file_paths_and_metadata(parsed_form_data)
statuses, error_message = FileUploader.new(form_id, metadata, file_paths, true).handle_uploads
end

[statuses, error_message]
end

[statuses, error_message]
end

def get_attachment_ids_and_form(parsed_form_data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,64 +186,62 @@
end
end

if Flipper.enabled?(:champva_file_recreate, @user)
describe '#handle_file_uploads' do
let(:controller) { IvcChampva::V1::UploadsController.new }

forms.each do |form_file|
form_id = form_file.gsub('vha_', '').gsub('.json', '').upcase
form_numbers_and_classes[form_id]

context "with form #{form_id}" do
let(:form_id) { form_id }
let(:parsed_form_data) do
JSON.parse(Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'form_json', form_file).read)
end
let(:file_paths) { ['/path/to/file1.pdf', '/path/to/file2.pdf'] }
let(:metadata) { { 'attachment_ids' => %w[id1 id2] } }
let(:file_uploader) { instance_double(IvcChampva::FileUploader) }
describe '#handle_file_uploads' do
let(:controller) { IvcChampva::V1::UploadsController.new }

forms.each do |form_file|
form_id = form_file.gsub('vha_', '').gsub('.json', '').upcase
form_numbers_and_classes[form_id]

context "with form #{form_id}" do
let(:form_id) { form_id }
let(:parsed_form_data) do
JSON.parse(Rails.root.join('modules', 'ivc_champva', 'spec', 'fixtures', 'form_json', form_file).read)
end
let(:file_paths) { ['/path/to/file1.pdf', '/path/to/file2.pdf'] }
let(:metadata) { { 'attachment_ids' => %w[id1 id2] } }
let(:file_uploader) { instance_double(IvcChampva::FileUploader) }

before do
allow(controller).to receive(:get_file_paths_and_metadata).and_return([file_paths, metadata])
allow(IvcChampva::FileUploader).to receive(:new).and_return(file_uploader)
end

context 'when file uploads succeed' do
before do
allow(controller).to receive(:get_file_paths_and_metadata).and_return([file_paths, metadata])
allow(IvcChampva::FileUploader).to receive(:new).and_return(file_uploader)
allow(file_uploader).to receive(:handle_uploads).and_return([[200], nil])
end

context 'when file uploads succeed' do
before do
allow(file_uploader).to receive(:handle_uploads).and_return([[200], nil])
end
it 'returns success statuses and no error message' do
statuses, error_message = controller.send(:handle_file_uploads, form_id, parsed_form_data)
expect(statuses).to eq([200])
expect(error_message).to be_nil
end
end

it 'returns success statuses and no error message' do
statuses, error_message = controller.send(:handle_file_uploads, form_id, parsed_form_data)
expect(statuses).to eq([200])
expect(error_message).to be_nil
end
context 'when file uploads fail with specific error message' do
before do
allow(file_uploader).to receive(:handle_uploads).and_return([['No such file or directory @ rb_sysopen'],
'File not found'])
end

context 'when file uploads fail with specific error message' do
before do
allow(file_uploader).to receive(:handle_uploads).and_return([['No such file or directory @ rb_sysopen'],
'File not found'])
end

it 'retries the file uploads and returns the final statuses and error message' do
allow(file_uploader).to receive(:handle_uploads).and_return([[200], nil])
statuses, error_message = controller.send(:handle_file_uploads, form_id, parsed_form_data)
expect(statuses).to eq([200])
expect(error_message).to be_nil
end
it 'retries the file uploads and returns the final statuses and error message' do
allow(file_uploader).to receive(:handle_uploads).and_return([[200], nil])
statuses, error_message = controller.send(:handle_file_uploads, form_id, parsed_form_data)
expect(statuses).to eq([200])
expect(error_message).to be_nil
end
end

context 'when file uploads fail with other errors' do
before do
allow(file_uploader).to receive(:handle_uploads).and_return([[400], 'Upload failed'])
end
context 'when file uploads fail with other errors' do
before do
allow(file_uploader).to receive(:handle_uploads).and_return([[400], 'Upload failed'])
end

it 'returns the error statuses and error message' do
statuses, error_message = controller.send(:handle_file_uploads, form_id, parsed_form_data)
expect(statuses).to eq([400])
expect(error_message).to eq('Upload failed')
end
it 'returns the error statuses and error message' do
statuses, error_message = controller.send(:handle_file_uploads, form_id, parsed_form_data)
expect(statuses).to eq([400])
expect(error_message).to eq('Upload failed')
end
end
end
Expand Down

0 comments on commit b7a1712

Please sign in to comment.