diff --git a/app/Http/Controllers/OpenAPI/TaskSchedulerSchema.php b/app/Http/Controllers/OpenAPI/TaskSchedulerSchema.php index ead95fc277d4..98459ba68542 100644 --- a/app/Http/Controllers/OpenAPI/TaskSchedulerSchema.php +++ b/app/Http/Controllers/OpenAPI/TaskSchedulerSchema.php @@ -10,11 +10,12 @@ * @OA\Property(property="paused",type="boolean",example="false",description="The scheduler paused state"), * @OA\Property(property="repeat_every",type="string",example="DAY",description="Accepted values (DAY,WEEK,MONTH,3MONTHS,YEAR)"), * @OA\Property(property="start_from",type="integer",example="1652898504",description="Timestamp when we should start the scheduler, default is today"), - * @OA\Property(property="date_range", type="string", example="last7", description="The string representation of the date range of data to be returned"), - * @OA\Property(property="date_key", type="string", example="created_at", description="The date column to search between."), - * @OA\Property(property="start_date", type="string", example="2022-10-31", description="The start date to search between"), - * @OA\Property(property="end_date", type="string", example="2022-10-31", description="The end date to search between"), - * @OA\Property( + * @OA\Property(property="job",type="string",example="create_credit_report",description="Job, we can find list of available jobs in Scheduler model"), + * @OA\Property(property="date_range", type="string", example="last7", description="The string representation of the date range of data to be returned"), + * @OA\Property(property="date_key", type="string", example="created_at", description="The date column to search between."), + * @OA\Property(property="start_date", type="string", example="2022-10-31", description="The start date to search between"), + * @OA\Property(property="end_date", type="string", example="2022-10-31", description="The end date to search between"), + * @OA\Property( * property="report_keys", * type="array", * @OA\Items( @@ -33,10 +34,14 @@ * schema="UpdateTaskSchedulerSchema", * type="object", * - * @OA\Property(property="paused",type="boolean",example="false",description="The scheduler paused state"), - * * @OA\Property(property="repeat_every",type="string",example="DAY",description="Accepted values (DAY,WEEK,MONTH,3MONTHS,YEAR)"), + * @OA\Property(property="paused",type="boolean",example="false",description="The scheduler paused state"), + * @OA\Property(property="repeat_every",type="string",example="DAY",description="Accepted values (DAY,WEEK,MONTH,3MONTHS,YEAR)"), * @OA\Property(property="start_from",type="integer",example="1652898504",description="Timestamp when we should start the scheduler, default is today"), - * + * @OA\Property(property="job",type="string",example="create_credit_report",description="Job, we can find list of available jobs in Scheduler model"), + * @OA\Property(property="date_range", type="string", example="last7", description="The string representation of the date range of data to be returned"), + * @OA\Property(property="date_key", type="string", example="created_at", description="The date column to search between."), + * @OA\Property(property="start_date", type="string", example="2022-10-31", description="The start date to search between"), + * @OA\Property(property="end_date", type="string", example="2022-10-31", description="The end date to search between"), * ) */ @@ -44,7 +49,7 @@ * @OA\Schema( * schema="UpdateJobForASchedulerSchema", * type="object", - * @OA\Property(property="job",type="string",example="create_client_report",description="Set action name, action names can be found in ScheduledJob Model"), + * @OA\Property(property="job",type="string",example="create_client_report",description="Set action name, action names can be found in Scheduler Model"), * * ) */ diff --git a/app/Http/Controllers/TaskSchedulerController.php b/app/Http/Controllers/TaskSchedulerController.php index 1961db6db8bd..00f573a55751 100644 --- a/app/Http/Controllers/TaskSchedulerController.php +++ b/app/Http/Controllers/TaskSchedulerController.php @@ -12,11 +12,9 @@ namespace App\Http\Controllers; use App\Http\Requests\TaskScheduler\CreateScheduledTaskRequest; -use App\Http\Requests\TaskScheduler\UpdateScheduledJobRequest; use App\Http\Requests\TaskScheduler\UpdateScheduleRequest; use App\Jobs\Ninja\TaskScheduler; use App\Jobs\Report\ProfitAndLoss; -use App\Models\ScheduledJob; use App\Models\Scheduler; use App\Repositories\TaskSchedulerRepository; use App\Transformers\TaskSchedulerTransformer; @@ -26,7 +24,7 @@ use Symfony\Component\HttpFoundation\Request; class TaskSchedulerController extends BaseController { - protected $entity_type = TaskScheduler::class; + protected $entity_type = Scheduler::class; protected $entity_transformer = TaskSchedulerTransformer::class; protected TaskSchedulerRepository $scheduler_repository; @@ -199,54 +197,7 @@ class TaskSchedulerController extends BaseController return $this->itemResponse($scheduler); } - /** - * @OA\PUT( - * path="/api/v1/task_scheduler/{id}/update_job/", - * 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"), - * @OA\Parameter( - * name="id", - * in="path", - * description="The Scheduler Hashed ID", - * example="D2J234DFA", - * required=true, - * @OA\Schema( - * type="string", - * format="string", - * ), - * ), - * @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"), - * ), - * ) - */ - public function updateJob(Scheduler $scheduler, UpdateScheduledJobRequest $request) - { - $scheduler->service()->updateJob($scheduler, $request); - return $this->itemResponse($scheduler); - } + /** * @OA\DELETE( diff --git a/app/Http/Requests/TaskScheduler/UpdateScheduleRequest.php b/app/Http/Requests/TaskScheduler/UpdateScheduleRequest.php index 1f1d888ce75c..e0bc8a3f9c0c 100644 --- a/app/Http/Requests/TaskScheduler/UpdateScheduleRequest.php +++ b/app/Http/Requests/TaskScheduler/UpdateScheduleRequest.php @@ -13,7 +13,6 @@ namespace App\Http\Requests\TaskScheduler; use App\Http\Requests\Request; -use App\Models\ScheduledJob; use Carbon\Carbon; use Illuminate\Validation\Rule; diff --git a/app/Jobs/Ninja/TaskScheduler.php b/app/Jobs/Ninja/TaskScheduler.php index eaf80e608762..89053b7f998e 100644 --- a/app/Jobs/Ninja/TaskScheduler.php +++ b/app/Jobs/Ninja/TaskScheduler.php @@ -14,8 +14,6 @@ namespace App\Jobs\Ninja; use App\Jobs\Report\SendToAdmin; use App\Libraries\MultiDB; -use App\Models\Company; -use App\Models\ScheduledJob; use App\Models\Scheduler; use Carbon\Carbon; use Illuminate\Bus\Queueable; @@ -46,22 +44,18 @@ class TaskScheduler implements ShouldQueue */ public function handle() { - foreach (MultiDB::$dbs as $db) + foreach (MultiDB::$dbs as $db) { MultiDB::setDB($db); - $pending_schedulers = $this->fetchJobs(); - - Scheduler::with('company','job') + Scheduler::with('company') ->where('paused', false) ->where('is_deleted', false) ->where('scheduled_run', '<', now()) ->cursor() ->each(function ($scheduler){ - $this->doJob($scheduler); - }); } @@ -70,59 +64,55 @@ class TaskScheduler implements ShouldQueue private function doJob(Scheduler $scheduler) { - nlog("Doing job {$scheduler->id}"); - - $job = $scheduler->job; + nlog("Doing job {$scheduler->action_name}"); + $company = $scheduler->company; - if (!$job) - return; - - $parameters = $job->parameters; + $parameters = $scheduler->parameters; - switch ($job->action_name) { - case ScheduledJob::CREATE_CLIENT_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'contacts.csv'); + switch ($scheduler->action_name) { + case Scheduler::CREATE_CLIENT_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'contacts.csv'); break; - case ScheduledJob::CREATE_CLIENT_CONTACT_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'clients.csv'); + case Scheduler::CREATE_CLIENT_CONTACT_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'clients.csv'); break; - case ScheduledJob::CREATE_CREDIT_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'credits.csv'); + case Scheduler::CREATE_CREDIT_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'credits.csv'); break; - case ScheduledJob::CREATE_DOCUMENT_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'documents.csv'); + case Scheduler::CREATE_DOCUMENT_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'documents.csv'); break; - case ScheduledJob::CREATE_EXPENSE_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'expense.csv'); + case Scheduler::CREATE_EXPENSE_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'expense.csv'); break; - case ScheduledJob::CREATE_INVOICE_ITEM_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'invoice_items.csv'); + case Scheduler::CREATE_INVOICE_ITEM_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'invoice_items.csv'); break; - case ScheduledJob::CREATE_INVOICE_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'invoices.csv'); + case Scheduler::CREATE_INVOICE_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'invoices.csv'); break; - case ScheduledJob::CREATE_PAYMENT_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'payments.csv'); + case Scheduler::CREATE_PAYMENT_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'payments.csv'); break; - case ScheduledJob::CREATE_PRODUCT_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'products.csv'); + case Scheduler::CREATE_PRODUCT_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'products.csv'); break; - case ScheduledJob::CREATE_PROFIT_AND_LOSS_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'profit_and_loss.csv'); + case Scheduler::CREATE_PROFIT_AND_LOSS_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'profit_and_loss.csv'); break; - case ScheduledJob::CREATE_QUOTE_ITEM_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'quote_items.csv'); + case Scheduler::CREATE_QUOTE_ITEM_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'quote_items.csv'); break; - case ScheduledJob::CREATE_QUOTE_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'quotes.csv'); + case Scheduler::CREATE_QUOTE_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'quotes.csv'); break; - case ScheduledJob::CREATE_RECURRING_INVOICE_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'recurring_invoices.csv'); + case Scheduler::CREATE_RECURRING_INVOICE_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'recurring_invoices.csv'); break; - case ScheduledJob::CREATE_TASK_REPORT: - SendToAdmin::dispatch($company, $parameters, $job->action_class, 'tasks.csv'); + case Scheduler::CREATE_TASK_REPORT: + SendToAdmin::dispatch($company, $parameters, $scheduler->action_class, 'tasks.csv'); break; } @@ -131,10 +121,4 @@ class TaskScheduler implements ShouldQueue $scheduler->save(); } - - private function fetchJobs() - { - return ; - } - } diff --git a/app/Models/ScheduledJob.php b/app/Models/ScheduledJob.php deleted file mode 100644 index a2baedebb226..000000000000 --- a/app/Models/ScheduledJob.php +++ /dev/null @@ -1,49 +0,0 @@ - 'array' - ]; -} diff --git a/app/Models/Scheduler.php b/app/Models/Scheduler.php index bc279d293b5e..052870f5eb6f 100644 --- a/app/Models/Scheduler.php +++ b/app/Models/Scheduler.php @@ -22,11 +22,14 @@ use Illuminate\Support\Carbon; * @property \Carbon\Carbon|mixed start_from * @property string repeat_every * @property \Carbon\Carbon|mixed scheduled_run - * @property mixed job * @property integer company_id * @property integer updated_at * @property integer created_at * @property integer deleted_at + * @property string action_name + * @property mixed company + * @property array parameters + * @property string action_class */ class Scheduler extends BaseModel { @@ -37,6 +40,10 @@ class Scheduler extends BaseModel 'paused', 'repeat_every', 'scheduled_run', + 'action_class', + 'action_name', + 'parameters', + 'company_id' ]; protected $casts = [ @@ -47,8 +54,9 @@ class Scheduler extends BaseModel 'deleted_at' => 'timestamp', 'paused' => 'boolean', 'is_deleted' => 'boolean', + 'parameters' => 'array', ]; - protected $appends = ['linked_job']; + const DAILY = 'DAY'; const WEEKLY = 'WEEK'; @@ -57,10 +65,21 @@ class Scheduler extends BaseModel const QUARTERLY = '3MONTHS'; const ANNUALLY = 'YEAR'; - public function getLinkedJobAttribute() - { - return $this->job ?? []; - } + const CREATE_CLIENT_REPORT = 'create_client_report'; + const CREATE_CLIENT_CONTACT_REPORT = 'create_client_contact_report'; + const CREATE_CREDIT_REPORT = 'create_credit_report'; + const CREATE_DOCUMENT_REPORT = 'create_document_report'; + const CREATE_EXPENSE_REPORT = 'create_expense_report'; + const CREATE_INVOICE_ITEM_REPORT = 'create_invoice_item_report'; + const CREATE_INVOICE_REPORT = 'create_invoice_report'; + const CREATE_PAYMENT_REPORT = 'create_payment_report'; + const CREATE_PRODUCT_REPORT = 'create_product_report'; + const CREATE_PROFIT_AND_LOSS_REPORT = 'create_profit_and_loss_report'; + const CREATE_QUOTE_ITEM_REPORT = 'create_quote_item_report'; + const CREATE_QUOTE_REPORT = 'create_quote_report'; + const CREATE_RECURRING_INVOICE_REPORT = 'create_recurring_invoice_report'; + const CREATE_TASK_REPORT = 'create_task_report'; + /** * Service entry points. @@ -70,10 +89,6 @@ class Scheduler extends BaseModel return new TaskSchedulerService($this); } - public function job(): \Illuminate\Database\Eloquent\Relations\HasOne - { - return $this->hasOne(ScheduledJob::class, 'scheduler_id', 'id'); - } public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo { diff --git a/app/Services/TaskScheduler/TaskSchedulerService.php b/app/Services/TaskScheduler/TaskSchedulerService.php index dbf54f059490..63b022a0d552 100644 --- a/app/Services/TaskScheduler/TaskSchedulerService.php +++ b/app/Services/TaskScheduler/TaskSchedulerService.php @@ -32,7 +32,6 @@ use App\Http\Requests\TaskScheduler\UpdateScheduledJobRequest; use App\Http\Requests\TaskScheduler\UpdateScheduleRequest; use App\Jobs\Report\ProfitAndLoss; use App\Models\Company; -use App\Models\ScheduledJob; use App\Models\Scheduler; use App\Utils\Ninja; use Carbon\Carbon; @@ -51,128 +50,116 @@ class TaskSchedulerService public function store(Scheduler $scheduler, CreateScheduledTaskRequest $request) { + $scheduler->action_name = $request->job; $scheduler->paused = $request->get('paused', 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->company_id = auth()->user()->company()->id; + $scheduler = $this->setJobParameters($scheduler, $request); $scheduler->save(); - - $this->createJob($request, $scheduler); } public function update(Scheduler $scheduler, UpdateScheduleRequest $request) { - - $data = $request->validated(); - - $update = $this->scheduler->update($data); - if ($update) { - return response(['successfully_updated_scheduler'], 200); + if (array_key_exists('job', $request->all())) { + $scheduler->action_name = $request->get('job'); + $scheduler = $this->setJobParameters($scheduler, $request); } - 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(); - + $data = $request->validated(); + $update = $this->scheduler->update($data); } private function runValidation($form_request, $data) { - $_syn_request_class = new $form_request(); - $_syn_request_class->setContainer(app()); - $_syn_request_class->initialize($data); - $_syn_request_class->prepareForValidation(); - $_syn_request_class->setValidator(Validator::make($_syn_request_class->all(), $_syn_request_class->rules())); + $_syn_request_class = new $form_request(); + $_syn_request_class->setContainer(app()); + $_syn_request_class->initialize($data); + $_syn_request_class->prepareForValidation(); + $_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) { - case ScheduledJob::CREATE_CLIENT_REPORT: - $job->action_name = ScheduledJob::CREATE_CLIENT_REPORT; - $job->action_class = $this->getClassPath(ClientExport::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + switch ($scheduler->action_name) { + case Scheduler::CREATE_CLIENT_REPORT: + $scheduler->action_name = Scheduler::CREATE_CLIENT_REPORT; + $scheduler->action_class = $this->getClassPath(ClientExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); 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; - $job->action_class = $this->getClassPath(ContactExport::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + $scheduler->action_name = Scheduler::CREATE_CREDIT_REPORT; + $scheduler->action_class = $this->getClassPath(CreditExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; - case ScheduledJob::CREATE_CREDIT_REPORT: - - $job->action_name = ScheduledJob::CREATE_CREDIT_REPORT; - $job->action_class = $this->getClassPath(CreditExport::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + case Scheduler::CREATE_DOCUMENT_REPORT: + $scheduler->action_name = Scheduler::CREATE_DOCUMENT_REPORT; + $scheduler->action_class = $this->getClassPath(DocumentExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; - case ScheduledJob::CREATE_DOCUMENT_REPORT: - $job->action_name = ScheduledJob::CREATE_DOCUMENT_REPORT; - $job->action_class = $this->getClassPath(DocumentExport::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + case Scheduler::CREATE_EXPENSE_REPORT: + $scheduler->action_name = Scheduler::CREATE_EXPENSE_REPORT; + $scheduler->action_class = $this->getClassPath(ExpenseExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; - case ScheduledJob::CREATE_EXPENSE_REPORT: - $job->action_name = ScheduledJob::CREATE_EXPENSE_REPORT; - $job->action_class = $this->getClassPath(ExpenseExport::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + case Scheduler::CREATE_INVOICE_ITEM_REPORT: + $scheduler->action_name = Scheduler::CREATE_INVOICE_ITEM_REPORT; + $scheduler->action_class = $this->getClassPath(InvoiceItemExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; - case ScheduledJob::CREATE_INVOICE_ITEM_REPORT: - $job->action_name = ScheduledJob::CREATE_INVOICE_ITEM_REPORT; - $job->action_class = $this->getClassPath(InvoiceItemExport::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + case Scheduler::CREATE_INVOICE_REPORT: + $scheduler->action_name = Scheduler::CREATE_INVOICE_REPORT; + $scheduler->action_class = $this->getClassPath(InvoiceExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; - case ScheduledJob::CREATE_INVOICE_REPORT: - $job->action_name = ScheduledJob::CREATE_INVOICE_REPORT; - $job->action_class = $this->getClassPath(InvoiceExport::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + case Scheduler::CREATE_PAYMENT_REPORT: + $scheduler->action_name = Scheduler::CREATE_PAYMENT_REPORT; + $scheduler->action_class = $this->getClassPath(PaymentExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; - case ScheduledJob::CREATE_PAYMENT_REPORT: - $job->action_name = ScheduledJob::CREATE_PAYMENT_REPORT; - $job->action_class = $this->getClassPath(PaymentExport::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + case Scheduler::CREATE_PRODUCT_REPORT: + $scheduler->action_name = Scheduler::CREATE_PRODUCT_REPORT; + $scheduler->action_class = $this->getClassPath(ProductExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; - case ScheduledJob::CREATE_PRODUCT_REPORT: - $job->action_name = ScheduledJob::CREATE_PRODUCT_REPORT; - $job->action_class = $this->getClassPath(ProductExport::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + case Scheduler::CREATE_PROFIT_AND_LOSS_REPORT: + $scheduler->action_name = Scheduler::CREATE_PROFIT_AND_LOSS_REPORT; + $scheduler->action_class = $this->getClassPath(ProfitAndLoss::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; - case ScheduledJob::CREATE_PROFIT_AND_LOSS_REPORT: - $job->action_name = ScheduledJob::CREATE_PROFIT_AND_LOSS_REPORT; - $job->action_class = $this->getClassPath(ProfitAndLoss::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + case Scheduler::CREATE_QUOTE_ITEM_REPORT: + $scheduler->action_name = Scheduler::CREATE_QUOTE_ITEM_REPORT; + $scheduler->action_class = $this->getClassPath(QuoteItemExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; - case ScheduledJob::CREATE_QUOTE_ITEM_REPORT: - $job->action_name = ScheduledJob::CREATE_QUOTE_ITEM_REPORT; - $job->action_class = $this->getClassPath(QuoteItemExport::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + case Scheduler::CREATE_QUOTE_REPORT: + $scheduler->action_name = Scheduler::CREATE_QUOTE_REPORT; + $scheduler->action_class = $this->getClassPath(QuoteExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; - case ScheduledJob::CREATE_QUOTE_REPORT: - $job->action_name = ScheduledJob::CREATE_QUOTE_REPORT; - $job->action_class = $this->getClassPath(QuoteExport::class); - $job->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); + case Scheduler::CREATE_RECURRING_INVOICE_REPORT: + $scheduler->action_name = Scheduler::CREATE_RECURRING_INVOICE_REPORT; + $scheduler->action_class = $this->getClassPath(RecurringInvoiceExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; - case ScheduledJob::CREATE_RECURRING_INVOICE_REPORT: - $job->action_name = ScheduledJob::CREATE_RECURRING_INVOICE_REPORT; - $job->action_class = $this->getClassPath(RecurringInvoiceExport::class); - $job->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()); + case Scheduler::CREATE_TASK_REPORT: + $scheduler->action_name = Scheduler::CREATE_TASK_REPORT; + $scheduler->action_class = $this->getClassPath(TaskExport::class); + $scheduler->parameters = $this->runValidation(GenericReportRequest::class, $request->all()); break; } - return $job; + + return $scheduler; } public function getClassPath($class): string @@ -180,15 +167,9 @@ class TaskSchedulerService return $class = is_object($class) ? get_class($class) : $class; } - public function updateJob(Scheduler $scheduler, UpdateScheduledJobRequest $request) { - $job = $scheduler->job; - if (!$job) { - return abort(404); - } - $job = $this->setJobParameters($job, $request); - $job->save(); - + $scheduler = $this->setJobParameters($scheduler, $request); + $scheduler->save(); } } diff --git a/app/Transformers/ScheduledJobTransformer.php b/app/Transformers/ScheduledJobTransformer.php deleted file mode 100644 index 2028fd2e0dcd..000000000000 --- a/app/Transformers/ScheduledJobTransformer.php +++ /dev/null @@ -1,30 +0,0 @@ - $this->encodePrimaryKey($job->id), - 'action_name' => (string)$job->action_name, - 'parameters' => (array)$job->parameters - ]; - } -} diff --git a/app/Transformers/TaskSchedulerTransformer.php b/app/Transformers/TaskSchedulerTransformer.php index 7cb96dc34293..d49bad7d22b2 100644 --- a/app/Transformers/TaskSchedulerTransformer.php +++ b/app/Transformers/TaskSchedulerTransformer.php @@ -11,8 +11,6 @@ namespace App\Transformers; - -use App\Models\ScheduledJob; use App\Models\Scheduler; use App\Utils\Traits\MakesHash; @@ -20,16 +18,6 @@ class TaskSchedulerTransformer extends EntityTransformer { use MakesHash; - protected $defaultIncludes = [ - 'job' - ]; - - public function includeJob(Scheduler $scheduler) - { - $transformer = new ScheduledJobTransformer($this->serializer); - - return $this->item($scheduler->job, $transformer, ScheduledJob::class); - } public function transform(Scheduler $scheduler) { @@ -43,6 +31,9 @@ class TaskSchedulerTransformer extends EntityTransformer 'updated_at' => (int)$scheduler->updated_at, 'created_at' => (int)$scheduler->created_at, 'archived_at' => (int) $scheduler->deleted_at, + 'action_name' => (string) $scheduler->action_name, + 'action_class' => (string) $scheduler->action_class, + 'parameters'=> (array)$scheduler->parameters, ]; } diff --git a/database/migrations/2022_05_18_162443_create_schedulers_table.php b/database/migrations/2022_05_18_162443_create_schedulers_table.php index 72e3fe743a0c..ff26d617b275 100644 --- a/database/migrations/2022_05_18_162443_create_schedulers_table.php +++ b/database/migrations/2022_05_18_162443_create_schedulers_table.php @@ -30,6 +30,9 @@ class CreateSchedulersTable extends Migration $table->timestamp('start_from'); $table->timestamp('scheduled_run'); $table->foreignIdFor(\App\Models\Company::class); + $table->string('action_name')->index(); + $table->string('action_class'); + $table->json('parameters')->nullable(); $table->timestamps(); $table->softDeletes(); }); diff --git a/database/migrations/2022_05_30_181109_drop_scheduled_jobs_table.php b/database/migrations/2022_05_30_181109_drop_scheduled_jobs_table.php new file mode 100644 index 000000000000..303620aee7a1 --- /dev/null +++ b/database/migrations/2022_05_30_181109_drop_scheduled_jobs_table.php @@ -0,0 +1,28 @@ + ['throttle:100,1', 'api_db', 'token_auth', 'locale Route::resource('task_scheduler', 'TaskSchedulerController')->except('edit')->parameters(['task_scheduler' => 'scheduler']); - Route::put('task_scheduler/{scheduler}/update_job','TaskSchedulerController@updateJob'); + Route::get('scheduler', 'SchedulerController@index'); Route::post('support/messages/send', 'Support\Messages\SendingController'); diff --git a/tests/Feature/Scheduler/SchedulerTest.php b/tests/Feature/Scheduler/SchedulerTest.php index bcaa4642fdad..638ed3fb1f0a 100644 --- a/tests/Feature/Scheduler/SchedulerTest.php +++ b/tests/Feature/Scheduler/SchedulerTest.php @@ -4,7 +4,6 @@ namespace Tests\Feature\Scheduler; use App\Export\CSV\ClientExport; -use App\Models\ScheduledJob; use App\Models\Scheduler; use App\Utils\Traits\MakesHash; use Carbon\Carbon; @@ -40,7 +39,7 @@ class SchedulerTest extends TestCase ThrottleRequests::class ); - // $this->withoutExceptionHandling(); + // $this->withoutExceptionHandling(); } @@ -48,7 +47,7 @@ class SchedulerTest extends TestCase { $data = [ 'repeat_every' => Scheduler::DAILY, - 'job' => ScheduledJob::CREATE_CLIENT_REPORT, + 'job' => Scheduler::CREATE_CLIENT_REPORT, 'date_key' => '123', 'report_keys' => ['test'], 'date_range' => 'all', @@ -57,16 +56,10 @@ class SchedulerTest extends TestCase $response = false; - // try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, ])->post('/api/v1/task_scheduler/', $data); - // } catch (ValidationException $e) { - // $message = json_decode($e->validator->getMessageBag(), 1); - // nlog($message); - // } - // $response->assertStatus(200); $response->assertSessionHasErrors(); @@ -109,12 +102,14 @@ class SchedulerTest extends TestCase ])->get('/api/v1/task_scheduler/' . $this->encodePrimaryKey($scheduler->id)); $arr = $response->json(); - $this->assertEquals('create_client_report', $arr['data']['job']['action_name']); + $this->assertEquals('create_client_report', $arr['data']['action_name']); } + + public function testSchedulerJobCanBeUpdated() { $response = $this->createScheduler(); @@ -124,10 +119,10 @@ class SchedulerTest extends TestCase $scheduler = Scheduler::find($this->decodePrimaryKey($id)); - $this->assertSame('create_client_report', $scheduler->job->action_name); + $this->assertSame('create_client_report', $scheduler->action_name); $updateData = [ - 'job' => ScheduledJob::CREATE_CREDIT_REPORT, + 'job' => Scheduler::CREATE_CREDIT_REPORT, 'date_range' => 'all', 'report_keys' => ['test1'] ]; @@ -135,19 +130,19 @@ class SchedulerTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->put('/api/v1/task_scheduler/' . $this->encodePrimaryKey($scheduler->id) . '/update_job', $updateData); + ])->put('/api/v1/task_scheduler/' . $this->encodePrimaryKey($scheduler->id), $updateData); - $updatedSchedulerJob = Scheduler::first()->job->action_name; + $updatedSchedulerJob = Scheduler::first()->action_name; $arr = $response->json(); - $this->assertSame('create_credit_report', $arr['data']['job']['action_name']); + $this->assertSame('create_credit_report', $arr['data']['action_name']); } public function createScheduler() { $data = [ 'repeat_every' => Scheduler::DAILY, - 'job' => ScheduledJob::CREATE_CLIENT_REPORT, + 'job' => Scheduler::CREATE_CLIENT_REPORT, 'date_key' => '123', 'report_keys' => ['test'], 'date_range' => 'all',