Skip to content

Commit

Permalink
APPEALS-60687 - Check for held hearings
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-ducker committed Nov 27, 2024
1 parent 28685e2 commit 18d0a8e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 222 deletions.
233 changes: 21 additions & 212 deletions app/jobs/hearings/va_box_download_job.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Hearings::VaBoxDownloadJob < CaseflowJob
include Hearings::SendTranscriptionIssuesEmail

queue_as :low_priority
S3_BUCKET = "vaec-appeals-caseflow"

Expand All @@ -10,6 +12,7 @@ class VaBoxDownloadUnzipError < StandardError; end
class VaBoxDownloadHearingError < StandardError; end
class VaBoxDownloadTranscriptionError < StandardError; end
class VaBoxDownloadTranscriptionPackageError < StandardError; end
class VaBoxDownloadHearingHeldError < StandardError; end

def perform(files_info)
@files = []
Expand All @@ -26,6 +29,12 @@ def perform(files_info)
def raise_error(message, error = VaBoxDownloadJobError)
cleanup_tmp_files
Rails.logger.error message

job_summary = message + "\n\nFiles in batch: \n\n"
@files.each do |file|
job_summary += file[:name] + "\n"
end
send_transcription_issues_email(job_summary)
fail error
end

Expand All @@ -45,7 +54,7 @@ def download_files_from_box(files)
}
end
rescue StandardError
raise_error("Failed to download file " + file[:name] + " from box", VaBoxDownloadBoxError)
raise_error("Failed to download file #{file[:name]} from box", VaBoxDownloadBoxError)
end
end
end
Expand Down Expand Up @@ -88,11 +97,17 @@ def confirm_hearing(file)
hearing = find_hearing(file)

if hearing
transcription = find_transcription(file)
if transcription
file[:transcription_id] = transcription.id
if hearing.held?
transcription = find_transcription(file)
if transcription
file[:transcription_id] = transcription.id
else
raise_error("Missing transcription for #{file[:name]}", VaBoxDownloadTranscriptionError)
end
else
raise_error("Missing transcription for #{file[:name]}", VaBoxDownloadTranscriptionError)
raise_error(
"Hearing (docket ##{file[:docket_number]}) not held for #{file[:name]}", VaBoxDownloadHearingHeldError
)
end
else
raise_error("Missing hearing for #{file[:name]}", VaBoxDownloadHearingError)
Expand Down Expand Up @@ -201,7 +216,7 @@ def unzip_file(file)

files
rescue StandardError => error
raise_error("Error unzipping file #{file[:path]}: #{error.message}", VaBoxDownloadUnzipError)
raise_error("Error unzipping file #{file[:name]}: #{error.message}", VaBoxDownloadUnzipError)
end
end

Expand All @@ -226,209 +241,3 @@ def s3_location(file_name, file_type)
"#{folder_name}/#{transcript_text_path}/#{file_name}"
end
end

# class Hearings::VaBoxDownloadJob < CaseflowJob
# queue_as :low_priority
# S3_BUCKET = "vaec-appeals-caseflow"

# class BoxDownloadError < StandardError; end
# class BoxDownloadJobFileUploadError < StandardError; end

# def perform(files_info)
# @all_paths = []
# box_service = ExternalApi::VaBoxService.new

# files_info.collect do |current_file|
# file_name = current_file[:name]
# tmp_folder = select_folder(file_name)
# file_extension = extract_file_extension(file_name)
# begin
# box_service.download_file(current_file[:id], tmp_folder)
# @all_paths << tmp_folder
# if file_extension == "zip"
# unzip_file(tmp_folder, current_file)
# else
# upload_s3_modified_transcription_file(tmp_folder, current_file)
# end
# end
# end
# cleanup_tmp_files
# end

# private

# def extract_file_extension(file_name)
# File.extname(file_name).delete(".").to_s
# end

# def update_database(current_information, file_status)
# transcription_records = TranscriptionFile.where(
# hearing_id: current_information["id"].to_i,
# hearing_type: current_information["hearing_type"],
# file_type: current_information["file_type"]
# )

# current_extension = extract_file_extension(current_information["file_name"])
# transcript_text_path = (current_extension == "pdf") ? "transcript_pdf" : "transcript_text"
# aws_link = create_link_aws(current_information, file_status, transcript_text_path)

