Skip to content

Commit

Permalink
Merge branch 'release-2024-fall' of github.com:ProcessMaker/processma…
Browse files Browse the repository at this point in the history
…ker into bugfix/FOUR-19987
  • Loading branch information
CarliPinell committed Nov 6, 2024
2 parents ce30521 + 80d800d commit 05b136a
Show file tree
Hide file tree
Showing 68 changed files with 2,301 additions and 484 deletions.
45 changes: 33 additions & 12 deletions ProcessMaker/Console/Commands/UpdateCommentsCaseNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use ProcessMaker\Models\Comment;

class UpdateCommentsCaseNumber extends Command
{
const CHUNK_SIZE = 5000;
const CHUNK_SIZE = 2000;

/**
* The name and signature of the console command.
Expand Down Expand Up @@ -42,11 +41,22 @@ public function handle()
->select('comments.id', 'process_requests.case_number')
->orderBy('comments.id', 'asc')
->chunk($chunkSize, function ($comments) {
foreach ($comments as $comment) {
// Update the comments.case_number with ptrocess_requests.case_number
DB::table('comments')
->where('id', $comment->id)
->update(['case_number' => $comment->case_number]);
$updates = $comments->mapWithKeys(function ($comment) {
if (!is_null($comment->case_number) && !empty($comment->case_number)) {
return [$comment->id => ['case_number' => $comment->case_number]];
}

return [];
})->toArray();
// Execute in bath the update
if (!empty($updates)) {
$query = 'UPDATE comments SET case_number = CASE id';
foreach ($updates as $id => $data) {
$query .= " WHEN {$id} THEN '{$data['case_number']}'";
}
$query .= ' END WHERE id IN (' . implode(',', array_keys($updates)) . ')';
DB::statement($query);
$this->info(count($updates) . ' comments updated in this chunk related to ProcessRequestToken');
}
});
// Update the comments related to ProcessRequest
Expand All @@ -57,11 +67,22 @@ public function handle()
->select('comments.id', 'process_requests.case_number')
->orderBy('comments.id', 'asc')
->chunk($chunkSize, function ($comments) {
foreach ($comments as $comment) {
// Update the comments.case_number with ptrocess_requests.case_number
DB::table('comments')
->where('id', $comment->id)
->update(['case_number' => $comment->case_number]);
$updates = $comments->mapWithKeys(function ($comment) {
if (!is_null($comment->case_number) && !empty($comment->case_number)) {
return [$comment->id => ['case_number' => $comment->case_number]];
}

return [];
})->toArray();
// Execute in bath the update
if (!empty($updates)) {
$query = 'UPDATE comments SET case_number = CASE id';
foreach ($updates as $id => $data) {
$query .= " WHEN {$id} THEN '{$data['case_number']}'";
}
$query .= ' END WHERE id IN (' . implode(',', array_keys($updates)) . ')';
DB::statement($query);
$this->info(count($updates) . ' comments updated in this chunk related to ProcessRequest');
}
});

Expand Down
13 changes: 13 additions & 0 deletions ProcessMaker/Http/Controllers/Api/DevLinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Http\Resources\ApiCollection;
use ProcessMaker\Jobs\DevLinkInstall;
use ProcessMaker\Exception\ValidationException;
use ProcessMaker\Models\Bundle;
use ProcessMaker\Models\BundleAsset;
use ProcessMaker\Models\DevLink;
Expand Down Expand Up @@ -211,6 +212,18 @@ public function addAsset(Request $request, Bundle $bundle)
$asset = $request->input('type')::findOrFail($request->input('id'));
$bundle->addAsset($asset);
}

public function addAssetToBundles(Request $request)
{
$bundles = $request->input('bundles');
foreach ($bundles as $id) {
$bundle = Bundle::find($id);
if ($bundle) {
$asset = $request->input('type')::findOrFail($request->input('id'));
$bundle->addAssetToBundles($asset);
}
}
}

public function sharedAssets(Request $request)
{
Expand Down
4 changes: 4 additions & 0 deletions ProcessMaker/Http/Controllers/Api/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public function index(Request $request, $getTotal = false, User $user = null)
// Apply filter overdue
$query->overdue($request->input('overdue'));

if ($request->input('processesIManage') === 'true') {
$this->applyProcessManager($query, $user);
}

// If only the total is being requested (by a Saved Search), send it now
if ($getTotal === true) {
return $query->count();
Expand Down
2 changes: 1 addition & 1 deletion ProcessMaker/Http/Resources/V1_1/CaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function toArray($request): array
*/
private function getParticipantData(array $participants): array
{
return array_map(fn($participant) => self::$users->get($participant), $participants);
return array_map(fn ($participant) => self::$users->get($participant), $participants);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions ProcessMaker/Models/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ public function addAsset(ProcessMakerModel $asset)
'asset_id' => $asset->id,
]);
}

