Skip to content

Commit

Permalink
Hotfix/appeals 37269 #2 (#22886)
Browse files Browse the repository at this point in the history
* adds script and tests for the script

* adjust the way scheduled_for is created since the previous method was removed

* refactor some of the script

* rename test file to include _spec

* push empty commit to rerun tests

---------

Co-authored-by: Zackary Borges-Rowe <borges-rowe_zackary@bah.com>
Co-authored-by: Ron Wabukenda <130374706+ronwabVa@users.noreply.github.com>
  • Loading branch information
3 people authored and davywentwortht2it committed Sep 30, 2024
1 parent adcc877 commit 1ad164c
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 0 deletions.
133 changes: 133 additions & 0 deletions app/jobs/missing_vacols_hearing_job_fix.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# frozen_string_literal: true

# ************************
# Remediates AssignHearingDispositionTasks that are stuck in the
# 'assigned' status due to a VACOLS record not existing for the
# associated LegacyHearing. This job will create a VACOLS record for the
# associated LegacyHearing, cancel the task, then destroy the VACOLS record.
# The VACOLS record is deleted because the exact time of the hearing cannot be
# recorded accurately and we do not want any false data to exist.
# ************************
class MissingVacolsHearingJobFix
def initialize
@stuck_job_report_service = StuckJobReportService.new
@start_time = nil
@end_time = nil
@tasks_missing_hearings = []
end

def perform
start_time
RequestStore[:current_user] = User.system_user
return "There are no stuck AssignHearingDisposition tasks." if questionable_tasks.blank?

process_missing_vacols_records

unless @tasks_missing_hearings.blank?
Rails.logger.error("ALERT------- Task Id's: #{@tasks_missing_hearings.to_sentence} are missing" \
" associated hearings. This requires manual remediation------- ALERT")
end
end_time
log_processing_time
end

def process_missing_vacols_records
questionable_tasks
# These tasks are stuck because they have a LegacyHearing associated with them that do not exist in VACOLS

stuck_tasks(questionable_tasks).each do |task|
task = Task.find(task.id)
appeal = task.appeal
hearing = task.hearing

attributes = get_attributes(appeal, hearing)
process_vacols_record(attributes, task)
end
rescue StandardError => error
Rails.logger.error("Something went wrong. Requires manual remediation. Error: #{error} Aborting...")

raise error
end

def questionable_tasks
Task.where(
type: "AssignHearingDispositionTask",
status: "assigned",
appeal_type: "LegacyAppeal",
assigned_at: 7.years.ago..6.months.ago
)
end

# :reek:FeatureEnvy
def stuck_tasks(questionable_tasks)
tasks_with_hearing_missing_vacols_records = []
questionable_tasks.each do |task|
if task.hearing.nil?
@tasks_missing_hearings.push(task.id)
next
end

if VACOLS::CaseHearing.find_by(hearing_pkseq: task.hearing.vacols_id).nil?
# adds the stuck task associated with the missing VACOLS hearing
tasks_with_hearing_missing_vacols_records.push(task)
end
end

tasks_with_hearing_missing_vacols_records
end

def process_vacols_record(attrs, task)
vacols_record = create_hearing_in_vacols(attrs)
if vacols_record
task.hearing.update(vacols_id: vacols_record[:hearing_pkseq])
task.cancelled!
vacols_record.destroy!
end
rescue StandardError => error
Rails.logger.error("Something went wrong. Requires manual remediation. Error: #{error} Aborting...")
raise Interrupt
end

def create_hearing_in_vacols(attrs)
vacols_record = VACOLS::CaseHearing.create_hearing!(
folder_nr: attrs[:appeal].vacols_id,
hearing_pkseq: attrs[:vacols_id],
hearing_date: VacolsHelper.format_datetime_with_utc_timezone(attrs[:scheduled_for]),
vdkey: attrs[:hearing_day].id,
hearing_type: attrs[:hearing_day].request_type,
room: attrs[:hearing_day].room,
board_member: attrs[:hearing_day].judge ? attrs[:hearing_day].judge.vacols_attorney_id : nil,
vdbvapoc: attrs[:hearing_day].bva_poc,
notes1: attrs[:notes]
)

vacols_record
end

def get_attributes(appeal, hearing)
scheduled_for = HearingDatetimeService.prepare_datetime_for_storage(
date: hearing.hearing_day.scheduled_for,
time_string: "1:00 PM Central Time (US & Canada)"
)

attrs = {
hearing_day: hearing.hearing_day,
appeal: appeal,
scheduled_for: scheduled_for,
notes: ""
}
attrs
end

def log_processing_time
(@end_time && @start_time) ? @end_time - @start_time : 0
end

def start_time
@start_time ||= Time.zone.now
end

