Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 249
FOUR-18601 populate cases_started table - sub processes#7421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
2fbba85eeec1e63c93fd4143664cfd3e202File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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,24 +24,21 @@ class CaseParticipatedRepository | ||
| */ | ||
| public function create(CaseStarted $case, TokenInterface $token): void | ||
| { | ||
| if ($this->checkIfCaseParticipatedExist($token->user->id, $case->case_number)) { | ||
caleeli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return; | ||
| } | ||
| try { | ||
| 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, | ||
| '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)) { | ||
caleeli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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(); | ||
devmiguelangel marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return !is_null($this->caseParticipated); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
devmiguelangel marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| */ | ||
| 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()); | ||
devmiguelangel marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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()); | ||
devmiguelangel marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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); | ||
devmiguelangel marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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)) { | ||
devmiguelangel marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return; | ||
| } | ||
| try { | ||
| $data = [ | ||
| 'case_status' => $instance->status, | ||
| @@ -130,34 +131,33 @@ 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; | ||
| if (!$user) { | ||
| 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()); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.