|
| 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 | +} |
0 commit comments