Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 249
FOUR-18603 cases started table and view the list of all cases in the system ep#7357
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
91e87f0794c16055d771ca1ca7a72fc73d7File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| <?php | ||
| namespace ProcessMaker\Http\Controllers\Api\V1_1; | ||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Http\Request; | ||
| 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 | ||
| { | ||
| /** | ||
| * Default fields used in the query select statement. | ||
| */ | ||
| protected $defaultFields = [ | ||
| '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', | ||
| ]; | ||
| const DEFAULT_SORT_DIRECTION = 'asc'; | ||
| /** | ||
| * 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| $sort = explode(':', $value); | ||
| $field = $sort[0]; | ||
| $order = $sort[1] ?? self::DEFAULT_SORT_DIRECTION; | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| 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, $search); | ||
| } else { | ||
| $q->orWhereFullText($field, $search . '*', ['mode' => 'boolean']); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
| namespace ProcessMaker\Http\Requests; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
| use ProcessMaker\Rules\SortBy; | ||
| 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', | ||
| 'sortBy' => ['sometimes', 'string', new SortBy], | ||
| 'filterBy' => 'sometimes|array', | ||
| 'search' => 'sometimes|string', | ||
| 'pageSize' => 'sometimes|integer|min:1', | ||
| 'page' => 'sometimes|integer|min:1', | ||
| ]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
| namespace ProcessMaker\Http\Resources\V1_1; | ||
| use ProcessMaker\Http\Resources\ApiResource; | ||
| class CaseResource extends ApiResource | ||
| { | ||
| /** | ||
| * Default fields that will be included in the response. | ||
| */ | ||
| protected static $defaultFields = [ | ||
devmiguelangel marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| '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; | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
| namespace ProcessMaker\Rules; | ||
| use Closure; | ||
| use Illuminate\Contracts\Validation\ValidationRule; | ||
| class SortBy implements ValidationRule | ||
| { | ||
| /** | ||
| * Run the validation rule. | ||
| * | ||
| * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail | ||
| */ | ||
| public function validate(string $attribute, mixed $value, Closure $fail): void | ||
| { | ||
| $items = explode(',', $value); | ||
| foreach ($items as $item) { | ||
| if (!preg_match('/^[a-zA-Z_]+:(asc|desc)$/', $item)) { | ||
| $fail("The $attribute must be a comma-separated list of field:asc|desc."); | ||
| } | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.