-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathRollbackProcessRequest.php
More file actions
237 lines (201 loc) · 6.76 KB
/
RollbackProcessRequest.php
File metadata and controls
237 lines (201 loc) · 6.76 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
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
<?php
namespace ProcessMaker;
use Illuminate\Support\Facades\Auth;
use ProcessMaker\Events\ActivityAssigned;
use ProcessMaker\Facades\WorkflowManager;
use ProcessMaker\Models\Comment;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Repositories\BpmnDocument;
class RollbackProcessRequest
{
/**
* Element types that can be rolled-back to
*
* @var array
*/
private $eligibleTypes = ['task', 'scriptTask', 'serviceTask'];
/**
* The current task (that is presumably failing or has some problem)\
*
* @var ProcessRequestToken
*/
private $currentTask;
/**
* A previously completed task that we want to rollback to.
*
* @var ProcessRequestToken
*/
private $rollbackToTask;
/**
* A copy of the rollbackToTask that will be the new active task.
*
* @var ProcessRequestToken
*/
private $newTask;
/**
* BPMN Definitions for the process
*
* @var ProcessMaker\Repositories\BpmnDocument
*/
private $processDefinitions;
/**
* Return the last task if its status was failing.
* We may need to update this logic later.
*
* @param ProcessRequest $processRequest
*
* @return ProcessRequestToken|null
*/
public function getErrorTask(ProcessRequest $processRequest) : ?ProcessRequestToken
{
$lastTask = $processRequest->tokens()->orderBy('id', 'desc')->first();
if (!$lastTask) {
return null;
}
if ($lastTask->status === 'FAILING') {
return $lastTask;
}
// Allow for gateway tasks
if (
$lastTask->element_type === 'gateway' &&
$lastTask->status === 'CLOSED' &&
$processRequest->status === 'ERROR'
) {
return $lastTask;
}
return null;
}
/**
* Find an element in the request that can be rolled-back to.
* Return null if none are eligible.
*
* @return ProcessRequestToken|null
*/
public function eligibleRollbackTask(ProcessRequestToken $currentTask) : ?ProcessRequestToken
{
$processRequest = $currentTask->processRequest;
return $processRequest->tokens()
->where('status', 'CLOSED')
->where('id', '<', $currentTask->id)
->where('element_id', '!=', $currentTask->element_id)
->whereIn('element_type', $this->eligibleTypes)
->orderBy('id', 'desc')
->first();
}
/**
* Rollback a token to the previous token and reset the request status.
*
* @param ProcessRequestToken $task
* @return ProcessRequestToken
*/
public function rollback(
ProcessRequestToken $currentTask,
BpmnDocument $processDefinitions
) : ProcessRequestToken {
$this->currentTask = $currentTask;
$this->processDefinitions = $processDefinitions;
$this->rollbackToTask = $this->eligibleRollbackTask($currentTask);
if (!$this->rollbackToTask) {
throw new \Exception('No eligible rollback task found');
}
switch ($this->rollbackToTask->element_type) {
case 'scriptTask':
$this->rollbackToScriptTask();
break;
case 'serviceTask':
$this->rollbackToServiceTask();
break;
case 'task':
$this->rollbackToTask();
break;
}
// update the process request status to active
$processRequest = $this->newTask->processRequest;
$processRequest->status = 'ACTIVE';
$process = $processRequest->process;
$processRequest->process_version_id = $process->getLatestVersion(
$processRequest->processVersion->alternative
)->id;
$processRequest->saveOrFail();
// update the current task status to closed
$currentTask->status = 'CLOSED';
$currentTask->saveOrFail();
$this->syncParentProcessStatus($processRequest);
return $this->newTask;
}
private function rollbackToTask()
{
$this->copyTask();
if ($this->newTask->user_id) {
$this->newTask->sendActivityActivatedNotifications();
}
if ($this->newTask->element_type === 'task') {
event(new ActivityAssigned($this->newTask));
}
$this->addComment();
}
private function rollbackToScriptTask()
{
$this->copyTask();
$bpmnTask = $this->processDefinitions->getEvent($this->newTask->element_id);
WorkflowManager::runScripTask($bpmnTask, $this->newTask);
$this->addComment();
}
private function rollbackToServiceTask()
{
$this->copyTask();
$bpmnTask = $this->processDefinitions->getEvent($this->newTask->element_id);
WorkflowManager::runServiceTask($bpmnTask, $this->newTask);
$this->addComment();
}
private function copyTask()
{
$newTask = $this->rollbackToTask->replicate();
$newTask->uuid = null;
$newTask->status = 'ACTIVE';
$newTask->saveOrFail();
$this->newTask = $newTask;
}
/**
* Add a comment that the task was rolled back.
*
* @return void
*/
private function addComment() : void
{
$user = Auth::user();
$userName = $user ? $user->fullname : __('The System');
Comment::create([
'type' => 'LOG',
'user_id' => $user ? $user->id : null,
'commentable_type' => ProcessRequest::class,
'commentable_id' => $this->currentTask->process_request_id,
'subject' => 'Rollback',
'body' => __(':user rolled back :failed_task_name to :new_task_name', [
'user' => $userName,
'failed_task_name' => $this->currentTask->element_name,
'new_task_name' => $this->newTask->element_name,
]),
'case_number' => isset($this->currentTask->case_number) ? $this->currentTask->case_number : null,
]);
}
/**
* When the rolled-back request is a subprocess, reactivate the parent request
* and the parent's call activity tokens that were in error.
*/
private function syncParentProcessStatus(ProcessRequest $processRequest): void
{
$parentRequest = $processRequest->parentRequest;
if (!$parentRequest) {
return;
}
if (in_array($parentRequest->status, ['ERROR', 'FAILING'])) {
$parentRequest->status = 'ACTIVE';
$parentRequest->saveOrFail();
}
ProcessRequestToken::where('subprocess_request_id', $processRequest->id)
->whereIn('status', ['ERROR', 'FAILING'])
->update(['status' => 'ACTIVE']);
}
}