mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
INA-5 | Refactor of service, removed references for SchedulerJob
This commit is contained in:
parent
f2d1ef6bbb
commit
876cb3d93c
@ -32,7 +32,6 @@ use App\Http\Requests\TaskScheduler\UpdateScheduledJobRequest;
|
|||||||
use App\Http\Requests\TaskScheduler\UpdateScheduleRequest;
|
use App\Http\Requests\TaskScheduler\UpdateScheduleRequest;
|
||||||
use App\Jobs\Report\ProfitAndLoss;
|
use App\Jobs\Report\ProfitAndLoss;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
use App\Models\ScheduledJob;
|
|
||||||
use App\Models\Scheduler;
|
use App\Models\Scheduler;
|
||||||
use App\Utils\Ninja;
|
use App\Utils\Ninja;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
@ -51,128 +50,116 @@ class TaskSchedulerService
|
|||||||
|
|
||||||
public function store(Scheduler $scheduler, CreateScheduledTaskRequest $request)
|
public function store(Scheduler $scheduler, CreateScheduledTaskRequest $request)
|
||||||
{
|
{
|
||||||
|
$scheduler->action_name = $request->job;
|
||||||
$scheduler->paused = $request->get('paused', false);
|
$scheduler->paused = $request->get('paused', false);
|
||||||
$scheduler->start_from = $request->get('start_from') ? Carbon::parse((int)$request->get('start_from')) : Carbon::now();
|
$scheduler->start_from = $request->get('start_from') ? Carbon::parse((int)$request->get('start_from')) : Carbon::now();
|
||||||
$scheduler->repeat_every = $request->get('repeat_every');
|
$scheduler->repeat_every = $request->get('repeat_every');
|
||||||
$scheduler->scheduled_run = $request->get('start_from') ? Carbon::parse((int)$request->get('start_from')) : Carbon::now();;
|
$scheduler->scheduled_run = $request->get('start_from') ? Carbon::parse((int)$request->get('start_from')) : Carbon::now();;
|
||||||
$scheduler->company_id = auth()->user()->company()->id;
|
$scheduler->company_id = auth()->user()->company()->id;
|
||||||
|
$scheduler = $this->setJobParameters($scheduler, $request);
|
||||||
$scheduler->save();
|
$scheduler->save();
|
||||||
|
|
||||||
$this->createJob($request, $scheduler);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(Scheduler $scheduler, UpdateScheduleRequest $request)
|
public function update(Scheduler $scheduler, UpdateScheduleRequest $request)
|
||||||
{
|
{
|
||||||
|
if (array_key_exists('job', $request->all())) {
|
||||||
$data = $request->validated();
|
$scheduler->action_name = $request->get('job');
|
||||||
|
$scheduler = $this->setJobParameters($scheduler, $request);
|
||||||
$update = $this->scheduler->update($data);
|
|
||||||
if ($update) {
|
|
||||||
return response(['successfully_updated_scheduler'], 200);
|
|
||||||
}
|
}
|
||||||
return response(['failed_to_update_scheduler'], 400);
|
$data = $request->validated();
|
||||||
}
|
$update = $this->scheduler->update($data);
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function runValidation($form_request, $data)
|
private function runValidation($form_request, $data)
|
||||||
{
|
{
|
||||||
$_syn_request_class = new $form_request();
|
$_syn_request_class = new $form_request();
|
||||||
$_syn_request_class->setContainer(app());
|
$_syn_request_class->setContainer(app());
|
||||||
$_syn_request_class->initialize($data);
|
$_syn_request_class->initialize($data);
|
||||||
$_syn_request_class->prepareForValidation();
|
$_syn_request_class->prepareForValidation();
|
||||||
$_syn_request_class->setValidator(Validator::make($_syn_request_class->all(), $_syn_request_class->rules()));
|
$_syn_request_class->setValidator(Validator::make($_syn_request_class->all(), $_syn_request_class->rules()));
|
||||||
|
|
||||||
return $_syn_request_class->validated();
|
return $_syn_request_class->validated();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setJobParameters(ScheduledJob $job, $request): ScheduledJob
|
public function setJobParameters(Scheduler $scheduler, $request)
|
||||||
{
|
{
|
||||||
switch ($request->job) {
|
switch ($scheduler->action_name) {
|
||||||
case ScheduledJob::CREATE_CLIENT_REPORT:
|
case Scheduler::CREATE_CLIENT_REPORT:
|
||||||
$job->action_name = ScheduledJob::CREATE_CLIENT_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_CLIENT_REPORT;
|
||||||
$job->action_class = $this->getClassPath(ClientExport::class);
|
$scheduler->action_class = $this->getClassPath(ClientExport::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_CLIENT_CONTACT_REPORT:
|
case Scheduler::CREATE_CLIENT_CONTACT_REPORT:
|
||||||
|
$scheduler->action_name = Scheduler::CREATE_CLIENT_CONTACT_REPORT;
|
||||||
|
$scheduler->action_class = $this->getClassPath(ContactExport::class);
|
||||||
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
|
break;
|
||||||
|
case Scheduler::CREATE_CREDIT_REPORT:
|
||||||
|
|
||||||
$job->action_name = ScheduledJob::CREATE_CLIENT_CONTACT_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_CREDIT_REPORT;
|
||||||
$job->action_class = $this->getClassPath(ContactExport::class);
|
$scheduler->action_class = $this->getClassPath(CreditExport::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_CREDIT_REPORT:
|
case Scheduler::CREATE_DOCUMENT_REPORT:
|
||||||
|
$scheduler->action_name = Scheduler::CREATE_DOCUMENT_REPORT;
|
||||||
$job->action_name = ScheduledJob::CREATE_CREDIT_REPORT;
|
$scheduler->action_class = $this->getClassPath(DocumentExport::class);
|
||||||
$job->action_class = $this->getClassPath(CreditExport::class);
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_DOCUMENT_REPORT:
|
case Scheduler::CREATE_EXPENSE_REPORT:
|
||||||
$job->action_name = ScheduledJob::CREATE_DOCUMENT_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_EXPENSE_REPORT;
|
||||||
$job->action_class = $this->getClassPath(DocumentExport::class);
|
$scheduler->action_class = $this->getClassPath(ExpenseExport::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_EXPENSE_REPORT:
|
case Scheduler::CREATE_INVOICE_ITEM_REPORT:
|
||||||
$job->action_name = ScheduledJob::CREATE_EXPENSE_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_INVOICE_ITEM_REPORT;
|
||||||
$job->action_class = $this->getClassPath(ExpenseExport::class);
|
$scheduler->action_class = $this->getClassPath(InvoiceItemExport::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_INVOICE_ITEM_REPORT:
|
case Scheduler::CREATE_INVOICE_REPORT:
|
||||||
$job->action_name = ScheduledJob::CREATE_INVOICE_ITEM_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_INVOICE_REPORT;
|
||||||
$job->action_class = $this->getClassPath(InvoiceItemExport::class);
|
$scheduler->action_class = $this->getClassPath(InvoiceExport::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_INVOICE_REPORT:
|
case Scheduler::CREATE_PAYMENT_REPORT:
|
||||||
$job->action_name = ScheduledJob::CREATE_INVOICE_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_PAYMENT_REPORT;
|
||||||
$job->action_class = $this->getClassPath(InvoiceExport::class);
|
$scheduler->action_class = $this->getClassPath(PaymentExport::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_PAYMENT_REPORT:
|
case Scheduler::CREATE_PRODUCT_REPORT:
|
||||||
$job->action_name = ScheduledJob::CREATE_PAYMENT_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_PRODUCT_REPORT;
|
||||||
$job->action_class = $this->getClassPath(PaymentExport::class);
|
$scheduler->action_class = $this->getClassPath(ProductExport::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_PRODUCT_REPORT:
|
case Scheduler::CREATE_PROFIT_AND_LOSS_REPORT:
|
||||||
$job->action_name = ScheduledJob::CREATE_PRODUCT_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_PROFIT_AND_LOSS_REPORT;
|
||||||
$job->action_class = $this->getClassPath(ProductExport::class);
|
$scheduler->action_class = $this->getClassPath(ProfitAndLoss::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_PROFIT_AND_LOSS_REPORT:
|
case Scheduler::CREATE_QUOTE_ITEM_REPORT:
|
||||||
$job->action_name = ScheduledJob::CREATE_PROFIT_AND_LOSS_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_QUOTE_ITEM_REPORT;
|
||||||
$job->action_class = $this->getClassPath(ProfitAndLoss::class);
|
$scheduler->action_class = $this->getClassPath(QuoteItemExport::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_QUOTE_ITEM_REPORT:
|
case Scheduler::CREATE_QUOTE_REPORT:
|
||||||
$job->action_name = ScheduledJob::CREATE_QUOTE_ITEM_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_QUOTE_REPORT;
|
||||||
$job->action_class = $this->getClassPath(QuoteItemExport::class);
|
$scheduler->action_class = $this->getClassPath(QuoteExport::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_QUOTE_REPORT:
|
case Scheduler::CREATE_RECURRING_INVOICE_REPORT:
|
||||||
$job->action_name = ScheduledJob::CREATE_QUOTE_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_RECURRING_INVOICE_REPORT;
|
||||||
$job->action_class = $this->getClassPath(QuoteExport::class);
|
$scheduler->action_class = $this->getClassPath(RecurringInvoiceExport::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
break;
|
||||||
case ScheduledJob::CREATE_RECURRING_INVOICE_REPORT:
|
case Scheduler::CREATE_TASK_REPORT:
|
||||||
$job->action_name = ScheduledJob::CREATE_RECURRING_INVOICE_REPORT;
|
$scheduler->action_name = Scheduler::CREATE_TASK_REPORT;
|
||||||
$job->action_class = $this->getClassPath(RecurringInvoiceExport::class);
|
$scheduler->action_class = $this->getClassPath(TaskExport::class);
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
$scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
||||||
break;
|
|
||||||
case ScheduledJob::CREATE_TASK_REPORT:
|
|
||||||
$job->action_name = ScheduledJob::CREATE_TASK_REPORT;
|
|
||||||
$job->action_class = $this->getClassPath(TaskExport::class);
|
|
||||||
$job->parameters = $this->runValidation(GenericReportRequest::class, $request->all());
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
return $job;
|
|
||||||
|
return $scheduler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getClassPath($class): string
|
public function getClassPath($class): string
|
||||||
@ -180,15 +167,9 @@ class TaskSchedulerService
|
|||||||
return $class = is_object($class) ? get_class($class) : $class;
|
return $class = is_object($class) ? get_class($class) : $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function updateJob(Scheduler $scheduler, UpdateScheduledJobRequest $request)
|
public function updateJob(Scheduler $scheduler, UpdateScheduledJobRequest $request)
|
||||||
{
|
{
|
||||||
$job = $scheduler->job;
|
$scheduler = $this->setJobParameters($scheduler, $request);
|
||||||
if (!$job) {
|
$scheduler->save();
|
||||||
return abort(404);
|
|
||||||
}
|
|
||||||
$job = $this->setJobParameters($job, $request);
|
|
||||||
$job->save();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user