public function addAssetToBundles(ProcessMakerModel $asset)
{
$message = null;
try {
$this->addAsset($asset);
} catch (ValidationException $ve) {
$message = $ve->getMessage();
}
return $message;
}

public function validateEditable()
{
Expand Down
1 change: 0 additions & 1 deletion ProcessMaker/ProcessTranslations/ScreenTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function applyTranslations($screen, $defaultLanguage = '')

$language = $this->getTargetLanguage($defaultLanguage);


return $this->searchTranslations($screen['screen_id'], $config, $language);
}

Expand Down
122 changes: 53 additions & 69 deletions ProcessMaker/Repositories/CaseParticipatedRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace ProcessMaker\Repositories;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use ProcessMaker\Models\CaseParticipated;
use ProcessMaker\Models\CaseStarted;
use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface;

class CaseParticipatedRepository
{
Expand All @@ -20,74 +20,72 @@ class CaseParticipatedRepository
* Store a new case participated.
*
* @param CaseStarted $case
* @param TokenInterface $token
* @param int $userId
* @return void
*/
public function create(CaseStarted $case, TokenInterface $token): void
public function create(CaseStarted $case, int $userId): void
{
if ($this->checkIfCaseParticipatedExist($token->user->id, $case->case_number)) {
return;
}

try {
$processData = CaseUtils::extractData($token->processRequest->process, 'PROCESS');
$requestData = CaseUtils::extractData($token->processRequest, 'REQUEST');
$taskData = CaseUtils::extractData($token, 'TASK');

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' => CaseUtils::storeProcesses(collect(), $processData),
'requests' => CaseUtils::storeRequests(collect(), $requestData),
'request_tokens' => CaseUtils::storeRequestTokens(collect(), $token->getKey()),
'tasks' => CaseUtils::storeTasks(collect(), $taskData),
'participants' => $case->participants,
'initiated_at' => $case->initiated_at,
'completed_at' => null,
'keywords' => $case->keywords,
]);
CaseParticipated::updateOrCreate(
[
'case_number' => $case->case_number,
'user_id' => $userId,
],
$this->mapCaseToArray($case, $userId)
);
} catch (\Exception $e) {
Log::error('CaseException: ' . $e->getMessage());
Log::error('CaseException: ' . $e->getTraceAsString());
$this->logException($e);
}
}

/**
* Update the case participated.
* Update the cases participated.
*
* @param CaseStarted $case
* @param TokenInterface $token
* @return void
*/
public function update(CaseStarted $case, TokenInterface $token)
public function update(CaseStarted $case): void
{
try {
if (!$this->checkIfCaseParticipatedExist($token->user->id, $case->case_number)) {
return;
}
CaseParticipated::where('case_number', $case->case_number)
->update($this->mapCaseToArray($case));
} catch (\Exception $e) {
$this->logException($e);
}
}

