Repository navigation
Expand file tree
/
Copy pathCaseParticipatedRepository.php
More file actions
122 lines (112 loc) · 3.71 KB
/
Copy pathCaseParticipatedRepository.php
File metadata and controls
122 lines (112 loc) · 3.71 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
<?php
namespace ProcessMaker\Repositories;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use ProcessMaker\Constants\CaseStatusConstants;
use ProcessMaker\Models\CaseParticipated;
use ProcessMaker\Models\CaseStarted;
class CaseParticipatedRepository
{
/**
* This property is used to store an instance of `CaseParticipated`
* when a case participated is updated.
* @var CaseParticipated|null
*/
protected ?CaseParticipated $caseParticipated;
/**
* Store a new case participated.
*
* @param CaseStarted $case
* @param int $userId
* @return void
*/
public function create(CaseStarted $case, int $userId): void
{
try {
CaseParticipated::updateOrCreate(
[
'case_number' => $case->case_number,
'user_id' => $userId,
],
$this->mapCaseToArray($case, $userId)
);
} catch (\Exception $e) {
$this->logException($e);
}
}
/**
* Update the cases participated.
*
* @param CaseStarted $case
* @return void
*/
public function update(CaseStarted $case): void
{
try {
CaseParticipated::where('case_number', $case->case_number)
->update($this->mapCaseToArray($case));
} catch (\Exception $e) {
$this->logException($e);
}
}
/**
* Maps properties of a `CaseStarted` object to an array, optionally including a user ID.
*
* @param CaseStarted case Takes a `CaseStarted` object and parameter as input and returns an array with specific
* properties mapped from the `CaseStarted` object.
* @param int userId Takes an optional `userId` parameter.
*
* @return array An array containing various properties of a CaseStarted object. If a userId is
* provided, it will also include the user_id in the returned array.
*/
private function mapCaseToArray(CaseStarted $case, int $userId = null): array
{
// Define the case status if is not set the stage
if (is_null($case->last_stage_id)) {
$case->last_stage_name = $case->case_status;
$case->progress = 50;
}
$data = [
'case_number' => $case->case_number,
'case_title' => $case->case_title,
'case_title_formatted' => $case->case_title_formatted,
'case_status' => $case->case_status,
'processes' => $case->processes,
'requests' => $case->requests,
'request_tokens' => $case->request_tokens,
'tasks' => $case->tasks,
'participants' => $case->participants,
'initiated_at' => $case->initiated_at,
'completed_at' => $case->completed_at,
'keywords' => $case->keywords,
'last_stage_id' => $case->last_stage_id,
'last_stage_name' => $case->last_stage_name,
'progress' => $case->progress ?? 0,
];
if ($userId !== null) {
$data['user_id'] = $userId;
}
return $data;
}
/**
* Update the status of a case participated.
*
* @param int $caseNumber
* @param array $statusData
* @return void
*/
public function updateStatus(int $caseNumber, array $statusData)
{
try {
CaseParticipated::where('case_number', $caseNumber)
->update($statusData);
} catch (\Exception $e) {
$this->logException($e);
}
}
private function logException(\Exception $e): void
{
Log::error('CaseException: ' . $e->getMessage());
Log::error('CaseException: ' . $e->getTraceAsString());
}
}