diff --git a/lib/helpers/pre_docket_ihp_tasks.rb b/lib/helpers/pre_docket_ihp_tasks.rb new file mode 100644 index 00000000000..f294217ab31 --- /dev/null +++ b/lib/helpers/pre_docket_ihp_tasks.rb @@ -0,0 +1,111 @@ +# frozen_string_literal: true + +# ************************ +# Remediates IHP tasks that are generated prior to completion of Pre-Docket task +# If an InformalHearingPresentationTask is present prior to PreDocketTask being completed +# we create a new DistributionTask and set the InformalHearingPresentationTask as a child +# This will become a blocking task and allow the PreDocketTask to be completed prior to +# the InformalHearingPresentationTask being completed +# ************************ +module WarRoom + class PreDocketIhpTasks + def run(appeal_uuid) + @appeal = Appeal.find_appeal_by_uuid_or_find_or_create_legacy_appeal_by_vacols_id(appeal_uuid) + if @appeal.appeal_state&.appeal_docketed + puts("Appeal has been docketed. Aborting...") + fail Interrupt + end + + predocket_task.update!(parent_id: ihp_task.id) + + ihp_task.update!(parent_id: distribution_task.id) + ihp_task.on_hold! + rescue ActiveRecord::RecordNotFound => _error + puts("Appeal was not found. Aborting...") + raise Interrupt + rescue StandardError => error + puts("Something went wrong. Requires manual remediation. Error: #{error} Aborting...") + raise Interrupt + end + + private + + def root_task + if @appeal.root_task + @root_task = @appeal.root_task + else + puts("No RootTask found. Aborting...") + fail Interrupt + end + end + + def distribution_task + @distribution_task ||= + if (distribution_tasks = @appeal.tasks.where(type: "DistributionTask").all).count > 1 + puts("Duplicate DistributionTask found. Remove the erroneous task and retry. Aborting...") + fail Interrupt + elsif distribution_tasks.count == 1 + distribution_tasks[0].on_hold! + distribution_tasks[0] + elsif distribution_tasks.empty? + dt = DistributionTask.create!(appeal: @appeal, parent: root_task) + dt.on_hold! + dt + else + puts("DistributionTask failed to inflate. Aborting...") + fail Interrupt + end + end + + # we look for only 1 PredocketTask. + # * If multiples are found we bail. + # * If none are found we bail. + def predocket_task + return @predocket_task unless @predocket_task.nil? + + predocket_tasks = @appeal.tasks.where(type: "PreDocketTask").all + if predocket_tasks.count > 1 + puts("Multiple PredocketTask found. Remove the erroneous task and retry. Aborting...") + fail Interrupt + elsif predocket_tasks.count < 1 + puts("No PredocketTask found. This may already be fixed. Aborting...") + fail Interrupt + else + @predocket_task = predocket_tasks[0] + end + end + + # we look for only 1 InformalHearingPresentationTask. + # * If multiples are found we bail. + # * If none are found we bail. + # The status of the InformalHearingPresentationTask must be + # * assigned + # * on_hold + # If the status is anything else we bail. + def ihp_task + return @ihp_task unless @ihp_task.nil? + + ihp_tasks = @appeal.tasks.where(type: "InformalHearingPresentationTask").all + if ihp_tasks.count > 1 + puts("Duplicate InformalHearingPresentationTask found. Remove the erroneous task and retry. Aborting...") + fail Interrupt + elsif ihp_tasks.count <= 0 + puts("No InformalHearingPresentationTask found. Aborting...") + fail Interrupt + end + + # TODO: Need to determine the branch for cancelled status' + # If the status is cancelled does the InformalHearingPresentationTask reopen after docketing the appeal? + possible_ihp_task = ihp_tasks[0] + if possible_ihp_task.status.include?([Constants.TASK_STATUSES.assigned, Constants.TASK_STATUSES.on_hold]) + @ihp_task = possible_ihp_task + elsif possible_ihp_task.status.include?([Constants.TASK_STATUSES.cancelled]) + puts("InformalHearingPresentationTask has a status of cancelled. This is not supported for automated remediation yet. Aborting...") + fail Interrupt + else + puts("InformalHearingPresentationTask is not in the correct status for remediation. Aborting...") + fail Interrupt + end + end + end +end diff --git a/lib/helpers/scripts/predocket-ihp-task-script.sh b/lib/helpers/scripts/predocket-ihp-task-script.sh new file mode 100755 index 00000000000..36252ac160e --- /dev/null +++ b/lib/helpers/scripts/predocket-ihp-task-script.sh @@ -0,0 +1,6 @@ +#! /bin/bash +cd /opt/caseflow-certification/src; bin/rails c << DONETOKEN +x = WarRoom::PreDocketIhpTasks.new +x.run("$1") +DONETOKEN +