diff --git a/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php b/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php index 4cabf9cb34..d4632cbd75 100644 --- a/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php +++ b/ProcessMaker/Http/Controllers/Api/ProcessRequestController.php @@ -792,11 +792,19 @@ 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') ->where('process_request_id', $request->id) ->whereIn('element_type', $elementTypes) - ->whereIn('status', ['CLOSED', 'TRIGGERED']) + ->whereIn('status', $statuses) + ->whereIn('element_id', $screenElementIds) ->orderBy('completed_at'); $response = @@ -807,14 +815,6 @@ public function screenRequested(Request $httpRequest, ProcessRequest $request) $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; - } - } $screen = $token->getScreenVersion() ?? null; if ($screen) { @@ -834,6 +834,57 @@ public function screenRequested(Request $httpRequest, ProcessRequest $request) return new ApiCollection($response); } + /** + * 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(); + } + + /** + * Whether a token should appear in the Forms tab (has a configured screen). + */ + private function tokenHasRequestedScreen(ProcessRequestToken $token): bool + { + $definition = $token->getDefinition(); + // 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; + } + + $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; + } + /** * Adding abe flag * @param int $id 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. */