# if transcription_records.count > 0
# update_transcription_file_record(transcription_records, current_information, file_status, aws_link)
# else
# create_transcription_file_record(current_information, file_status, aws_link)
# end

# modified_task_tree(current_information, file_status)
# rescue ActiveRecord::RecordInvalid => error
# Rails.logger.error "Failed to create transcription file: #{error.message}"
# end

# def modified_task_tree(current_information, file_status)
# if current_information["hearing_type"] == "Hearing"
# current_hearing = Hearing.find(current_information["id"])
# if current_hearing.disposition == "held"
# create_review_transcript_task(current_hearing.appeal_id, file_status)
# end
# else
# current_legacy_hearing = LegacyHearing.find(current_information["id"].to_i)
# current_vacols_id = current_legacy_hearing.current_vacols_id
# current_appeal_id = current_legacy_hearing.appeal_id
# hearing_record = VACOLS::CaseHearing.for_appeals(current_vacols_id)
# if hearing_record.hearing_disp == "H"
# create_review_transcript_task(current_appeal_id, file_status)
# end
# end
# end

# def create_review_transcript_task(appeal_id, file_status)
# if file_status == "Successful upload (AWS)"
# appeal = Appeal.find(appeal_id)
# root_task = RootTask.find_or_create_by!(appeal: appeal)
# ReviewTranscriptTask.create!(
# appeal: appeal,
# parent: root_task
# )
# end
# end

# def create_link_aws(current_information, file_status, transcript_text_path)
# if file_status == "Failed upload (AWS)"
# nil
# else
# "vaec-appeals-caseflow-test/#{transcript_text_path}/#{current_information['file_name']}"
# end
# end

# def create_transcription_file_record(current_information, file_status, aws_link)
# if current_information["hearing_type"] == "Hearing"
# transcription_id = Hearing.find(current_information["id"]).transcription&.id
# else
# current_legacy_hearing = LegacyHearing.find(current_information["id"].to_i)
# current_vacols_id = current_legacy_hearing.current_vacols_id
# hearings = VACOLS::CaseHearing.for_appeals(current_vacols_id)
# transcription_id = hearings.taskno
# end

# unless transcription_id
# fail StandardError, "Transcription for #{current_information['hearing_type']} \
# - ID: #{current_information['id']} does not exist."
# end

# add_transcription_file_record(current_information, file_status, aws_link, transcription_id)
# end

# def add_transcription_file_record(current_information, file_status, aws_link, transcription_id)
# TranscriptionFile.create!(
# hearing_id: current_information["id"],
# hearing_type: current_information["hearing_type"],
# docket_number: current_information["docket_number"],
# file_name: current_information["file_name"],
# file_type: current_information["file_type"],
# file_status: file_status,
# transcription_id: transcription_id,
# date_upload_aws: Time.zone.now,
# aws_link: aws_link
# )
# end

# def update_transcription_file_record(transcription_records, current_information, file_status, aws_link)
# transcription_records.each do |tr|
# tr.update!(
# date_upload_aws: Time.zone.now,
# updated_at: Time.zone.now,
# date_returned_box: current_information["date_returned_box"],
# file_status: file_status,
# aws_link: aws_link
# )
# end
# end

# def select_folder(file_name)
# file_extension = extract_file_extension(file_name)
# current_path = Rails.root.join("tmp", "file_from_box", file_extension.to_s, file_name.to_s)
# FileUtils.mkdir_p(File.dirname(current_path)) unless Dir.exist?(file_extension)
# current_path
# end

# def unzip_file(tmp_folder, current_file)
# file_extension = extract_file_extension(current_file[:name])
# Zip::File.open(tmp_folder) do |zip_file|
# list_files = []
# zip_file.each do |f|
# f_path = Rails.root.join("tmp", "file_from_box", file_extension.to_s, f.name.to_s)
# FileUtils.mkdir_p(File.dirname(f_path))
# zip_file.extract(f, f_path) unless File.exist?(f_path)
# list_files << f_path unless File.directory?(f_path.to_s)
# end
# list_files.each do |my_file|
# @all_paths << my_file.to_s
# upload_s3_modified_transcription_file(my_file.to_s, current_file)
# end
# end
# end

# def upload_to_s3(tmp_folder, file_name)
# begin
# S3Service.store_file(s3_location(file_name), tmp_folder, :filepath)
# rescue StandardError => error
# Rails.logger.error "Error to upload #{file_name} to S3: #{error.message}"
# raise BoxDownloadJobFileUploadError
# end
# end

