Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ProcessMaker/Http/Controllers/Api/V1_1/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\ProcessTranslations\TranslationManager;
use ProcessMaker\Services\TaskCompletionRawService;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class TaskController extends Controller
{
public function __construct(
private readonly TaskCompletionRawService $taskCompletionRawService,
) {
}

protected $defaultFields = [
'id',
'element_id',
Expand Down Expand Up @@ -151,4 +158,26 @@ public function showInterstitial($taskId)

return $response;
}

/**
* Complete a task using the raw-query optimized path.
*/
public function update(Request $request, int $taskId)
{
if ($request->input('status') !== 'COMPLETED') {
abort(422, __('Only task completion is supported on this endpoint. Use PUT /api/1.0/tasks/{id} for other updates.'));
}

try {
$task = $this->taskCompletionRawService->completeTask(
$taskId,
json_optimize_decode($request->getContent(), true) ?: [],
$request->user(),
);
} catch (NotFoundHttpException $exception) {
return response()->json(['message' => $exception->getMessage()], 404);
}

return response()->json($task);
}
}
2 changes: 1 addition & 1 deletion ProcessMaker/Jobs/BpmnAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function handle()
return $response;
}

public function transferInternalContext(BpmnAction $action): void
public function transferInternalContext(self $action): void
{
$action->engine = $this->engine;
$action->instance = $this->instance;
Expand Down
13 changes: 12 additions & 1 deletion ProcessMaker/Jobs/GenerateUserRecommendations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ProcessMaker\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
Expand All @@ -12,17 +13,27 @@
use ProcessMaker\Models\User;
use ProcessMaker\RecommendationEngine;

class GenerateUserRecommendations implements ShouldQueue
class GenerateUserRecommendations implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Seconds the unique lock is held so duplicate dispatches for the same user are dropped.
*/
public int $uniqueFor = 60;

/**
* Create a new job instance.
*/
public function __construct(public int $user_id)
{
}

public function uniqueId(): string
{
return (string) $this->user_id;
}

public function middleware(): array
{
return [(new WithoutOverlapping($this->user_id))->dontRelease()];
Expand Down
22 changes: 22 additions & 0 deletions ProcessMaker/Models/ProcessRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@ public function shouldBeSearchable()
}
}

/**
* Determine if the model existed in the search index prior to an update.
* Prevents unnecessary RemoveFromSearch jobs when indexed search is disabled.
*
* @return bool
*/
public function wasSearchableBeforeUpdate()
{
return $this->shouldBeSearchable();
}

/**
* Determine if the model existed in the search index prior to deletion.
* Prevents unnecessary RemoveFromSearch jobs when indexed search is disabled.
*
* @return bool
*/
public function wasSearchableBeforeDelete()
{
return $this->shouldBeSearchable();
}

/**
* Get the indexable data array for the model.
*
Expand Down
22 changes: 22 additions & 0 deletions ProcessMaker/Models/ProcessRequestToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,28 @@ public function shouldBeSearchable()
}
}

/**
* Determine if the model existed in the search index prior to an update.
* Prevents unnecessary RemoveFromSearch jobs when indexed search is disabled.
*
* @return bool
*/
public function wasSearchableBeforeUpdate()
{
return $this->shouldBeSearchable();
}

/**
* Determine if the model existed in the search index prior to deletion.
* Prevents unnecessary RemoveFromSearch jobs when indexed search is disabled.
*
* @return bool
*/
public function wasSearchableBeforeDelete()
{
return $this->shouldBeSearchable();
}

