Skip to content

Commit

Permalink
fix cr observations
Browse files Browse the repository at this point in the history
  • Loading branch information
devmiguelangel committed Sep 12, 2024
1 parent a1ca7a7 commit 2fc73d7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
10 changes: 7 additions & 3 deletions ProcessMaker/Http/Controllers/Api/V1_1/CaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

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
{
/**
* Default fields used in the query select statement.
*/
protected $defaultFields = [
'case_number',
'user_id',
Expand Down Expand Up @@ -58,6 +60,8 @@ class CaseController extends Controller
'updated_at',
];

const DEFAULT_SORT_DIRECTION = 'asc';

/**
* Get a list of all started cases.
*
Expand Down Expand Up @@ -136,7 +140,7 @@ private function sortBy(Request $request, Builder $query): void

$sort = explode(':', $value);
$field = $sort[0];
$order = $sort[1] ?? 'asc';
$order = $sort[1] ?? self::DEFAULT_SORT_DIRECTION;

if (in_array($field, $this->sortableFields)) {
$query->orderBy($field, $order);
Expand Down Expand Up @@ -189,7 +193,7 @@ private function search(Request $request, Builder $query): void
$query->where(function ($q) use ($search) {
foreach ($this->searchableFields as $field) {
if ($field === 'case_number') {
$q->orWhere($field, 'like', "%$search%");
$q->orWhere($field, $search);
} else {
$q->orWhereFullText($field, $search . '*', ['mode' => 'boolean']);
}
Expand Down
5 changes: 3 additions & 2 deletions ProcessMaker/Http/Requests/GetAllCasesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ProcessMaker\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use ProcessMaker\Rules\SortBy;

class GetAllCasesRequest extends FormRequest
{
Expand All @@ -23,8 +24,8 @@ public function rules(): array
{
return [
'userId' => 'sometimes|integer',
'status' => 'sometimes|in:in_progress,completed',
'sortBy' => 'sometimes|string',
'status' => 'sometimes|in:IN_PROGRESS,COMPLETED',
'sortBy' => ['sometimes', 'string', new SortBy],
'filterBy' => 'sometimes|array',
'search' => 'sometimes|string',
'pageSize' => 'sometimes|integer|min:1',
Expand Down
3 changes: 3 additions & 0 deletions ProcessMaker/Http/Resources/V1_1/CaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class CaseResource extends ApiResource
{
/**
* Default fields that will be included in the response.
*/
protected static $defaultFields = [
'case_number',
'user_id',
Expand Down
25 changes: 25 additions & 0 deletions ProcessMaker/Rules/SortBy.php
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.");
}
}
}
}
2 changes: 1 addition & 1 deletion database/factories/CaseStartedFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function definition(): array
'user_id' => fake()->randomElement([1, 3]),
'case_title' => fake()->words(3, true),
'case_title_formatted' => fake()->words(3, true),
'case_status' => fake()->randomElement(['in_progress', 'completed']),
'case_status' => fake()->randomElement(['IN_PROGRESS', 'COMPLETED']),
'processes' => array_map(function() {
return [
'id' => fake()->randomNumber(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up(): void
$table->unsignedInteger('case_number')->primary();
$table->unsignedInteger('user_id');
$table->string('case_title', 255);
$table->string('case_title_formatted', 255);
$table->text('case_title_formatted');
$table->string('case_status', 20);
$table->json('processes');
$table->json('requests');
Expand Down

0 comments on commit 2fc73d7

Please sign in to comment.