# def s3_location(file_name)
# transcript_text_path = (extract_file_extension(file_name) == "pdf") ? "transcript_pdf" : "transcript_text"
# folder_name = (Rails.deploy_env == :prod) ? S3_BUCKET : "#{S3_BUCKET}-#{Rails.deploy_env}"
# "#{folder_name}/#{transcript_text_path}/#{file_name}"
# end

# def get_information(file_name, current_file)
# info = {}
# info["id"] = file_name.split("_")[1]
# info["hearing_type"] = file_name.split("_")[2].split(".")[0]
# info["docket_number"] = file_name.split("_")[0]
# info["file_type"] = File.extname(file_name).delete(".").to_s
# info["date_returned_box"] = current_file[:created_at]
# info["file_name"] = file_name
# info
# end

# def upload_s3_modified_transcription_file(file_path, current_file)
# begin
# if File.exist?(file_path)
# file_name = File.basename(file_path)
# s3_upload_result = upload_to_s3(file_path, file_name)
# file_status = (s3_upload_result == file_path) ? "Failed upload (AWS)" : "Successful upload (AWS)"
# info = get_information(file_name, current_file)
# update_database(info, file_status)
# else
# fail "The file does not exist"
# end
# end
# end

# def cleanup_tmp_files
# @all_paths&.each { |path| File.delete(path) if File.exist?(path) }
# Rails.logger.info("Cleaned up the following files from tmp: #{@all_paths}")
# end
# end
1 change: 1 addition & 0 deletions spec/factories/vacols/case_hearing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
factory :case_hearing, class: VACOLS::CaseHearing do
hearing_type { HearingDay::REQUEST_TYPES[:video] }
hearing_date { Time.zone.today }
hearing_pkseq { nil }
room { 1 }
folder_nr { create(:case).bfkey }

Expand Down
28 changes: 18 additions & 10 deletions spec/jobs/hearings/va_box_download_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# frozen_string_literal: true

RSpec.describe Hearings::VaBoxDownloadJob, type: :job do
# open question:

# 3. Account for XLS, make sure it has a package

# 4. Held hearings

def create_file_info(hearing, file_id, file_type)
{
name: "#{hearing.docket_number}_#{hearing.id}_#{hearing.class.name}.#{file_type}",
Expand All @@ -16,14 +10,16 @@ def create_file_info(hearing, file_id, file_type)
}
end

let!(:hearing_1) { create(:hearing) }
let!(:hearing_1) { create(:hearing, disposition: "held") }
let!(:transcription_1) { create(:transcription, hearing: hearing_1, hearing_type: "Hearing") }

let(:legacy_hearing_1) { create(:legacy_hearing) }
let!(:legacy_hearing_1) { create(:legacy_hearing, disposition: "H") }
let!(:legacy_hearing_2) { create(:legacy_hearing) }

let!(:transcription_2) { create(:transcription, hearing: legacy_hearing_1, hearing_type: "LegacyHearing") }

let!(:hearing_2) { create(:hearing) }
let!(:hearing_3) { create(:hearing) }
let!(:hearing_2) { create(:hearing, disposition: "held") }
let!(:hearing_3) { create(:hearing, disposition: "held") }
let!(:transcription_3) { create(:transcription, hearing: hearing_3, hearing_type: "Hearing") }

let!(:transcription_package_1) { create(:transcription_package, task_number: "BVA2024001") }
Expand Down Expand Up @@ -59,6 +55,12 @@ def create_file_info(hearing, file_id, file_type)
]
end

let(:file_info_hearing_not_held) do
[
create_file_info(legacy_hearing_2, "164008615821", "pdf")
]
end

let(:file_info_no_transcript) do
[
create_file_info(hearing_2, "164008615821", "pdf")
Expand Down Expand Up @@ -199,6 +201,12 @@ def create_file_info(hearing, file_id, file_type)
)
end

it "handles errors from hearing not being held" do
expect { described_class.perform_now(file_info_hearing_not_held) }.to raise_error(
Hearings::VaBoxDownloadJob::VaBoxDownloadHearingHeldError
)
end

it "handles errors from missing transcript" do
expect { described_class.perform_now(file_info_no_transcript) }.to raise_error(
Hearings::VaBoxDownloadJob::VaBoxDownloadTranscriptionError
Expand Down

0 comments on commit 18d0a8e

Please sign in to comment.