From afd4d8294b019979e441a75cf94ebe817e8a969c Mon Sep 17 00:00:00 2001 From: "Marco A. Nina Mena" Date: Thu, 6 Nov 2025 18:21:34 -0400 Subject: [PATCH] Apply process manager filter before PMQL to prevent conflicts, and ensure self-service tasks are added after PMQL to override potential filtering issues. --- .../Http/Controllers/Api/TaskController.php | 9 ++--- .../Traits/TaskControllerIndexMethods.php | 33 +++++++++++++++++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Api/TaskController.php b/ProcessMaker/Http/Controllers/Api/TaskController.php index ee80c79975..5c2613e293 100644 --- a/ProcessMaker/Http/Controllers/Api/TaskController.php +++ b/ProcessMaker/Http/Controllers/Api/TaskController.php @@ -142,16 +142,17 @@ public function index(Request $request, $getTotal = false, User $user = null) $this->applyStatusFilter($query, $request); - $this->applyPmql($query, $request, $user); - - $this->applyAdvancedFilter($query, $request); - + // Apply process manager filter BEFORE PMQL to avoid conflicts with is_self_service filtering if ($request->input('processesIManage') === 'true') { $this->applyProcessManager($query, $user); } else { $this->applyForCurrentUser($query, $user); } + $this->applyPmql($query, $request, $user); + + $this->applyAdvancedFilter($query, $request); + // Apply filter overdue $query->overdue($request->input('overdue')); diff --git a/ProcessMaker/Traits/TaskControllerIndexMethods.php b/ProcessMaker/Traits/TaskControllerIndexMethods.php index 146e5e4c1e..a3e1c6c678 100644 --- a/ProcessMaker/Traits/TaskControllerIndexMethods.php +++ b/ProcessMaker/Traits/TaskControllerIndexMethods.php @@ -289,6 +289,20 @@ private function applyPmql($query, $request, $user) } catch (SyntaxError $e) { abort('Your PMQL contains invalid syntax.', 400); } + + // After PMQL is applied, if processesIManage is active, add self-service tasks + // This is done after PMQL to avoid the is_self_service = 0 filter that PMQL might add + if ($request->input('processesIManage') === 'true' && isset($query->getQuery()->processManagerIds)) { + $ids = $query->getQuery()->processManagerIds; + $selfServiceTaskIds = ProcessRequestToken::select(['id']) + ->whereIn('process_id', $ids) + ->where('is_self_service', 1) + ->whereNull('user_id') + ->where('status', 'ACTIVE'); + + // Add self-service tasks using orWhereIn to override PMQL's is_self_service = 0 filter + $query->orWhereIn('process_request_tokens.id', $selfServiceTaskIds); + } } } @@ -340,11 +354,26 @@ public function applyProcessManager($query, $user) ->orWhereRaw("JSON_CONTAINS(JSON_EXTRACT(properties, '$.manager_id'), CAST(? AS JSON))", [$user->id]); }) ->where('status', 'ACTIVE') - ->get() + ->pluck('id') ->toArray(); + if (empty($ids)) { + // If user is not a manager of any process, return no results + $query->whereRaw('1 = 0'); + + return; + } + + // Show tasks from processes the user manages that are ACTIVE + // OR show self-service tasks from those processes + // Store the process IDs in the query so we can use them later to add self-service tasks + // We'll add self-service tasks after PMQL is applied to avoid the is_self_service = 0 filter + $query->getQuery()->processManagerIds = $ids; + + // Apply condition for regular tasks from managed processes + // Self-service tasks will be added after PMQL to avoid conflicts $query->where(function ($query) use ($ids) { - $query->whereIn('process_request_tokens.process_id', array_column($ids, 'id')) + $query->whereIn('process_request_tokens.process_id', $ids) ->where('process_request_tokens.status', 'ACTIVE'); }); }