diff --git a/ProcessMaker/Repositories/CaseParticipatedRepository.php b/ProcessMaker/Repositories/CaseParticipatedRepository.php index 025d3fe7d8..1a22abebe2 100644 --- a/ProcessMaker/Repositories/CaseParticipatedRepository.php +++ b/ProcessMaker/Repositories/CaseParticipatedRepository.php @@ -8,6 +8,13 @@ class CaseParticipatedRepository { + /** + * This property is used to store an instance of `CaseParticipated` + * when a case participated is updated. + * @var CaseParticipated|null + */ + protected ?CaseParticipated $caseParticipated; + /** * Store a new case participated. * @@ -17,6 +24,10 @@ class CaseParticipatedRepository */ public function create(CaseStarted $case, TokenInterface $token): void { + if ($this->checkIfCaseParticipatedExist($token->user->id, $case->case_number)) { + return; + } + try { CaseParticipated::create([ 'user_id' => $token->user->id, @@ -24,17 +35,10 @@ public function create(CaseStarted $case, TokenInterface $token): void 'case_title' => $case->case_title, 'case_title_formatted' => $case->case_title_formatted, 'case_status' => $case->case_status, - 'processes' => $case->processes, - 'requests' => $case->requests, - 'request_tokens' => [$token->getKey()], - 'tasks' => [ - [ - 'id' => $token->getKey(), - 'element_id' => $token->element_id, - 'name' => $token->element_name, - 'process_id' => $token->process_id, - ], - ], + '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, @@ -54,32 +58,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(); - - // 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(); + if (!$this->checkIfCaseParticipatedExist($token->user->id, $case->case_number)) { + return; + } - $caseParticipated->update([ + $this->caseParticipated->updateOrFail([ 'case_title' => $case->case_title, 'case_title_formatted' => $case->case_title_formatted, 'case_status' => $case->case_status, - 'processes' => $case->processes, - 'requests' => $case->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) { @@ -103,4 +93,23 @@ public function updateStatus(int $caseNumber, array $statusData) \Log::error($e->getMessage()); } } + + /** + * 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) + ->where('case_number', $caseNumber) + ->first(); + + return !is_null($this->caseParticipated); + } } diff --git a/ProcessMaker/Repositories/CaseRepository.php b/ProcessMaker/Repositories/CaseRepository.php index e77f14665b..7ef6ca49ec 100644 --- a/ProcessMaker/Repositories/CaseRepository.php +++ b/ProcessMaker/Repositories/CaseRepository.php @@ -10,6 +10,15 @@ 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; + public function __construct(protected CaseParticipatedRepository $caseParticipatedRepository) { } @@ -21,34 +30,27 @@ 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()); + return; } - try { - $processes = [ - [ - 'id' => $instance->process->id, - 'name' => $instance->process->name, - ], - ]; + if ($this->checkIfCaseStartedExist($instance->case_number)) { + $this->updateSubProcesses($instance); - $requests = [ - [ - 'id' => $instance->id, - 'name' => $instance->name, - 'parent_request_id' => $instance->parentRequest->id ?? 0, - ], - ]; + return; + } + try { 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 === self::CASE_STATUS_ACTIVE ? 'IN_PROGRESS' : $instance->status, + 'processes' => CaseUtils::storeProcesses($instance, collect()), + 'requests' => CaseUtils::storeRequests($instance, collect()), 'request_tokens' => [], 'tasks' => [], 'participants' => [], @@ -57,6 +59,7 @@ public function create(ExecutionInstanceInterface $instance): void ]); } catch (\Exception $e) { \Log::error($e->getMessage()); + \Log::error($e->getTraceAsString()); } } @@ -69,34 +72,27 @@ 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 { - $case = CaseStarted::where('case_number', $instance->case_number)->first(); + if (!$this->checkIfCaseStartedExist($instance->case_number)) { + \Log::error('Case number not found. instance: ' . $instance->id); - if (is_null($case)) { return; } - $case->case_title = $instance->case_title; - $case->case_status = $instance->status === 'ACTIVE' ? 'IN_PROGRESS' : $instance->status; - - $case->request_tokens = $case->request_tokens->push($token->getKey()) - ->unique() - ->values(); - - if (!in_array($token->element_type, ['scriptTask'])) { - $case->tasks = $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->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); + $this->case->tasks = CaseUtils::storeTasks($token, $this->case->tasks); - $this->updateParticipants($case, $token); + $this->updateParticipants($token); - $case->saveOrFail(); + $this->case->saveOrFail(); } catch (\Exception $e) { \Log::error($e->getMessage()); } @@ -110,6 +106,11 @@ 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; + } + try { $data = [ 'case_status' => $instance->status, @@ -130,11 +131,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 +142,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 +168,31 @@ 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 = CaseUtils::storeProcesses($instance, $this->case->processes); + $this->case->requests = CaseUtils::storeRequests($instance, $this->case->requests); + + $this->case->saveOrFail(); + } catch (\Exception $th) { + \Log::error($th->getMessage()); + } } } 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; + } +} 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..db4204931c --- /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', + ]); + } + + 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..2b597a5bee 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); @@ -390,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); + } }