forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchingTasks.php
More file actions
87 lines (73 loc) · 2.39 KB
/
MatchingTasks.php
File metadata and controls
87 lines (73 loc) · 2.39 KB
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
<?php
namespace ProcessMaker\InboxRules;
use Illuminate\Support\Collection;
use ProcessMaker\Models\InboxRule;
use ProcessMaker\Models\ProcessRequestToken;
/**
* This Class is used in 2 ways
* 1. After a task is assigned, it checks to see if it matches any active
* InboxRules in the system and returns the InboxRule models.
* 2. The `get` method returns all tasks that match a given InboxRule
*/
class MatchingTasks
{
public function matchingInboxRules(ProcessRequestToken $task): array
{
if (!$task || !$task->user_id) {
return [];
}
$matchingInboxRules = [];
//The Foreach has only inbox rules ACTIVE=true and user_id = $task->user_id
foreach ($this->queryInboxRules($task) as $rule) {
if ($this->isEndDatePast($rule)) {
continue;
}
if ($this->matchesSavedSearch($rule, $task)) {
$matchingInboxRules[] = $rule;
}
}
return $matchingInboxRules;
}
public function shouldSkipRule($rule): bool
{
return $this->isEndDatePast($rule);
}
public function matchesSavedSearch($rule, $task): bool
{
return $rule->saved_search_id !== null && $this->matchesResultInSavedSearch($rule, $task);
}
public function get(InboxRule $inboxRule) : Collection
{
if ($savedSearch = $inboxRule->savedSearch) {
return $savedSearch->query->get();
}
if ($task = $inboxRule->task) {
return ProcessRequestToken::where([
'process_id' => $task->process_id,
'element_id' => $task->element_id,
'user_id' => $inboxRule->user_id,
'status' => 'ACTIVE',
])->get();
}
}
public function matchesResultInSavedSearch($rule, $task)
{
return $rule->savedSearch->query
->where('process_request_tokens.user_id', $task->user_id)
->where('process_request_tokens.id', $task->id)
->exists();
}
public function queryInboxRules($task)
{
return InboxRule::where('active', true)
->where('user_id', $task->user_id)
->get();
}
public function isEndDatePast($rule) : bool
{
if ($rule->end_date && $rule->end_date->isPast()) {
return true;
}
return false;
}
}