Repository navigation
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91e87f0
feat: add cases_started migration
devmiguelangel 794c160
feat: add cases started model
devmiguelangel 55d771c
feat: add new get_all_cases endpoint
devmiguelangel a1ca7a7
Merge branch 'feature/FOUR-18078' into FOUR-18603
devmiguelangel 2fc73d7
fix cr observations
devmiguelangel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
200 changes: 200 additions & 0 deletions
200
ProcessMaker/Http/Controllers/Api/V1_1/CaseController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = [ | ||
| '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.
Resolved
|
||
| } | ||
|
|
||
| $sort = explode(':', $value); | ||
| $field = $sort[0]; | ||
| $order = $sort[1] ?? 'asc'; | ||
|
devmiguelangel marked this conversation as resolved.
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.
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.
Resolved
|
||
| } else { | ||
| $q->orWhereFullText($field, $search . '*', ['mode' => 'boolean']); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
Resolved
|
||
| 'sortBy' => 'sometimes|string', | ||
| 'filterBy' => 'sometimes|array', | ||
| 'search' => 'sometimes|string', | ||
| 'pageSize' => 'sometimes|integer|min:1', | ||
| 'page' => 'sometimes|integer|min:1', | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.