-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathCaseController.php
More file actions
227 lines (205 loc) · 6.88 KB
/
CaseController.php
File metadata and controls
227 lines (205 loc) · 6.88 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
<?php
namespace ProcessMaker\Http\Controllers\Api;
use Illuminate\Http\JsonResponse;
use ProcessMaker\Http\Controllers\Api\Actions\Cases\DeleteCase;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
class CaseController extends Controller
{
/**
* Get stage information for cases
*/
public function getStagePerCase(?string $case_number = null): JsonResponse
{
if (!empty($case_number)) {
$responseData = $this->getSpecificCaseStages($case_number);
return response()->json($responseData);
}
$responseData = [
'parentRequest' => [],
'requestCount' => 0,
'all_stages' => [],
'current_stage' => [],
'stages_per_case' => $this->getDefaultCaseStages(),
];
return response()->json($responseData);
}
/**
* Delete a case and its related requests.
*
* @param string $case_number
* @return JsonResponse
*
* @OA\Delete(
* path="/cases/{case_number}",
* summary="Delete a case and its related requests",
* operationId="deleteCase",
* tags={"Cases"},
* @OA\Parameter(
* description="Case number to delete",
* in="path",
* name="case_number",
* required=true,
* @OA\Schema(type="string")
* ),
* @OA\Response(
* response=204,
* description="success"
* ),
* @OA\Response(
* response=401,
* description="Unauthorized"
* ),
* @OA\Response(response=404, ref="#/components/responses/404"),
* @OA\Response(
* response=409,
* description="Conflict"
* ),
* @OA\Response(
* response=500,
* description="Internal Server Error"
* ),
* )
*/
public function destroy(string $case_number): JsonResponse
{
(new DeleteCase)($case_number);
return response()->json([], 204);
}
/**
* Get specific case stages information
* @param string $caseNumber The unique identifier of the case to retrieve stages for
* @return array
*/
private function getSpecificCaseStages(string $caseNumber): array
{
$allRequests = ProcessRequest::where('case_number', $caseNumber)->get();
// Check if any requests were found
if ($allRequests->isEmpty()) {
return $this->getDefaultCaseStages();
}
$parentRequest = null;
$requestCount = $allRequests->count();
// Search the parent request parent_request_id and load $request
foreach ($allRequests as $request) {
if (is_null($request->parent_request_id)) {
$parentRequest = $request;
break;
}
}
$stagesPerCase = $this->getStagesSummary($parentRequest);
return [
'parentRequest' => [
'id' => $parentRequest->id,
'case_number' => $parentRequest->case_number,
'status' => $parentRequest->status,
'completed_at' => $parentRequest->completed_at,
],
'requestCount' => $requestCount,
'all_stages' => [],
'current_stage' => [],
'stages_per_case' => $stagesPerCase,
];
}
/**
* Get default case stages with status handling
*
* @param string|null $status The status to set for the stages
* @return array
*/
private function getDefaultCaseStages(?string $status = null): array
{
return [
[
'id' => 0,
'name' => 'In Progress',
'status' => $this->mapStatus($status, 'In Progress'),
'completed_at' => '',
],
[
'id' => 0,
'name' => 'Completed',
'status' => $this->mapStatus($status, 'Completed'),
'completed_at' => '',
],
];
}
/**
* Map the status for each stage based on the input status
*
* @param string|null $status The input status to map
* @param string $stageName The name of the stage ('In Progress' or 'Completed')
* @return string The mapped status
*/
private function mapStatus(?string $status, string $stageName): string
{
if ($status === 'COMPLETED') {
return 'Done';
}
if ($status === 'ACTIVE') {
return match ($stageName) {
'In Progress' => 'In Progress',
'Completed' => 'Pending',
default => 'Pending'
};
}
return 'Pending';
}
/**
* Get the stages summary based on the provided request.
*
* @param ProcessRequest $request
* @return array An array of stage results, each containing the stage ID, name, status,
* and completion date.
*/
private function getStagesSummary(ProcessRequest $request): array
{
$requestId = $request->id;
$processId = $request->process_id;
$process = Process::where('id', $processId)->first();
if ($process && !empty($process->stages)) {
$allStages = $process->stages;
} else {
// Return the default stages if the process does not have
return $this->getDefaultCaseStages($request->status);
}
$allCurrentStages = ProcessRequestToken::where('process_request_id', $requestId)
->select('stage_id', 'stage_name', 'status', 'completed_at')
->get()
->toArray();
if (empty($allCurrentStages)) {
// TO_DO: define what happen if the process does not have task, is a valid use case
}
// Helper to map status
$mapStatus = function ($status) {
if ($status === 'CLOSED') {
return 'Done';
} elseif ($status === 'ACTIVE') {
return 'In Progress';
} else {
return 'Pending';
}
};
$stageResult = [];
// Initialize stage counts with zero for all stages
foreach ($allStages as $stage) {
$stageData = [
'id' => $stage['id'],
'name' => $stage['name'],
'status' => 'Pending',
'completed_at' => '',
];
foreach ($allCurrentStages as $task) {
if ($task['stage_id'] === $stage['id']) {
$stageData['status'] = $mapStatus($task['status']);
$stageData['completed_at'] = $task['completed_at'] ?? '';
break;
}
}
$stageResult[] = $stageData;
}
return $stageResult;
}
}