1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Add filters for Scheduler

This commit is contained in:
David Bomba 2023-02-26 18:21:23 +11:00
parent 8aae36bef3
commit fe40771a24
5 changed files with 192 additions and 63 deletions

View File

@ -4193,7 +4193,7 @@
*/
public static function lock($name, $seconds = 0, $owner = null)
{
/** @var \Illuminate\Cache\FileStore $instance */
/** @var \Illuminate\Cache\RedisStore $instance */
return $instance->lock($name, $seconds, $owner);
}
/**
@ -4206,7 +4206,7 @@
*/
public static function restoreLock($name, $owner)
{
/** @var \Illuminate\Cache\FileStore $instance */
/** @var \Illuminate\Cache\RedisStore $instance */
return $instance->restoreLock($name, $owner);
}
/**
@ -4217,30 +4217,65 @@
*/
public static function flush()
{
/** @var \Illuminate\Cache\FileStore $instance */
/** @var \Illuminate\Cache\RedisStore $instance */
return $instance->flush();
}
/**
* Get the Filesystem instance.
* Get the Redis connection instance.
*
* @return \Illuminate\Filesystem\Filesystem
* @return \Illuminate\Redis\Connections\Connection
* @static
*/
public static function getFilesystem()
public static function connection()
{
/** @var \Illuminate\Cache\FileStore $instance */
return $instance->getFilesystem();
/** @var \Illuminate\Cache\RedisStore $instance */
return $instance->connection();
}
/**
* Get the working directory of the cache.
* Get the Redis connection instance that should be used to manage locks.
*
* @return string
* @return \Illuminate\Redis\Connections\Connection
* @static
*/
public static function getDirectory()
public static function lockConnection()
{
/** @var \Illuminate\Cache\FileStore $instance */
return $instance->getDirectory();
/** @var \Illuminate\Cache\RedisStore $instance */
return $instance->lockConnection();
}
/**
* Specify the name of the connection that should be used to store data.
*
* @param string $connection
* @return void
* @static
*/
public static function setConnection($connection)
{
/** @var \Illuminate\Cache\RedisStore $instance */
$instance->setConnection($connection);
}
/**
* Specify the name of the connection that should be used to manage locks.
*
* @param string $connection
* @return \Illuminate\Cache\RedisStore
* @static
*/
public static function setLockConnection($connection)
{
/** @var \Illuminate\Cache\RedisStore $instance */
return $instance->setLockConnection($connection);
}
/**
* Get the Redis database instance.
*
* @return \Illuminate\Contracts\Redis\Factory
* @static
*/
public static function getRedis()
{
/** @var \Illuminate\Cache\RedisStore $instance */
return $instance->getRedis();
}
/**
* Get the cache key prefix.
@ -4250,8 +4285,20 @@
*/
public static function getPrefix()
{
/** @var \Illuminate\Cache\FileStore $instance */
/** @var \Illuminate\Cache\RedisStore $instance */
return $instance->getPrefix();
}
/**
* Set the cache key prefix.
*
* @param string $prefix
* @return void
* @static
*/
public static function setPrefix($prefix)
{
/** @var \Illuminate\Cache\RedisStore $instance */
$instance->setPrefix($prefix);
}
}
@ -9854,45 +9901,44 @@
return $instance->setConnectionName($name);
}
/**
* Release a reserved job back onto the queue after (n) seconds.
* Migrate the delayed jobs that are ready to the regular queue.
*
* @param string $queue
* @param \Illuminate\Queue\Jobs\DatabaseJobRecord $job
* @param int $delay
* @return mixed
* @param string $from
* @param string $to
* @param int $limit
* @return array
* @static
*/
public static function release($queue, $job, $delay)
public static function migrateExpiredJobs($from, $to)
{
/** @var \Illuminate\Queue\DatabaseQueue $instance */
return $instance->release($queue, $job, $delay);
/** @var \Illuminate\Queue\RedisQueue $instance */
return $instance->migrateExpiredJobs($from, $to);
}
/**
* Delete a reserved job from the queue.
*
* @param string $queue
* @param string $id
* @param \Illuminate\Queue\Jobs\RedisJob $job
* @return void
* @throws \Throwable
* @static
*/
public static function deleteReserved($queue, $id)
public static function deleteReserved($queue, $job)
{
/** @var \Illuminate\Queue\DatabaseQueue $instance */
$instance->deleteReserved($queue, $id);
/** @var \Illuminate\Queue\RedisQueue $instance */
$instance->deleteReserved($queue, $job);
}
/**
* Delete a reserved job from the reserved queue and release it.
*
* @param string $queue
* @param \Illuminate\Queue\Jobs\DatabaseJob $job
* @param \Illuminate\Queue\Jobs\RedisJob $job
* @param int $delay
* @return void
* @static
*/
public static function deleteAndRelease($queue, $job, $delay)
{
/** @var \Illuminate\Queue\DatabaseQueue $instance */
/** @var \Illuminate\Queue\RedisQueue $instance */
$instance->deleteAndRelease($queue, $job, $delay);
}
/**
@ -9904,7 +9950,7 @@
*/
public static function clear($queue)
{
/** @var \Illuminate\Queue\DatabaseQueue $instance */
/** @var \Illuminate\Queue\RedisQueue $instance */
return $instance->clear($queue);
}
/**
@ -9916,19 +9962,30 @@
*/
public static function getQueue($queue)
{
/** @var \Illuminate\Queue\DatabaseQueue $instance */
/** @var \Illuminate\Queue\RedisQueue $instance */
return $instance->getQueue($queue);
}
/**
* Get the underlying database instance.
* Get the connection for the queue.
*
* @return \Illuminate\Database\Connection
* @return \Illuminate\Redis\Connections\Connection
* @static
*/
public static function getDatabase()
public static function getConnection()
{
/** @var \Illuminate\Queue\DatabaseQueue $instance */
return $instance->getDatabase();
/** @var \Illuminate\Queue\RedisQueue $instance */
return $instance->getConnection();
}
/**
* Get the underlying Redis instance.
*
* @return \Illuminate\Contracts\Redis\Factory
* @static
*/
public static function getRedis()
{
/** @var \Illuminate\Queue\RedisQueue $instance */
return $instance->getRedis();
}
/**
* Get the backoff for an object-based queue handler.
@ -9939,7 +9996,7 @@
*/
public static function getJobBackoff($job)
{ //Method inherited from \Illuminate\Queue\Queue
/** @var \Illuminate\Queue\DatabaseQueue $instance */
/** @var \Illuminate\Queue\RedisQueue $instance */
return $instance->getJobBackoff($job);
}
/**
@ -9951,7 +10008,7 @@
*/
public static function getJobExpiration($job)
{ //Method inherited from \Illuminate\Queue\Queue
/** @var \Illuminate\Queue\DatabaseQueue $instance */
/** @var \Illuminate\Queue\RedisQueue $instance */
return $instance->getJobExpiration($job);
}
/**
@ -9963,7 +10020,7 @@
*/
public static function createPayloadUsing($callback)
{ //Method inherited from \Illuminate\Queue\Queue
\Illuminate\Queue\DatabaseQueue::createPayloadUsing($callback);
\Illuminate\Queue\RedisQueue::createPayloadUsing($callback);
}
/**
* Get the container instance being used by the connection.
@ -9973,7 +10030,7 @@
*/
public static function getContainer()
{ //Method inherited from \Illuminate\Queue\Queue
/** @var \Illuminate\Queue\DatabaseQueue $instance */
/** @var \Illuminate\Queue\RedisQueue $instance */
return $instance->getContainer();
}
/**
@ -9985,7 +10042,7 @@
*/
public static function setContainer($container)
{ //Method inherited from \Illuminate\Queue\Queue
/** @var \Illuminate\Queue\DatabaseQueue $instance */
/** @var \Illuminate\Queue\RedisQueue $instance */
$instance->setContainer($container);
}
@ -17797,25 +17854,6 @@
/**
*
*
* @method static void createSubscription(array|string $channels, \Closure $callback, string $method = 'subscribe')
* @method static \Illuminate\Redis\Limiters\ConcurrencyLimiterBuilder funnel(string $name)
* @method static \Illuminate\Redis\Limiters\DurationLimiterBuilder throttle(string $name)
* @method static mixed client()
* @method static void subscribe(array|string $channels, \Closure $callback)
* @method static void psubscribe(array|string $channels, \Closure $callback)
* @method static mixed command(string $method, array $parameters = [])
* @method static void listen(\Closure $callback)
* @method static string|null getName()
* @method static \Illuminate\Redis\Connections\Connection setName(string $name)
* @method static \Illuminate\Contracts\Events\Dispatcher getEventDispatcher()
* @method static void setEventDispatcher(\Illuminate\Contracts\Events\Dispatcher $events)
* @method static void unsetEventDispatcher()
* @method static void macro(string $name, object|callable $macro)
* @method static void mixin(object $mixin, bool $replace = true)
* @method static bool hasMacro(string $name)
* @method static void flushMacros()
* @method static mixed macroCall(string $method, array $parameters)
* @see \Illuminate\Redis\RedisManager
*/
class Redis {
/**

View File

@ -0,0 +1,65 @@
<?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;
/**
* SchedulerFilters.
*/
class SchedulerFilters extends QueryFilters
{
/**
* Filter based on search text.
*
* @param string query filter
* @return Builder
* @deprecated
*/
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();
}
}

