mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-13 06:32:40 +01:00
INA-5 | Moved Actions to the service | Controller clean-up
This commit is contained in:
parent
be5439c1ec
commit
6aa01001c9
@ -3,174 +3,59 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Export\CSV\ClientExport;
|
||||
use App\Export\CSV\ContactExport;
|
||||
use App\Export\CSV\CreditExport;
|
||||
use App\Export\CSV\DocumentExport;
|
||||
use App\Export\CSV\ExpenseExport;
|
||||
use App\Export\CSV\InvoiceExport;
|
||||
use App\Export\CSV\InvoiceItemExport;
|
||||
use App\Export\CSV\PaymentExport;
|
||||
use App\Export\CSV\ProductExport;
|
||||
use App\Export\CSV\QuoteExport;
|
||||
use App\Export\CSV\QuoteItemExport;
|
||||
use App\Export\CSV\RecurringInvoiceExport;
|
||||
use App\Export\CSV\TaskExport;
|
||||
use App\Http\Requests\Report\GenericReportRequest;
|
||||
use App\Http\Requests\Report\ProfitLossRequest;
|
||||
use App\Http\Requests\TaskScheduler\CreateScheduledTaskRequest;
|
||||
use App\Http\Requests\TaskScheduler\UpdateScheduledJobRequest;
|
||||
use App\Http\Requests\TaskScheduler\UpdateScheduleRequest;
|
||||
use App\Jobs\Report\ProfitAndLoss;
|
||||
use App\Models\ScheduledJob;
|
||||
use App\Models\Scheduler;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class TaskSchedulerController extends BaseController
|
||||
{
|
||||
|
||||
public function index()
|
||||
{
|
||||
return Scheduler::all();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \App\Http\Requests\TaskScheduler\CreateScheduledTaskRequest $request
|
||||
*/
|
||||
public function store(CreateScheduledTaskRequest $request)
|
||||
{
|
||||
|
||||
$scheduler = new Scheduler();
|
||||
$scheduler->paused = $request->get('paused', false);
|
||||
$scheduler->archived = (bool)$request->get('archived', false);
|
||||
$scheduler->start_from = $request->get('start_from') ? Carbon::parse((int)$request->get('start_from')) : Carbon::now();
|
||||
$scheduler->repeat_every = $request->get('repeat_every');
|
||||
$scheduler->scheduled_run = $request->get('start_from') ? Carbon::parse((int)$request->get('start_from')) : Carbon::now();;
|
||||
$scheduler->save();
|
||||
|
||||
if ($this->createJob($request, $scheduler)) {
|
||||
$job = ScheduledJob::query()->latest()->first();
|
||||
}
|
||||
return $job;
|
||||
return $scheduler->service()->store($scheduler, $request);
|
||||
}
|
||||
|
||||
public function createJob(CreateScheduledTaskRequest $request, Scheduler $scheduler): bool
|
||||
public function show(Scheduler $scheduler): Scheduler
|
||||
{
|
||||
$job = new ScheduledJob();
|
||||
return $scheduler;
|
||||
}
|
||||
|
||||
switch ($request->job) {
|
||||
case 'client_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
//custom rules for example here we require date_range but in genericRequest class we don't
|
||||
$rules['date_range'] = 'string|required';
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_CLIENT_REPORT;
|
||||
$job->action_class = $this->getClassPath(ClientExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'client_contact_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_CLIENT_CONTACT_REPORT;
|
||||
$job->action_class = $this->getClassPath(ContactExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'credit_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_CREDIT_REPORT;
|
||||
$job->action_class = $this->getClassPath(CreditExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'document_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_DOCUMENT_REPORT;
|
||||
$job->action_class = $this->getClassPath(DocumentExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'expense_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_EXPENSE_REPORT;
|
||||
$job->action_class = $this->getClassPath(ExpenseExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'invoice_item_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_INVOICE_ITEM_REPORT;
|
||||
$job->action_class = $this->getClassPath(InvoiceItemExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'invoice_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_INVOICE_REPORT;
|
||||
$job->action_class = $this->getClassPath(InvoiceExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'payment_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_PAYMENT_REPORT;
|
||||
$job->action_class = $this->getClassPath(PaymentExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'product_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_PRODUCT_REPORT;
|
||||
$job->action_class = $this->getClassPath(ProductExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'profit_and_loss_report':
|
||||
$rules = (new ProfitLossRequest())->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_PROFIT_AND_LOSS_REPORT;
|
||||
$job->action_class = $this->getClassPath(ProfitAndLoss::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'quote_item_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_QUOTE_ITEM_REPORT;
|
||||
$job->action_class = $this->getClassPath(QuoteItemExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'quote_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_QUOTE_REPORT;
|
||||
$job->action_class = $this->getClassPath(QuoteExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'recurring_invoice_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_RECURRING_INVOICE_REPORT;
|
||||
$job->action_class = $this->getClassPath(RecurringInvoiceExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
case 'task_report':
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validated = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_TASK_REPORT;
|
||||
$job->action_class = $this->getClassPath(TaskExport::class);
|
||||
$job->parameters = $this->saveActionParameters($rules, $request);
|
||||
break;
|
||||
/**
|
||||
* @param \App\Models\Scheduler $scheduler
|
||||
* @param \App\Http\Requests\TaskScheduler\UpdateScheduleRequest $request
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Scheduler $scheduler, UpdateScheduleRequest $request)
|
||||
{
|
||||
return $scheduler->service()->update($scheduler, $request);
|
||||
}
|
||||
|
||||
}
|
||||
$job->scheduler_id = $scheduler->id;
|
||||
$job->company_id = auth()->user()->company()->id;
|
||||
return $job->save();
|
||||
public function updateJob(Scheduler $scheduler, UpdateScheduledJobRequest $request)
|
||||
{
|
||||
return $scheduler->service()->updateJob($scheduler, $request);
|
||||
|
||||
}
|
||||
|
||||
public function getClassPath($class): string
|
||||
{
|
||||
return $class = is_object($class) ? get_class($class) : $class;
|
||||
}
|
||||
|
||||
public function saveActionParameters(array $rules, $request): array
|
||||
public function destroy(Scheduler $scheduler)
|
||||
{
|
||||
$parameters = [];
|
||||
foreach ($rules as $rule => $key) {
|
||||
if (isset($request->{$rule})) {
|
||||
$parameters[$rule] = $request->{$rule};
|
||||
}
|
||||
}
|
||||
return $parameters;
|
||||
return $scheduler->service()->destroy($scheduler);
|
||||
}
|
||||
|
||||
|
||||
|
216
app/Services/TaskScheduler/TaskSchedulerService.php
Normal file
216
app/Services/TaskScheduler/TaskSchedulerService.php
Normal file
@ -0,0 +1,216 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Services\TaskScheduler;
|
||||
|
||||
|
||||
use App\Export\CSV\ClientExport;
|
||||
use App\Export\CSV\ContactExport;
|
||||
use App\Export\CSV\CreditExport;
|
||||
use App\Export\CSV\DocumentExport;
|
||||
use App\Export\CSV\ExpenseExport;
|
||||
use App\Export\CSV\InvoiceExport;
|
||||
use App\Export\CSV\InvoiceItemExport;
|
||||
use App\Export\CSV\PaymentExport;
|
||||
use App\Export\CSV\ProductExport;
|
||||
use App\Export\CSV\QuoteExport;
|
||||
use App\Export\CSV\QuoteItemExport;
|
||||
use App\Export\CSV\RecurringInvoiceExport;
|
||||
use App\Export\CSV\TaskExport;
|
||||
use App\Http\Requests\Report\GenericReportRequest;
|
||||
use App\Http\Requests\Report\ProfitLossRequest;
|
||||
use App\Http\Requests\TaskScheduler\CreateScheduledTaskRequest;
|
||||
use App\Http\Requests\TaskScheduler\UpdateScheduledJobRequest;
|
||||
use App\Http\Requests\TaskScheduler\UpdateScheduleRequest;
|
||||
use App\Jobs\Report\ProfitAndLoss;
|
||||
use App\Models\ScheduledJob;
|
||||
use App\Models\Scheduler;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class TaskSchedulerService
|
||||
{
|
||||
public Scheduler $scheduler;
|
||||
|
||||
public function __construct(Scheduler $scheduler)
|
||||
{
|
||||
$this->scheduler = $scheduler;
|
||||
}
|
||||
|
||||
public function store(Scheduler $scheduler, CreateScheduledTaskRequest $request)
|
||||
{
|
||||
$scheduler->paused = $request->get('paused', false);
|
||||
$scheduler->archived = (bool)$request->get('archived', false);
|
||||
$scheduler->start_from = $request->get('start_from') ? Carbon::parse((int)$request->get('start_from')) : Carbon::now();
|
||||
$scheduler->repeat_every = $request->get('repeat_every');
|
||||
$scheduler->scheduled_run = $request->get('start_from') ? Carbon::parse((int)$request->get('start_from')) : Carbon::now();;
|
||||
$scheduler->save();
|
||||
|
||||
if ($this->createJob($request, $scheduler)) {
|
||||
return response(['job_has_been_created'], 200);
|
||||
}
|
||||
return response(['failed_to_create_job'], 400);
|
||||
}
|
||||
|
||||
public function update(Scheduler $scheduler, UpdateScheduleRequest $request)
|
||||
{
|
||||
$data = $request->validated();
|
||||
if ($request->has('start_from')) {
|
||||
$data['start_from'] = Carbon::parse((int)$request->get('start_from'));
|
||||
$data['scheduled_run'] = Carbon::parse((int)$request->get('start_from'));
|
||||
}
|
||||
$update = $this->scheduler->update($data);
|
||||
if ($update) {
|
||||
return response(['successfully_updated_scheduler']);
|
||||
}
|
||||
return response(['failed_to_update_scheduler'], 400);
|
||||
}
|
||||
|
||||
public function createJob(CreateScheduledTaskRequest $request, Scheduler $scheduler): bool
|
||||
{
|
||||
$job = new ScheduledJob();
|
||||
$job = $this->setJobParameters($job, $request);
|
||||
$job->scheduler_id = $scheduler->id;
|
||||
$job->company_id = auth()->user()->company()->id;
|
||||
return $job->save();
|
||||
|
||||
}
|
||||
|
||||
public function setJobParameters(ScheduledJob $job, $request): ScheduledJob
|
||||
{
|
||||
switch ($request->job) {
|
||||
case ScheduledJob::CREATE_CLIENT_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
//custom rules for example here we require date_range but in genericRequest class we don't
|
||||
$rules['date_range'] = 'string|required';
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_CLIENT_REPORT;
|
||||
$job->action_class = $this->getClassPath(ClientExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_CLIENT_CONTACT_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_CLIENT_CONTACT_REPORT;
|
||||
$job->action_class = $this->getClassPath(ContactExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_CREDIT_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_CREDIT_REPORT;
|
||||
$job->action_class = $this->getClassPath(CreditExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_DOCUMENT_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_DOCUMENT_REPORT;
|
||||
$job->action_class = $this->getClassPath(DocumentExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_EXPENSE_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_EXPENSE_REPORT;
|
||||
$job->action_class = $this->getClassPath(ExpenseExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_INVOICE_ITEM_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_INVOICE_ITEM_REPORT;
|
||||
$job->action_class = $this->getClassPath(InvoiceItemExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_INVOICE_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_INVOICE_REPORT;
|
||||
$job->action_class = $this->getClassPath(InvoiceExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_PAYMENT_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_PAYMENT_REPORT;
|
||||
$job->action_class = $this->getClassPath(PaymentExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_PRODUCT_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_PRODUCT_REPORT;
|
||||
$job->action_class = $this->getClassPath(ProductExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_PROFIT_AND_LOSS_REPORT:
|
||||
$rules = (new ProfitLossRequest())->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_PROFIT_AND_LOSS_REPORT;
|
||||
$job->action_class = $this->getClassPath(ProfitAndLoss::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_QUOTE_ITEM_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_QUOTE_ITEM_REPORT;
|
||||
$job->action_class = $this->getClassPath(QuoteItemExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_QUOTE_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_QUOTE_REPORT;
|
||||
$job->action_class = $this->getClassPath(QuoteExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_RECURRING_INVOICE_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_RECURRING_INVOICE_REPORT;
|
||||
$job->action_class = $this->getClassPath(RecurringInvoiceExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
case ScheduledJob::CREATE_TASK_REPORT:
|
||||
$rules = (new GenericReportRequest)->rules();
|
||||
$validatedJobData = $request->validate($rules);
|
||||
$job->action_name = ScheduledJob::CREATE_TASK_REPORT;
|
||||
$job->action_class = $this->getClassPath(TaskExport::class);
|
||||
$job->parameters = $validatedJobData;
|
||||
break;
|
||||
|
||||
}
|
||||
return $job;
|
||||
}
|
||||
|
||||
public function getClassPath($class): string
|
||||
{
|
||||
return $class = is_object($class) ? get_class($class) : $class;
|
||||
}
|
||||
|
||||
public function destroy(Scheduler $scheduler)
|
||||
{
|
||||
$job = $scheduler->job;
|
||||
if ($job) {
|
||||
$job->delete();
|
||||
}
|
||||
$scheduler->delete();
|
||||
return response(['successfully_deleted_scheduler_and_job_associated_to_him']);
|
||||
}
|
||||
|
||||
public function updateJob(Scheduler $scheduler, UpdateScheduledJobRequest $request)
|
||||
{
|
||||
$job = $scheduler->job;
|
||||
if (!$job) {
|
||||
return abort(404);
|
||||
}
|
||||
$job = $this->setJobParameters($job, $request);
|
||||
$job->save();
|
||||
|
||||
return response('job_successfully_updated');
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user