mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Add group setting filters
This commit is contained in:
parent
ed3b8240d6
commit
3217b68f8e
@ -86,7 +86,7 @@ class BankIntegrationFilters extends QueryFilters
|
||||
/**
|
||||
* Sorts the list based on $sort.
|
||||
*
|
||||
* @param string sort formatted as column|asc
|
||||
* @param string $sort formatted as column|asc
|
||||
* @return Builder
|
||||
*/
|
||||
public function sort(string $sort = ''): Builder
|
||||
@ -103,7 +103,7 @@ class BankIntegrationFilters extends QueryFilters
|
||||
/**
|
||||
* Filters the query by the users company ID.
|
||||
*
|
||||
* @return Illuminate\Database\Query\Builder
|
||||
* @return Builder
|
||||
*/
|
||||
public function entityFilter(): Builder
|
||||
{
|
||||
|
79
app/Filters/GroupSettingFilters.php
Normal file
79
app/Filters/GroupSettingFilters.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Filters;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* GroupSettingFilters.
|
||||
*/
|
||||
class GroupSettingFilters extends QueryFilters
|
||||
{
|
||||
/**
|
||||
* Filter by name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return Builder
|
||||
*/
|
||||
public function name(string $name = ''): Builder
|
||||
{nlog("filter");
|
||||
if (strlen($name) == 0) {
|
||||
return $this->builder;
|
||||
}
|
||||
|
||||
return $this->builder->where('name', 'like', '%'.$name.'%');
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter based on search text.
|
||||
*
|
||||
* @param string $filter
|
||||
* @return Builder
|
||||
*/
|
||||
public function filter(string $filter = ''): Builder
|
||||
{
|
||||
if (strlen($filter) == 0) {
|
||||
return $this->builder;
|
||||
}
|
||||
|
||||
return $this->builder->where(function ($query) use ($filter) {
|
||||
$query->where('name', 'like', '%'.$filter.'%');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the list based on $sort.
|
||||
*
|
||||
* @param string $sort formatted as column|asc
|
||||
* @return Builder
|
||||
*/
|
||||
public function sort(string $sort = ''): Builder
|
||||
{
|
||||
$sort_col = explode('|', $sort);
|
||||
|
||||
if (!is_array($sort_col) || count($sort_col) != 2) {
|
||||
return $this->builder;
|
||||
}
|
||||
|
||||
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the query by the users company ID.
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function entityFilter(): Builder
|
||||
{
|
||||
return $this->builder->company();
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Factory\GroupSettingFactory;
|
||||
use App\Filters\GroupSettingFilters;
|
||||
use App\Http\Requests\GroupSetting\CreateGroupSettingRequest;
|
||||
use App\Http\Requests\GroupSetting\DestroyGroupSettingRequest;
|
||||
use App\Http\Requests\GroupSetting\EditGroupSettingRequest;
|
||||
@ -49,47 +50,9 @@ class GroupSettingController extends BaseController
|
||||
$this->group_setting_repo = $group_setting_repo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
*
|
||||
* @OA\Get(
|
||||
* path="/api/v1/group_settings",
|
||||
* operationId="getGroupSettings",
|
||||
* tags={"group_settings"},
|
||||
* summary="Gets a list of group_settings",
|
||||
* description="Lists group_settings, search and filters allow fine grained lists to be generated.
|
||||
|
||||
Query parameters can be added to performed more fine grained filtering of the group_settings, these are handled by the GroupSettingFilters class which defines the methods available",
|
||||
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
||||
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
||||
* @OA\Parameter(ref="#/components/parameters/include"),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="A list of group_settings",
|
||||
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
||||
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
||||
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
||||
* @OA\JsonContent(ref="#/components/schemas/GroupSetting"),
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=422,
|
||||
* description="Validation error",
|
||||
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
||||
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response="default",
|
||||
* description="Unexpected Error",
|
||||
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
||||
* ),
|
||||
* )
|
||||
*/
|
||||
public function index()
|
||||
public function index(GroupSettingFilters $filters)
|
||||
{
|
||||
$group_settings = GroupSetting::whereCompanyId(auth()->user()->company()->id);
|
||||
$group_settings = GroupSetting::filter($filters);
|
||||
|
||||
return $this->listResponse($group_settings);
|
||||
}
|
||||
|
@ -11,10 +11,11 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Filterable;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
|
||||
|
||||
/**
|
||||
* App\Models\GroupSetting
|
||||
@ -61,7 +62,8 @@ class GroupSetting extends StaticModel
|
||||
{
|
||||
use MakesHash;
|
||||
use SoftDeletes;
|
||||
|
||||
use Filterable;
|
||||
|
||||
protected $casts = [
|
||||
'settings' => 'object',
|
||||
'updated_at' => 'timestamp',
|
||||
|
@ -38,6 +38,43 @@ class GroupSettingTest extends TestCase
|
||||
$this->makeTestData();
|
||||
}
|
||||
|
||||
|
||||
public function testAddGroupFilters()
|
||||
{
|
||||
$settings = new \stdClass;
|
||||
$settings->currency_id = '1';
|
||||
|
||||
$data = [
|
||||
'name' => 'testX',
|
||||
'settings' => $settings,
|
||||
];
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])->postJson('/api/v1/group_settings', $data);
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
$arr = $response->json();
|
||||
|
||||
$this->assertEquals('testX', $arr['data']['name']);
|
||||
$this->assertEquals(0, $arr['data']['archived_at']);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])->get('/api/v1/group_settings?name=fdfdfd');
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
$arr = $response->json();
|
||||
|
||||
$this->assertCount(0, $arr['data']);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function testAddGroupSettings()
|
||||
{
|
||||
$settings = new \stdClass;
|
||||
|
Loading…
x
Reference in New Issue
Block a user