1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 22:22:32 +01:00

INA-5 | Set multidb, set company_id as database parameter

This commit is contained in:
Nikola Cirkovic 2022-05-21 20:53:22 +02:00
parent 39400d3b08
commit 5a6f4801ce
4 changed files with 39 additions and 6 deletions

View File

@ -51,6 +51,8 @@ class TaskSchedulerController extends BaseController
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);
@ -150,6 +152,7 @@ class TaskSchedulerController extends BaseController
}
$job->scheduler_id = $scheduler->id;
$job->company_id = auth()->user()->company()->id;
return $job->save();
}
@ -167,7 +170,6 @@ class TaskSchedulerController extends BaseController
$parameters[$rule] = $request->{$rule};
}
}
$parameters['company'] = auth()->user()->company();
return $parameters;
}

View File

@ -13,6 +13,7 @@ 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;
@ -45,6 +46,7 @@ class TaskScheduler implements ShouldQueue
*/
public function handle()
{
$pending_schedulers = $this->fetchJobs();
foreach ($pending_schedulers as $scheduler) {
$this->doJob($scheduler);
@ -54,11 +56,15 @@ class TaskScheduler implements ShouldQueue
private function doJob(Scheduler $scheduler)
{
$job = $scheduler->job;
$parameters = $job->parameters;
$company = Company::where('company_key', $parameters['company']['company_key'])->first();
if (!$job) {
$company = Company::find($job->company_id);
if (!$job || !$company) {
return;
}
MultiDB::setDb($company->db);
$parameters = $job->parameters;
switch ($job->action_name) {
case ScheduledJob::CREATE_CLIENT_REPORT:
SendToAdmin::dispatch($company, $parameters, $job->action_class, 'contacts.csv');
@ -106,6 +112,30 @@ class TaskScheduler implements ShouldQueue
}
//setup new scheduled_run
$amount_of_days_until_next_run = $this->getAmountOfDays($scheduler->repeat_every);
$scheduler->scheduled_run = Carbon::now()->addDays($amount_of_days_until_next_run);
$scheduler->save();
}
private function getAmountOfDays(string $repeat_every): int
{
switch ($repeat_every) {
case Scheduler::DAILY:
return 1;
break;
case Scheduler::MONTHLY:
return 30;
break;
case Scheduler::WEEKLY:
return 7;
break;
case Scheduler::QUARTERLY:
return 90;
break;
case Scheduler::ANNUALLY:
return 365;
break;
}
}
private function fetchJobs()

View File

@ -19,6 +19,7 @@ use Illuminate\Database\Eloquent\Model;
* @property array parameters
* @property string action_name
* @property integer scheduler_id
* @property integer company_id
*/
class ScheduledJob extends Model
{
@ -40,9 +41,8 @@ class ScheduledJob extends Model
const CREATE_TASK_REPORT = 'create_task_report';
protected $fillable = ['action_class', 'action_name', 'parameters', 'scheduler_id'];
protected $fillable = ['action_class', 'action_name', 'parameters', 'scheduler_id','company_id'];
protected $casts = [
'required_parameters' => 'array',
'scheduled_run' => 'date',
'parameters' => 'array'
];

View File

@ -27,6 +27,7 @@ class CreateScheduledJobsTable extends Migration
$table->string('action_name');
$table->string('action_class');
$table->json('parameters')->nullable();
$table->foreignIdFor(\App\Models\Company::class);
$table->foreignIdFor(\App\Models\Scheduler::class);
$table->timestamps();
});