$processData = CaseUtils::extractData($token->processRequest->process, 'PROCESS');
$requestData = CaseUtils::extractData($token->processRequest, 'REQUEST');
$taskData = CaseUtils::extractData($token, 'TASK');
/**
* Maps properties of a `CaseStarted` object to an array, optionally including a user ID.
*
* @param CaseStarted case Takes a `CaseStarted` object and parameter as input and returns an array with specific
* properties mapped from the `CaseStarted` object.
* @param int userId Takes an optional `userId` parameter.
*
* @return array An array containing various properties of a CaseStarted object. If a userId is
* provided, it will also include the user_id in the returned array.
*/
private function mapCaseToArray(CaseStarted $case, int $userId = null): array
{
$data = [
'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' => $case->request_tokens,
'tasks' => $case->tasks,
'participants' => $case->participants,
'initiated_at' => $case->initiated_at,
'completed_at' => $case->completed_at,
'keywords' => $case->keywords,
];

$this->caseParticipated->updateOrFail([
'case_title' => $case->case_title,
'case_title_formatted' => $case->case_title_formatted,
'case_status' => $case->case_status,
'processes' => CaseUtils::storeProcesses($this->caseParticipated->processes, $processData),
'requests' => CaseUtils::storeRequests($this->caseParticipated->requests, $requestData),
'request_tokens' => CaseUtils::storeRequestTokens($this->caseParticipated->request_tokens, $token->getKey()),
'tasks' => CaseUtils::storeTasks($this->caseParticipated->tasks, $taskData),
'participants' => $case->participants,
'keywords' => $case->keywords,
]);
} catch (\Exception $e) {
Log::error('CaseException: ' . $e->getMessage());
Log::error('CaseException: ' . $e->getTraceAsString());
if ($userId !== null) {
$data['user_id'] = $userId;
}

return $data;
}

/**
Expand All @@ -103,27 +101,13 @@ public function updateStatus(int $caseNumber, array $statusData)
CaseParticipated::where('case_number', $caseNumber)
->update($statusData);
} catch (\Exception $e) {
Log::error('CaseException: ' . $e->getMessage());
Log::error('CaseException: ' . $e->getTraceAsString());
$this->logException($e);
}
}

/**
* 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
private function logException(\Exception $e): void
{
$this->caseParticipated = CaseParticipated::where('user_id', $userId)
->where('case_number', $caseNumber)
->first();

return !is_null($this->caseParticipated);
Log::error('CaseException: ' . $e->getMessage());
Log::error('CaseException: ' . $e->getTraceAsString());
}
}
18 changes: 7 additions & 11 deletions ProcessMaker/Repositories/CaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function updateStatus(ExecutionInstanceInterface $instance): void
];

if ($caseStatus === CaseStatusConstants::COMPLETED) {
$data['completed_at'] = Carbon::now();
$data['completed_at'] = $instance->completed_at;
}

// Update the case started and case participated
Expand All @@ -153,19 +153,13 @@ private function updateParticipants(TokenInterface $token): void
{
$user = $token->user;

if (!$user) {
return;
}

$participantExists = $this->case->participants->contains($user->id);
$this->case->participants = CaseUtils::storeParticipants($this->case->participants, $user?->id);

if (!$participantExists) {
$this->case->participants->push($user->id);

$this->caseParticipatedRepository->create($this->case, $token);
if (!is_null($user?->id)) {
$this->caseParticipatedRepository->create($this->case, $user->id);
}

$this->caseParticipatedRepository->update($this->case, $token);
$this->caseParticipatedRepository->update($this->case);
}

/**
Expand Down Expand Up @@ -206,6 +200,8 @@ private function updateSubProcesses(ExecutionInstanceInterface $instance): void
$this->case->requests = CaseUtils::storeRequests($this->case->requests, $requestData);

$this->case->saveOrFail();

$this->caseParticipatedRepository->update($this->case);
} catch (\Exception $e) {
Log::error('CaseException: ' . $e->getMessage());
Log::error('CaseException: ' . $e->getTraceAsString());
Expand Down
Loading

0 comments on commit 05b136a

Please sign in to comment.