2018-03-31 11:28:19 +08:00
|
|
|
<?php
|
|
|
|
|
2018-04-07 06:14:01 +08:00
|
|
|
namespace App\Cron\Nightly;
|
2018-03-31 11:28:19 +08:00
|
|
|
|
|
|
|
use App\Events\CronNightly;
|
|
|
|
use App\Interfaces\Listener;
|
|
|
|
use App\Models\Enums\UserState;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Services\UserService;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if any pilots should be set to ON LEAVE status
|
|
|
|
*/
|
|
|
|
class PilotLeave extends Listener
|
|
|
|
{
|
|
|
|
private $userSvc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PilotLeave constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(UserService $userSvc)
|
|
|
|
{
|
|
|
|
$this->userSvc = $userSvc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set any users to being on leave after X days
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-31 11:28:19 +08:00
|
|
|
* @param CronNightly $event
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-31 11:28:19 +08:00
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function handle(CronNightly $event): void
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
if (setting('pilots.auto_leave_days') === 0) {
|
2018-03-31 11:28:19 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$date = Carbon::now()->subDay(setting('pilots.auto_leave_days'));
|
|
|
|
$users = User::where('status', UserState::ACTIVE)
|
|
|
|
->whereDate('updated_at', '<', $date);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($users as $user) {
|
2018-07-13 11:23:45 +08:00
|
|
|
Log::info('Setting user '.$user->ident.' to ON LEAVE status');
|
2018-03-31 11:36:01 +08:00
|
|
|
$this->userSvc->setStatusOnLeave($user);
|
2018-03-31 11:28:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|