Skip to content

Add query-based pagination to tasks controller [next] #6949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
28 changes: 5 additions & 23 deletions ProcessMaker/Http/Controllers/Api/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,47 +122,29 @@ public function index(Request $request, $getTotal = false, User $user = null)

$this->applyForCurrentUser($query, $user);

try {
// Count up the total number of results, but
// watch for PMQL query exceptions and handle
// them if they occur
$totalResultCount = $query->count();
} catch (QueryException $e) {
return $this->handleQueryException($e);
}

// If only the total is being requested (by a Saved Search), send it now
if ($getTotal === true) {
return $totalResultCount;
}

// Apply filter overdue
$query->overdue($request->input('overdue'));

// If we should manually add pagination to the
// query in advance (also used by saved search)
if ($this->isPaginationEnabled()) {
$query->limit($request->input('per_page', 10));
// If only the total is being requested (by a Saved Search), send it now
if ($getTotal === true) {
return $query->count();
}

try {
$response = $query->get();
$response = $query->paginate($request->input('per_page', 10));
} catch (QueryException $e) {
return $this->handleQueryException($e);
}

$response = $this->applyUserFilter($response, $request, $user);

// Map each item through its resource
$response = $this->applyResource($response);

$inOverdueQuery = ProcessRequestToken::query()
->whereIn('id', $response->pluck('id'))
->where('due_at', '<', Carbon::now());

$response->inOverdue = $inOverdueQuery->count();

return new TaskCollection($response, $totalResultCount);
return new TaskCollection($response);
}

/**
Expand Down
37 changes: 0 additions & 37 deletions ProcessMaker/Traits/TaskControllerIndexMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,6 @@

trait TaskControllerIndexMethods
{
/**
* Manually enable paginated results from the
* index method()
*
* @return void
*/
public function enableIndexPagination(): void
{
static::$paginate = true;
}

/**
* Determine if pagination was manually set for the
* index() method results
*
* @return bool
*/
public function isPaginationEnabled(): bool
{
return static::$paginate === true;
}

/**
* Used by saved search to paginate the results
* from the index() method
*
* @var bool
*/
protected static bool $paginate = false;

private function indexBaseQuery($request)
{
$query = ProcessRequestToken::with(['processRequest', 'user', 'draft']);
Expand Down Expand Up @@ -300,13 +270,6 @@ private function applyUserFilter($response, $request, $user)
return $response;
}

private function applyResource($response)
{
return $response->map(function ($processRequestToken) {
return new Resource($processRequestToken);
});
}

private function applyForCurrentUser($query, $user)
{
if ($user->is_administrator) {
Expand Down
Loading