-
Notifications
You must be signed in to change notification settings - Fork 19
/
task.rb
715 lines (554 loc) · 21.1 KB
/
task.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# frozen_string_literal: true
##
# Base model for all tasks in generic organizational task queues in Caseflow
# Tasks represent work to be done by judges, attorneys, VSOs, and anyone else who touches a Veteran's appeal.
# Supports common actions like:
# - marking tasks complete
# - assigning a task to a team
# - assigning a task to an individual
# rubocop:disable Metrics/ClassLength
class Task < CaseflowRecord
has_paper_trail on: [:update, :destroy]
acts_as_tree
include PrintsTaskTree
include TaskExtensionForHearings
include HasAppealUpdatedSince
belongs_to :assigned_to, polymorphic: true
belongs_to :assigned_by, class_name: "User"
belongs_to :appeal, polymorphic: true
has_many :attorney_case_reviews, dependent: :destroy
has_many :task_timers, dependent: :destroy
has_one :cached_appeal, ->(task) { where(appeal_type: task.appeal_type) }, foreign_key: :appeal_id
validates :assigned_to, :appeal, :type, :status, presence: true
validate :status_is_valid_on_create, on: :create
validate :assignee_status_is_valid_on_create, on: :create
validate :parent_can_have_children
before_create :set_assigned_at
before_create :verify_org_task_unique
after_create :create_and_auto_assign_child_task, if: :automatically_assign_org_task?
after_create :tell_parent_task_child_task_created
before_save :set_timestamp
after_update :update_parent_status, if: :task_just_closed_and_has_parent?
after_update :update_children_status_after_closed, if: :task_just_closed?
after_update :cancel_task_timers, if: :task_just_closed?
enum status: {
Constants.TASK_STATUSES.assigned.to_sym => Constants.TASK_STATUSES.assigned,
Constants.TASK_STATUSES.in_progress.to_sym => Constants.TASK_STATUSES.in_progress,
Constants.TASK_STATUSES.on_hold.to_sym => Constants.TASK_STATUSES.on_hold,
Constants.TASK_STATUSES.completed.to_sym => Constants.TASK_STATUSES.completed,
Constants.TASK_STATUSES.cancelled.to_sym => Constants.TASK_STATUSES.cancelled
}
# This suppresses a warning about the :open scope overwriting the Kernel#open method
# https://ruby-doc.org/core-2.6.3/Kernel.html#method-i-open
class << self; undef_method :open; end
scope :active, -> { where(status: active_statuses) }
scope :open, -> { where(status: open_statuses) }
scope :closed, -> { where(status: closed_statuses) }
scope :not_cancelled, -> { where.not(status: Constants.TASK_STATUSES.cancelled) }
scope :recently_closed, -> { closed.where(closed_at: (Time.zone.now - 1.week)..Time.zone.now) }
scope :incomplete_or_recently_closed, -> { open.or(recently_closed) }
# Equivalent to .reject(&:hide_from_queue_table_view) but offloads that to the database.
scope :visible_in_queue_table_view, lambda {
where.not(
type: Task.descendants.select(&:hide_from_queue_table_view).map(&:name)
)
}
scope :not_decisions_review, lambda {
where.not(
type: DecisionReviewTask.descendants.map(&:name) + ["DecisionReviewTask"]
)
}
scope :with_assignees, -> { joins(Task.joins_with_assignees_clause) }
scope :with_assigners, -> { joins(Task.joins_with_assigners_clause) }
scope :with_cached_appeals, -> { joins(Task.joins_with_cached_appeals_clause) }
############################################################################################
## class methods
class << self
def label
name.titlecase
end
def closed_statuses
[Constants.TASK_STATUSES.completed, Constants.TASK_STATUSES.cancelled]
end
def active_statuses
[Constants.TASK_STATUSES.assigned, Constants.TASK_STATUSES.in_progress]
end
def open_statuses
active_statuses.concat([Constants.TASK_STATUSES.on_hold])
end
def create_many_from_params(params_array, current_user)
multi_transaction do
params_array.map { |params| create_from_params(params, current_user) }
end
end
def modify_params_for_create(params)
if params.key?(:instructions) && !params[:instructions].is_a?(Array)
params[:instructions] = [params[:instructions]]
end
params
end
def hide_from_queue_table_view
false
end
def cannot_have_children
false
end
def verify_user_can_create!(user, parent)
can_create = parent&.available_actions(user)&.map do |action|
parent.build_action_hash(action, user)
end&.any? do |action|
action.dig(:data, :type) == name || action.dig(:data, :options)&.any? { |option| option.dig(:value) == name }
end
if !parent&.actions_allowable?(user) || !can_create
user_description = user ? "User #{user.id}" : "nil User"
parent_description = parent ? " from #{parent.class.name} #{parent.id}" : ""
message = "#{user_description} cannot assign #{name}#{parent_description}."
fail Caseflow::Error::ActionForbiddenError, message: message
end
end
def child_task_assignee(_parent, params)
Object.const_get(params[:assigned_to_type]).find(params[:assigned_to_id])
end
def child_assigned_by_id(parent, current_user)
return current_user.id if current_user
return parent.assigned_to_id if parent && parent.assigned_to_type == User.name
end
def most_recently_updated
order(:updated_at).last
end
def any_recently_updated(*tasks_arrays)
tasks_arrays.find(&:any?)&.most_recently_updated
end
def create_from_params(params, user)
parent_task = Task.find(params[:parent_id])
fail Caseflow::Error::ChildTaskAssignedToSameUser if parent_task.assigned_to_id == params[:assigned_to_id] &&
parent_task.assigned_to_type == params[:assigned_to_type]
verify_user_can_create!(user, parent_task)
params = modify_params_for_create(params)
child = create_child_task(parent_task, user, params)
parent_task.update!(status: params[:status]) if params[:status]
child
end
def create_child_task(parent, current_user, params)
Task.create!(
type: name,
appeal: parent.appeal,
assigned_by_id: child_assigned_by_id(parent, current_user),
parent_id: parent.id,
assigned_to: params[:assigned_to] || child_task_assignee(parent, params),
instructions: params[:instructions]
)
end
def assigners_table_clause
"(SELECT id, full_name AS display_name FROM users) AS assigners"
end
def joins_with_assigners_clause
"LEFT JOIN #{Task.assigners_table_clause} ON assigners.id = tasks.assigned_by_id"
end
def assignees_table_clause
"(SELECT id, 'Organization' AS type, name AS display_name FROM organizations " \
"UNION " \
"SELECT id, 'User' AS type, css_id AS display_name FROM users)" \
"AS assignees"
end
def joins_with_assignees_clause
"INNER JOIN #{Task.assignees_table_clause} ON " \
"assignees.id = tasks.assigned_to_id AND assignees.type = tasks.assigned_to_type"
end
def joins_with_cached_appeals_clause
"left join #{CachedAppeal.table_name} "\
"on #{CachedAppeal.table_name}.appeal_id = #{Task.table_name}.appeal_id "\
"and #{CachedAppeal.table_name}.appeal_type = #{Task.table_name}.appeal_type"
end
end
########################################################################################
## instance methods
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def available_actions(user)
return [] unless user
if assigned_to == user
return [
Constants.TASK_ACTIONS.ASSIGN_TO_TEAM.to_h,
Constants.TASK_ACTIONS.REASSIGN_TO_PERSON.to_h,
Constants.TASK_ACTIONS.TOGGLE_TIMED_HOLD.to_h,
Constants.TASK_ACTIONS.MARK_COMPLETE.to_h,
Constants.TASK_ACTIONS.CANCEL_TASK.to_h
]
end
if task_is_assigned_to_user_within_organization?(user)
return [
Constants.TASK_ACTIONS.REASSIGN_TO_PERSON.to_h
]
end
if task_is_assigned_to_users_organization?(user)
return [
Constants.TASK_ACTIONS.ASSIGN_TO_TEAM.to_h,
Constants.TASK_ACTIONS.ASSIGN_TO_PERSON.to_h,
Constants.TASK_ACTIONS.MARK_COMPLETE.to_h,
Constants.TASK_ACTIONS.CANCEL_TASK.to_h
]
end
[]
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
# Use the existence of an organization-level task to prevent duplicates since there should only ever be one org-level
# task active at a time for a single appeal.
def verify_org_task_unique
return if !open?
if appeal.tasks.open.where(
type: type,
assigned_to: assigned_to,
parent: parent
).any? && assigned_to.is_a?(Organization)
fail(
Caseflow::Error::DuplicateOrgTask,
docket_number: appeal.docket_number,
task_type: self.class.name,
assignee_type: assigned_to.class.name
)
end
end
def label
self.class.label
end
def default_instructions
[]
end
# When a status is "active" we expect properties of the task to change. When a task is not "active" we expect that
# properties of the task will not change.
def open?
!self.class.closed_statuses.include?(status)
end
def open_with_no_children?
open? && children.empty?
end
def active?
self.class.active_statuses.include?(status)
end
# available_actions() returns an array of options selected by
# the subclass from TASK_ACTIONS that looks something like:
# [ { "label": "Assign to person", "value": "modal/assign_to_person", "func": "assignable_users" }, ... ]
def available_actions_unwrapper(user)
actions_available?(user) ? available_actions(user).map { |action| build_action_hash(action, user) } : []
end
def build_action_hash(action, user)
TaskActionHelper.build_hash(action, self, user)
end
# A wrapper around actions_allowable that also disallows doing actions to on_hold tasks.
def actions_available?(user)
return false if status == Constants.TASK_STATUSES.on_hold && !on_timed_hold?
actions_allowable?(user)
end
def actions_allowable?(user)
return false if !open?
# Users who are assigned a subtask of an organization don't have actions on the organizational task.
return false if assigned_to.is_a?(Organization) && children.any? { |child| child.assigned_to == user }
true
end
def assigned_by_display_name
if assigned_by.try(:full_name)
return assigned_by.full_name.split(" ")
end
["", ""]
end
def post_dispatch_task?
dispatch_task = appeal.tasks.completed.find_by(type: [BvaDispatchTask.name, QualityReviewTask.name])
return false unless dispatch_task
created_at > dispatch_task.closed_at
end
def children_attorney_tasks
children.where(type: AttorneyTask.name)
end
def on_timed_hold?
!active_child_timed_hold_task.nil?
end
def active_child_timed_hold_task
children.open.find { |task| task.type == TimedHoldTask.name }
end
def cancel_timed_hold
active_child_timed_hold_task&.update!(status: Constants.TASK_STATUSES.cancelled)
end
def calculated_placed_on_hold_at
active_child_timed_hold_task&.timer_start_time || placed_on_hold_at
end
def calculated_on_hold_duration
timed_hold_task = active_child_timed_hold_task
(timed_hold_task&.timer_end_time&.to_date &.- timed_hold_task&.timer_start_time&.to_date)&.to_i
end
def update_task_type(params)
multi_transaction do
new_branch_task = first_ancestor_of_type.create_twin_of_type(params)
new_child_task = new_branch_task.last_descendant_of_type
if assigned_to.is_a?(User) && new_child_task.assigned_to.is_a?(User) &&
parent.assigned_to_same_org?(new_child_task.parent)
new_child_task.update!(assigned_to: assigned_to)
end
# Move children from the old childmost task to the new childmost task
children.open.each { |child| child.update!(parent_id: last_descendant_of_type.id) }
# Cancel all tasks under the old task type branch
first_ancestor_of_type.cancel_descendants
[first_ancestor_of_type.descendants, new_branch_task.first_ancestor_of_type.descendants].flatten
end
end
def assigned_to_same_org?(task_to_check)
assigned_to.is_a?(Organization) && assigned_to.eql?(task_to_check.assigned_to)
end
def first_ancestor_of_type
same_task_type?(parent) ? parent.first_ancestor_of_type : self
end
def last_descendant_of_type
child_of_task_type = children.open.detect { |child| same_task_type?(child) }
child_of_task_type&.last_descendant_of_type || self
end
def same_task_type?(task_to_check)
type.eql?(task_to_check&.type)
end
def cancel_descendants
descendants.each { |desc| desc.update!(status: Constants.TASK_STATUSES.cancelled) }
end
def create_twin_of_type(_params)
fail Caseflow::Error::ActionForbiddenError, message: "Cannot change type of this task"
end
def update_from_params(params, current_user)
verify_user_can_update!(current_user)
return reassign(params[:reassign], current_user) if params[:reassign]
update_with_instructions(params)
[self]
end
def update_with_instructions(params)
params[:instructions] = flattened_instructions(params)
update!(params)
end
def flattened_instructions(params)
[instructions, params.dig(:instructions).presence].flatten.compact
end
def hide_from_queue_table_view
self.class.hide_from_queue_table_view
end
def duplicate_org_task
assigned_to.is_a?(Organization) && children.any? do |child_task|
User.name == child_task.assigned_to_type && type == child_task.type
end
end
def hide_from_case_timeline
duplicate_org_task
end
def hide_from_task_snapshot
duplicate_org_task
end
def legacy?
appeal_type == LegacyAppeal.name
end
def ama?
appeal_type == Appeal.name
end
def days_waiting
(Time.zone.today - assigned_at.to_date).to_i if assigned_at
end
def latest_attorney_case_review
return @latest_attorney_case_review if defined?(@latest_attorney_case_review)
@latest_attorney_case_review = AttorneyCaseReview
.where(task_id: Task.where(appeal: appeal)
.pluck(:id))
.order(:created_at).last
end
def prepared_by_display_name
return nil unless latest_attorney_case_review
if latest_attorney_case_review.attorney.try(:full_name)
return latest_attorney_case_review.attorney.full_name.split(" ")
end
["", ""]
end
def when_child_task_completed(child_task)
update_status_if_children_tasks_are_closed(child_task)
end
def when_child_task_created(child_task)
cancel_timed_hold unless child_task.is_a?(TimedHoldTask)
if !on_hold?
Raven.capture_message("Closed task #{id} re-opened because child task created") if !open?
update!(status: :on_hold)
end
end
def task_is_assigned_to_users_organization?(user)
assigned_to.is_a?(Organization) && assigned_to.user_has_access?(user)
end
def task_is_assigned_to_user_within_organization?(user)
parent&.assigned_to.is_a?(Organization) &&
assigned_to.is_a?(User) &&
parent.assigned_to.user_has_access?(user)
end
def assigned_to_vso_user?
assigned_to.is_a?(User) && assigned_to.vso_employee?
end
def can_be_updated_by_user?(user)
available_actions_unwrapper(user).any?
end
def verify_user_can_update!(user)
unless can_be_updated_by_user?(user)
fail Caseflow::Error::ActionForbiddenError, message: "Current user cannot access this task"
end
end
def reassign(reassign_params, current_user)
sibling = dup.tap do |task|
task.assigned_by_id = self.class.child_assigned_by_id(parent, current_user)
task.assigned_to = self.class.child_task_assignee(parent, reassign_params)
task.instructions = flattened_instructions(reassign_params)
task.status = Constants.TASK_STATUSES.assigned
task.save!
end
# Preserve the open children and status of the old task
children.select(&:stays_with_reassigned_parent?).each { |child| child.update!(parent_id: sibling.id) }
sibling.update!(status: status)
update_with_instructions(status: Constants.TASK_STATUSES.cancelled, instructions: reassign_params[:instructions])
[sibling, self, sibling.children].flatten
end
def root_task(task_id = nil)
task_id = id if task_id.nil?
return parent.root_task(task_id) if parent
return self if type == RootTask.name
fail Caseflow::Error::NoRootTask, task_id: task_id
end
def descendants
[self, children.map(&:descendants)].flatten
end
def ancestor_task_of_type(task_type)
return nil unless parent
parent.is_a?(task_type) ? parent : parent.ancestor_task_of_type(task_type)
end
def previous_task
nil
end
def cancel_task_and_child_subtasks
# Cancel all descendants at the same time to avoid after_update hooks marking some tasks as completed.
# it would be better if we could allow the callbacks to happen sanely
descendant_ids = descendants.pluck(:id)
# by avoiding callbacks, we aren't saving PaperTrail versions
# Manually save the state before and after.
tasks = Task.open.where(id: descendant_ids)
transaction do
tasks.each { |task| task.paper_trail.save_with_version }
tasks.update_all(
status: Constants.TASK_STATUSES.cancelled,
closed_at: Time.zone.now
)
tasks.each { |task| task.reload.paper_trail.save_with_version }
end
end
def timeline_title
"#{type} completed"
end
def serializer_class
::WorkQueue::TaskSerializer
end
def assigned_to_label
assigned_to.is_a?(Organization) ? assigned_to.name : assigned_to.css_id
end
def child_must_have_active_assignee?
true
end
def stays_with_reassigned_parent?
open?
end
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
private
def create_and_auto_assign_child_task(options = {})
dup.tap do |child_task|
child_task.assigned_to = assigned_to.next_assignee(**options)
child_task.parent = self
child_task.save!
end
end
def automatically_assign_org_task?
assigned_to.is_a?(Organization) && assigned_to.automatically_assign_to_member?
end
def update_parent_status
parent.when_child_task_completed(self)
end
def tell_parent_task_child_task_created
parent&.when_child_task_created(self)
end
def update_children_status_after_closed
active_child_timed_hold_task&.update!(status: Constants.TASK_STATUSES.cancelled)
end
def cancel_task_timers
task_timers.processable.each do |task_timer|
task_timer.update!(canceled_at: Time.zone.now)
end
end
def task_just_closed?
saved_change_to_attribute?("status") && !open?
end
def task_just_closed_and_has_parent?
task_just_closed? && parent
end
def update_status_if_children_tasks_are_closed(child_task)
if children.any? && children.open.empty? && on_hold?
if assigned_to.is_a?(Organization) && cascade_closure_from_child_task?(child_task)
return all_children_cancelled_or_completed
end
update!(status: Constants.TASK_STATUSES.assigned)
end
end
def all_children_cancelled_or_completed
if all_children_cancelled?
update!(status: Constants.TASK_STATUSES.cancelled)
else
update!(status: Constants.TASK_STATUSES.completed)
end
end
def all_children_cancelled?
children.pluck(:status).uniq == [Constants.TASK_STATUSES.cancelled]
end
def cascade_closure_from_child_task?(child_task)
type == child_task&.type
end
def set_assigned_at
self.assigned_at = created_at unless assigned_at
end
STATUS_TIMESTAMPS = {
assigned: :assigned_at,
in_progress: :started_at,
on_hold: :placed_on_hold_at,
completed: :closed_at,
cancelled: :closed_at
}.freeze
def set_timestamp
return unless will_save_change_to_attribute?(:status)
timestamp_to_update = STATUS_TIMESTAMPS[status_change_to_be_saved&.last&.to_sym]
return if will_save_change_to_attribute?(timestamp_to_update)
self[timestamp_to_update] = Time.zone.now
nullify_closed_at_if_reopened if closed_at.present?
end
def nullify_closed_at_if_reopened
return unless self.class.open_statuses.include?(status_change_to_be_saved&.last)
self.closed_at = nil
end
def status_is_valid_on_create
if status != Constants.TASK_STATUSES.assigned
fail Caseflow::Error::InvalidStatusOnTaskCreate, task_type: type
end
true
end
def assignee_status_is_valid_on_create
if parent&.child_must_have_active_assignee? && assigned_to.is_a?(User) && !assigned_to.active?
fail Caseflow::Error::InvalidAssigneeStatusOnTaskCreate, assignee: assigned_to
end
true
end
def parent_can_have_children
if parent&.class&.cannot_have_children
fail Caseflow::Error::InvalidParentTask, message: "Child tasks cannot be created for #{parent.type}s"
end
true
end
end
# rubocop:enable Metrics/ClassLength