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
*
* @ copyright Copyright ( c ) 2022. Invoice Ninja LLC ( https :// invoiceninja . com )
*
* @ license https :// www . elastic . co / licensing / elastic - license
*/
2022-05-19 00:34:09 +02:00
namespace App\Http\Controllers ;
use App\Http\Requests\TaskScheduler\CreateScheduledTaskRequest ;
2022-05-23 01:33:56 +02:00
use App\Http\Requests\TaskScheduler\UpdateScheduledJobRequest ;
use App\Http\Requests\TaskScheduler\UpdateScheduleRequest ;
2022-05-25 00:06:42 +02:00
use App\Jobs\Ninja\TaskScheduler ;
2022-05-19 00:34:09 +02:00
use App\Jobs\Report\ProfitAndLoss ;
use App\Models\ScheduledJob ;
use App\Models\Scheduler ;
2022-05-26 04:32:37 +02:00
use App\Repositories\TaskSchedulerRepository ;
2022-05-25 00:06:42 +02:00
use App\Transformers\TaskSchedulerTransformer ;
2022-05-19 00:34:09 +02:00
use Carbon\Carbon ;
2022-05-23 01:33:56 +02:00
use Illuminate\Database\Eloquent\Model ;
2022-05-19 00:34:09 +02:00
use Symfony\Component\HttpFoundation\Request ;
class TaskSchedulerController extends BaseController
{
2022-05-25 00:06:42 +02:00
protected $entity_type = TaskScheduler :: class ;
protected $entity_transformer = TaskSchedulerTransformer :: class ;
2022-05-26 04:32:37 +02:00
protected TaskSchedulerRepository $scheduler_repository ;
2022-05-26 04:18:53 +02:00
2022-05-26 04:32:37 +02:00
public function __construct ( TaskSchedulerRepository $scheduler_repository )
2022-05-26 04:18:53 +02:00
{
parent :: __construct ();
$this -> scheduler_repository = $scheduler_repository ;
}
2022-05-25 00:06:42 +02:00
2022-05-23 02:13:23 +02:00
/**
* @ OA\GET (
* path = " /api/v1/task_scheduler/ " ,
* operationId = " getTaskSchedulers " ,
* tags = { " task_scheduler " },
* summary = " Task Scheduler Index " ,
* description = " Get all schedulers with associated jobs " ,
* @ OA\Parameter ( ref = " #/components/parameters/X-Api-Secret " ),
* @ 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
2022-05-23 01:33:56 +02:00
/**
2022-05-23 02:13:23 +02:00
* @ OA\Post (
* path = " /api/v1/task_scheduler/ " ,
* operationId = " createTaskScheduler " ,
* tags = { " task_scheduler " },
* 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 " ,
* @ OA\Parameter ( ref = " #/components/parameters/X-Api-Secret " ),
* @ 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
*/
2022-05-23 02:13:23 +02:00
2022-05-23 01:33:56 +02:00
public function store ( CreateScheduledTaskRequest $request )
{
$scheduler = new Scheduler ();
2022-05-26 04:18:53 +02:00
$scheduler -> service () -> store ( $scheduler , $request );
return $this -> itemResponse ( $scheduler );
2022-05-19 00:34:09 +02:00
}
2022-05-23 02:13:23 +02:00
/**
* @ OA\GET (
2022-05-27 04:17:06 +02:00
* path = " /api/v1/task_scheduler/ { id} " ,
2022-05-23 02:13:23 +02:00
* operationId = " showTaskScheduler " ,
* tags = { " task_scheduler " },
* summary = " Show given scheduler " ,
* description = " Get scheduler with associated job " ,
* @ OA\Parameter ( ref = " #/components/parameters/X-Api-Secret " ),
* @ 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 " ),
* ),
* )
*/
2022-05-25 00:06:42 +02:00
public function show ( 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 (
2022-05-27 04:17:06 +02:00
* path = " /api/v1/task_scheduler/ { id} " ,
2022-05-23 02:13:23 +02:00
* operationId = " updateTaskScheduler " ,
* tags = { " task_scheduler " },
* summary = " Update task scheduler " ,
* description = " Update task scheduler " ,
* @ OA\Parameter ( ref = " #/components/parameters/X-Api-Secret " ),
* @ 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 ,
* @ OA\JsonContent ( ref = " #/components/schemas/UpdateTaskSchedulerSchema " )
* ),
* @ 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
*/
public function update ( Scheduler $scheduler , UpdateScheduleRequest $request )
{
2022-05-26 04:18:53 +02:00
$scheduler -> service () -> update ( $scheduler , $request );
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\PUT (
2022-05-27 04:17:06 +02:00
* path = " /api/v1/task_scheduler/ { id}/update_job/ " ,
2022-05-23 02:13:23 +02:00
* operationId = " updateTaskSchedulerJob " ,
* tags = { " task_scheduler " },
* summary = " Update job for a task scheduler " ,
* description = " Update job for a task scheduler | if we are changing action for a job, we should send the request for a new job same as we are creating new scheduler " ,
* @ OA\Parameter ( ref = " #/components/parameters/X-Api-Secret " ),
* @ 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\RequestBody (
* required = true ,
* @ OA\JsonContent ( ref = " #/components/schemas/UpdateJobForASchedulerSchema " )
* ),
* @ 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
public function updateJob ( Scheduler $scheduler , UpdateScheduledJobRequest $request )
2022-05-19 00:34:09 +02:00
{
2022-05-26 04:18:53 +02:00
$scheduler -> service () -> updateJob ( $scheduler , $request );
return $this -> itemResponse ( $scheduler );
2022-05-19 00:34:09 +02:00
}
2022-05-23 02:13:23 +02:00
/**
* @ OA\DELETE (
2022-05-27 04:17:06 +02:00
* path = " /api/v1/task_scheduler/ { id} " ,
2022-05-23 02:13:23 +02:00
* operationId = " destroyTaskScheduler " ,
* tags = { " task_scheduler " },
* summary = " Destroy Task Scheduler " ,
* description = " Destroy task scheduler and its associated job " ,
* @ OA\Parameter ( ref = " #/components/parameters/X-Api-Secret " ),
* @ 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 " ),
* ),
* )
*/
2022-05-23 01:33:56 +02:00
public function destroy ( Scheduler $scheduler )
2022-05-19 00:34:09 +02:00
{
2022-05-26 04:18:53 +02:00
$this -> scheduler_repository -> delete ( $scheduler );
return $this -> itemResponse ( $scheduler -> fresh ());
2022-05-19 00:34:09 +02:00
}
}