Skip to content
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

FOUR-18603 cases started table and view the list of all cases in the system ep #7357

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
200 changes: 200 additions & 0 deletions ProcessMaker/Http/Controllers/Api/V1_1/CaseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

namespace ProcessMaker\Http\Controllers\Api\V1_1;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Http\Requests\GetAllCasesRequest;
use ProcessMaker\Http\Resources\V1_1\CaseResource;
use ProcessMaker\Models\CaseStarted;

class CaseController extends Controller
{
protected $defaultFields = [
devmiguelangel marked this conversation as resolved.
Show resolved Hide resolved
'case_number',
'user_id',
'case_title',
'case_title_formatted',
'case_status',
'processes',
'requests',
'request_tokens',
'tasks',
'participants',
'initiated_at',
'completed_at',
];

protected $sortableFields = [
'case_number',
'initiated_at',
'completed_at',
];

protected $filterableFields = [
'case_number',
'case_title',
'case_status',
'processes',
'requests',
'request_tokens',
'tasks',
'participants',
'initiated_at',
'completed_at',
];

protected $searchableFields = [
'case_number',
'case_title',
];

protected $dateFields = [
'initiated_at',
'completed_at',
'created_at',
'updated_at',
];

/**
* Get a list of all started cases.
*
* @param Request $request
*
* @queryParam userId int Filter by user ID.
* @queryParam status string Filter by case status.
* @queryParam sortBy string Sort by field:asc,field2:desc,...
* @queryParam filterBy array Filter by field=value&field2=value2&...
* @queryParam search string Search by case number or case title.
* @queryParam pageSize int Number of items per page.
* @queryParam page int Page number.
*
* @return array
*/
public function getAllCases(GetAllCasesRequest $request): array
{
$pageSize = $request->get('pageSize', 15);

$query = CaseStarted::select($this->defaultFields);

$this->filters($request, $query);

$pagination = CaseResource::collection($query->paginate($pageSize));

return [
'data' => $pagination->items(),
'meta' => [
'total' => $pagination->total(),
'perPage' => $pagination->perPage(),
'currentPage' => $pagination->currentPage(),
'lastPage' => $pagination->lastPage(),
],
];
}

/**
* Apply filters to the query.
*
* @param Request $request
* @param Builder $query
*
* @return void
*/
private function filters(Request $request, Builder $query): void
{
if ($request->has('userId')) {
$query->where('user_id', $request->get('userId'));
}

if ($request->has('status')) {
$query->where('case_status', $request->get('status'));
}

$this->search($request, $query);
$this->filterBy($request, $query);
$this->sortBy($request, $query);
}

/**
* Sort the query.
*
* @param Request $request: Query parameter format: sortBy=field:asc,field2:desc,...
* @param Builder $query
*
* @return void
*/
private function sortBy(Request $request, Builder $query): void
{
$sort = explode(',', $request->get('sortBy'));

foreach ($sort as $value) {
if (!preg_match('/^[a-zA-Z_]+:(asc|desc)$/', $value)) {
continue;
devmiguelangel marked this conversation as resolved.
Show resolved Hide resolved
}

$sort = explode(':', $value);
$field = $sort[0];
$order = $sort[1] ?? 'asc';
devmiguelangel marked this conversation as resolved.
Show resolved Hide resolved

if (in_array($field, $this->sortableFields)) {
$query->orderBy($field, $order);
}
}
}

/**
* Filter the query.
*
* @param Request $request: Query parameter format: filterBy[field]=value&filterBy[field2]=value2&...
* @param Builder $query
* @param array $dateFields List of date fields in current model
*
* @return void
*/
private function filterBy(Request $request, Builder $query): void
{
if ($request->has('filterBy')) {
$filterByValue = $request->get('filterBy');

foreach ($filterByValue as $key => $value) {
if (!in_array($key, $this->filterableFields)) {
continue;
devmiguelangel marked this conversation as resolved.
Show resolved Hide resolved
}

if (in_array($key, $this->dateFields)) {
$query->whereDate($key, $value);
continue;
}

$query->where($key, $value);
}
}
}

/**
* Search by case number or case title.

* @param Request $request: Query parameter format: search=keyword
* @param Builder $query
*
* @return void
*/
private function search(Request $request, Builder $query): void
{
if ($request->has('search')) {
$search = $request->get('search');

$query->where(function ($q) use ($search) {
foreach ($this->searchableFields as $field) {
if ($field === 'case_number') {
$q->orWhere($field, 'like', "%$search%");
devmiguelangel marked this conversation as resolved.
Show resolved Hide resolved
} else {
$q->orWhereFullText($field, $search . '*', ['mode' => 'boolean']);
}
}
});
}
}
}
34 changes: 34 additions & 0 deletions ProcessMaker/Http/Requests/GetAllCasesRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace ProcessMaker\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class GetAllCasesRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'userId' => 'sometimes|integer',
'status' => 'sometimes|in:in_progress,completed',
devmiguelangel marked this conversation as resolved.
Show resolved Hide resolved
'sortBy' => 'sometimes|string',
'filterBy' => 'sometimes|array',
'search' => 'sometimes|string',
'pageSize' => 'sometimes|integer|min:1',
'page' => 'sometimes|integer|min:1',
];
}
}
34 changes: 34 additions & 0 deletions ProcessMaker/Http/Resources/V1_1/CaseResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace ProcessMaker\Http\Resources\V1_1;

use ProcessMaker\Http\Resources\ApiResource;

class CaseResource extends ApiResource
{
protected static $defaultFields = [
devmiguelangel marked this conversation as resolved.
Show resolved Hide resolved
'case_number',
'user_id',
'case_title',
'case_title_formatted',
'case_status',
'processes',
'requests',
'request_tokens',
'tasks',
'participants',
'initiated_at',
'completed_at',
];

public function toArray($request): array
{
$data = [];

foreach (static::$defaultFields as $field) {
$data[$field] = $this->$field;
}

return $data;
}
}
55 changes: 55 additions & 0 deletions ProcessMaker/Models/CaseStarted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace ProcessMaker\Models;

use Database\Factories\CaseStartedFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
use Illuminate\Database\Eloquent\Factories\Factory;
use ProcessMaker\Models\ProcessMakerModel;

class CaseStarted extends ProcessMakerModel
{
use HasFactory;

protected $table = 'cases_started';

protected $fillable = [
'case_number',
'user_id',
'case_title',
'case_title_formatted',
'case_status',
'processes',
'requests',
'request_tokens',
'tasks',
'participants',
'initiated_at',
'completed_at',
'keywords',
];

protected $casts = [
'processes' => AsArrayObject::class,
'requests' => AsArrayObject::class,
'request_tokens' => AsArrayObject::class,
'tasks' => AsArrayObject::class,
'participants' => AsArrayObject::class,
'initiated_at' => 'datetime',
'completed_at' => 'datetime',
];

protected static function newFactory(): Factory
{
return CaseStartedFactory::new();
}

/**
* Get the user that owns the case.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
Loading
Loading