1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Controllers/TaskSchedulerController.php

111 lines
3.2 KiB
PHP
Raw Normal View History

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-26 08:21:23 +01:00
use App\Filters\SchedulerFilters;
2023-01-13 02:43:38 +01:00
use App\Http\Requests\TaskScheduler\CreateSchedulerRequest;
2023-03-18 08:24:56 +01:00
use App\Http\Requests\TaskScheduler\DestroySchedulerRequest;
2023-01-13 02:43:38 +01:00
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
class TaskSchedulerController extends BaseController
{
2023-01-13 02:43:38 +01:00
use MakesHash;
2023-01-13 02:43:38 +01:00
protected $entity_type = Scheduler::class;
2023-01-13 02:43:38 +01:00
protected $entity_transformer = SchedulerTransformer::class;
2023-01-13 02:43:38 +01:00
public function __construct(protected SchedulerRepository $scheduler_repository)
{
parent::__construct();
}
2022-05-25 00:06:42 +02:00
2023-02-26 08:21:23 +01:00
public function index(SchedulerFilters $filters)
2022-05-19 00:34:09 +02:00
{
2023-02-26 08:21:23 +01:00
$schedulers = Scheduler::filter($filters);
2022-05-25 00:06:42 +02:00
return $this->listResponse($schedulers);
}
2022-05-19 00:34:09 +02:00
2023-01-13 02:43:38 +01:00
public function create(CreateSchedulerRequest $request)
{
2024-01-14 05:51:31 +01:00
/** @var \App\Models\User $user */
$user = auth()->user();
$scheduler = SchedulerFactory::create($user->company()->id, auth()->user()->id);
2023-01-13 02:43:38 +01:00
return $this->itemResponse($scheduler);
}
public function store(StoreSchedulerRequest $request)
{
2024-01-14 05:51:31 +01:00
/** @var \App\Models\User $user */
$user = auth()->user();
$scheduler = $this->scheduler_repository->save($request->all(), SchedulerFactory::create($user->company()->id, auth()->user()->id));
return $this->itemResponse($scheduler);
2022-05-19 00:34:09 +02:00
}
2023-05-18 00:31:21 +02:00
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-19 00:34:09 +02:00
2023-01-13 02:43:38 +01:00
public function update(UpdateSchedulerRequest $request, Scheduler $scheduler)
{
2023-01-13 02:43:38 +01:00
$this->scheduler_repository->save($request->all(), $scheduler);
return $this->itemResponse($scheduler);
2022-05-19 00:34:09 +02:00
}
2023-01-13 02:43:38 +01:00
public function destroy(DestroySchedulerRequest $request, Scheduler $scheduler)
2022-05-19 00:34:09 +02:00
{
$this->scheduler_repository->delete($scheduler);
return $this->itemResponse($scheduler->fresh());
2022-05-19 00:34:09 +02:00
}
2023-01-13 02:43:38 +01:00
public function bulk()
{
2024-01-14 05:51:31 +01:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-01-13 02:43:38 +01:00
$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));
2024-01-14 05:51:31 +01:00
$task_schedulers->each(function ($task_scheduler, $key) use ($action, $user) {
if ($user->can('edit', $task_scheduler)) {
2023-01-13 02:43:38 +01:00
$this->scheduler_repository->{$action}($task_scheduler);
}
});
return $this->listResponse(Scheduler::withTrashed()->whereIn('id', $this->transformKeys($ids)));
}
2022-05-19 00:34:09 +02:00
}