From 2fbba858fc61d0b08f477966d66c3432f1152ce8 Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Wed, 25 Sep 2024 15:14:47 -0400 Subject: [PATCH 1/5] feat: populate sub processes --- .../CaseParticipatedRepository.php | 83 ++++++++++++++----- ProcessMaker/Repositories/CaseRepository.php | 81 ++++++++++++++---- 2 files changed, 126 insertions(+), 38 deletions(-) diff --git a/ProcessMaker/Repositories/CaseParticipatedRepository.php b/ProcessMaker/Repositories/CaseParticipatedRepository.php index 025d3fe7d8..85f261bb66 100644 --- a/ProcessMaker/Repositories/CaseParticipatedRepository.php +++ b/ProcessMaker/Repositories/CaseParticipatedRepository.php @@ -18,23 +18,42 @@ class CaseParticipatedRepository public function create(CaseStarted $case, TokenInterface $token): void { try { + $processes = [ + [ + 'id' => $token->processRequest->process->id, + 'name' => $token->processRequest->process->name, + ], + ]; + + $requests = [ + [ + 'id' => $token->processRequest->id, + 'name' => $token->processRequest->name, + 'parent_request_id' => $token->processRequest->parentRequest->id, + ], + ]; + + $tasks = collect(); + + if (in_array($token->element_type, ['task'])) { + $tasks->push([ + 'id' => $token->getKey(), + 'element_id' => $token->element_id, + 'name' => $token->element_name, + 'process_id' => $token->process_id, + ]); + } + CaseParticipated::create([ 'user_id' => $token->user->id, '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, + 'processes' => $processes, + 'requests' => $requests, 'request_tokens' => [$token->getKey()], - 'tasks' => [ - [ - 'id' => $token->getKey(), - 'element_id' => $token->element_id, - 'name' => $token->element_name, - 'process_id' => $token->process_id, - ], - ], + 'tasks' => $tasks, 'participants' => $case->participants, 'initiated_at' => $case->initiated_at, 'completed_at' => null, @@ -58,26 +77,50 @@ public function update(CaseStarted $case, TokenInterface $token) ->where('case_number', $case->case_number) ->first(); + if (is_null($caseParticipated)) { + return; + } + + // Store the sub-processes and requests + $processes = $caseParticipated->processes->push([ + 'id' => $token->processRequest->process->id, + 'name' => $token->processRequest->process->name, + ]) + ->unique('id') + ->values(); + + $requests = $caseParticipated->requests->push([ + 'id' => $token->processRequest->id, + 'name' => $token->processRequest->name, + 'parent_request_id' => $token->processRequest->parentRequest->id, + ]) + ->unique('id') + ->values(); + // Add the token data to the request_tokens $requestTokens = $caseParticipated->request_tokens->push($token->getKey()) ->unique() ->values(); // Add the task data to the tasks - $tasks = $caseParticipated->tasks->push([ - 'id' => $token->getKey(), - 'element_id' => $token->element_id, - 'name' => $token->element_name, - 'process_id' => $token->process_id, - ]) - ->unique('id') - ->values(); + $tasks = $caseParticipated->tasks; + + if (in_array($token->element_type, ['task'])) { + $tasks = $tasks->push([ + 'id' => $token->getKey(), + 'element_id' => $token->element_id, + 'name' => $token->element_name, + 'process_id' => $token->process_id, + ]) + ->unique('id') + ->values(); + } $caseParticipated->update([ 'case_title' => $case->case_title, 'case_title_formatted' => $case->case_title_formatted, 'case_status' => $case->case_status, - 'processes' => $case->processes, - 'requests' => $case->requests, + 'processes' => $processes, + 'requests' => $requests, 'request_tokens' => $requestTokens, 'tasks' => $tasks, 'participants' => $case->participants, diff --git a/ProcessMaker/Repositories/CaseRepository.php b/ProcessMaker/Repositories/CaseRepository.php index e77f14665b..cc498f42ee 100644 --- a/ProcessMaker/Repositories/CaseRepository.php +++ b/ProcessMaker/Repositories/CaseRepository.php @@ -10,6 +10,11 @@ class CaseRepository implements CaseRepositoryInterface { + /** + * @var CaseStarted|null + */ + protected ?CaseStarted $case; + public function __construct(protected CaseParticipatedRepository $caseParticipatedRepository) { } @@ -22,6 +27,8 @@ public function __construct(protected CaseParticipatedRepository $caseParticipat public function create(ExecutionInstanceInterface $instance): void { if (is_null($instance->case_number) || $this->checkIfCaseStartedExist($instance->case_number)) { + $this->updateSubProcesses($instance); + return; } @@ -37,7 +44,7 @@ public function create(ExecutionInstanceInterface $instance): void [ 'id' => $instance->id, 'name' => $instance->name, - 'parent_request_id' => $instance->parentRequest->id ?? 0, + 'parent_request_id' => $instance->parentRequest->id, ], ]; @@ -70,21 +77,19 @@ public function create(ExecutionInstanceInterface $instance): void public function update(ExecutionInstanceInterface $instance, TokenInterface $token): void { try { - $case = CaseStarted::where('case_number', $instance->case_number)->first(); - - if (is_null($case)) { + if (!$this->checkIfCaseStartedExist($instance->case_number)) { return; } - $case->case_title = $instance->case_title; - $case->case_status = $instance->status === 'ACTIVE' ? 'IN_PROGRESS' : $instance->status; + $this->case->case_title = $instance->case_title; + $this->case->case_status = $instance->status === 'ACTIVE' ? 'IN_PROGRESS' : $instance->status; - $case->request_tokens = $case->request_tokens->push($token->getKey()) + $this->case->request_tokens = $this->case->request_tokens->push($token->getKey()) ->unique() ->values(); - if (!in_array($token->element_type, ['scriptTask'])) { - $case->tasks = $case->tasks->push([ + if (in_array($token->element_type, ['task'])) { + $this->case->tasks = $this->case->tasks->push([ 'id' => $token->getKey(), 'element_id' => $token->element_id, 'name' => $token->element_name, @@ -94,9 +99,9 @@ public function update(ExecutionInstanceInterface $instance, TokenInterface $tok ->values(); } - $this->updateParticipants($case, $token); + $this->updateParticipants($token); - $case->saveOrFail(); + $this->case->saveOrFail(); } catch (\Exception $e) { \Log::error($e->getMessage()); } @@ -110,6 +115,10 @@ public function update(ExecutionInstanceInterface $instance, TokenInterface $tok */ public function updateStatus(ExecutionInstanceInterface $instance): void { + if (!is_null($instance->parent_request_id)) { + return; + } + try { $data = [ 'case_status' => $instance->status, @@ -130,11 +139,10 @@ public function updateStatus(ExecutionInstanceInterface $instance): void /** * Update the participants of the case started. * - * @param CaseStarted $case * @param TokenInterface $token * @return void */ - private function updateParticipants(CaseStarted $case, TokenInterface $token): void + private function updateParticipants(TokenInterface $token): void { $user = $token->user; @@ -142,22 +150,22 @@ private function updateParticipants(CaseStarted $case, TokenInterface $token): v return; } - $participantExists = $case->participants->contains(function ($participant) use ($user) { + $participantExists = $this->case->participants->contains(function ($participant) use ($user) { return $participant['id'] === $user->id; }); if (!$participantExists) { - $case->participants->push([ + $this->case->participants->push([ 'id' => $user->id, 'name' => $user->getFullName(), 'title' => $user->title, 'avatar' => $user->avatar, ]); - $this->caseParticipatedRepository->create($case, $token); + $this->caseParticipatedRepository->create($this->case, $token); } - $this->caseParticipatedRepository->update($case, $token); + $this->caseParticipatedRepository->update($this->case, $token); } /** @@ -168,6 +176,43 @@ private function updateParticipants(CaseStarted $case, TokenInterface $token): v */ private function checkIfCaseStartedExist(int $caseNumber): bool { - return CaseStarted::where('case_number', $caseNumber)->count() > 0; + $this->case = CaseStarted::where('case_number', $caseNumber)->first(); + + return !is_null($this->case); + } + + /** + * Update the processes and requests of the case started. + * + * @param ExecutionInstanceInterface $instance + * @return void + */ + private function updateSubProcesses(ExecutionInstanceInterface $instance): void + { + if (is_null($instance->parent_request_id)) { + return; + } + + try { + // Store the sub-processes and requests + $this->case->processes = $this->case->processes->push([ + 'id' => $instance->process->id, + 'name' => $instance->process->name, + ]) + ->unique('id') + ->values(); + + $this->case->requests = $this->case->requests->push([ + 'id' => $instance->id, + 'name' => $instance->name, + 'parent_request_id' => $instance->parentRequest->id, + ]) + ->unique('id') + ->values(); + + $this->case->saveOrFail(); + } catch (\Exception $th) { + \Log::error($th->getMessage()); + } } } From eeec1e6cef54675f17d6fb7e4cdbc7f75848731b Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Thu, 26 Sep 2024 08:25:01 -0400 Subject: [PATCH 2/5] feat: ref common functions --- .../CaseParticipatedRepository.php | 104 +++++------------- ProcessMaker/Repositories/CaseRepository.php | 66 +++-------- ProcessMaker/Repositories/CaseUtils.php | 84 ++++++++++++++ 3 files changed, 130 insertions(+), 124 deletions(-) create mode 100644 ProcessMaker/Repositories/CaseUtils.php diff --git a/ProcessMaker/Repositories/CaseParticipatedRepository.php b/ProcessMaker/Repositories/CaseParticipatedRepository.php index 85f261bb66..7f7085c8ef 100644 --- a/ProcessMaker/Repositories/CaseParticipatedRepository.php +++ b/ProcessMaker/Repositories/CaseParticipatedRepository.php @@ -8,6 +8,11 @@ class CaseParticipatedRepository { + /** + * @var CaseParticipated|null + */ + protected ?CaseParticipated $caseParticipated; + /** * Store a new case participated. * @@ -17,43 +22,21 @@ class CaseParticipatedRepository */ public function create(CaseStarted $case, TokenInterface $token): void { - try { - $processes = [ - [ - 'id' => $token->processRequest->process->id, - 'name' => $token->processRequest->process->name, - ], - ]; - - $requests = [ - [ - 'id' => $token->processRequest->id, - 'name' => $token->processRequest->name, - 'parent_request_id' => $token->processRequest->parentRequest->id, - ], - ]; - - $tasks = collect(); - - if (in_array($token->element_type, ['task'])) { - $tasks->push([ - 'id' => $token->getKey(), - 'element_id' => $token->element_id, - 'name' => $token->element_name, - 'process_id' => $token->process_id, - ]); - } + if ($this->checkIfCaseParticipatedExist($token->user->id, $case->case_number)) { + return; + } - CaseParticipated::create([ + try { + $this->caseParticipated = CaseParticipated::create([ 'user_id' => $token->user->id, 'case_number' => $case->case_number, 'case_title' => $case->case_title, 'case_title_formatted' => $case->case_title_formatted, 'case_status' => $case->case_status, - 'processes' => $processes, - 'requests' => $requests, - 'request_tokens' => [$token->getKey()], - 'tasks' => $tasks, + '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' => $case->participants, 'initiated_at' => $case->initiated_at, 'completed_at' => null, @@ -73,56 +56,18 @@ public function create(CaseStarted $case, TokenInterface $token): void public function update(CaseStarted $case, TokenInterface $token) { try { - $caseParticipated = CaseParticipated::where('user_id', $token->user->id) - ->where('case_number', $case->case_number) - ->first(); - - if (is_null($caseParticipated)) { + if (!$this->checkIfCaseParticipatedExist($token->user->id, $case->case_number)) { return; } - // Store the sub-processes and requests - $processes = $caseParticipated->processes->push([ - 'id' => $token->processRequest->process->id, - 'name' => $token->processRequest->process->name, - ]) - ->unique('id') - ->values(); - - $requests = $caseParticipated->requests->push([ - 'id' => $token->processRequest->id, - 'name' => $token->processRequest->name, - 'parent_request_id' => $token->processRequest->parentRequest->id, - ]) - ->unique('id') - ->values(); - - // Add the token data to the request_tokens - $requestTokens = $caseParticipated->request_tokens->push($token->getKey()) - ->unique() - ->values(); - // Add the task data to the tasks - $tasks = $caseParticipated->tasks; - - if (in_array($token->element_type, ['task'])) { - $tasks = $tasks->push([ - 'id' => $token->getKey(), - 'element_id' => $token->element_id, - 'name' => $token->element_name, - 'process_id' => $token->process_id, - ]) - ->unique('id') - ->values(); - } - - $caseParticipated->update([ + $this->caseParticipated->updateOrFail([ 'case_title' => $case->case_title, 'case_title_formatted' => $case->case_title_formatted, 'case_status' => $case->case_status, - 'processes' => $processes, - 'requests' => $requests, - 'request_tokens' => $requestTokens, - 'tasks' => $tasks, + 'processes' => CaseUtils::storeProcesses($token->processRequest, $this->caseParticipated->processes), + 'requests' => CaseUtils::storeRequests($token->processRequest, $this->caseParticipated->requests), + 'request_tokens' => CaseUtils::storeRequestTokens($token->getKey(), $this->caseParticipated->request_tokens), + 'tasks' => CaseUtils::storeTasks($token, $this->caseParticipated->tasks), 'participants' => $case->participants, ]); } catch (\Exception $e) { @@ -146,4 +91,13 @@ public function updateStatus(int $caseNumber, array $statusData) \Log::error($e->getMessage()); } } + + private function checkIfCaseParticipatedExist(int $userId, int $caseNumber): bool + { + $this->caseParticipated = CaseParticipated::where('user_id', $userId) + ->where('case_number', $caseNumber) + ->first(); + + return !is_null($this->caseParticipated); + } } diff --git a/ProcessMaker/Repositories/CaseRepository.php b/ProcessMaker/Repositories/CaseRepository.php index cc498f42ee..8ed1835a5d 100644 --- a/ProcessMaker/Repositories/CaseRepository.php +++ b/ProcessMaker/Repositories/CaseRepository.php @@ -26,36 +26,25 @@ public function __construct(protected CaseParticipatedRepository $caseParticipat */ public function create(ExecutionInstanceInterface $instance): void { - if (is_null($instance->case_number) || $this->checkIfCaseStartedExist($instance->case_number)) { + if (is_null($instance->case_number)) { + \Log::error('Case number is required. instance: ' . $instance->getKey()); + } + + if ($this->checkIfCaseStartedExist($instance->case_number)) { $this->updateSubProcesses($instance); return; } try { - $processes = [ - [ - 'id' => $instance->process->id, - 'name' => $instance->process->name, - ], - ]; - - $requests = [ - [ - 'id' => $instance->id, - 'name' => $instance->name, - 'parent_request_id' => $instance->parentRequest->id, - ], - ]; - - CaseStarted::create([ + $this->case = CaseStarted::create([ 'case_number' => $instance->case_number, 'user_id' => $instance->user_id, 'case_title' => $instance->case_title, 'case_title_formatted' => $instance->case_title_formatted, - 'case_status' => 'IN_PROGRESS', - 'processes' => $processes, - 'requests' => $requests, + 'case_status' => $instance->status === 'ACTIVE' ? 'IN_PROGRESS' : $instance->status, + 'processes' => CaseUtils::storeProcesses($instance, collect()), + 'requests' => CaseUtils::storeRequests($instance, collect()), 'request_tokens' => [], 'tasks' => [], 'participants' => [], @@ -64,6 +53,8 @@ public function create(ExecutionInstanceInterface $instance): void ]); } catch (\Exception $e) { \Log::error($e->getMessage()); + // trace + \Log::error($e->getTraceAsString()); } } @@ -78,26 +69,15 @@ public function update(ExecutionInstanceInterface $instance, TokenInterface $tok { try { if (!$this->checkIfCaseStartedExist($instance->case_number)) { + \Log::error('Case number not found. instance: ' . $instance->id); + return; } $this->case->case_title = $instance->case_title; $this->case->case_status = $instance->status === 'ACTIVE' ? 'IN_PROGRESS' : $instance->status; - - $this->case->request_tokens = $this->case->request_tokens->push($token->getKey()) - ->unique() - ->values(); - - if (in_array($token->element_type, ['task'])) { - $this->case->tasks = $this->case->tasks->push([ - 'id' => $token->getKey(), - 'element_id' => $token->element_id, - 'name' => $token->element_name, - 'process_id' => $token->process_id, - ]) - ->unique('id') - ->values(); - } + $this->case->request_tokens = CaseUtils::storeRequestTokens($token->getKey(), $this->case->request_tokens); + $this->case->tasks = CaseUtils::storeTasks($token, $this->case->tasks); $this->updateParticipants($token); @@ -195,20 +175,8 @@ private function updateSubProcesses(ExecutionInstanceInterface $instance): void try { // Store the sub-processes and requests - $this->case->processes = $this->case->processes->push([ - 'id' => $instance->process->id, - 'name' => $instance->process->name, - ]) - ->unique('id') - ->values(); - - $this->case->requests = $this->case->requests->push([ - 'id' => $instance->id, - 'name' => $instance->name, - 'parent_request_id' => $instance->parentRequest->id, - ]) - ->unique('id') - ->values(); + $this->case->processes = CaseUtils::storeProcesses($instance, $this->case->processes); + $this->case->requests = CaseUtils::storeRequests($instance, $this->case->requests); $this->case->saveOrFail(); } catch (\Exception $th) { diff --git a/ProcessMaker/Repositories/CaseUtils.php b/ProcessMaker/Repositories/CaseUtils.php new file mode 100644 index 0000000000..2d93e1d098 --- /dev/null +++ b/ProcessMaker/Repositories/CaseUtils.php @@ -0,0 +1,84 @@ +push([ + 'id' => $instance->process->id, + 'name' => $instance->process->name, + ]) + ->unique('id') + ->values(); + } + + /** + * Store requests. + * + * @param ExecutionInstanceInterface $instance + * @param Collection $requests + * @return Collection + */ + public static function storeRequests(ExecutionInstanceInterface $instance, Collection $requests) + { + return $requests->push([ + 'id' => $instance->id, + 'name' => $instance->name, + 'parent_request_id' => $instance?->parentRequest?->id, + ]) + ->unique('id') + ->values(); + } + + /** + * Store request tokens. + * + * @param int $tokenId + * @param Collection $requestTokens + * @return Collection + */ + public static function storeRequestTokens(int $tokenId, Collection $requestTokens) + { + return $requestTokens->push($tokenId) + ->unique() + ->values(); + } + + /** + * Store tasks. + * + * @param TokenInterface $token + * @param Collection $tasks + * @return Collection + */ + public static function storeTasks(TokenInterface $token, Collection $tasks) + { + if (in_array($token->element_type, self::ALLOWED_ELEMENT_TYPES)) { + return $tasks->push([ + 'id' => $token->getKey(), + 'element_id' => $token->element_id, + 'name' => $token->element_name, + 'process_id' => $token->process_id, + ]) + ->unique('id') + ->values(); + } + + return $tasks; + } +} From 3c93fd469561df0be12ab6406217d4f42e3fffb9 Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Thu, 26 Sep 2024 14:40:02 -0400 Subject: [PATCH 3/5] test: populate case sub processes --- ProcessMaker/Repositories/CaseRepository.php | 9 +- tests/Feature/Cases/CaseParticipatedTest.php | 13 +- .../Cases/CaseStartedSubProcessTest.php | 400 ++++++++++++++++++ tests/Feature/Cases/CaseStartedTest.php | 5 + 4 files changed, 425 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/Cases/CaseStartedSubProcessTest.php diff --git a/ProcessMaker/Repositories/CaseRepository.php b/ProcessMaker/Repositories/CaseRepository.php index 8ed1835a5d..27ed0ff6a5 100644 --- a/ProcessMaker/Repositories/CaseRepository.php +++ b/ProcessMaker/Repositories/CaseRepository.php @@ -28,6 +28,8 @@ public function create(ExecutionInstanceInterface $instance): void { if (is_null($instance->case_number)) { \Log::error('Case number is required. instance: ' . $instance->getKey()); + + return; } if ($this->checkIfCaseStartedExist($instance->case_number)) { @@ -53,7 +55,6 @@ public function create(ExecutionInstanceInterface $instance): void ]); } catch (\Exception $e) { \Log::error($e->getMessage()); - // trace \Log::error($e->getTraceAsString()); } } @@ -67,6 +68,12 @@ 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()); + + return; + } + try { if (!$this->checkIfCaseStartedExist($instance->case_number)) { \Log::error('Case number not found. instance: ' . $instance->id); diff --git a/tests/Feature/Cases/CaseParticipatedTest.php b/tests/Feature/Cases/CaseParticipatedTest.php index d28e80c0dc..a0b6795116 100644 --- a/tests/Feature/Cases/CaseParticipatedTest.php +++ b/tests/Feature/Cases/CaseParticipatedTest.php @@ -38,6 +38,7 @@ public function test_create_case_participated() $token = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token); @@ -53,7 +54,7 @@ public function test_create_case_participated() 'processes->[0]->name' => $process->name, 'requests->[0]->id' => $instance->id, 'requests->[0]->name' => $instance->name, - 'requests->[0]->parent_request_id' => $instance->parent_request_id ?? 0, + 'requests->[0]->parent_request_id' => $instance->parent_request_id, 'request_tokens->[0]' => $token->id, 'tasks->[0]->id' => $token->id, 'tasks->[0]->element_id' => $token->element_id, @@ -86,6 +87,7 @@ public function test_create_multiple_case_participated() $token = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token); @@ -107,6 +109,7 @@ public function test_create_multiple_case_participated() $token2 = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token2); @@ -156,6 +159,7 @@ public function test_update_case_participated_users() $token = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token); @@ -177,6 +181,7 @@ public function test_update_case_participated_users() $token2 = ProcessRequestToken::factory()->create([ 'user_id' => $user2->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token2); @@ -215,6 +220,7 @@ public function test_update_case_participated_user_tasks() $token = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token); @@ -233,6 +239,7 @@ public function test_update_case_participated_user_tasks() $token2 = ProcessRequestToken::factory()->create([ 'user_id' => $user2->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token2); @@ -251,6 +258,7 @@ public function test_update_case_participated_user_tasks() $token3 = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token3); @@ -291,6 +299,7 @@ public function test_update_case_participated_completed() $token = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token); @@ -310,6 +319,7 @@ public function test_update_case_participated_completed() $token2 = ProcessRequestToken::factory()->create([ 'user_id' => $user2->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token2); @@ -329,6 +339,7 @@ public function test_update_case_participated_completed() $token3 = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token3); diff --git a/tests/Feature/Cases/CaseStartedSubProcessTest.php b/tests/Feature/Cases/CaseStartedSubProcessTest.php new file mode 100644 index 0000000000..1aee94a0a1 --- /dev/null +++ b/tests/Feature/Cases/CaseStartedSubProcessTest.php @@ -0,0 +1,400 @@ +user = User::factory()->create(); + $this->process = Process::factory()->create(); + $this->parentRequest = ProcessRequest::factory()->create([ + 'user_id' => $this->user->id, + 'status' => 'ACTIVE', + 'process_id' => $this->process->id, + ]); + $this->parentToken = ProcessRequestToken::factory()->create([ + 'user_id' => $this->user->id, + 'process_request_id' => $this->parentRequest->id, + 'element_type' => 'task', + ]); + $this->subProcess = Process::factory()->create(); + $this->childRequest = ProcessRequest::factory()->create([ + 'user_id' => $this->user->id, + 'status' => 'ACTIVE', + 'parent_request_id' => $this->parentRequest->id, + 'process_id' => $this->subProcess->id, + ]); + $this->childToken = ProcessRequestToken::factory()->create([ + 'user_id' => $this->user->id, + 'process_request_id' => $this->childRequest->id, + 'element_type' => 'task', + ]); + + $this->user2 = User::factory()->create(); + $this->childToken2 = ProcessRequestToken::factory()->create([ + 'user_id' => $this->user2->id, + 'process_request_id' => $this->childRequest->id, + 'element_type' => 'task', + ]); + + parent::setUp(); + } + + public function test_create_case_sub_process() + { + $repoParticipant = Mockery::mock(CaseParticipatedRepository::class); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->parentRequest); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->childRequest); + + $this->assertDatabaseCount('cases_started', 1); + $this->assertDatabaseHas('cases_started', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + ]); + } + + public function test_create_case_processes() + { + $repoParticipant = Mockery::mock(CaseParticipatedRepository::class); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->parentRequest); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->childRequest); + + $this->assertDatabaseCount('cases_started', 1); + $this->assertDatabaseHas('cases_started', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'processes->[0]->id' => $this->process->id, + 'processes->[0]->name' => $this->process->name, + 'processes->[1]->id' => $this->subProcess->id, + 'processes->[1]->name' => $this->subProcess->name, + ]); + } + + public function test_create_case_requests() + { + $repoParticipant = Mockery::mock(CaseParticipatedRepository::class); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->parentRequest); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->childRequest); + + $this->assertDatabaseCount('cases_started', 1); + $this->assertDatabaseHas('cases_started', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'requests->[0]->id' => $this->parentRequest->id, + 'requests->[0]->name' => $this->parentRequest->name, + 'requests->[0]->parent_request_id' => $this->parentRequest->parent_request_id, + 'requests->[1]->id' => $this->childRequest->id, + 'requests->[1]->name' => $this->childRequest->name, + 'requests->[1]->parent_request_id' => $this->childRequest->parent_request_id, + ]); + } + + public function test_create_case_request_tokens() + { + $repoParticipant = new CaseParticipatedRepository(); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->parentRequest); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->childRequest); + $repo->update($this->parentRequest, $this->parentToken); + $repo->update($this->childRequest, $this->childToken); + + $this->assertDatabaseCount('cases_started', 1); + $this->assertDatabaseHas('cases_started', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'request_tokens->[0]' => $this->parentToken->id, + 'request_tokens->[1]' => $this->childToken->id, + ]); + } + + public function test_create_case_tasks() + { + $repoParticipant = new CaseParticipatedRepository(); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->parentRequest); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->childRequest); + $repo->update($this->parentRequest, $this->parentToken); + $repo->update($this->childRequest, $this->childToken); + + $this->assertDatabaseCount('cases_started', 1); + $this->assertDatabaseHas('cases_started', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'tasks->[0]->id' => $this->parentToken->id, + 'tasks->[0]->element_id' => $this->parentToken->element_id, + 'tasks->[0]->name' => $this->parentToken->element_name, + 'tasks->[0]->process_id' => $this->parentToken->process_id, + 'tasks->[1]->id' => $this->childToken->id, + 'tasks->[1]->element_id' => $this->childToken->element_id, + 'tasks->[1]->name' => $this->childToken->element_name, + 'tasks->[1]->process_id' => $this->childToken->process_id, + ]); + } + + public function test_create_case_participated_processes() + { + $repoParticipant = new CaseParticipatedRepository(); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->parentRequest); + $repo->create($this->childRequest); + + $this->assertDatabaseCount('cases_participated', 0); + + $repo->update($this->parentRequest, $this->parentToken); + $repo->update($this->childRequest, $this->childToken); + $repo->update($this->childRequest, $this->childToken2); + + $this->assertDatabaseCount('cases_participated', 2); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'processes->[0]->id' => $this->process->id, + 'processes->[0]->name' => $this->process->name, + 'processes->[1]->id' => $this->subProcess->id, + 'processes->[1]->name' => $this->subProcess->name, + ]); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user2->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'processes->[0]->id' => $this->subProcess->id, + 'processes->[0]->name' => $this->subProcess->name, + ]); + } + + public function test_create_case_participated_requests() + { + $repoParticipant = new CaseParticipatedRepository(); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->parentRequest); + $repo->create($this->childRequest); + + $this->assertDatabaseCount('cases_participated', 0); + + $repo->update($this->parentRequest, $this->parentToken); + $repo->update($this->childRequest, $this->childToken); + $repo->update($this->childRequest, $this->childToken2); + + $this->assertDatabaseCount('cases_participated', 2); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'requests->[0]->id' => $this->parentRequest->id, + 'requests->[0]->name' => $this->parentRequest->name, + 'requests->[0]->parent_request_id' => $this->parentRequest->parent_request_id, + 'requests->[1]->id' => $this->childRequest->id, + 'requests->[1]->name' => $this->childRequest->name, + 'requests->[1]->parent_request_id' => $this->childRequest->parent_request_id, + ]); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user2->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'requests->[0]->id' => $this->childRequest->id, + 'requests->[0]->name' => $this->childRequest->name, + 'requests->[0]->parent_request_id' => $this->childRequest->parent_request_id, + ]); + } + + public function test_create_case_participated_request_tokens() + { + $repoParticipant = new CaseParticipatedRepository(); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->parentRequest); + $repo->create($this->childRequest); + + $this->assertDatabaseCount('cases_participated', 0); + + $repo->update($this->parentRequest, $this->parentToken); + $repo->update($this->childRequest, $this->childToken); + $repo->update($this->childRequest, $this->childToken2); + + $this->assertDatabaseCount('cases_participated', 2); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'request_tokens->[0]' => $this->parentToken->id, + 'request_tokens->[1]' => $this->childToken->id, + ]); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user2->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'request_tokens->[0]' => $this->childToken2->id, + ]); + } + + public function test_create_case_participated_tasks() + { + $repoParticipant = new CaseParticipatedRepository(); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->parentRequest); + $repo->create($this->childRequest); + + $this->assertDatabaseCount('cases_participated', 0); + + $repo->update($this->parentRequest, $this->parentToken); + $repo->update($this->childRequest, $this->childToken); + $repo->update($this->childRequest, $this->childToken2); + + $this->assertDatabaseCount('cases_participated', 2); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'tasks->[0]->id' => $this->parentToken->id, + 'tasks->[0]->element_id' => $this->parentToken->element_id, + 'tasks->[0]->name' => $this->parentToken->element_name, + 'tasks->[0]->process_id' => $this->parentToken->process_id, + 'tasks->[1]->id' => $this->childToken->id, + 'tasks->[1]->element_id' => $this->childToken->element_id, + 'tasks->[1]->name' => $this->childToken->element_name, + 'tasks->[1]->process_id' => $this->childToken->process_id, + ]); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user2->id, + 'case_title' => 'Case #' . $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'tasks->[0]->id' => $this->childToken2->id, + 'tasks->[0]->element_id' => $this->childToken2->element_id, + 'tasks->[0]->name' => $this->childToken2->element_name, + 'tasks->[0]->process_id' => $this->childToken2->process_id, + 'tasks->[1]->id' => null, + 'tasks->[1]->element_id' => null, + 'tasks->[1]->name' => null, + 'tasks->[1]->process_id' => null, + ]); + } + + public function test_update_case_participated_completed() + { + $repoParticipant = new CaseParticipatedRepository(); + + $repo = new CaseRepository($repoParticipant); + $repo->create($this->parentRequest); + $repo->create($this->childRequest); + + $repo->update($this->parentRequest, $this->parentToken); + $repo->update($this->childRequest, $this->childToken); + $repo->update($this->childRequest, $this->childToken2); + + $this->assertDatabaseCount('cases_started', 1); + $this->assertDatabaseCount('cases_participated', 2); + + $this->childRequest->status = 'COMPLETED'; + $repo->updateStatus($this->childRequest); + + $this->assertDatabaseHas('cases_started', [ + 'case_number' => $this->parentRequest->case_number, + 'case_status' => 'IN_PROGRESS', + 'completed_at' => null, + ]); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user->id, + 'case_status' => 'IN_PROGRESS', + 'completed_at' => null, + ]); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user2->id, + 'case_status' => 'IN_PROGRESS', + 'completed_at' => null, + ]); + + $this->parentRequest->status = 'COMPLETED'; + $repo->updateStatus($this->parentRequest); + + $this->assertDatabaseHas('cases_started', [ + 'case_number' => $this->parentRequest->case_number, + 'case_status' => 'COMPLETED', + 'completed_at' => now(), + ]); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user->id, + 'case_status' => 'COMPLETED', + 'completed_at' => now(), + ]); + $this->assertDatabaseHas('cases_participated', [ + 'case_number' => $this->parentRequest->case_number, + 'user_id' => $this->user2->id, + 'case_status' => 'COMPLETED', + 'completed_at' => now(), + ]); + } +} diff --git a/tests/Feature/Cases/CaseStartedTest.php b/tests/Feature/Cases/CaseStartedTest.php index 87a87067d0..ffbd3a12d6 100644 --- a/tests/Feature/Cases/CaseStartedTest.php +++ b/tests/Feature/Cases/CaseStartedTest.php @@ -146,6 +146,7 @@ public function test_update_case_started_request_tokens() $token = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token); @@ -185,6 +186,7 @@ public function test_update_case_started_tasks() $token = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token); @@ -202,6 +204,7 @@ public function test_update_case_started_tasks() $token2 = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token2); @@ -245,6 +248,7 @@ public function test_update_case_started_script_tasks() $token = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token); @@ -364,6 +368,7 @@ public function test_update_case_started_status() $token = ProcessRequestToken::factory()->create([ 'user_id' => $user->id, 'process_request_id' => $instance->id, + 'element_type' => 'task', ]); $repo->update($instance, $token); From 143664c28b423ea3ab2cfd9ca6dfe6bf3b39b020 Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Thu, 26 Sep 2024 15:38:42 -0400 Subject: [PATCH 4/5] test: fix CaseStartedSubProcessTest setup --- tests/Feature/Cases/CaseStartedSubProcessTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/Cases/CaseStartedSubProcessTest.php b/tests/Feature/Cases/CaseStartedSubProcessTest.php index 1aee94a0a1..db4204931c 100644 --- a/tests/Feature/Cases/CaseStartedSubProcessTest.php +++ b/tests/Feature/Cases/CaseStartedSubProcessTest.php @@ -36,6 +36,8 @@ class CaseStartedSubProcessTest extends TestCase protected function setUp(): void { + parent::setUp(); + $this->user = User::factory()->create(); $this->process = Process::factory()->create(); $this->parentRequest = ProcessRequest::factory()->create([ @@ -67,8 +69,6 @@ protected function setUp(): void 'process_request_id' => $this->childRequest->id, 'element_type' => 'task', ]); - - parent::setUp(); } public function test_create_case_sub_process() From fd3e2022fe8c7a7eb689d71390f6cf393c1df5f9 Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Mon, 30 Sep 2024 11:55:26 -0400 Subject: [PATCH 5/5] fix: add cr suggestions --- .../CaseParticipatedRepository.php | 14 ++++++++++- ProcessMaker/Repositories/CaseRepository.php | 11 ++++++--- tests/Feature/Cases/CaseStartedTest.php | 23 +++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/ProcessMaker/Repositories/CaseParticipatedRepository.php b/ProcessMaker/Repositories/CaseParticipatedRepository.php index 7f7085c8ef..1a22abebe2 100644 --- a/ProcessMaker/Repositories/CaseParticipatedRepository.php +++ b/ProcessMaker/Repositories/CaseParticipatedRepository.php @@ -9,6 +9,8 @@ class CaseParticipatedRepository { /** + * This property is used to store an instance of `CaseParticipated` + * when a case participated is updated. * @var CaseParticipated|null */ protected ?CaseParticipated $caseParticipated; @@ -27,7 +29,7 @@ public function create(CaseStarted $case, TokenInterface $token): void } try { - $this->caseParticipated = CaseParticipated::create([ + CaseParticipated::create([ 'user_id' => $token->user->id, 'case_number' => $case->case_number, 'case_title' => $case->case_title, @@ -92,6 +94,16 @@ public function updateStatus(int $caseNumber, array $statusData) } } + /** + * Check if a case participated exists. + * If it exists, store the instance in the property. + * The property is used to update the JSON fields of the case participated. + * + * @param int $userId + * @param int $caseNumber + * + * @return bool + */ private function checkIfCaseParticipatedExist(int $userId, int $caseNumber): bool { $this->caseParticipated = CaseParticipated::where('user_id', $userId) diff --git a/ProcessMaker/Repositories/CaseRepository.php b/ProcessMaker/Repositories/CaseRepository.php index 27ed0ff6a5..7ef6ca49ec 100644 --- a/ProcessMaker/Repositories/CaseRepository.php +++ b/ProcessMaker/Repositories/CaseRepository.php @@ -10,7 +10,11 @@ class CaseRepository implements CaseRepositoryInterface { + const CASE_STATUS_ACTIVE = 'ACTIVE'; + /** + * This property is used to store an instance of `CaseStarted` + * when a case started is updated. * @var CaseStarted|null */ protected ?CaseStarted $case; @@ -39,12 +43,12 @@ public function create(ExecutionInstanceInterface $instance): void } try { - $this->case = CaseStarted::create([ + CaseStarted::create([ '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, + 'case_status' => $instance->status === self::CASE_STATUS_ACTIVE ? 'IN_PROGRESS' : $instance->status, 'processes' => CaseUtils::storeProcesses($instance, collect()), 'requests' => CaseUtils::storeRequests($instance, collect()), 'request_tokens' => [], @@ -82,7 +86,7 @@ public function update(ExecutionInstanceInterface $instance, TokenInterface $tok } $this->case->case_title = $instance->case_title; - $this->case->case_status = $instance->status === 'ACTIVE' ? 'IN_PROGRESS' : $instance->status; + $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); $this->case->tasks = CaseUtils::storeTasks($token, $this->case->tasks); @@ -102,6 +106,7 @@ public function update(ExecutionInstanceInterface $instance, TokenInterface $tok */ public function updateStatus(ExecutionInstanceInterface $instance): void { + // If a sub-process is completed, do not update the case started status if (!is_null($instance->parent_request_id)) { return; } diff --git a/tests/Feature/Cases/CaseStartedTest.php b/tests/Feature/Cases/CaseStartedTest.php index ffbd3a12d6..2b597a5bee 100644 --- a/tests/Feature/Cases/CaseStartedTest.php +++ b/tests/Feature/Cases/CaseStartedTest.php @@ -395,4 +395,27 @@ public function test_update_case_started_status() 'tasks->[0]->process_id' => $token->process_id, ]); } + + 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->create($instance); + + $this->assertDatabaseCount('cases_started', 0); + + $token = ProcessRequestToken::factory()->create([ + 'user_id' => $user->id, + 'process_request_id' => $instance->id, + 'element_type' => 'task', + ]); + + $repo->update($instance, $token); + $this->assertDatabaseCount('cases_started', 0); + } }