forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaseRepository.php
More file actions
225 lines (191 loc) · 7.64 KB
/
CaseRepository.php
File metadata and controls
225 lines (191 loc) · 7.64 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
<?php
namespace ProcessMaker\Repositories;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use ProcessMaker\Constants\CaseStatusConstants;
use ProcessMaker\Contracts\CaseRepositoryInterface;
use ProcessMaker\Models\CaseStarted;
use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface;
use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface;
class CaseRepository implements CaseRepositoryInterface
{
/**
* @var CaseParticipatedRepository
*/
protected CaseParticipatedRepository $caseParticipatedRepository;
/**
* This property is used to store an instance of `CaseStarted`
* when a case started is updated.
* @var CaseStarted|null
*/
protected ?CaseStarted $case;
public function __construct()
{
$this->caseParticipatedRepository = new CaseParticipatedRepository();
}
/**
* Store a new case started.
*
* @param ExecutionInstanceInterface $instance
* @return void
*/
public function create(ExecutionInstanceInterface $instance): void
{
if (is_null($instance->case_number)) {
Log::error('case number is required, method=create, instance=' . $instance->getKey());
return;
}
if ($this->checkIfCaseStartedExist($instance->case_number)) {
$this->updateSubProcesses($instance);
return;
}
try {
$processData = CaseUtils::extractData($instance->process, 'PROCESS');
$requestData = CaseUtils::extractData($instance, 'REQUEST');
$dataKeywords = CaseUtils::extractData($instance, 'KEYWORD');
// Check the case status
if (is_null($instance->last_stage_id)) {
$instance->last_stage_name = $instance->case_status;
$instance->progress = 50;
}
CaseStarted::create([
'case_number' => $instance->case_number,
'user_id' => $instance->user_id,
'case_title' => $instance->case_title,
'case_title_formatted' => $instance->case_title_formatted,
'case_status' => CaseUtils::getStatus($instance->status),
'processes' => CaseUtils::storeProcesses(collect(), $processData),
'requests' => CaseUtils::storeRequests(collect(), $requestData),
'request_tokens' => [],
'tasks' => [],
'participants' => [],
'initiated_at' => $instance->initiated_at,
'completed_at' => null,
'keywords' => CaseUtils::getKeywords($dataKeywords),
'last_stage_id' => $instance->last_stage_id,
'last_stage_name' => $instance->last_stage_name,
'progress' => 0,
]);
} catch (\Exception $e) {
Log::error('CaseException: ' . $e->getMessage());
Log::error('CaseException: ' . $e->getTraceAsString());
}
}
/**
* Update the case started.
*
* @param ExecutionInstanceInterface $instance
* @param TokenInterface $token
* @return void
*/
public function update(ExecutionInstanceInterface $instance, TokenInterface $token): void
{
if (!$this->checkIfCaseStartedExist($instance->case_number)) {
return;
}
try {
$taskData = CaseUtils::extractData($token, 'TASK');
$dataKeywords = CaseUtils::extractData($instance, 'KEYWORD');
$this->case->case_title = $instance->case_title;
$this->case->case_title_formatted = $instance->case_title_formatted;
$this->case->case_status = CaseUtils::getStatus($instance->status);
$this->case->request_tokens = CaseUtils::storeRequestTokens($this->case->request_tokens, $token->getKey());
$this->case->tasks = CaseUtils::storeTasks($this->case->tasks, $taskData);
$this->case->keywords = CaseUtils::getKeywords($dataKeywords);
$this->case->last_stage_id = $token->stage_id;
$this->case->last_stage_name = $token->stage_name;
$this->case->progress = calculateProgressById($token->stage_id, $instance?->process?->stages);
$this->updateParticipants($token);
$this->case->saveOrFail();
} catch (\Exception $e) {
Log::error('CaseException: ' . $e->getMessage());
Log::error('CaseException: ' . $e->getTraceAsString());
}
}
/**
* Update the status of a case started.
*
* @param ExecutionInstanceInterface $instance
* @return void
*/
public function updateStatus(ExecutionInstanceInterface $instance): void
{
if (is_null($instance->case_number)) {
return;
}
// If a sub-process is completed, do not update the case started status
if (!is_null($instance->parent_request_id)) {
return;
}
try {
$caseStatus = CaseUtils::getStatus($instance->status);
$data = [
'case_status' => $caseStatus,
];
if (in_array($caseStatus, [CaseStatusConstants::COMPLETED, CaseStatusConstants::CANCELED])) {
$data['completed_at'] = $instance->completed_at;
$data['last_stage_name'] = $caseStatus;
$data['progress'] = 100;
}
// Update the case started and case participated
CaseStarted::where('case_number', $instance->case_number)->update($data);
$this->caseParticipatedRepository->updateStatus($instance->case_number, $data);
} catch (\Exception $e) {
Log::error('CaseException: ' . $e->getMessage());
Log::error('CaseException: ' . $e->getTraceAsString());
}
}
/**
* Update the participants of the case started.
*
* @param TokenInterface $token
* @return void
*/
private function updateParticipants(TokenInterface $token): void
{
$user = $token->user;
$this->case->participants = CaseUtils::storeParticipants($this->case->participants, $user?->id);
if (!is_null($user?->id)) {
$this->caseParticipatedRepository->create($this->case, $user->id);
}
$this->caseParticipatedRepository->update($this->case);
}
/**
* Check if the case started exist.
*
* @param int|null $caseNumber
* @return bool
*/
private function checkIfCaseStartedExist(int | null $caseNumber): bool
{
if (is_null($caseNumber)) {
return false;
}
$this->case = CaseStarted::where('case_number', $caseNumber)->first();
return !is_null($this->case);
}
/**
* Update the processes and requests of the case started.
*
* @param ExecutionInstanceInterface $instance
* @return void
*/
private function updateSubProcesses(ExecutionInstanceInterface $instance): void
{
if (is_null($instance->parent_request_id)) {
return;
}
try {
$processData = CaseUtils::extractData($instance->process, 'PROCESS');
$requestData = CaseUtils::extractData($instance, 'REQUEST');
// Store the sub-processes and requests
$this->case->processes = CaseUtils::storeProcesses($this->case->processes, $processData);
$this->case->requests = CaseUtils::storeRequests($this->case->requests, $requestData);
$this->case->saveOrFail();
$this->caseParticipatedRepository->update($this->case);
} catch (\Exception $e) {
Log::error('CaseException: ' . $e->getMessage());
Log::error('CaseException: ' . $e->getTraceAsString());
}
}
}