Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store cancelled_by_id in task table #14365

Closed
4 tasks
hschallhorn opened this issue May 21, 2020 · 5 comments
Closed
4 tasks

Store cancelled_by_id in task table #14365

hschallhorn opened this issue May 21, 2020 · 5 comments
Assignees
Labels
Eng: Data Mark when data has been backfilled, or the issue has any data integrity concerns Feature: case-details Priority: Medium Blocking issue w/workaround, or "second in" priority for new work. Product: caseflow-queue Stakeholder: BVA Functionality associated with the Board of Veterans' Appeals workflows/feature requests Team: Echo 🐬 Type: Tech-Improvement

Comments

@hschallhorn
Copy link
Contributor

hschallhorn commented May 21, 2020

Currently, to show who has cancelled a task in case timeline, we are interrogating papertrail records to find out who cancelled a task

caseflow/app/models/task.rb

Lines 584 to 594 in 660bcf5

def cancelled_by
return nil unless cancelled?
any_status_matcher = Constants::TASK_STATUSES.keys.join("|")
task_cancelled_version_matcher = "%status:\\n- (#{any_status_matcher})\\n- #{Constants.TASK_STATUSES.cancelled}%"
record = versions.order(:created_at).where("object_changes SIMILAR TO ?", task_cancelled_version_matcher).last
return nil unless record
User.find_by_id(record.whodunnit)
end

Papertrail is a diagnostic and investigation tool, not something we should be using to display information to a user. Instead, we should store the id of the canceler on the actual task.

AC

Suggested steps

  1. Add cancelled_by_id migration to the tasks table
  2. Add association in task model belongs_to :cancelled_by, class_name: "User" and remove cancelled_by hacky method
  3. Ensure we're saving the canceller id when we cancel tasks. Including
    1. when the last issue on an appeal is removed (and we cancel all tasks)
    2. when the cancellation of a task cascades up or down a task tree
  4. Backfill any nil cancelled_bys on cancelled tasks by checking the paper trail records, if they exist
Task.cancelled.each do |task|
  any_status_matcher = Constants::TASK_STATUSES.keys.join("|")
  task_cancelled_version_matcher = "%status:\\n- (#{any_status_matcher})\\n- #{Constants.TASK_STATUSES.cancelled}%"
  record = task.versions.order(:created_at).where("object_changes SIMILAR TO ?", task_cancelled_version_matcher).last

  record&.whodunnit
end

Notes

  • Be sure to add :cancelled_by to task_includes to ensure we're eager loading these users for all tasks on an appeal
@hschallhorn hschallhorn added Type: Tech-Improvement Product: caseflow-queue Feature: case-details Stakeholder: BVA Functionality associated with the Board of Veterans' Appeals workflows/feature requests Eng: Data Mark when data has been backfilled, or the issue has any data integrity concerns Team: Echo 🐬 Priority: Medium Blocking issue w/workaround, or "second in" priority for new work. labels May 21, 2020
@yoomlam
Copy link
Contributor

yoomlam commented May 21, 2020

what is this chart?

1 | 
2 | 
3 | |||||||
5 | ||||
8 | 

Review Bat Team docs and update any reference scripts that cancel to update this field

Why 3?

  • add column, add code to update column and check column
  • straightforward, but a few moving parts

Why 5?

  • May be hard to track down all places we cancel tasks. Will we have the cancelling user here as well?

@yoomlam
Copy link
Contributor

yoomlam commented Jun 10, 2020

Once this is done, add a comment to Create Cancelled_By_ID in Task Table #123 so reporting ETL can be updated.

@hschallhorn
Copy link
Contributor Author

Hacky method pulled from code for reuse in backfill.

  def cancelled_by
    return nil unless cancelled?

    any_status_matcher = Constants::TASK_STATUSES.keys.join("|")
    task_cancelled_version_matcher = "%status:\\n- (#{any_status_matcher})\\n- #{Constants.TASK_STATUSES.cancelled}%"
    record = versions.order(:created_at).where("object_changes SIMILAR TO ?", task_cancelled_version_matcher).last

    return nil unless record

    User.find_by_id(record.whodunnit)
  end

@hschallhorn hschallhorn self-assigned this Sep 3, 2020
@hschallhorn
Copy link
Contributor Author

Updating tasks in prod draft. We can only update tasks that were cancelled and have a whodunnit in their versions records

any_status_matcher = Constants::TASK_STATUSES.keys.join("|")
task_cancelled_version_matcher = "%status:\\n- (#{any_status_matcher})\\n- #{Constants.TASK_STATUSES.cancelled}%"

Task.cancelled.each do |task|
  whodunnit = task.versions.order(:created_at).where("object_changes SIMILAR TO ?", task_cancelled_version_matcher).where.not(whodunnit: nil).last&.whodunnit
  task.update!(cancelled_by_id: whodunnit) if whodunnit
end

@hschallhorn
Copy link
Contributor Author

Task.cancelled.where.not(cancelled_by_id: nil).count
=> 886

any_status_matcher = Constants::TASK_STATUSES.keys.join("|")
task_cancelled_version_matcher = "%status:\\n- (#{any_status_matcher})\\n- #{Constants.TASK_STATUSES.cancelled}%"

tasks = Task.cancelled.where(cancelled_by_id: nil)
versions = PaperTrail::Version.where(item: tasks).where("object_changes SIMILAR TO ?", task_cancelled_version_matcher).where.not(whodunnit: nil)

version = versions.limit(1).first
version.item.update!(cancelled_by_id: version.whodunnit)
version.item.cancelled_by_id
=> 11140

versions.each { |version| version.item.update!(cancelled_by_id: version.whodunnit) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Eng: Data Mark when data has been backfilled, or the issue has any data integrity concerns Feature: case-details Priority: Medium Blocking issue w/workaround, or "second in" priority for new work. Product: caseflow-queue Stakeholder: BVA Functionality associated with the Board of Veterans' Appeals workflows/feature requests Team: Echo 🐬 Type: Tech-Improvement
Projects
None yet
Development

No branches or pull requests

2 participants