def end_time
@end_time ||= Time.zone.now
end
end
5 changes: 5 additions & 0 deletions lib/helpers/scripts/missing_vacols_hearing_job_fix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /bin/bash
cd /opt/caseflow-certification/src; bin/rails c << DONETOKEN
x = MissingVacolsHearingJobFix.new
x.perform
DONETOKEN
88 changes: 88 additions & 0 deletions spec/jobs/missing_vacols_hearing_job_fix_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# frozen_string_literal: true

describe MissingVacolsHearingJobFix do
subject { MissingVacolsHearingJobFix.new }

let!(:hearing) { create(:legacy_hearing, :with_tasks) }
let!(:hearing2) { create(:legacy_hearing, :with_tasks) }
let!(:hearing3) { create(:legacy_hearing, :with_tasks) }

context "missing_vacols_hearing" do
context "when AssignHearingDispositionTask is outdated and the associated hearing doesn't exist in VACOLS" do
it "cancels the task" do
task = AssignHearingDispositionTask.find_by(appeal: hearing.appeal)
task2 = AssignHearingDispositionTask.find_by(appeal: hearing2.appeal)
task3 = AssignHearingDispositionTask.find_by(appeal: hearing3.appeal)

vacols_case2 = VACOLS::CaseHearing.find_by(hearing_pkseq: task2.hearing.vacols_id)
vacols_case3 = VACOLS::CaseHearing.find_by(hearing_pkseq: task3.hearing.vacols_id)

task2.update(assigned_at: 3.years.ago)
task3.update(assigned_at: 1.year.ago)

expect(task.status).to eq("assigned")
expect(task2.status).to eq("assigned")
expect(VACOLS::CaseHearing.count).to eq(3)
vacols_case2.destroy!
vacols_case3.destroy!

expect(VACOLS::CaseHearing.count).to eq(1)
subject.perform

expect(VACOLS::CaseHearing.count).to eq(1)
expect(task.reload.status).to eq("assigned")
expect(task2.reload.status).to eq("cancelled")
expect(task3.reload.status).to eq("cancelled")
expect(task.hearing).to_not eq(nil)
expect(task3.hearing).to_not eq(nil)
end
end
context "when one of the AssignHearingDispositionTasks does not have an associated hearing" do
it "continues with the rest of the tasks and logs the error" do
allow(Rails.logger).to receive(:error)
task2 = AssignHearingDispositionTask.find_by(appeal: hearing2.appeal)
task3 = AssignHearingDispositionTask.find_by(appeal: hearing3.appeal)
vacols_case2 = VACOLS::CaseHearing.find_by(hearing_pkseq: task2.hearing.vacols_id)
vacols_case3 = VACOLS::CaseHearing.find_by(hearing_pkseq: task3.hearing.vacols_id)
task2.update(assigned_at: 3.years.ago)
task3.update(assigned_at: 1.year.ago)
string = "ALERT------- Task Id's: #{task2.id} are missing associated "\
"hearings. This requires manual remediation------- ALERT"
vacols_case2.destroy
vacols_case3.destroy
task2.hearing.destroy
expect(task2.status).to eq(Constants.TASK_STATUSES.assigned)
expect(task3.status).to eq(Constants.TASK_STATUSES.assigned)
expect(task3.status).to eq(Constants.TASK_STATUSES.assigned)
Rails.logger.should_receive(:error).with(string)
subject.perform
expect(task3.reload.status).to eq(Constants.TASK_STATUSES.cancelled)
expect(task2.reload.status).to eq(Constants.TASK_STATUSES.assigned)
end
end
context "when one of the qualifying tasks is outside of the assigned_at parameters " do
it "should not process tasks that have an 'assigned_at' of over 7 years or before 5 months" do
task2 = AssignHearingDispositionTask.find_by(appeal: hearing2.appeal)
task3 = AssignHearingDispositionTask.find_by(appeal: hearing3.appeal)
vacols_case2 = VACOLS::CaseHearing.find_by(hearing_pkseq: task2.hearing.vacols_id)
vacols_case3 = VACOLS::CaseHearing.find_by(hearing_pkseq: task3.hearing.vacols_id)

task2.update(assigned_at: 3.months.ago)
task3.update(assigned_at: 8.years.ago)
vacols_case2.destroy
vacols_case3.destroy

expect(task2.status).to eq(Constants.TASK_STATUSES.assigned)
expect(task3.status).to eq(Constants.TASK_STATUSES.assigned)
subject.perform
expect(task2.status).to eq(Constants.TASK_STATUSES.assigned)
expect(task3.status).to eq(Constants.TASK_STATUSES.assigned)
end
end
context "when there are no qualifying AssignHearingDispositionTasks" do
it "should not fail/ return a successful message" do
expect(subject.perform).to be_truthy
end
end
end
end

0 comments on commit 1ad164c

Please sign in to comment.