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
69 changes: 60 additions & 9 deletions ProcessMaker/Http/Controllers/Api/ProcessRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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) {
Expand All @@ -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<int, string>
*/
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
Expand Down
126 changes: 126 additions & 0 deletions tests/Feature/Api/ProcessRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<?xml version="1.0"?>'
. '<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"'
. ' xmlns:pm="http://processmaker.com/BPMN/2.0/Schema.xsd">'
. '<bpmn:process id="ProcessId">'
. '<bpmn:startEvent id="node_start" name="Start"/>'
. '<bpmn:task id="node_task1" pm:screenRef="' . $screen->id . '" name="Task1"/>'
. '<bpmn:exclusiveGateway id="node_gateway" name="XOR"/>'
. '<bpmn:task id="node_task2" pm:screenRef="' . $screen->id . '" name="Task2"/>'
. '<bpmn:endEvent id="node_end" name="End"/>'
. '</bpmn:process></bpmn:definitions>';

$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.
*/
Expand Down
Loading