Repository navigation
FOUR-18602 populate cases_started table DB Exception when populating the cases_started table #7445
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eaf113b
feat: add cases sync artisan command
devmiguelangel e6a3edc
feat: add custom exception logs
devmiguelangel e92b52a
feat: add case sync repo
devmiguelangel 6df43f6
case repository refactor
devmiguelangel c9572a6
test: case exception tests
devmiguelangel 7055d40
fix: case exception handling and logging
devmiguelangel d7ea383
fix: use status active constant
devmiguelangel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| namespace ProcessMaker\Console\Commands; | ||
|
|
||
| use Illuminate\Console\Command; | ||
| use ProcessMaker\Repositories\CaseSyncRepository; | ||
|
|
||
| class CasesSync extends Command | ||
| { | ||
| /** | ||
| * The name and signature of the console command. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $signature = 'cases:sync | ||
| {--request_ids= : Comma-separated list of request IDs to sync}'; | ||
|
|
||
| /** | ||
| * The console command description. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $description = 'Sync requests with the cases tables'; | ||
|
|
||
| /** | ||
| * Execute the console command. | ||
| */ | ||
| public function handle() | ||
| { | ||
| $requestIds = $this->option('request_ids'); | ||
| $requestIds = $requestIds ? explode(',', $requestIds) : []; | ||
|
|
||
| if (count($requestIds) > 0) { | ||
| $data = CaseSyncRepository::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.'); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| <?php | ||
|
|
||
| namespace ProcessMaker\Repositories; | ||
|
|
||
| use Illuminate\Support\Collection; | ||
| use ProcessMaker\Models\CaseParticipated; | ||
| use ProcessMaker\Models\CaseStarted; | ||
| use ProcessMaker\Models\ProcessRequest; | ||
|
|
||
| class CaseSyncRepository | ||
| { | ||
| /** | ||
| * Sync the cases started. | ||
| * | ||
| * @param array $requestIds | ||
| * @return array | ||
| */ | ||
| public static function syncCases(array $requestIds): array | ||
| { | ||
| $requests = ProcessRequest::with([ | ||
| 'tokens', 'process', 'childRequests.process', 'childRequests.tokens', 'participants', | ||
| ]) | ||
| ->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, | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.