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

Validate that an appeal can have only one JudgeAssignTask active at a time #16072

Merged
merged 70 commits into from Jun 25, 2021

Conversation

eileen-nava
Copy link
Contributor

@eileen-nava eileen-nava commented Mar 30, 2021

Bumps #15220

Description

Add a validation that ensures only one JudgeAssignTask can be created at a time. This functionality can be extended to other tasks where there should only be one, such as ChangeHearingDispositionTask. I am leaving that work for another PR.

Acceptance Criteria

  • Code compiles correctly
  • No existing tests break
  • There is test coverage for the new validation
  • It is still possible to reassign a judge to a case through the UI

Testing Plan

  1. Ensure that automated tests pass.
  2. While running the site locally, test the reassign judge scenario
  • Switch to user BVAAABSHIRE (VACO)
  • Find an appeal that has a JudgeAssignTask already associated with it
  • In the “currently active tasks” section, select “re-assign judge”
    ReassignJudge
  • Ensure that you can still re-assign a judge without getting any error messages
  1. Through rails console, test that a second JudgeAssignTask cannot be made for the same appeal
    Find an appeal with a JudgeAssignTask. Next, find a user that will be assigned_to, and assigned_by.
appeal = JudgeAssignTask.last.appeal
assigned_to = User.first
assigned_by = User.last

Watch the validation fail after attempting to make a second JudgeAssignTask for this appeal.

FactoryBot.create(:ama_judge_assign_task, assigned_to: assigned_to, assigned_by: assigned_by, appeal: appeal)

Lessons Learned Along the Way

I wanted to document some of the bumps I hit while working on this PR.

The test initially failed on setup in the let block where it creates the first JudgeAssignTask. While debugging with Pry, I found that the validation was called twice when creating the first JudgeAssignTask.

While working on it with Yoom, he pointed out that I had written a validation which was called both when the record was inserted in the database and saved to the database.
Relevant documentation quote:

Creating and saving a new record will send an SQL INSERT operation to the database. Updating an existing record will send an SQL UPDATE operation instead. Validations are typically run before these commands are sent to the database.

Since my custom validation ran on update and create, it also caused unrelated tests to fail on setup. I changed the validation to only be called when the JudgeAssignTask task is saved to the database. Here's helpful documentation about custom validations.

Next, I locally tested reassigning a JudgeAssignTask to see if the functionality still works. Initially, it did not work because the reassign flow is

  1. create a duplicate task and assign it to another user
  2. cancel old task
    The on create validation made it impossible to do the above. I edited the reassign method so that it will allow two JudgeAssignTasks to simultaneously exist. I found this StackOverflow post helpful for my approach.

Updated approach

After pairing with Yoom and Bar, I revised my original approach in favor of using thread-local variables. You can read more about this approach in the associated hackmd document's Section 5, Thread-local variables. I want to highlight that thread-local variables use the Thread.current.thread_variable_set("foo", "bar") syntax, whereas fiber-local variables use the Thread.current["foo"] = "bar" syntax.

@codeclimate
Copy link

codeclimate bot commented Mar 30, 2021

Code Climate has analyzed commit 51873e2 and detected 0 issues on this pull request.

View more on Code Climate.

@eileen-nava eileen-nava changed the title WIP: Eileen/15220 validate one task open Eileen/15220 validate one task open Mar 31, 2021
@eileen-nava

This comment has been minimized.

@eileen-nava eileen-nava force-pushed the eileen/15220-validate-one-task-open branch from 37377f4 to 63861d3 Compare March 31, 2021 21:27
app/models/task.rb Outdated Show resolved Hide resolved
app/models/task.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@yoomlam yoomlam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woohoo!

@eileen-nava
Copy link
Contributor Author

eileen-nava commented Jun 24, 2021

@yoomlam Here is the expected error message.
Screen Shot 2021-06-24 at 3 36 10 PM

@va-bot
Copy link
Collaborator

va-bot commented Jun 25, 2021

1 Warning
⚠️ This PR modifies React components — consider adding/updating corresponding Storybook file

Generated by 🚫 Danger

@eileen-nava
Copy link
Contributor Author

@yoomlam I consulted design about the error message wording after getting the PR approved by Ann-Marie during a demo. Below is the current wording.

Screen Shot 2021-06-25 at 2 13 20 PM

I will merge this after the build passes.

@yoomlam
Copy link
Contributor

yoomlam commented Jun 25, 2021

@eileen-nava I think that message may be too specific. If the validation fails for other scenarios, this error message (mentioning "closing the window" and "previous window") is confusing.

@eileen-nava
Copy link
Contributor Author

@eileen-nava I think that message may be too specific. If the validation fails for other scenarios, this error message (mentioning "closing the window" and "previous window") is confusing.

Good point. I'll check in with design again. Thank you!

@eileen-nava
Copy link
Contributor Author

Here is the current error message.
Screen Shot 2021-06-25 at 3 31 38 PM

@eileen-nava eileen-nava added the Ready-to-Merge This PR is ready to be merged and will be picked up by va-bot to automatically merge to master label Jun 25, 2021
@va-bot va-bot merged commit 4069964 into master Jun 25, 2021
@va-bot va-bot deleted the eileen/15220-validate-one-task-open branch June 25, 2021 21:40
va-bot pushed a commit that referenced this pull request Aug 16, 2021
…eDecisionReviewTask (#16586)

Resolves jira ticket [Research and prevent recurrence of bug where one case has multiple JudgeAssignTasks, JudgeDecisionReviewTasks, and AttorneyTasks](https://vajira.max.gov/projects/CASEFLOW/issues/CASEFLOW-2075) .
Relates to github PR [Write feature test to replicate scenario with multiple Judge/Attorney tasks](#16582).

### Description
A bug occurred in production where a user created multiple Judge and Attorney tasks by doing the same actions in two different tabs, as described in [this comment from Yoom](https://github.com/department-of-veterans-affairs/dsva-vacols/issues/212#issuecomment-889401070).
The `JudgeAssignTask` [model already had a validation ](#16072) to ensure that an appeal cannot have more than one `JudgeAssignTask` open at the same time.  I expanded this validation to the `JudgeDecisionReviewTask` and `AttorneyTask` models.  I also refactored the validation to use instance variables instead of thread-local variables, as suggested in [this StackOverflow post](https://stackoverflow.com/questions/8881712/skip-certain-validation-method-in-model) that Yoom linked in a previous thread.


### Acceptance Criteria
- [ ] Code compiles correctly
- [ ] All automated tests pass
- [ ] Task validations no longer use thread-local variables
- [ ] If a user attempts to act on the same `JudgeAssignTask` in two different windows, they will not be able to create multiple `JudgeDecisionReviewTask`s and `AttorneyTask`s. 

### Testing Plan
[ ] Find an appeal with an open `JudgeAssignTask`
```
jat = JudgeAssignTask.open.last
appeal = jat.appeal
```
[ ] On local caseflow, switch users to the judge who has the `JudgeAssignTask` assigned to them
[ ] Navigate to the case details page for the relevant appeal. Open the same page in a second tab
`/queue/appeals/${appeal.uuid}`
[ ] In the first tab, reassign the `JudgeAssignTask` to another judge
[ ] In the second tab, attempt to complete the `JudgeAssignTask` by assigning the case to an attorney.  You should not be able to complete this action
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready-to-Merge This PR is ready to be merged and will be picked up by va-bot to automatically merge to master
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants