Fixes for scheduler

This commit is contained in:
David Bomba 2022-05-27 17:01:15 +10:00
parent 13a1447b79
commit cf141e36c8
6 changed files with 91 additions and 49 deletions

View File

@ -47,6 +47,9 @@ class GenericReportRequest extends Request
if(!array_key_exists('report_keys', $input))
$input['report_keys'] = [];
if(!array_key_exists('send_email', $input))
$input['send_email'] = true;
$this->replace($input);
}
}

View File

@ -22,13 +22,6 @@ trait RuntimeFormRequest
$instance = $validator->getValidatorInstance();
return $instance;
// if ($instance->fails()) {
// return $instance->errors();
// }
// $validator->passedValidation();
// return $validator->all();
}

View File

@ -27,4 +27,14 @@ class CreateScheduledTaskRequest extends Request
'job' => 'required',
];
}
public function prepareForValidation()
{
$input = $this->all();
if(!array_key_exists('start_from', $input))
$input['start_from'] = now();
$this->replace($input);
}
}

View File

@ -41,13 +41,13 @@ class UpdateScheduleRequest extends Request
public function prepareForValidation()
{
$request = $this->all();
$input = $this->all();
if (isset($request['start_from'])) {
$request['scheduled_run'] = Carbon::parse((int)$request['start_from']);
$request['start_from'] = Carbon::parse((int)$request['start_from']);
if (isset($input['start_from'])) {
$input['scheduled_run'] = Carbon::parse((int)$input['start_from']);
$input['start_from'] = Carbon::parse((int)$input['start_from']);
}
$this->replace($request);
$this->replace($input);
}
}

View File

@ -90,98 +90,100 @@ class TaskSchedulerService
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$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);
$validator = GenericReportRequest::runFormRequest($request->all());
$validatedJobData = $validator->validate();
$job->action_name = ScheduledJob::CREATE_TASK_REPORT;
$job->action_class = $this->getClassPath(TaskExport::class);
$job->parameters = $validatedJobData;

View File

@ -15,6 +15,7 @@ use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\Session;
use Tests\MockUnitData;
use Tests\TestCase;
use Illuminate\Validation\ValidationException;
class SchedulerTest extends TestCase
{
@ -35,10 +36,12 @@ class SchedulerTest extends TestCase
$this->makeTestData();
$this->withoutMiddleware(
ThrottleRequests::class
);
// $this->withoutExceptionHandling();
}
public function testSchedulerCantBeCreatedWithWrongData()
@ -48,13 +51,23 @@ class SchedulerTest extends TestCase
'job' => ScheduledJob::CREATE_CLIENT_REPORT,
'date_key' => '123',
'report_keys' => ['test'],
// 'date_range' => 'all',
'date_range' => 'all',
// 'start_from' => '2022-01-01'
];
$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();
@ -62,10 +75,14 @@ class SchedulerTest extends TestCase
public function testSchedulerCanBeUpdated()
{
$this->createScheduler();
$response = $this->createScheduler();
nlog($response);
$arr = $response->json();
$id = $arr['data']['id'];
$scheduler = Scheduler::find($this->decodePrimaryKey($id));
$scheduler = Scheduler::first();
$updateData = [
'start_from' => 1655934741
];
@ -80,10 +97,12 @@ class SchedulerTest extends TestCase
public function testSchedulerCanBeSeen()
{
$this->createScheduler();
$response = $this->createScheduler();
$arr = $response->json();
$id = $arr['data']['id'];
$scheduler = Scheduler::first();
$scheduler = Scheduler::find($this->decodePrimaryKey($id));
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
@ -98,9 +117,13 @@ class SchedulerTest extends TestCase
public function testSchedulerCanBeDeleted()
{
$this->createScheduler();
$response = $this->createScheduler();
$arr = $response->json();
$id = $arr['data']['id'];
$scheduler = Scheduler::find($this->decodePrimaryKey($id));
$scheduler = Scheduler::first();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
@ -112,9 +135,13 @@ class SchedulerTest extends TestCase
public function testSchedulerJobCanBeUpdated()
{
$this->createScheduler();
$response = $this->createScheduler();
$arr = $response->json();
$id = $arr['data']['id'];
$scheduler = Scheduler::find($this->decodePrimaryKey($id));
$scheduler = Scheduler::first();
$this->assertSame('create_client_report', $scheduler->job->action_name);
$updateData = [
@ -130,6 +157,7 @@ class SchedulerTest extends TestCase
$updatedSchedulerJob = Scheduler::first()->job->action_name;
$arr = $response->json();
$this->assertSame('create_credit_report', $arr['data']['job']['action_name']);
}
@ -137,6 +165,11 @@ class SchedulerTest extends TestCase
{
$response = $this->createScheduler();
$arr = $response->json();
$id = $arr['data']['id'];
$scheduler = Scheduler::find($this->decodePrimaryKey($id));
$all_schedulers = Scheduler::count();
$this->assertSame(1, $all_schedulers);
@ -153,6 +186,7 @@ class SchedulerTest extends TestCase
'date_key' => '123',
'report_keys' => ['test'],
'date_range' => 'all',
'start_from' => '2022-01-01'
];
return $response = $this->withHeaders([