-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathApplyRecommendation.php
More file actions
61 lines (51 loc) · 1.94 KB
/
ApplyRecommendation.php
File metadata and controls
61 lines (51 loc) · 1.94 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
<?php
namespace ProcessMaker;
use Illuminate\Auth\Access\AuthorizationException;
use ProcessMaker\Events\TasksUpdated;
use ProcessMaker\Jobs\GenerateUserRecommendations;
use ProcessMaker\Models\Recommendation;
use ProcessMaker\Models\User;
use ProcessMaker\Notifications\ApplyActionNotification;
class ApplyRecommendation
{
private $errors = [];
public function run(string $action, Recommendation $recommendation, User $user, array $params = [])
{
$errorMessage = null;
$query = $recommendation->baseQuery($user);
foreach ($query->get() as $task) {
switch($action) {
case 'mark_as_priority':
$task->update(['is_priority' => true]);
break;
case 'reassign_to_user':
$toUser = User::active()->find($params['to_user_id']);
if (!$toUser) {
$errorMessage = __('No user selected');
break;
}
try {
$task->reassign($toUser->id, $user);
} catch(AuthorizationException $e) {
$this->errors[] = $e->getMessage();
}
break;
default:
break;
}
}
if (!empty($this->errors)) {
$count = count($this->errors);
$errorMessage =
':count tasks were not reassigned because the task settings prevent them from being reassigned';
$errorMessage = __($errorMessage, ['count' => $count]);
$user->notify(
new ApplyActionNotification($errorMessage)
);
}
// Reload the user's tasks list
event(new TasksUpdated($user->id, $errorMessage));
// Generate recommendations again so updated tasks are considered
GenerateUserRecommendations::dispatch($user->id);
}
}