From c4e33f288a8608f990a82108eb2d7e7ff7bf92b6 Mon Sep 17 00:00:00 2001 From: CarliPinell Date: Wed, 11 Nov 2026 12:04:03 -0400 Subject: [PATCH 1/2] Observation resolved --- .../Api/ProcessRequestController.php | 64 ++++++--- tests/Feature/Api/ProcessRequestsTest.php | 126 ++++++++++++++++++ 2 files changed, 171 insertions(+), 19 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php b/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php index 4cabf9cb34..322ebadea4 100644 --- a/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php +++ b/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php @@ -10,6 +10,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; +use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; @@ -793,28 +794,29 @@ public function screenRequested(Request $httpRequest, ProcessRequest $request) { $elementTypes = ['task', 'userTask', 'startEvent']; $query = ProcessRequestToken::query(); - $query->select('id', 'element_id', 'process_id', 'process_request_id', 'data', 'token_properties') + $query->select('id', 'element_id', 'process_id', 'process_request_id', 'data', 'token_properties', 'completed_at') ->where('process_request_id', $request->id) ->whereIn('element_type', $elementTypes) ->whereIn('status', ['CLOSED', 'TRIGGERED']) - ->orderBy('completed_at'); - - $response = - $query->orderBy( + ->orderBy('completed_at') + ->orderBy( $httpRequest->input('order_by', 'id'), $httpRequest->input('order_direction', 'asc') - )->paginate($httpRequest->input('per_page', 10)); - - $collection = $response->getCollection() - ->transform(function ($token): ?object { - $definition = $token->getDefinition(); - if (!array_key_exists('screenRef', $definition)) { - $config = isset($definition['config']) ? json_decode($definition['config']) : null; - $screenRef = (isset($config) && isset($config->web_entry)) ? $config->web_entry->screen_id ?? null : null; - if (!$screenRef) { - return null; - } - } + ); + + // Filter tokens that actually resolve to a screen before paginating so + // gateway/startEvent (and other non-screen) tokens do not inflate meta.total. + $eligibleTokens = $query->get()->filter(function (ProcessRequestToken $token) { + return $this->tokenHasRequestedScreen($token); + })->values(); + + $total = $eligibleTokens->count(); + $perPage = (int) $httpRequest->input('per_page', 10); + $page = (int) $httpRequest->input('page', 1); + + $collection = $eligibleTokens + ->forPage($page, $perPage) + ->map(function (ProcessRequestToken $token) { $screen = $token->getScreenVersion() ?? null; if ($screen) { @@ -829,9 +831,33 @@ public function screenRequested(Request $httpRequest, ProcessRequest $request) ->reject(fn ($item) => $item === null) ->values(); - $response->setCollection($collection); + $paginator = new LengthAwarePaginator( + $collection, + $total, + $perPage, + $page, + ['path' => $httpRequest->url(), 'query' => $httpRequest->query()] + ); - return new ApiCollection($response); + return new ApiCollection($paginator); + } + + /** + * Whether a token should appear in the Forms tab (has a configured screen). + */ + private function tokenHasRequestedScreen(ProcessRequestToken $token): bool + { + $definition = $token->getDefinition(); + if (!empty($definition['screenRef'])) { + return true; + } + + $config = isset($definition['config']) ? json_decode($definition['config']) : null; + $screenRef = (isset($config) && isset($config->web_entry)) + ? ($config->web_entry->screen_id ?? null) + : null; + + return (bool) $screenRef; } /** diff --git a/tests/Feature/Api/ProcessRequestsTest.php b/tests/Feature/Api/ProcessRequestsTest.php index d7c2de3811..dcec4bf1b4 100644 --- a/tests/Feature/Api/ProcessRequestsTest.php +++ b/tests/Feature/Api/ProcessRequestsTest.php @@ -1286,6 +1286,132 @@ public function testScreenRequestedPaginatesOnlyTaskTokens() $beyondLastPage->assertJsonPath('meta.total_pages', 8); } + /** + * Exclusive gateway loops must not inflate Forms tab pagination with + * startEvent / gateway tokens that have no screen. + * + * Reproduces: start -> Task1 -> XOR -> (loop Task1 | Task2 -> end) + * where non-screen tokens would previously make meta.total show e.g. "1-3 of 6". + * + * @group process_requests + */ + public function testScreenRequestedDoesNotCountGatewayLoopTokensWithoutScreens() + { + $screen = Screen::factory()->create(); + $bpmn = '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . ''; + + $process = Process::factory()->create([ + 'bpmn' => $bpmn, + 'user_id' => $this->user->id, + ]); + + $request = ProcessRequest::factory()->create([ + 'process_id' => $process->id, + 'process_version_id' => $process->getLatestVersion()->id, + ]); + + $baseTime = now()->subHours(1); + + // Tokens produced by: start -> Task1 -> gateway -> Task1 -> gateway -> Task2 -> end + ProcessRequestToken::factory()->create([ + 'process_id' => $process->id, + 'process_request_id' => $request->id, + 'element_id' => 'node_start', + 'element_type' => 'startEvent', + 'status' => 'TRIGGERED', + 'completed_at' => $baseTime->copy()->addMinutes(1), + ]); + ProcessRequestToken::factory()->create([ + 'process_id' => $process->id, + 'process_request_id' => $request->id, + 'element_id' => 'node_task1', + 'element_type' => 'task', + 'status' => 'CLOSED', + 'completed_at' => $baseTime->copy()->addMinutes(2), + ]); + ProcessRequestToken::factory()->create([ + 'process_id' => $process->id, + 'process_request_id' => $request->id, + 'element_id' => 'node_gateway', + 'element_type' => 'gateway', + 'status' => 'CLOSED', + 'completed_at' => $baseTime->copy()->addMinutes(3), + ]); + ProcessRequestToken::factory()->create([ + 'process_id' => $process->id, + 'process_request_id' => $request->id, + 'element_id' => 'node_task1', + 'element_type' => 'task', + 'status' => 'CLOSED', + 'completed_at' => $baseTime->copy()->addMinutes(4), + ]); + ProcessRequestToken::factory()->create([ + 'process_id' => $process->id, + 'process_request_id' => $request->id, + 'element_id' => 'node_gateway', + 'element_type' => 'gateway', + 'status' => 'CLOSED', + 'completed_at' => $baseTime->copy()->addMinutes(5), + ]); + ProcessRequestToken::factory()->create([ + 'process_id' => $process->id, + 'process_request_id' => $request->id, + 'element_id' => 'node_task2', + 'element_type' => 'task', + 'status' => 'CLOSED', + 'completed_at' => $baseTime->copy()->addMinutes(6), + ]); + ProcessRequestToken::factory()->create([ + 'process_id' => $process->id, + 'process_request_id' => $request->id, + 'element_id' => 'node_end', + 'element_type' => 'end_event', + 'status' => 'CLOSED', + 'completed_at' => $baseTime->copy()->addMinutes(7), + ]); + // Extra startEvent-typed tokens without screens (would previously inflate total to 6). + ProcessRequestToken::factory()->create([ + 'process_id' => $process->id, + 'process_request_id' => $request->id, + 'element_id' => 'node_start', + 'element_type' => 'startEvent', + 'status' => 'TRIGGERED', + 'completed_at' => $baseTime->copy()->addMinutes(8), + ]); + ProcessRequestToken::factory()->create([ + 'process_id' => $process->id, + 'process_request_id' => $request->id, + 'element_id' => 'node_start', + 'element_type' => 'startEvent', + 'status' => 'TRIGGERED', + 'completed_at' => $baseTime->copy()->addMinutes(9), + ]); + + $route = route('api.requests.detail.screen', ['request' => $request->id]); + $response = $this->apiCall('GET', $route, [ + 'page' => 1, + 'per_page' => 3, + 'order_by' => 'completed_at', + 'order_direction' => 'asc', + ]); + + $response->assertStatus(200); + // Only the three task submissions with screens (Task1, Task1 loop, Task2), + // not "1-3 of 6" from startEvent tokens without screens. + $response->assertJsonPath('meta.total', 3); + $response->assertJsonPath('meta.total_pages', 1); + $this->assertCount(3, $response->json('data')); + } + /** * Get a list of Requests by Cases. */ From 3e65bbe1884c50e4cbfd1672d41c4972db961d7b Mon Sep 17 00:00:00 2001 From: CarliPinell Date: Wed, 11 Nov 2026 12:26:56 -0400 Subject: [PATCH 2/2] adding improvements --- .../Api/ProcessRequestController.php | 81 ++++++++++++------- 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php b/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php index 322ebadea4..d4632cbd75 100644 --- a/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php +++ b/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php @@ -10,7 +10,6 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; -use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; @@ -793,30 +792,29 @@ public function getRequestToken(Request $httpRequest, ProcessRequest $request) public function screenRequested(Request $httpRequest, ProcessRequest $request) { $elementTypes = ['task', 'userTask', 'startEvent']; + $statuses = ['CLOSED', 'TRIGGERED']; + + // Only BPMN elements that resolve to a screen belong in the Forms tab. Gateway/startEvent + // tokens without a screen must not inflate the pagination total, so the eligible elements + // are resolved first (once per element, not once per token) and used to filter in SQL. + $screenElementIds = $this->elementIdsWithRequestedScreen($request, $elementTypes, $statuses); + $query = ProcessRequestToken::query(); - $query->select('id', 'element_id', 'process_id', 'process_request_id', 'data', 'token_properties', 'completed_at') + $query->select('id', 'element_id', 'process_id', 'process_request_id', 'data', 'token_properties') ->where('process_request_id', $request->id) ->whereIn('element_type', $elementTypes) - ->whereIn('status', ['CLOSED', 'TRIGGERED']) - ->orderBy('completed_at') - ->orderBy( + ->whereIn('status', $statuses) + ->whereIn('element_id', $screenElementIds) + ->orderBy('completed_at'); + + $response = + $query->orderBy( $httpRequest->input('order_by', 'id'), $httpRequest->input('order_direction', 'asc') - ); - - // Filter tokens that actually resolve to a screen before paginating so - // gateway/startEvent (and other non-screen) tokens do not inflate meta.total. - $eligibleTokens = $query->get()->filter(function (ProcessRequestToken $token) { - return $this->tokenHasRequestedScreen($token); - })->values(); - - $total = $eligibleTokens->count(); - $perPage = (int) $httpRequest->input('per_page', 10); - $page = (int) $httpRequest->input('page', 1); + )->paginate($httpRequest->input('per_page', 10)); - $collection = $eligibleTokens - ->forPage($page, $perPage) - ->map(function (ProcessRequestToken $token) { + $collection = $response->getCollection() + ->transform(function ($token): ?object { $screen = $token->getScreenVersion() ?? null; if ($screen) { @@ -831,15 +829,40 @@ public function screenRequested(Request $httpRequest, ProcessRequest $request) ->reject(fn ($item) => $item === null) ->values(); - $paginator = new LengthAwarePaginator( - $collection, - $total, - $perPage, - $page, - ['path' => $httpRequest->url(), 'query' => $httpRequest->query()] - ); + $response->setCollection($collection); + + return new ApiCollection($response); + } - return new ApiCollection($paginator); + /** + * Get the BPMN element ids of the request tokens that have a screen configured. + * + * The definition only depends on the element, so it is evaluated once per distinct + * element_id using a single lightweight token (without the heavy data columns). + * + * @return array + */ + private function elementIdsWithRequestedScreen(ProcessRequest $request, array $elementTypes, array $statuses): array + { + $sampleTokenIds = ProcessRequestToken::query() + ->where('process_request_id', $request->id) + ->whereIn('element_type', $elementTypes) + ->whereIn('status', $statuses) + ->groupBy('element_id') + ->selectRaw('MIN(id) as id') + ->pluck('id'); + + return ProcessRequestToken::query() + ->select('id', 'element_id', 'process_request_id') + ->whereIn('id', $sampleTokenIds) + ->get() + ->filter(function (ProcessRequestToken $token) use ($request) { + $token->setRelation('processRequest', $request); + + return $this->tokenHasRequestedScreen($token); + }) + ->pluck('element_id') + ->all(); } /** @@ -848,7 +871,9 @@ public function screenRequested(Request $httpRequest, ProcessRequest $request) private function tokenHasRequestedScreen(ProcessRequestToken $token): bool { $definition = $token->getDefinition(); - if (!empty($definition['screenRef'])) { + // Keep the original semantics: the presence of the key is enough (empty values + // fall back to the default form screen in ProcessRequestToken::getScreen()). + if (array_key_exists('screenRef', $definition)) { return true; }