Skip to content

Commit 1533e47

Browse files
feat: update cases task status
1 parent ac72bc2 commit 1533e47

3 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace ProcessMaker\Repositories;
4+
5+
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Facades\Log;
7+
8+
class CaseTaskRepository
9+
{
10+
protected string $table = '';
11+
12+
public function __construct(private int $caseNumber, private $task)
13+
{
14+
}
15+
16+
/**
17+
* The function `updateCaseStartedTaskStatus` updates the status of a task in a case started record.
18+
*/
19+
public function updateCaseStartedTaskStatus()
20+
{
21+
$this->table = 'cases_started';
22+
$this->updateTaskStatus();
23+
}
24+
25+
/**
26+
* The function `updateCaseParticipatedTaskStatus` updates the status of a task in a case participated record.
27+
*/
28+
public function updateCaseParticipatedTaskStatus()
29+
{
30+
$this->table = 'cases_participated';
31+
$this->updateTaskStatus();
32+
}
33+
34+
/**
35+
* The function `updateTaskStatus` updates the status of a task in a case, handling exceptions and logging errors.
36+
*
37+
* @return If the case is not found, the function will log an error message and then return without performing any
38+
* further actions.
39+
*/
40+
public function updateTaskStatus()
41+
{
42+
try {
43+
$case = $this->findCaseByTaskId($this->caseNumber, (string)$this->task->id);
44+
45+
if (!$case) {
46+
Log::error('CaseException: ' . 'Case not found, case_number=' . $this->caseNumber . ', task_id=' . $this->task->id);
47+
48+
return;
49+
}
50+
51+
$taskIndex = explode('.', $case->task_index);
52+
53+
$this->updateTaskStatusInCase($this->caseNumber, $taskIndex[0], $this->task->status);
54+
} catch (\Exception $e) {
55+
Log::error('CaseException: ' . $e->getMessage());
56+
Log::error('CaseException: ' . $e->getTraceAsString());
57+
}
58+
}
59+
60+
/**
61+
* The function `findCaseByTaskId` retrieves a specific case by its number and a task ID from a database table.
62+
*
63+
* @param int caseNumber The `caseNumber` parameter is an integer representing the case number that you want to search
64+
* for in the database. It is used to filter the query results based on the specified case number.
65+
* @param string taskId The `findCaseByTaskId` function is used to retrieve a case by its case number and a specific
66+
* task ID. The function queries a database table to find a case that matches the provided case number and contains a
67+
* task with the specified task ID.
68+
*
69+
* @return ?object The `findCaseByTaskId` function is returning an object with the following properties:
70+
* - `case_number`: The case number associated with the task ID
71+
* - `task_index`: The index of the task within the `tasks` JSON column that matches the provided task ID
72+
*/
73+
private function findCaseByTaskId(int $caseNumber, string $taskId): ?object
74+
{
75+
return DB::table($this->table)
76+
->select([
77+
'case_number',
78+
DB::raw("JSON_UNQUOTE(JSON_SEARCH(tasks, 'one', ?, NULL, '$[*].id')) as task_index")
79+
])
80+
->where('case_number', $caseNumber)
81+
->whereJsonContains('tasks', ['id' => $taskId])
82+
->setBindings([$taskId], 'select')
83+
->first();
84+
}
85+
86+
/**
87+
* The function `updateTaskStatusInCase` updates the status of a specific task in a case record in a database table.
88+
*
89+
* @param int caseNumber The `caseNumber` parameter is an integer that represents the case number associated with the
90+
* task that needs to be updated.
91+
* @param string taskIndex The `taskIndex` parameter in the `updateTaskStatusInCase` function is used to specify the
92+
* index of the task within the JSON array that needs to be updated. It is a string that represents the path to the
93+
* specific task within the JSON structure. For example, if the tasks are stored
94+
* @param string status The `updateTaskStatusInCase` function is used to update the status of a specific task in a
95+
* case. The parameters are as follows:
96+
*/
97+
private function updateTaskStatusInCase(int $caseNumber, string $taskIndex, string $status): void
98+
{
99+
DB::table($this->table)
100+
->where('case_number', $caseNumber)
101+
->update([
102+
'tasks' => DB::raw('JSON_SET(tasks, "' . $taskIndex . '.status", "' . $status . '")'),
103+
]);
104+
}
105+
}

‎ProcessMaker/Repositories/CaseUtils.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public static function storeTasks(Collection $tasks, ?array $taskData = []): Col
152152
&& in_array($taskData['element_type'], self::ALLOWED_ELEMENT_TYPES)
153153
) {
154154
unset($taskData['element_type']);
155+
$taskData['id'] = (string)$taskData['id'];
155156
$tasks->prepend($taskData);
156157
}
157158

‎ProcessMaker/Repositories/TokenRepository.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ public function persistActivityException(ActivityInterface $activity, TokenInter
308308
$token->updateTokenProperties();
309309
$token->save();
310310
$token->setId($token->getKey());
311+
312+
// Update Case Started Task
313+
$caseTaskRepo = new CaseTaskRepository($token->getInstance()->case_number, $token);
314+
$caseTaskRepo->updateCaseStartedTaskStatus();
315+
$caseTaskRepo->updateCaseParticipatedTaskStatus();
316+
311317
$request = $token->getInstance();
312318
$request->notifyProcessUpdated('ACTIVITY_EXCEPTION', $token);
313319
}
@@ -342,6 +348,12 @@ public function persistActivityCompleted(ActivityInterface $activity, TokenInter
342348
$token->updateTokenProperties();
343349
$token->save();
344350
$token->setId($token->getKey());
351+
352+
// Update Case Started Task
353+
$caseTaskRepo = new CaseTaskRepository($token->getInstance()->case_number, $token);
354+
$caseTaskRepo->updateCaseStartedTaskStatus();
355+
$caseTaskRepo->updateCaseParticipatedTaskStatus();
356+
345357
$request = $token->getInstance();
346358
$request->notifyProcessUpdated('ACTIVITY_COMPLETED', $token);
347359
}

0 commit comments

Comments
 (0)