2022-05-19 00:34:09 +02:00
< ? php
2022-05-23 21:25:27 +02:00
/**
* Invoice Ninja ( https :// invoiceninja . com ) .
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @ copyright Copyright ( c ) 2023. Invoice Ninja LLC ( https :// invoiceninja . com )
2022-05-23 21:25:27 +02:00
*
* @ license https :// www . elastic . co / licensing / elastic - license
*/
2022-05-19 00:34:09 +02:00
namespace App\Http\Controllers ;
2023-01-13 02:43:38 +01:00
use App\Factory\SchedulerFactory ;
2023-02-16 02:36:09 +01:00
use App\Http\Requests\Task\DestroySchedulerRequest ;
2023-01-13 02:43:38 +01:00
use App\Http\Requests\TaskScheduler\CreateSchedulerRequest ;
use App\Http\Requests\TaskScheduler\ShowSchedulerRequest ;
use App\Http\Requests\TaskScheduler\StoreSchedulerRequest ;
use App\Http\Requests\TaskScheduler\UpdateSchedulerRequest ;
2022-05-19 00:34:09 +02:00
use App\Models\Scheduler ;
2023-01-13 02:43:38 +01:00
use App\Repositories\SchedulerRepository ;
use App\Transformers\SchedulerTransformer ;
use App\Utils\Traits\MakesHash ;
2022-05-19 00:34:09 +02:00
use Symfony\Component\HttpFoundation\Request ;
class TaskSchedulerController extends BaseController
{
2023-01-13 02:43:38 +01:00
use MakesHash ;
2022-06-21 11:57:17 +02:00
2023-01-13 02:43:38 +01:00
protected $entity_type = Scheduler :: class ;
2022-06-21 11:57:17 +02:00
2023-01-13 02:43:38 +01:00
protected $entity_transformer = SchedulerTransformer :: class ;
2022-05-26 04:18:53 +02:00
2023-01-13 02:43:38 +01:00
public function __construct ( protected SchedulerRepository $scheduler_repository )
2022-05-26 04:18:53 +02:00
{
parent :: __construct ();
}
2022-05-25 00:06:42 +02:00
2022-05-23 02:13:23 +02:00
/**
* @ OA\GET (
2023-01-13 02:43:38 +01:00
* path = " /api/v1/task_schedulers/ " ,
2022-05-23 02:13:23 +02:00
* operationId = " getTaskSchedulers " ,
2023-01-13 02:43:38 +01:00
* tags = { " task_schedulers " },
2022-05-23 02:13:23 +02:00
* summary = " Task Scheduler Index " ,
* description = " Get all schedulers with associated jobs " ,
* @ OA\Parameter ( ref = " #/components/parameters/X-Requested-With " ),
* @ OA\Response (
* response = 200 ,
* description = " success " ,
* @ 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\Response (
* response = " default " ,
* description = " Unexpected Error " ,
* @ OA\JsonContent ( ref = " #/components/schemas/Error " ),
* ),
* )
*/
2022-05-23 01:33:56 +02:00
public function index ()
2022-05-19 00:34:09 +02:00
{
2022-05-25 00:06:42 +02:00
$schedulers = Scheduler :: where ( 'company_id' , auth () -> user () -> company () -> id );
return $this -> listResponse ( $schedulers );
2022-05-23 01:33:56 +02:00
}
2022-05-19 00:34:09 +02:00
2023-01-13 02:43:38 +01:00
/**
* Show the form for creating a new resource .
*
* @ param CreateSchedulerRequest $request The request
*
* @ return Response
*
*
* @ OA\Get (
* path = " /api/v1/invoices/task_schedulers " ,
* operationId = " getTaskScheduler " ,
* tags = { " task_schedulers " },
* summary = " Gets a new blank scheduler object " ,
* description = " Returns a blank object with default values " ,
2023-02-10 10:21:10 +01:00
* @ OA\Parameter ( ref = " #/components/parameters/X-API-TOKEN " ),
2023-01-13 02:43:38 +01:00
* @ OA\Parameter ( ref = " #/components/parameters/X-Requested-With " ),
* @ OA\Parameter ( ref = " #/components/parameters/include " ),
* @ OA\Response (
* response = 200 ,
* description = " A blank scheduler object " ,
* @ 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/TaskSchedulerSchema " ),
* ),
* @ 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 create ( CreateSchedulerRequest $request )
{
$scheduler = SchedulerFactory :: create ( auth () -> user () -> company () -> id , auth () -> user () -> id );
return $this -> itemResponse ( $scheduler );
}
2022-05-23 01:33:56 +02:00
/**
2022-05-23 02:13:23 +02:00
* @ OA\Post (
2023-01-13 02:43:38 +01:00
* path = " /api/v1/task_schedulers/ " ,
2022-05-23 02:13:23 +02:00
* operationId = " createTaskScheduler " ,
2023-01-13 02:43:38 +01:00
* tags = { " task_schedulers " },
2022-05-23 02:13:23 +02:00
* summary = " Create task scheduler with job " ,
* description = " Create task scheduler with a job (action(job) request should be sent via request also. Example: We want client report to be job which will be run
* multiple times , we should send the same parameters in the request as we would send if we wanted to get report , see example " ,
2023-02-10 10:21:10 +01:00
* @ OA\Parameter ( ref = " #/components/parameters/X-API-SECRET " ),
2022-05-23 02:13:23 +02:00
* @ OA\Parameter ( ref = " #/components/parameters/X-Requested-With " ),
* @ OA\RequestBody (
* required = true ,
* @ OA\JsonContent ( ref = " #/components/schemas/TaskSchedulerSchema " )
* ),
* @ OA\Response (
* response = 200 ,
* description = " success " ,
* @ 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\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 " ),
* ),
* )
2022-05-23 01:33:56 +02:00
*/
2023-01-13 02:43:38 +01:00
public function store ( StoreSchedulerRequest $request )
2022-05-23 01:33:56 +02:00
{
2023-01-13 02:43:38 +01:00
$scheduler = $this -> scheduler_repository -> save ( $request -> all (), SchedulerFactory :: create ( auth () -> user () -> company () -> id , auth () -> user () -> id ));
2022-06-21 11:57:17 +02:00
2022-05-26 04:18:53 +02:00
return $this -> itemResponse ( $scheduler );
2022-05-19 00:34:09 +02:00
}
2022-05-23 02:13:23 +02:00
/**
* @ OA\GET (
2023-01-13 02:43:38 +01:00
* path = " /api/v1/task_schedulers/ { id} " ,
2022-05-23 02:13:23 +02:00
* operationId = " showTaskScheduler " ,
2023-01-13 02:43:38 +01:00
* tags = { " task_schedulers " },
2022-05-23 02:13:23 +02:00
* summary = " Show given scheduler " ,
* description = " Get scheduler with associated job " ,
* @ OA\Parameter ( ref = " #/components/parameters/X-Requested-With " ),
2022-05-27 04:17:06 +02:00
* @ OA\Parameter (
* name = " id " ,
* in = " path " ,
* description = " The Scheduler Hashed ID " ,
* example = " D2J234DFA " ,
* required = true ,
* @ OA\Schema (
* type = " string " ,
* format = " string " ,
* ),
* ),
2022-05-23 02:13:23 +02:00
* @ OA\Response (
* response = 200 ,
* description = " success " ,
* @ 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\Response (
* response = " default " ,
* description = " Unexpected Error " ,
* @ OA\JsonContent ( ref = " #/components/schemas/Error " ),
* ),
* )
*/
2023-01-13 02:43:38 +01:00
public function show ( ShowSchedulerRequest $request , Scheduler $scheduler )
2022-05-19 00:34:09 +02:00
{
2022-05-25 00:06:42 +02:00
return $this -> itemResponse ( $scheduler );
2022-05-23 01:33:56 +02:00
}
2022-05-19 00:34:09 +02:00
2022-05-23 01:33:56 +02:00
/**
2022-05-23 02:13:23 +02:00
* @ OA\PUT (
2023-01-13 02:43:38 +01:00
* path = " /api/v1/task_schedulers/ { id} " ,
2022-05-23 02:13:23 +02:00
* operationId = " updateTaskScheduler " ,
2023-01-13 02:43:38 +01:00
* tags = { " task_schedulers " },
2022-05-23 02:13:23 +02:00
* summary = " Update task scheduler " ,
* description = " Update task scheduler " ,
2023-02-10 10:21:10 +01:00
* @ OA\Parameter ( ref = " #/components/parameters/X-API-SECRET " ),
2022-05-23 02:13:23 +02:00
* @ OA\Parameter ( ref = " #/components/parameters/X-Requested-With " ),
2022-05-27 04:17:06 +02:00
* @ OA\Parameter (
* name = " id " ,
* in = " path " ,
* description = " The Scheduler Hashed ID " ,
* example = " D2J234DFA " ,
* required = true ,
* @ OA\Schema (
* type = " string " ,
* format = " string " ,
* ),
* ), * @ OA\RequestBody (
2022-05-23 02:13:23 +02:00
* required = true ,
2023-01-13 02:43:38 +01:00
* @ OA\JsonContent ( ref = " #/components/schemas/TaskSchedulerSchema " )
2022-05-23 02:13:23 +02:00
* ),
* @ OA\Response (
* response = 200 ,
* description = " success " ,
* @ 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\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 " ),
* ),
* )
2022-05-23 01:33:56 +02:00
*/
2023-01-13 02:43:38 +01:00
public function update ( UpdateSchedulerRequest $request , Scheduler $scheduler )
2022-05-23 01:33:56 +02:00
{
2023-01-13 02:43:38 +01:00
$this -> scheduler_repository -> save ( $request -> all (), $scheduler );
2022-06-21 11:57:17 +02:00
2022-05-26 04:18:53 +02:00
return $this -> itemResponse ( $scheduler );
2022-05-19 00:34:09 +02:00
}
2022-05-23 21:58:05 +02:00
2022-05-23 02:13:23 +02:00
/**
* @ OA\DELETE (
2023-01-13 02:43:38 +01:00
* path = " /api/v1/task_schedulers/ { id} " ,
2022-05-23 02:13:23 +02:00
* operationId = " destroyTaskScheduler " ,
2023-01-13 02:43:38 +01:00
* tags = { " task_schedulers " },
2022-05-23 02:13:23 +02:00
* summary = " Destroy Task Scheduler " ,
* description = " Destroy task scheduler and its associated job " ,
* @ OA\Parameter ( ref = " #/components/parameters/X-Requested-With " ),
2022-05-27 04:17:06 +02:00
* @ OA\Parameter (
* name = " id " ,
* in = " path " ,
* description = " The Scheduler Hashed ID " ,
* example = " D2J234DFA " ,
* required = true ,
* @ OA\Schema (
* type = " string " ,
* format = " string " ,
* ),
* ),
2022-05-23 02:13:23 +02:00
* @ OA\Response (
* response = 200 ,
* description = " success " ,
* @ 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\Response (
* response = " default " ,
* description = " Unexpected Error " ,
* @ OA\JsonContent ( ref = " #/components/schemas/Error " ),
* ),
* )
*/
2023-01-13 02:43:38 +01:00
public function destroy ( DestroySchedulerRequest $request , Scheduler $scheduler )
2022-05-19 00:34:09 +02:00
{
2022-05-26 04:18:53 +02:00
$this -> scheduler_repository -> delete ( $scheduler );
2022-06-21 11:57:17 +02:00
2022-05-26 04:18:53 +02:00
return $this -> itemResponse ( $scheduler -> fresh ());
2022-05-19 00:34:09 +02:00
}
2023-01-13 02:43:38 +01:00
/**
* Perform bulk actions on the list view .
*
* @ return Response
*
*
* @ OA\Post (
* path = " /api/v1/task_schedulers/bulk " ,
* operationId = " bulkTaskSchedulerActions " ,
* tags = { " task_schedulers " },
* summary = " Performs bulk actions on an array of task_schedulers " ,
* description = " " ,
2023-02-10 10:21:10 +01:00
* @ OA\Parameter ( ref = " #/components/parameters/X-API-TOKEN " ),
2023-01-13 02:43:38 +01:00
* @ OA\Parameter ( ref = " #/components/parameters/X-Requested-With " ),
* @ OA\Parameter ( ref = " #/components/parameters/index " ),
* @ OA\RequestBody (
* description = " array of ids " ,
* required = true ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema (
* type = " array " ,
* @ OA\Items (
* type = " integer " ,
* description = " Array of hashed IDs to be bulk 'actioned " ,
* example = " [0,1,2,3] " ,
* ),
* )
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " The TaskSchedule response " ,
* @ 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 " ),
2023-01-18 00:47:41 +01:00
* @ OA\JsonContent ( ref = " #/components/schemas/TaskSchedulerSchema " ),
2023-01-13 02:43:38 +01:00
* ),
* @ 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 bulk ()
{
$action = request () -> input ( 'action' );
2023-02-16 02:36:09 +01:00
if ( ! in_array ( $action , [ 'archive' , 'restore' , 'delete' ])) {
2023-01-13 02:43:38 +01:00
return response () -> json ([ 'message' => 'Bulk action does not exist' ], 400 );
2023-02-16 02:36:09 +01:00
}
2023-01-13 02:43:38 +01:00
$ids = request () -> input ( 'ids' );
$task_schedulers = Scheduler :: withTrashed () -> find ( $this -> transformKeys ( $ids ));
$task_schedulers -> each ( function ( $task_scheduler , $key ) use ( $action ) {
if ( auth () -> user () -> can ( 'edit' , $task_scheduler )) {
$this -> scheduler_repository -> { $action }( $task_scheduler );
}
});
return $this -> listResponse ( Scheduler :: withTrashed () -> whereIn ( 'id' , $this -> transformKeys ( $ids )));
}
2022-05-19 00:34:09 +02:00
}