/**
* Boot application as a process instance.
*
Expand Down
2 changes: 2 additions & 0 deletions ProcessMaker/Nayra/Managers/WorkflowManagerDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ public function runScripTask(ScriptTaskInterface $scriptTask, Token $token)

if ($this->canRunInlineTask($token, $scriptTask)) {
$this->runInlineTask($token, RunScriptTask::class);

return;
}

Expand All @@ -285,6 +286,7 @@ public function runServiceTask(ServiceTaskInterface $serviceTask, Token $token)

if ($this->canRunInlineTask($token, $serviceTask)) {
$this->runInlineTask($token, RunServiceTask::class);

return;
}

Expand Down
11 changes: 11 additions & 0 deletions ProcessMaker/Repositories/ExecutionInstanceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ProcessMaker\Nayra\Contracts\Repositories\ExecutionInstanceRepositoryInterface;
use ProcessMaker\Nayra\Contracts\Repositories\StorageInterface;
use ProcessMaker\Nayra\RepositoryTrait;
use ProcessMaker\Repositories\TokenPersistenceRawRepository;
use ProcessMaker\SanitizeHelper;

/**
Expand Down Expand Up @@ -236,6 +237,16 @@ public function persistInstanceUpdated(ExecutionInstanceInterface $instance)
return;
}

if (
config('app.token_persistence_raw_enabled', false)
&& $instance instanceof ProcessRequest
) {
app(TokenPersistenceRawRepository::class)->persistInstanceUpdated($instance);
CaseUpdateStatus::dispatchSync($instance);

return;
}

// Save updated instance
if (!$instance->status) {
$instance->status = 'ACTIVE';
Expand Down
160 changes: 160 additions & 0 deletions ProcessMaker/Repositories/TaskCompletionRawRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

declare(strict_types=1);

namespace ProcessMaker\Repositories;

use Illuminate\Support\Facades\DB;
use stdClass;

class TaskCompletionRawRepository
{
public function __construct(
private readonly ProcessExecutionRawRepository $executionRawRepository,
) {
}

public function findTaskForUpdate(int $taskId): ?stdClass
{
$row = DB::selectOne(
'SELECT id, status, user_id, process_id, process_request_id, element_id, element_type,
element_name, is_self_service, self_service_groups
FROM process_request_tokens
WHERE id = ? AND element_type = ?
LIMIT 1',
[$taskId, 'task']
);

if ($row === null) {
return null;
}

$row->is_self_service = (bool) $row->is_self_service;
$row->self_service_groups = $this->decodeJson($row->self_service_groups);

return $row;
}

public function findProcessForComplete(int $processId): ?stdClass
{
$row = DB::selectOne(
'SELECT id, bpmn, start_events, properties, status, name, process_category_id
FROM processes
WHERE id = ? AND deleted_at IS NULL
LIMIT 1',
[$processId]
);

if ($row === null) {
return null;
}

$row->properties = $this->decodeJson($row->properties) ?? [];
$row->manager_id = $this->decodeManagerIds($row->properties['manager_id'] ?? null);
$row->start_events = $this->decodeJson($row->start_events);

return $row;
}

public function findProcessRequestForComplete(int $processRequestId): ?stdClass
{
$row = DB::selectOne(
'SELECT id, process_id, process_version_id, status, do_not_sanitize, user_id,
parent_request_id, process_collaboration_id
FROM process_requests
WHERE id = ?
LIMIT 1',
[$processRequestId]
);

if ($row === null) {
return null;
}

$row->do_not_sanitize = $this->decodeJson($row->do_not_sanitize) ?? [];

return $row;
}

public function findProcessVersionForComplete(?int $processVersionId): ?stdClass
{
if ($processVersionId === null) {
return null;
}

$row = DB::selectOne(
'SELECT id, process_id, bpmn, start_events, status, name, alternative
FROM process_versions
WHERE id = ?
LIMIT 1',
[$processVersionId]
);

if ($row === null) {
return null;
}

$row->start_events = $this->decodeJson($row->start_events);

return $row;
}

public function taskHasDraft(int $taskId): bool
{
return $this->executionRawRepository->taskHasDraftRaw($taskId);
}

public function findTaskForResponse(int $taskId): ?stdClass
{
return DB::selectOne(
'SELECT id, element_name, element_id, element_type, status, due_at, process_request_id,
user_id, process_id, is_self_service, self_service_groups, token_properties,
created_at, updated_at, completed_at
FROM process_request_tokens
WHERE id = ?
LIMIT 1',
[$taskId]
);
}

/**
* @return list<int>
*/
private function decodeManagerIds(mixed $value): array
{
if ($value === null || $value === '') {
return [];
}

if (is_array($value)) {
return array_map('intval', $value);
}

if (is_numeric($value)) {
return [(int) $value];
}

$decoded = $this->decodeJson($value);

if (is_array($decoded)) {
return array_map('intval', $decoded);
}

return [];
}

private function decodeJson(mixed $value): mixed
{
if ($value === null || $value === '') {
return null;
}

if (is_array($value)) {
return $value;
}

$decoded = json_decode((string) $value, true);

return json_last_error() === JSON_ERROR_NONE ? $decoded : null;
}
}
Loading
Loading