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
124 lines (110 loc) · 2.91 KB
/
MatchingTasks.php
File metadata and controls
124 lines (110 loc) · 2.91 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
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
<?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
{
/**
* @param ProcessRequestToken $task
*
* @return array
*/
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;
}
/**
* @param $rule
*
* @return bool
*/
public function shouldSkipRule($rule): bool
{
return $this->isEndDatePast($rule);
}
/**
* @param $rule
* @param $task
*
* @return bool
*/
public function matchesSavedSearch($rule, $task): bool
{
return $rule->saved_search_id !== null && $this->matchesResultInSavedSearch($rule, $task);
}
/**
* @param InboxRule $inboxRule
*
* @return Collection
*/
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();
}
}
/**
* @param $rule
* @param $task
*
* @return mixed
*/
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();
}
/**
* @param $task
*
* @return mixed
*/
public function queryInboxRules($task)
{
return InboxRule::where('active', true)
->where('user_id', $task->user_id)
->get();
}
/**
* @param $rule
*
* @return bool
*/
public function isEndDatePast($rule) : bool
{
if ($rule->end_date && $rule->end_date->isPast()) {
return true;
}
return false;
}
}