View File

@ -12,6 +12,7 @@
namespace App\Http\Controllers;
use App\Factory\SchedulerFactory;
use App\Filters\SchedulerFilters;
use App\Http\Requests\TaskScheduler\DestroySchedulerRequest;
use App\Http\Requests\TaskScheduler\CreateSchedulerRequest;
use App\Http\Requests\TaskScheduler\ShowSchedulerRequest;
@ -58,9 +59,9 @@ class TaskSchedulerController extends BaseController
* ),
* )
*/
public function index()
public function index(SchedulerFilters $filters)
{
$schedulers = Scheduler::where('company_id', auth()->user()->company()->id);
$schedulers = Scheduler::filter($filters);
return $this->listResponse($schedulers);
}

View File

@ -32,7 +32,8 @@ use Illuminate\Database\Eloquent\SoftDeletes;
class Scheduler extends BaseModel
{
use SoftDeletes;
use Filterable;
protected $fillable = [
'name',
'frequency_id',

View File

@ -54,7 +54,31 @@ class SchedulerTest extends TestCase
);
}
public function testSchedulerGet3()
{
$scheduler = SchedulerFactory::create($this->company->id, $this->user->id);
$scheduler->name = "hello";
$scheduler->save();
$scheduler = SchedulerFactory::create($this->company->id, $this->user->id);
$scheduler->name = "goodbye";
$scheduler->save();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/task_schedulers?filter=hello');
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals('hello', $arr['data'][0]['name']);
$this->assertCount(1, $arr['data']);
}
public function testSchedulerGet2()
{