From eaf113b33dce8ccf592a0f13401a79e649a41b39 Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Mon, 30 Sep 2024 10:13:43 -0400 Subject: [PATCH 1/7] feat: add cases sync artisan command --- ProcessMaker/Console/Commands/CasesSync.php | 48 ++++++++++++++ ProcessMaker/Repositories/CaseRepository.php | 67 ++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 ProcessMaker/Console/Commands/CasesSync.php diff --git a/ProcessMaker/Console/Commands/CasesSync.php b/ProcessMaker/Console/Commands/CasesSync.php new file mode 100644 index 0000000000..960e41d6fa --- /dev/null +++ b/ProcessMaker/Console/Commands/CasesSync.php @@ -0,0 +1,48 @@ +option('request_ids'); + $requestIds = $requestIds ? explode(',', $requestIds) : []; + + if (count($requestIds) > 0) { + $data = CaseRepository::syncCases($requestIds); + + foreach ($data['successes'] as $value) { + $this->info('Case started synced ' . $value); + } + + foreach ($data['errors'] as $value) { + $this->error('Error syncing case started ' . $value); + } + } else { + $this->error('Please specify a list of request IDs.'); + } + } +} diff --git a/ProcessMaker/Repositories/CaseRepository.php b/ProcessMaker/Repositories/CaseRepository.php index 7ef6ca49ec..c6820e58a3 100644 --- a/ProcessMaker/Repositories/CaseRepository.php +++ b/ProcessMaker/Repositories/CaseRepository.php @@ -4,7 +4,9 @@ use Carbon\Carbon; use ProcessMaker\Contracts\CaseRepositoryInterface; +use ProcessMaker\Models\CaseParticipated; use ProcessMaker\Models\CaseStarted; +use ProcessMaker\Models\ProcessRequest; use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface; use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface; @@ -195,4 +197,69 @@ private function updateSubProcesses(ExecutionInstanceInterface $instance): void \Log::error($th->getMessage()); } } + + public static function syncCases(array $requestIds): array + { + $requests = ProcessRequest::with(['tokens', 'parentRequest'])->whereIn('id', $requestIds)->get(); + $successes = []; + $errors = []; + + foreach ($requests as $instance) { + try { + $caseStarted = CaseStarted::updateOrCreate( + ['case_number' => $instance->case_number], + [ + 'user_id' => $instance->user_id, + 'case_title' => $instance->case_title, + 'case_title_formatted' => $instance->case_title_formatted, + 'case_status' => $instance->status === 'ACTIVE' ? 'IN_PROGRESS' : $instance->status, + 'processes' => CaseUtils::storeProcesses($instance, collect()), + 'requests' => CaseUtils::storeRequests($instance, collect()), + 'request_tokens' => CaseUtils::storeRequestTokens($instance->tokens->first()->getKey(), collect()), + 'tasks' => CaseUtils::storeTasks($instance->tokens->first(), collect()), + 'participants' => $instance->participants, + 'initiated_at' => $instance->initiated_at, + 'completed_at' => $instance->completed_at, + ], + ); + + foreach ($instance->tokens as $token) { + if ($token->element_type === 'task') { + self::syncCasesParticipated($caseStarted, $token); + } + } + + $successes[] = $caseStarted->case_number; + } catch (\Exception $e) { + $errors[] = $instance->case_number . ' ' . $e->getMessage(); + } + } + + return [ + 'successes' => $successes, + 'errors' => $errors, + ]; + } + + public static function syncCasesParticipated($caseStarted, $token) + { + CaseParticipated::updateOrCreate( + [ + 'case_number' => $caseStarted->case_number, + 'user_id' => $token->user_id, + ], + [ + 'case_title' => $caseStarted->case_title, + 'case_title_formatted' => $caseStarted->case_title_formatted, + 'case_status' => $caseStarted->case_status, + 'processes' => CaseUtils::storeProcesses($token->processRequest, collect()), + 'requests' => CaseUtils::storeRequests($token->processRequest, collect()), + 'request_tokens' => CaseUtils::storeRequestTokens($token->getKey(), collect()), + 'tasks' => CaseUtils::storeTasks($token, collect()), + 'participants' => $caseStarted->participants, + 'initiated_at' => $caseStarted->initiated_at, + 'completed_at' => $caseStarted->completed_at, + ] + ); + } } From e6a3edc24143c70e93aab5968f5b23113e9b9204 Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Fri, 27 Sep 2024 19:26:41 -0400 Subject: [PATCH 2/7] feat: add custom exception logs --- .../CaseParticipatedRepository.php | 9 +++++--- ProcessMaker/Repositories/CaseRepository.php | 22 +++++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/ProcessMaker/Repositories/CaseParticipatedRepository.php b/ProcessMaker/Repositories/CaseParticipatedRepository.php index 1a22abebe2..5f6f31e567 100644 --- a/ProcessMaker/Repositories/CaseParticipatedRepository.php +++ b/ProcessMaker/Repositories/CaseParticipatedRepository.php @@ -44,7 +44,8 @@ public function create(CaseStarted $case, TokenInterface $token): void 'completed_at' => null, ]); } catch (\Exception $e) { - \Log::error($e->getMessage()); + \Log::error('CaseParticipatedException: ', $e->getMessage()); + \Log::error('CaseParticipatedException: ', $e->getTraceAsString()); } } @@ -73,7 +74,8 @@ public function update(CaseStarted $case, TokenInterface $token) 'participants' => $case->participants, ]); } catch (\Exception $e) { - \Log::error($e->getMessage()); + \Log::error('CaseParticipatedException: ', $e->getMessage()); + \Log::error('CaseParticipatedException: ', $e->getTraceAsString()); } } @@ -90,7 +92,8 @@ public function updateStatus(int $caseNumber, array $statusData) CaseParticipated::where('case_number', $caseNumber) ->update($statusData); } catch (\Exception $e) { - \Log::error($e->getMessage()); + \Log::error('CaseParticipatedException: ', $e->getMessage()); + \Log::error('CaseParticipatedException: ', $e->getTraceAsString()); } } diff --git a/ProcessMaker/Repositories/CaseRepository.php b/ProcessMaker/Repositories/CaseRepository.php index c6820e58a3..e333b9e6aa 100644 --- a/ProcessMaker/Repositories/CaseRepository.php +++ b/ProcessMaker/Repositories/CaseRepository.php @@ -33,7 +33,7 @@ public function __construct(protected CaseParticipatedRepository $caseParticipat public function create(ExecutionInstanceInterface $instance): void { if (is_null($instance->case_number)) { - \Log::error('Case number is required. instance: ' . $instance->getKey()); + \Log::error('CaseStartedError: case number is required, method=create, instance=' . $instance->getKey()); return; } @@ -60,8 +60,8 @@ public function create(ExecutionInstanceInterface $instance): void 'completed_at' => null, ]); } catch (\Exception $e) { - \Log::error($e->getMessage()); - \Log::error($e->getTraceAsString()); + \Log::error('CaseStartedException: ', $e->getMessage()); + \Log::error('CaseStartedException: ', $e->getTraceAsString()); } } @@ -75,14 +75,14 @@ public function create(ExecutionInstanceInterface $instance): void public function update(ExecutionInstanceInterface $instance, TokenInterface $token): void { if (is_null($instance->case_number)) { - \Log::error('Case number is required. instance: ' . $instance->getKey()); + \Log::error('CaseStartedError: case number is required, method=update, instance=' . $instance->getKey()); return; } try { if (!$this->checkIfCaseStartedExist($instance->case_number)) { - \Log::error('Case number not found. instance: ' . $instance->id); + \Log::error('CaseStartedError: case started not found, method=update, instance=' . $instance->getKey()); return; } @@ -96,7 +96,8 @@ public function update(ExecutionInstanceInterface $instance, TokenInterface $tok $this->case->saveOrFail(); } catch (\Exception $e) { - \Log::error($e->getMessage()); + \Log::error('CaseStartedException: ', $e->getMessage()); + \Log::error('CaseStartedException: ', $e->getTraceAsString()); } } @@ -126,7 +127,8 @@ public function updateStatus(ExecutionInstanceInterface $instance): void CaseStarted::where('case_number', $instance->case_number)->update($data); $this->caseParticipatedRepository->updateStatus($instance->case_number, $data); } catch (\Exception $e) { - \Log::error($e->getMessage()); + \Log::error('CaseStartedException: ', $e->getMessage()); + \Log::error('CaseStartedException: ', $e->getTraceAsString()); } } @@ -193,8 +195,10 @@ private function updateSubProcesses(ExecutionInstanceInterface $instance): void $this->case->requests = CaseUtils::storeRequests($instance, $this->case->requests); $this->case->saveOrFail(); - } catch (\Exception $th) { - \Log::error($th->getMessage()); + } catch (\Exception $e) { + \Log::error('CaseStartedException: ', $e->getMessage()); + \Log::error('CaseStartedException: ', $e->getTraceAsString()); + } } From e92b52a3ec1547d499425ee5618febe4cdbc2d7f Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Tue, 1 Oct 2024 14:13:53 -0400 Subject: [PATCH 3/7] feat: add case sync repo --- ProcessMaker/Console/Commands/CasesSync.php | 5 +- ProcessMaker/Exception/CaseException.php | 16 ++ .../CaseParticipatedRepository.php | 10 +- ProcessMaker/Repositories/CaseRepository.php | 105 ++-------- .../Repositories/CaseSyncRepository.php | 190 ++++++++++++++++++ ProcessMaker/Repositories/CaseUtils.php | 2 + 6 files changed, 228 insertions(+), 100 deletions(-) create mode 100644 ProcessMaker/Exception/CaseException.php create mode 100644 ProcessMaker/Repositories/CaseSyncRepository.php diff --git a/ProcessMaker/Console/Commands/CasesSync.php b/ProcessMaker/Console/Commands/CasesSync.php index 960e41d6fa..e23d9ce0d4 100644 --- a/ProcessMaker/Console/Commands/CasesSync.php +++ b/ProcessMaker/Console/Commands/CasesSync.php @@ -3,8 +3,7 @@ namespace ProcessMaker\Console\Commands; use Illuminate\Console\Command; -use ProcessMaker\Repositories\CaseRepository; -use ProcessMaker\Repositories\CaseUtils; +use ProcessMaker\Repositories\CaseSyncRepository; class CasesSync extends Command { @@ -32,7 +31,7 @@ public function handle() $requestIds = $requestIds ? explode(',', $requestIds) : []; if (count($requestIds) > 0) { - $data = CaseRepository::syncCases($requestIds); + $data = CaseSyncRepository::syncCases($requestIds); foreach ($data['successes'] as $value) { $this->info('Case started synced ' . $value); diff --git a/ProcessMaker/Exception/CaseException.php b/ProcessMaker/Exception/CaseException.php new file mode 100644 index 0000000000..9a7966c346 --- /dev/null +++ b/ProcessMaker/Exception/CaseException.php @@ -0,0 +1,16 @@ +getTraceAsString()); + } +} diff --git a/ProcessMaker/Repositories/CaseParticipatedRepository.php b/ProcessMaker/Repositories/CaseParticipatedRepository.php index 5f6f31e567..1e2e108c45 100644 --- a/ProcessMaker/Repositories/CaseParticipatedRepository.php +++ b/ProcessMaker/Repositories/CaseParticipatedRepository.php @@ -2,6 +2,7 @@ namespace ProcessMaker\Repositories; +use ProcessMaker\Exception\CaseException; use ProcessMaker\Models\CaseParticipated; use ProcessMaker\Models\CaseStarted; use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface; @@ -44,8 +45,7 @@ public function create(CaseStarted $case, TokenInterface $token): void 'completed_at' => null, ]); } catch (\Exception $e) { - \Log::error('CaseParticipatedException: ', $e->getMessage()); - \Log::error('CaseParticipatedException: ', $e->getTraceAsString()); + throw new CaseException($e->getMessage()); } } @@ -74,8 +74,7 @@ public function update(CaseStarted $case, TokenInterface $token) 'participants' => $case->participants, ]); } catch (\Exception $e) { - \Log::error('CaseParticipatedException: ', $e->getMessage()); - \Log::error('CaseParticipatedException: ', $e->getTraceAsString()); + throw new CaseException($e->getMessage()); } } @@ -92,8 +91,7 @@ public function updateStatus(int $caseNumber, array $statusData) CaseParticipated::where('case_number', $caseNumber) ->update($statusData); } catch (\Exception $e) { - \Log::error('CaseParticipatedException: ', $e->getMessage()); - \Log::error('CaseParticipatedException: ', $e->getTraceAsString()); + throw new CaseException($e->getMessage()); } } diff --git a/ProcessMaker/Repositories/CaseRepository.php b/ProcessMaker/Repositories/CaseRepository.php index e333b9e6aa..14c508c4b7 100644 --- a/ProcessMaker/Repositories/CaseRepository.php +++ b/ProcessMaker/Repositories/CaseRepository.php @@ -4,9 +4,8 @@ use Carbon\Carbon; use ProcessMaker\Contracts\CaseRepositoryInterface; -use ProcessMaker\Models\CaseParticipated; +use ProcessMaker\Exception\CaseException; use ProcessMaker\Models\CaseStarted; -use ProcessMaker\Models\ProcessRequest; use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface; use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface; @@ -33,9 +32,7 @@ public function __construct(protected CaseParticipatedRepository $caseParticipat public function create(ExecutionInstanceInterface $instance): void { if (is_null($instance->case_number)) { - \Log::error('CaseStartedError: case number is required, method=create, instance=' . $instance->getKey()); - - return; + throw new CaseException('case number is required, method=create, instance=' . $instance->getKey()); } if ($this->checkIfCaseStartedExist($instance->case_number)) { @@ -60,8 +57,7 @@ public function create(ExecutionInstanceInterface $instance): void 'completed_at' => null, ]); } catch (\Exception $e) { - \Log::error('CaseStartedException: ', $e->getMessage()); - \Log::error('CaseStartedException: ', $e->getTraceAsString()); + throw new CaseException($e->getMessage()); } } @@ -74,19 +70,11 @@ public function create(ExecutionInstanceInterface $instance): void */ public function update(ExecutionInstanceInterface $instance, TokenInterface $token): void { - if (is_null($instance->case_number)) { - \Log::error('CaseStartedError: case number is required, method=update, instance=' . $instance->getKey()); - - return; + if (!$this->checkIfCaseStartedExist($instance->case_number)) { + throw new CaseException('case started not found, method=update, instance=' . $instance->getKey()); } try { - if (!$this->checkIfCaseStartedExist($instance->case_number)) { - \Log::error('CaseStartedError: case started not found, method=update, instance=' . $instance->getKey()); - - return; - } - $this->case->case_title = $instance->case_title; $this->case->case_status = $instance->status === self::CASE_STATUS_ACTIVE ? 'IN_PROGRESS' : $instance->status; $this->case->request_tokens = CaseUtils::storeRequestTokens($token->getKey(), $this->case->request_tokens); @@ -96,8 +84,7 @@ public function update(ExecutionInstanceInterface $instance, TokenInterface $tok $this->case->saveOrFail(); } catch (\Exception $e) { - \Log::error('CaseStartedException: ', $e->getMessage()); - \Log::error('CaseStartedException: ', $e->getTraceAsString()); + throw new CaseException($e->getMessage()); } } @@ -127,8 +114,7 @@ public function updateStatus(ExecutionInstanceInterface $instance): void CaseStarted::where('case_number', $instance->case_number)->update($data); $this->caseParticipatedRepository->updateStatus($instance->case_number, $data); } catch (\Exception $e) { - \Log::error('CaseStartedException: ', $e->getMessage()); - \Log::error('CaseStartedException: ', $e->getTraceAsString()); + throw new CaseException($e->getMessage()); } } @@ -167,11 +153,15 @@ private function updateParticipants(TokenInterface $token): void /** * Check if the case started exist. * - * @param int $caseNumber + * @param int|null $caseNumber * @return bool */ - private function checkIfCaseStartedExist(int $caseNumber): 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); @@ -196,74 +186,7 @@ private function updateSubProcesses(ExecutionInstanceInterface $instance): void $this->case->saveOrFail(); } catch (\Exception $e) { - \Log::error('CaseStartedException: ', $e->getMessage()); - \Log::error('CaseStartedException: ', $e->getTraceAsString()); - + throw new CaseException($e->getMessage()); } } - - public static function syncCases(array $requestIds): array - { - $requests = ProcessRequest::with(['tokens', 'parentRequest'])->whereIn('id', $requestIds)->get(); - $successes = []; - $errors = []; - - foreach ($requests as $instance) { - try { - $caseStarted = CaseStarted::updateOrCreate( - ['case_number' => $instance->case_number], - [ - 'user_id' => $instance->user_id, - 'case_title' => $instance->case_title, - 'case_title_formatted' => $instance->case_title_formatted, - 'case_status' => $instance->status === 'ACTIVE' ? 'IN_PROGRESS' : $instance->status, - 'processes' => CaseUtils::storeProcesses($instance, collect()), - 'requests' => CaseUtils::storeRequests($instance, collect()), - 'request_tokens' => CaseUtils::storeRequestTokens($instance->tokens->first()->getKey(), collect()), - 'tasks' => CaseUtils::storeTasks($instance->tokens->first(), collect()), - 'participants' => $instance->participants, - 'initiated_at' => $instance->initiated_at, - 'completed_at' => $instance->completed_at, - ], - ); - - foreach ($instance->tokens as $token) { - if ($token->element_type === 'task') { - self::syncCasesParticipated($caseStarted, $token); - } - } - - $successes[] = $caseStarted->case_number; - } catch (\Exception $e) { - $errors[] = $instance->case_number . ' ' . $e->getMessage(); - } - } - - return [ - 'successes' => $successes, - 'errors' => $errors, - ]; - } - - public static function syncCasesParticipated($caseStarted, $token) - { - CaseParticipated::updateOrCreate( - [ - 'case_number' => $caseStarted->case_number, - 'user_id' => $token->user_id, - ], - [ - 'case_title' => $caseStarted->case_title, - 'case_title_formatted' => $caseStarted->case_title_formatted, - 'case_status' => $caseStarted->case_status, - 'processes' => CaseUtils::storeProcesses($token->processRequest, collect()), - 'requests' => CaseUtils::storeRequests($token->processRequest, collect()), - 'request_tokens' => CaseUtils::storeRequestTokens($token->getKey(), collect()), - 'tasks' => CaseUtils::storeTasks($token, collect()), - 'participants' => $caseStarted->participants, - 'initiated_at' => $caseStarted->initiated_at, - 'completed_at' => $caseStarted->completed_at, - ] - ); - } } diff --git a/ProcessMaker/Repositories/CaseSyncRepository.php b/ProcessMaker/Repositories/CaseSyncRepository.php new file mode 100644 index 0000000000..2d270d1974 --- /dev/null +++ b/ProcessMaker/Repositories/CaseSyncRepository.php @@ -0,0 +1,190 @@ +whereIn('id', $requestIds) + ->get(); + + $successes = []; + $errors = []; + + foreach ($requests as $instance) { + try { + if (!is_null($instance->parent_request_id)) { + continue; + } + + $csProcesses = CaseUtils::storeProcesses($instance, collect()); + $csRequests = CaseUtils::storeRequests($instance, collect()); + $csRequestTokens = collect(); + $csTasks = collect(); + $participants = $instance->participants->map->only('id', 'fullName', 'title', 'avatar'); + $status = $instance->status === 'ACTIVE' ? 'IN_PROGRESS' : $instance->status; + + $cpData = self::prepareCaseStartedData($instance, $status, $participants); + + self::processTokens($instance, $cpData, $csRequestTokens, $csTasks); + + self::processChildRequests($instance, $cpData, $csProcesses, $csRequests, $participants, $csRequestTokens, $csTasks); + + $caseStarted = CaseStarted::updateOrCreate( + ['case_number' => $instance->case_number], + [ + 'user_id' => $instance->user_id, + 'case_title' => $instance->case_title, + 'case_title_formatted' => $instance->case_title_formatted, + 'case_status' => $status, + 'processes' => $csProcesses, + 'requests' => $csRequests, + 'request_tokens' => $csRequestTokens, + 'tasks' => $csTasks, + 'participants' => $participants, + 'initiated_at' => $instance->initiated_at, + 'completed_at' => $instance->completed_at, + ], + ); + + $successes[] = $caseStarted->case_number; + } catch (\Exception $e) { + $errors[] = $instance->case_number . ' ' . $e->getMessage(); + } + } + + return [ + 'successes' => $successes, + 'errors' => $errors, + ]; + } + + /** + * Prepare the case started data. + * + * @param ProcessRequest $instance + * @param string $status + * @param Collection $participants + * @return array + */ + private static function prepareCaseStartedData($instance, $status, $participants) + { + return [ + 'case_title' => $instance->case_title, + 'case_title_formatted' => $instance->case_title_formatted, + 'case_status' => $status, + 'processes' => CaseUtils::storeProcesses($instance, collect()), + 'requests' => CaseUtils::storeRequests($instance, collect()), + 'request_tokens' => collect(), + 'tasks' => collect(), + 'participants' => $participants, + 'initiated_at' => $instance->initiated_at, + 'completed_at' => $instance->completed_at, + ]; + } + + /** + * Process the tokens. + * + * @param ProcessRequest $instance + * @param array $cpData + * @param Collection $csRequestTokens + * @param Collection $csTasks + * @return void + */ + private static function processTokens($instance, &$cpData, &$csRequestTokens, &$csTasks) + { + foreach ($instance->tokens as $token) { + if (in_array($token->element_type, CaseUtils::ALLOWED_REQUEST_TOKENS)) { + $cpData['processes'] = CaseUtils::storeProcesses($instance, $cpData['processes']); + $cpData['requests'] = CaseUtils::storeRequests($instance, $cpData['requests']); + $cpData['request_tokens'] = CaseUtils::storeRequestTokens($token->getKey(), $cpData['request_tokens']); + $cpData['tasks'] = CaseUtils::storeTasks($token, $cpData['tasks']); + + $csRequestTokens = CaseUtils::storeRequestTokens($token->getKey(), $csRequestTokens); + $csTasks = CaseUtils::storeTasks($token, $csTasks); + + self::syncCasesParticipated($instance->case_number, $token->user_id, $cpData); + } + } + } + + /** + * Process the child requests. + * + * @param ProcessRequest $instance + * @param array $cpData + * @param Collection $csProcesses + * @param Collection $csRequests + * @param Collection $participants + * @param Collection $csRequestTokens + * @param Collection $csTasks + * @return void + */ + private static function processChildRequests( + $instance, &$cpData, &$csProcesses, &$csRequests, &$participants, &$csRequestTokens, &$csTasks + ) + { + foreach ($instance->childRequests as $subProcess) { + $cpData['processes'] = CaseUtils::storeProcesses($subProcess, collect()); + $cpData['requests'] = CaseUtils::storeRequests($subProcess, collect()); + $cpData['request_tokens'] = collect(); + $cpData['tasks'] = collect(); + + $csProcesses = CaseUtils::storeProcesses($subProcess, $csProcesses); + $csRequests = CaseUtils::storeRequests($subProcess, $csRequests); + + $participants = $participants + ->merge($subProcess->participants->map->only('id', 'fullName', 'title', 'avatar')) + ->unique('id') + ->values(); + + foreach ($subProcess->tokens as $spToken) { + if (in_array($spToken->element_type, CaseUtils::ALLOWED_REQUEST_TOKENS)) { + $cpData['processes'] = CaseUtils::storeProcesses($subProcess, $cpData['processes']); + $cpData['requests'] = CaseUtils::storeRequests($subProcess, $cpData['requests']); + $cpData['request_tokens'] = CaseUtils::storeRequestTokens($spToken->getKey(), $cpData['request_tokens']); + $cpData['tasks'] = CaseUtils::storeTasks($spToken, $cpData['tasks']); + + $csRequestTokens = CaseUtils::storeRequestTokens($spToken->getKey(), $csRequestTokens); + $csTasks = CaseUtils::storeTasks($spToken, $csTasks); + + self::syncCasesParticipated($instance->case_number, $spToken->user_id, $cpData); + } + } + } + } + + /** + * Sync the cases participated. + * + * @param CaseStarted $caseStarted + * @param TokenInterface $token + * @return void + */ + private static function syncCasesParticipated($caseNumber, $userId, $data) + { + CaseParticipated::updateOrCreate( + [ + 'case_number' => $caseNumber, + 'user_id' => $userId, + ], + $data, + ); + } +} diff --git a/ProcessMaker/Repositories/CaseUtils.php b/ProcessMaker/Repositories/CaseUtils.php index 2d93e1d098..527de49434 100644 --- a/ProcessMaker/Repositories/CaseUtils.php +++ b/ProcessMaker/Repositories/CaseUtils.php @@ -10,6 +10,8 @@ class CaseUtils { const ALLOWED_ELEMENT_TYPES = ['task']; + const ALLOWED_REQUEST_TOKENS = ['task', 'scriptTask', 'callActivity']; + /** * Store processes. * From 6df43f6149e09ddc430516c36952cf21dc620cda Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Tue, 1 Oct 2024 14:42:01 -0400 Subject: [PATCH 4/7] case repository refactor --- ProcessMaker/Repositories/CaseRepository.php | 7 ++- tests/Feature/Cases/CaseParticipatedTest.php | 16 ++---- .../Cases/CaseStartedSubProcessTest.php | 51 ++++++------------- tests/Feature/Cases/CaseStartedTest.php | 31 ++++------- 4 files changed, 36 insertions(+), 69 deletions(-) diff --git a/ProcessMaker/Repositories/CaseRepository.php b/ProcessMaker/Repositories/CaseRepository.php index 14c508c4b7..ecfa3c038a 100644 --- a/ProcessMaker/Repositories/CaseRepository.php +++ b/ProcessMaker/Repositories/CaseRepository.php @@ -13,6 +13,10 @@ class CaseRepository implements CaseRepositoryInterface { const CASE_STATUS_ACTIVE = 'ACTIVE'; + /** + * @var CaseParticipatedRepository + */ + protected CaseParticipatedRepository $caseParticipatedRepository; /** * This property is used to store an instance of `CaseStarted` * when a case started is updated. @@ -20,8 +24,9 @@ class CaseRepository implements CaseRepositoryInterface */ protected ?CaseStarted $case; - public function __construct(protected CaseParticipatedRepository $caseParticipatedRepository) + public function __construct() { + $this->caseParticipatedRepository = new CaseParticipatedRepository(); } /** * Store a new case started. diff --git a/tests/Feature/Cases/CaseParticipatedTest.php b/tests/Feature/Cases/CaseParticipatedTest.php index a0b6795116..01c3d21c49 100644 --- a/tests/Feature/Cases/CaseParticipatedTest.php +++ b/tests/Feature/Cases/CaseParticipatedTest.php @@ -6,7 +6,6 @@ use ProcessMaker\Models\ProcessRequest; use ProcessMaker\Models\ProcessRequestToken; use ProcessMaker\Models\User; -use ProcessMaker\Repositories\CaseParticipatedRepository; use ProcessMaker\Repositories\CaseRepository; use Tests\TestCase; @@ -21,8 +20,7 @@ public function test_create_case_participated() 'process_id' => $process->id, ]); - $repoParticipant = new CaseParticipatedRepository(); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseHas('cases_started', [ @@ -72,8 +70,7 @@ public function test_create_multiple_case_participated() 'process_id' => $process->id, ]); - $repoParticipant = new CaseParticipatedRepository(); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseHas('cases_started', [ @@ -144,8 +141,7 @@ public function test_update_case_participated_users() 'process_id' => $process->id, ]); - $repoParticipant = new CaseParticipatedRepository(); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseHas('cases_started', [ @@ -211,8 +207,7 @@ public function test_update_case_participated_user_tasks() 'process_id' => $process->id, ]); - $repoParticipant = new CaseParticipatedRepository(); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseCount('cases_started', 1); @@ -290,8 +285,7 @@ public function test_update_case_participated_completed() 'process_id' => $process->id, ]); - $repoParticipant = new CaseParticipatedRepository(); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseCount('cases_started', 1); diff --git a/tests/Feature/Cases/CaseStartedSubProcessTest.php b/tests/Feature/Cases/CaseStartedSubProcessTest.php index db4204931c..619c0e77f2 100644 --- a/tests/Feature/Cases/CaseStartedSubProcessTest.php +++ b/tests/Feature/Cases/CaseStartedSubProcessTest.php @@ -9,7 +9,6 @@ use ProcessMaker\Models\ProcessRequest; use ProcessMaker\Models\ProcessRequestToken; use ProcessMaker\Models\User; -use ProcessMaker\Repositories\CaseParticipatedRepository; use ProcessMaker\Repositories\CaseRepository; class CaseStartedSubProcessTest extends TestCase @@ -73,12 +72,10 @@ protected function setUp(): void public function test_create_case_sub_process() { - $repoParticipant = Mockery::mock(CaseParticipatedRepository::class); - - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->parentRequest); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->childRequest); $this->assertDatabaseCount('cases_started', 1); @@ -92,12 +89,10 @@ public function test_create_case_sub_process() public function test_create_case_processes() { - $repoParticipant = Mockery::mock(CaseParticipatedRepository::class); - - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->parentRequest); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->childRequest); $this->assertDatabaseCount('cases_started', 1); @@ -115,12 +110,10 @@ public function test_create_case_processes() public function test_create_case_requests() { - $repoParticipant = Mockery::mock(CaseParticipatedRepository::class); - - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->parentRequest); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->childRequest); $this->assertDatabaseCount('cases_started', 1); @@ -140,12 +133,10 @@ public function test_create_case_requests() public function test_create_case_request_tokens() { - $repoParticipant = new CaseParticipatedRepository(); - - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->parentRequest); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->childRequest); $repo->update($this->parentRequest, $this->parentToken); $repo->update($this->childRequest, $this->childToken); @@ -163,12 +154,10 @@ public function test_create_case_request_tokens() public function test_create_case_tasks() { - $repoParticipant = new CaseParticipatedRepository(); - - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->parentRequest); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->childRequest); $repo->update($this->parentRequest, $this->parentToken); $repo->update($this->childRequest, $this->childToken); @@ -192,9 +181,7 @@ public function test_create_case_tasks() public function test_create_case_participated_processes() { - $repoParticipant = new CaseParticipatedRepository(); - - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->parentRequest); $repo->create($this->childRequest); @@ -227,9 +214,7 @@ public function test_create_case_participated_processes() public function test_create_case_participated_requests() { - $repoParticipant = new CaseParticipatedRepository(); - - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->parentRequest); $repo->create($this->childRequest); @@ -265,9 +250,7 @@ public function test_create_case_participated_requests() public function test_create_case_participated_request_tokens() { - $repoParticipant = new CaseParticipatedRepository(); - - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->parentRequest); $repo->create($this->childRequest); @@ -297,9 +280,7 @@ public function test_create_case_participated_request_tokens() public function test_create_case_participated_tasks() { - $repoParticipant = new CaseParticipatedRepository(); - - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->parentRequest); $repo->create($this->childRequest); @@ -342,9 +323,7 @@ public function test_create_case_participated_tasks() public function test_update_case_participated_completed() { - $repoParticipant = new CaseParticipatedRepository(); - - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($this->parentRequest); $repo->create($this->childRequest); diff --git a/tests/Feature/Cases/CaseStartedTest.php b/tests/Feature/Cases/CaseStartedTest.php index 2b597a5bee..d63cea61ee 100644 --- a/tests/Feature/Cases/CaseStartedTest.php +++ b/tests/Feature/Cases/CaseStartedTest.php @@ -9,7 +9,6 @@ use ProcessMaker\Models\ProcessRequest; use ProcessMaker\Models\ProcessRequestToken; use ProcessMaker\Models\User; -use ProcessMaker\Repositories\CaseParticipatedRepository; use ProcessMaker\Repositories\CaseRepository; class CaseStartedTest extends TestCase @@ -19,12 +18,11 @@ class CaseStartedTest extends TestCase public function test_create_case() { $user = User::factory()->create(); - $repoParticipant = Mockery::mock(CaseParticipatedRepository::class); $instance = ProcessRequest::factory()->create([ 'user_id' => $user->id, ]); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseCount('cases_started', 1); @@ -39,7 +37,6 @@ public function test_create_case() public function test_create_multiple_cases() { $user = User::factory()->create(); - $repoParticipant = Mockery::mock(CaseParticipatedRepository::class); $instance1 = ProcessRequest::factory()->create([ 'user_id' => $user->id, ]); @@ -47,7 +44,7 @@ public function test_create_multiple_cases() 'user_id' => $user->id, ]); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance1); $repo->create($instance2); @@ -71,14 +68,13 @@ public function test_create_case_started_processes() $process = Process::factory()->create(); $user = User::factory()->create(); - $repoParticipant = Mockery::mock(CaseParticipatedRepository::class); $instance = ProcessRequest::factory()->create([ 'user_id' => $user->id, 'process_id' => $process->id, ]); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseCount('cases_started', 1); @@ -97,14 +93,13 @@ public function test_create_case_started_requests() $process = Process::factory()->create(); $user = User::factory()->create(); - $repoParticipant = Mockery::mock(CaseParticipatedRepository::class); $instance = ProcessRequest::factory()->create([ 'user_id' => $user->id, 'process_id' => $process->id, ]); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseCount('cases_started', 1); @@ -130,9 +125,8 @@ public function test_update_case_started_request_tokens() 'process_id' => $process->id, ]); - $repoParticipant = new CaseParticipatedRepository(); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseCount('cases_started', 1); @@ -170,9 +164,8 @@ public function test_update_case_started_tasks() 'process_id' => $process->id, ]); - $repoParticipant = new CaseParticipatedRepository(); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseCount('cases_started', 1); @@ -232,9 +225,8 @@ public function test_update_case_started_script_tasks() 'process_id' => $process->id, ]); - $repoParticipant = new CaseParticipatedRepository(); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseCount('cases_started', 1); @@ -293,9 +285,8 @@ public function test_update_case_started_participants() 'process_id' => $process->id, ]); - $repoParticipant = new CaseParticipatedRepository(); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseCount('cases_started', 1); @@ -352,9 +343,8 @@ public function test_update_case_started_status() 'process_id' => $process->id, ]); - $repoParticipant = new CaseParticipatedRepository(); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseCount('cases_started', 1); @@ -399,12 +389,11 @@ public function test_update_case_started_status() public function test_try_update_if_case_has_not_been_created() { $user = User::factory()->create(); - $repoParticipant = Mockery::mock(CaseParticipatedRepository::class); $instance = ProcessRequest::factory()->create([ 'user_id' => null, ]); - $repo = new CaseRepository($repoParticipant); + $repo = new CaseRepository(); $repo->create($instance); $this->assertDatabaseCount('cases_started', 0); From c9572a6d2f4cc232f858d6ec44cbe53c3fe78ae1 Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Tue, 1 Oct 2024 16:02:33 -0400 Subject: [PATCH 5/7] test: case exception tests --- ProcessMaker/Exception/CaseException.php | 3 +- .../CaseParticipatedRepository.php | 16 ++- ProcessMaker/Repositories/CaseRepository.php | 27 ++-- tests/Feature/Cases/CaseExceptionTest.php | 135 ++++++++++++++++++ 4 files changed, 163 insertions(+), 18 deletions(-) create mode 100644 tests/Feature/Cases/CaseExceptionTest.php diff --git a/ProcessMaker/Exception/CaseException.php b/ProcessMaker/Exception/CaseException.php index 9a7966c346..4eb688de16 100644 --- a/ProcessMaker/Exception/CaseException.php +++ b/ProcessMaker/Exception/CaseException.php @@ -9,8 +9,7 @@ class CaseException extends Exception { public function __construct($message = "", $code = 0, Exception $previous = null) { - parent::__construct($message, $code, $previous); Log::error('CaseException: ' . $message); - Log::error('CaseException: ' . $this->getTraceAsString()); + parent::__construct($message, $code, $previous); } } diff --git a/ProcessMaker/Repositories/CaseParticipatedRepository.php b/ProcessMaker/Repositories/CaseParticipatedRepository.php index 1e2e108c45..d46ca53075 100644 --- a/ProcessMaker/Repositories/CaseParticipatedRepository.php +++ b/ProcessMaker/Repositories/CaseParticipatedRepository.php @@ -2,6 +2,7 @@ namespace ProcessMaker\Repositories; +use Illuminate\Support\Facades\Log; use ProcessMaker\Exception\CaseException; use ProcessMaker\Models\CaseParticipated; use ProcessMaker\Models\CaseStarted; @@ -44,8 +45,9 @@ public function create(CaseStarted $case, TokenInterface $token): void 'initiated_at' => $case->initiated_at, 'completed_at' => null, ]); - } catch (\Exception $e) { - throw new CaseException($e->getMessage()); + } catch (CaseException $e) { + Log::error('CaseException: ' . $e->getMessage()); + Log::error('CaseException: ' . $e->getTraceAsString()); } } @@ -73,8 +75,9 @@ public function update(CaseStarted $case, TokenInterface $token) 'tasks' => CaseUtils::storeTasks($token, $this->caseParticipated->tasks), 'participants' => $case->participants, ]); - } catch (\Exception $e) { - throw new CaseException($e->getMessage()); + } catch (CaseException $e) { + Log::error('CaseException: ' . $e->getMessage()); + Log::error('CaseException: ' . $e->getTraceAsString()); } } @@ -90,8 +93,9 @@ public function updateStatus(int $caseNumber, array $statusData) try { CaseParticipated::where('case_number', $caseNumber) ->update($statusData); - } catch (\Exception $e) { - throw new CaseException($e->getMessage()); + } catch (CaseException $e) { + Log::error('CaseException: ' . $e->getMessage()); + Log::error('CaseException: ' . $e->getTraceAsString()); } } diff --git a/ProcessMaker/Repositories/CaseRepository.php b/ProcessMaker/Repositories/CaseRepository.php index ecfa3c038a..d985d54117 100644 --- a/ProcessMaker/Repositories/CaseRepository.php +++ b/ProcessMaker/Repositories/CaseRepository.php @@ -3,6 +3,7 @@ namespace ProcessMaker\Repositories; use Carbon\Carbon; +use Illuminate\Support\Facades\Log; use ProcessMaker\Contracts\CaseRepositoryInterface; use ProcessMaker\Exception\CaseException; use ProcessMaker\Models\CaseStarted; @@ -37,7 +38,8 @@ public function __construct() public function create(ExecutionInstanceInterface $instance): void { if (is_null($instance->case_number)) { - throw new CaseException('case number is required, method=create, instance=' . $instance->getKey()); + Log::error('case number is required, method=create, instance=' . $instance->getKey()); + return; } if ($this->checkIfCaseStartedExist($instance->case_number)) { @@ -61,8 +63,9 @@ public function create(ExecutionInstanceInterface $instance): void 'initiated_at' => $instance->initiated_at, 'completed_at' => null, ]); - } catch (\Exception $e) { - throw new CaseException($e->getMessage()); + } catch (CaseException $e) { + Log::error('CaseException: ' . $e->getMessage()); + Log::error('CaseException: ' . $e->getTraceAsString()); } } @@ -76,7 +79,8 @@ public function create(ExecutionInstanceInterface $instance): void public function update(ExecutionInstanceInterface $instance, TokenInterface $token): void { if (!$this->checkIfCaseStartedExist($instance->case_number)) { - throw new CaseException('case started not found, method=update, instance=' . $instance->getKey()); + Log::error('case started not found, method=update, instance=' . $instance->getKey()); + return; } try { @@ -88,8 +92,9 @@ public function update(ExecutionInstanceInterface $instance, TokenInterface $tok $this->updateParticipants($token); $this->case->saveOrFail(); - } catch (\Exception $e) { - throw new CaseException($e->getMessage()); + } catch (CaseException $e) { + Log::error('CaseException: ' . $e->getMessage()); + Log::error('CaseException: ' . $e->getTraceAsString()); } } @@ -118,8 +123,9 @@ public function updateStatus(ExecutionInstanceInterface $instance): void // 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) { - throw new CaseException($e->getMessage()); + } catch (CaseException $e) { + Log::error('CaseException: ' . $e->getMessage()); + Log::error('CaseException: ' . $e->getTraceAsString()); } } @@ -190,8 +196,9 @@ private function updateSubProcesses(ExecutionInstanceInterface $instance): void $this->case->requests = CaseUtils::storeRequests($instance, $this->case->requests); $this->case->saveOrFail(); - } catch (\Exception $e) { - throw new CaseException($e->getMessage()); + } catch (CaseException $e) { + Log::error('CaseException: ' . $e->getMessage()); + Log::error('CaseException: ' . $e->getTraceAsString()); } } } diff --git a/tests/Feature/Cases/CaseExceptionTest.php b/tests/Feature/Cases/CaseExceptionTest.php new file mode 100644 index 0000000000..0930d1720f --- /dev/null +++ b/tests/Feature/Cases/CaseExceptionTest.php @@ -0,0 +1,135 @@ +user = User::factory()->create(); + $this->process = Process::factory()->create(); + + $this->instance = ProcessRequest::factory()->create([ + 'user_id' => $this->user->id, + 'process_id' => $this->process->id, + ]); + $this->token = ProcessRequestToken::factory()->create([ + 'user_id' => $this->user->id, + 'process_request_id' => $this->instance->id, + 'element_type' => 'task', + ]); + + $this->instance2 = ProcessRequest::factory()->create([ + 'user_id' => $this->user->id, + 'process_id' => $this->process->id, + ]); + $this->token2 = ProcessRequestToken::factory()->create([ + 'user_id' => $this->user->id, + 'process_request_id' => $this->instance2->id, + 'element_type' => 'task', + ]); + } + + public function test_create_case_missing_case_number(): void + { + $this->withoutExceptionHandling(); + + $this->instance->case_number = null; + $repo = new CaseRepository(); + $repo->create($this->instance); + + $this->assertDatabaseCount('cases_started', 0); + } + + public function test_create_case_missing_user_id(): void + { + $this->withoutExceptionHandling(); + + try { + $this->instance->user_id = null; + $repo = new CaseRepository(); + $repo->create($this->instance); + } catch (\Exception $e) { + $this->assertStringContainsString('Column \'user_id\' cannot be null', $e->getMessage()); + } + + $this->assertDatabaseCount('cases_started', 0); + } + + public function test_create_case_missing_case_title(): void + { + $this->withoutExceptionHandling(); + + try { + $this->instance->case_title = null; + $repo = new CaseRepository(); + $repo->create($this->instance); + } catch (\Exception $e) { + $this->assertStringContainsString('Column \'case_title\' cannot be null', $e->getMessage()); + } + + $this->assertDatabaseCount('cases_started', 0); + } + + public function test_update_case_missing_case_started(): void + { + $this->withoutExceptionHandling(); + + try { + $this->instance->case_title = null; + $repo = new CaseRepository(); + $repo->create($this->instance); + } catch (\Exception $e) { + $this->assertStringContainsString('Column \'case_title\' cannot be null', $e->getMessage()); + } + + $this->assertDatabaseCount('cases_started', 0); + + try { + $repo->update($this->instance, $this->token); + } catch (\Exception $e) { + $this->assertEquals( + 'case started not found, method=update, instance=' . $this->instance->getKey(), $e->getMessage() + ); + } + } + + public function test_artisan_sync_command_missing_ids(): void + { + $this->artisan('cases:sync') + ->expectsOutput('Please specify a list of request IDs.') + ->assertExitCode(0); + } + + public function test_artisan_sync_command_success(): void + { + $this->artisan('cases:sync --request_ids=1,2') + ->expectsOutput('Case started synced 1') + ->expectsOutput('Case started synced 2') + ->assertExitCode(0); + + $this->assertDatabaseCount('cases_started', 2); + } +} From 7055d40c21bba26bbabfdcbd6df42726183ee6bf Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Tue, 1 Oct 2024 22:52:12 -0400 Subject: [PATCH 6/7] fix: case exception handling and logging --- ProcessMaker/Exception/CaseException.php | 15 --------------- .../Repositories/CaseParticipatedRepository.php | 7 +++---- ProcessMaker/Repositories/CaseRepository.php | 9 ++++----- tests/Feature/Cases/CaseExceptionTest.php | 7 +++---- 4 files changed, 10 insertions(+), 28 deletions(-) delete mode 100644 ProcessMaker/Exception/CaseException.php diff --git a/ProcessMaker/Exception/CaseException.php b/ProcessMaker/Exception/CaseException.php deleted file mode 100644 index 4eb688de16..0000000000 --- a/ProcessMaker/Exception/CaseException.php +++ /dev/null @@ -1,15 +0,0 @@ - $case->initiated_at, 'completed_at' => null, ]); - } catch (CaseException $e) { + } catch (\Exception $e) { Log::error('CaseException: ' . $e->getMessage()); Log::error('CaseException: ' . $e->getTraceAsString()); } @@ -75,7 +74,7 @@ public function update(CaseStarted $case, TokenInterface $token) 'tasks' => CaseUtils::storeTasks($token, $this->caseParticipated->tasks), 'participants' => $case->participants, ]); - } catch (CaseException $e) { + } catch (\Exception $e) { Log::error('CaseException: ' . $e->getMessage()); Log::error('CaseException: ' . $e->getTraceAsString()); } @@ -93,7 +92,7 @@ public function updateStatus(int $caseNumber, array $statusData) try { CaseParticipated::where('case_number', $caseNumber) ->update($statusData); - } catch (CaseException $e) { + } catch (\Exception $e) { Log::error('CaseException: ' . $e->getMessage()); Log::error('CaseException: ' . $e->getTraceAsString()); } diff --git a/ProcessMaker/Repositories/CaseRepository.php b/ProcessMaker/Repositories/CaseRepository.php index d985d54117..f1b72d3b8d 100644 --- a/ProcessMaker/Repositories/CaseRepository.php +++ b/ProcessMaker/Repositories/CaseRepository.php @@ -5,7 +5,6 @@ use Carbon\Carbon; use Illuminate\Support\Facades\Log; use ProcessMaker\Contracts\CaseRepositoryInterface; -use ProcessMaker\Exception\CaseException; use ProcessMaker\Models\CaseStarted; use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface; use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface; @@ -63,7 +62,7 @@ public function create(ExecutionInstanceInterface $instance): void 'initiated_at' => $instance->initiated_at, 'completed_at' => null, ]); - } catch (CaseException $e) { + } catch (\Exception $e) { Log::error('CaseException: ' . $e->getMessage()); Log::error('CaseException: ' . $e->getTraceAsString()); } @@ -92,7 +91,7 @@ public function update(ExecutionInstanceInterface $instance, TokenInterface $tok $this->updateParticipants($token); $this->case->saveOrFail(); - } catch (CaseException $e) { + } catch (\Exception $e) { Log::error('CaseException: ' . $e->getMessage()); Log::error('CaseException: ' . $e->getTraceAsString()); } @@ -123,7 +122,7 @@ public function updateStatus(ExecutionInstanceInterface $instance): void // Update the case started and case participated CaseStarted::where('case_number', $instance->case_number)->update($data); $this->caseParticipatedRepository->updateStatus($instance->case_number, $data); - } catch (CaseException $e) { + } catch (\Exception $e) { Log::error('CaseException: ' . $e->getMessage()); Log::error('CaseException: ' . $e->getTraceAsString()); } @@ -196,7 +195,7 @@ private function updateSubProcesses(ExecutionInstanceInterface $instance): void $this->case->requests = CaseUtils::storeRequests($instance, $this->case->requests); $this->case->saveOrFail(); - } catch (CaseException $e) { + } catch (\Exception $e) { Log::error('CaseException: ' . $e->getMessage()); Log::error('CaseException: ' . $e->getTraceAsString()); } diff --git a/tests/Feature/Cases/CaseExceptionTest.php b/tests/Feature/Cases/CaseExceptionTest.php index 0930d1720f..5bf3317dd4 100644 --- a/tests/Feature/Cases/CaseExceptionTest.php +++ b/tests/Feature/Cases/CaseExceptionTest.php @@ -2,7 +2,6 @@ namespace Tests\Feature\Cases; -use Exception; use ProcessMaker\Models\Process; use ProcessMaker\Models\ProcessRequest; use ProcessMaker\Models\ProcessRequestToken; @@ -125,9 +124,9 @@ public function test_artisan_sync_command_missing_ids(): void public function test_artisan_sync_command_success(): void { - $this->artisan('cases:sync --request_ids=1,2') - ->expectsOutput('Case started synced 1') - ->expectsOutput('Case started synced 2') + $this->artisan('cases:sync --request_ids=' . $this->instance->id . ',' . $this->instance2->id) + ->expectsOutput('Case started synced ' . $this->instance->case_number) + ->expectsOutput('Case started synced ' . $this->instance2->case_number) ->assertExitCode(0); $this->assertDatabaseCount('cases_started', 2); From d7ea3838eb6cc9e2c66166af1948cc212936b518 Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Wed, 2 Oct 2024 13:18:55 -0400 Subject: [PATCH 7/7] fix: use status active constant --- ProcessMaker/Repositories/CaseSyncRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProcessMaker/Repositories/CaseSyncRepository.php b/ProcessMaker/Repositories/CaseSyncRepository.php index 2d270d1974..64ac329e20 100644 --- a/ProcessMaker/Repositories/CaseSyncRepository.php +++ b/ProcessMaker/Repositories/CaseSyncRepository.php @@ -37,7 +37,7 @@ public static function syncCases(array $requestIds): array $csRequestTokens = collect(); $csTasks = collect(); $participants = $instance->participants->map->only('id', 'fullName', 'title', 'avatar'); - $status = $instance->status === 'ACTIVE' ? 'IN_PROGRESS' : $instance->status; + $status = $instance->status === CaseRepository::CASE_STATUS_ACTIVE ? 'IN_PROGRESS' : $instance->status; $cpData = self::prepareCaseStartedData($instance, $status, $participants);