2017-07-04 14:05:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Service;
|
2017-12-23 06:32:21 +08:00
|
|
|
use App\Events\UserRegistered;
|
2017-12-03 00:55:17 +08:00
|
|
|
use App\Events\UserStateChanged;
|
2017-12-23 06:32:21 +08:00
|
|
|
use App\Events\UserStatsChanged;
|
2020-05-16 23:46:25 +08:00
|
|
|
use App\Exceptions\PilotIdNotFound;
|
2019-07-17 01:54:14 +08:00
|
|
|
use App\Exceptions\UserPilotIdExists;
|
2020-07-15 10:02:35 +08:00
|
|
|
use App\Models\Airline;
|
2021-04-23 22:33:13 +08:00
|
|
|
use App\Models\Bid;
|
2018-08-20 22:42:54 +08:00
|
|
|
use App\Models\Enums\PirepState;
|
2017-12-31 10:40:32 +08:00
|
|
|
use App\Models\Enums\UserState;
|
2018-08-20 22:42:54 +08:00
|
|
|
use App\Models\Pirep;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Models\Rank;
|
|
|
|
use App\Models\Role;
|
|
|
|
use App\Models\User;
|
2021-04-23 22:33:13 +08:00
|
|
|
use App\Models\UserFieldValue;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Repositories\AircraftRepository;
|
2020-05-16 23:46:25 +08:00
|
|
|
use App\Repositories\AirlineRepository;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Repositories\SubfleetRepository;
|
2019-08-11 08:42:35 +08:00
|
|
|
use App\Repositories\UserRepository;
|
2018-03-01 05:01:23 +08:00
|
|
|
use App\Support\Units\Time;
|
2020-02-24 06:21:26 +08:00
|
|
|
use App\Support\Utils;
|
2021-02-18 07:54:18 +08:00
|
|
|
use Carbon\Carbon;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Support\Collection;
|
2021-04-23 22:33:13 +08:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2019-08-09 00:50:43 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-07-17 01:54:14 +08:00
|
|
|
use function is_array;
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class UserService extends Service
|
2017-07-04 14:05:37 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
private $aircraftRepo;
|
2020-05-16 23:46:25 +08:00
|
|
|
private $airlineRepo;
|
2020-09-30 01:21:24 +08:00
|
|
|
private $fareSvc;
|
2018-08-27 00:40:04 +08:00
|
|
|
private $subfleetRepo;
|
2019-08-11 08:42:35 +08:00
|
|
|
private $userRepo;
|
2018-01-20 01:46:30 +08:00
|
|
|
|
|
|
|
/**
|
2018-02-23 05:15:00 +08:00
|
|
|
* @param AircraftRepository $aircraftRepo
|
2020-05-16 23:46:25 +08:00
|
|
|
* @param AirlineRepository $airlineRepo
|
2020-09-30 01:21:24 +08:00
|
|
|
* @param FareService $fareSvc
|
2018-01-20 01:46:30 +08:00
|
|
|
* @param SubfleetRepository $subfleetRepo
|
2019-08-11 08:42:35 +08:00
|
|
|
* @param UserRepository $userRepo
|
2018-01-20 01:46:30 +08:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2018-02-21 02:59:49 +08:00
|
|
|
AircraftRepository $aircraftRepo,
|
2020-05-16 23:46:25 +08:00
|
|
|
AirlineRepository $airlineRepo,
|
2020-09-30 01:21:24 +08:00
|
|
|
FareService $fareSvc,
|
2019-08-11 08:42:35 +08:00
|
|
|
SubfleetRepository $subfleetRepo,
|
|
|
|
UserRepository $userRepo
|
2018-01-20 01:46:30 +08:00
|
|
|
) {
|
2018-02-21 02:59:49 +08:00
|
|
|
$this->aircraftRepo = $aircraftRepo;
|
2020-05-16 23:46:25 +08:00
|
|
|
$this->airlineRepo = $airlineRepo;
|
2020-09-30 01:21:24 +08:00
|
|
|
$this->fareSvc = $fareSvc;
|
2018-01-20 01:46:30 +08:00
|
|
|
$this->subfleetRepo = $subfleetRepo;
|
2019-08-11 08:42:35 +08:00
|
|
|
$this->userRepo = $userRepo;
|
2018-01-20 01:46:30 +08:00
|
|
|
}
|
|
|
|
|
2020-10-01 00:58:45 +08:00
|
|
|
/**
|
|
|
|
* Find the user and return them with all of the data properly attached
|
|
|
|
*
|
|
|
|
* @param $user_id
|
|
|
|
*
|
2021-04-23 22:33:13 +08:00
|
|
|
* @return User|null
|
2020-10-01 00:58:45 +08:00
|
|
|
*/
|
2021-04-23 22:33:13 +08:00
|
|
|
public function getUser($user_id): ?User
|
2020-10-01 00:58:45 +08:00
|
|
|
{
|
2021-04-23 22:33:13 +08:00
|
|
|
/** @var User $user */
|
2020-10-01 00:58:45 +08:00
|
|
|
$user = $this->userRepo
|
|
|
|
->with(['airline', 'bids', 'rank'])
|
|
|
|
->find($user_id);
|
|
|
|
|
2021-04-24 01:47:54 +08:00
|
|
|
if (empty($user)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-23 22:33:13 +08:00
|
|
|
if ($user->state === UserState::DELETED) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:58:45 +08:00
|
|
|
// Load the proper subfleets to the rank
|
|
|
|
$user->rank->subfleets = $this->getAllowableSubfleets($user);
|
|
|
|
$user->subfleets = $user->rank->subfleets;
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2017-12-15 13:51:57 +08:00
|
|
|
/**
|
2017-12-23 06:32:21 +08:00
|
|
|
* Register a pilot. Also attaches the initial roles
|
|
|
|
* required, and then triggers the UserRegistered event
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2020-02-24 06:21:26 +08:00
|
|
|
* @param array $attrs Array with the user data
|
2019-11-27 22:19:20 +08:00
|
|
|
* @param array $roles List of "display_name" of groups to assign
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-02 06:20:13 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2020-02-24 06:21:26 +08:00
|
|
|
* @return User
|
2017-12-15 13:51:57 +08:00
|
|
|
*/
|
2020-10-13 00:49:11 +08:00
|
|
|
public function createUser(array $attrs, array $roles = []): User
|
2017-12-15 13:51:57 +08:00
|
|
|
{
|
2020-02-24 06:21:26 +08:00
|
|
|
$user = User::create($attrs);
|
|
|
|
$user->api_key = Utils::generateApiKey();
|
|
|
|
$user->curr_airport_id = $user->home_airport_id;
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Determine if we want to auto accept
|
2018-09-04 16:11:51 +08:00
|
|
|
if (setting('pilots.auto_accept') === true) {
|
2017-12-31 10:40:32 +08:00
|
|
|
$user->state = UserState::ACTIVE;
|
2017-12-15 13:51:57 +08:00
|
|
|
} else {
|
2017-12-31 10:40:32 +08:00
|
|
|
$user->state = UserState::PENDING;
|
2017-12-23 03:54:25 +08:00
|
|
|
}
|
2017-12-15 13:51:57 +08:00
|
|
|
|
2017-12-23 02:46:46 +08:00
|
|
|
$user->save();
|
2017-12-15 13:51:57 +08:00
|
|
|
|
2019-06-21 04:52:37 +08:00
|
|
|
// Attach any additional roles
|
2019-11-27 22:19:20 +08:00
|
|
|
if (!empty($roles) && is_array($roles)) {
|
|
|
|
foreach ($roles as $role) {
|
|
|
|
$this->addUserToRole($user, $role);
|
2017-12-30 06:56:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Let's check their rank and where they should start
|
2017-12-15 13:51:57 +08:00
|
|
|
$this->calculatePilotRank($user);
|
2017-12-23 02:46:46 +08:00
|
|
|
$user->refresh();
|
|
|
|
|
2017-12-15 13:51:57 +08:00
|
|
|
event(new UserRegistered($user));
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2021-04-23 22:33:13 +08:00
|
|
|
/**
|
|
|
|
* Remove the user. But don't actually delete them - set the name to deleted, email to
|
|
|
|
* something random
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function removeUser(User $user)
|
|
|
|
{
|
|
|
|
$user->name = 'Deleted User';
|
|
|
|
$user->email = Utils::generateApiKey().'@deleted-user.com';
|
|
|
|
$user->api_key = Utils::generateApiKey();
|
|
|
|
$user->password = Hash::make(Utils::generateApiKey());
|
|
|
|
$user->state = UserState::DELETED;
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
// Detach all roles from this user
|
|
|
|
$user->detachRoles($user->roles);
|
|
|
|
|
|
|
|
// Delete any fields which might have personal information
|
|
|
|
UserFieldValue::where('user_id', $user->id)->delete();
|
|
|
|
|
|
|
|
// Remove any bids
|
|
|
|
Bid::where('user_id', $user->id)->delete();
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
/**
|
|
|
|
* Add a user to a given role
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param string $roleName
|
|
|
|
*
|
|
|
|
* @return User
|
|
|
|
*/
|
2021-04-23 22:33:13 +08:00
|
|
|
public function addUserToRole(User $user, string $roleName): User
|
2019-11-27 22:19:20 +08:00
|
|
|
{
|
2019-12-22 12:46:41 +08:00
|
|
|
$role = Role::where(['name' => $roleName])->first();
|
2019-11-27 22:19:20 +08:00
|
|
|
$user->attachRole($role);
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find and return the next available pilot ID (usually just the max+1)
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getNextAvailablePilotId(): int
|
|
|
|
{
|
|
|
|
return (int) User::max('pilot_id') + 1;
|
|
|
|
}
|
|
|
|
|
2019-07-17 01:54:14 +08:00
|
|
|
/**
|
|
|
|
* Find the next available pilot ID and set the current user's pilot_id to that +1
|
|
|
|
* Called from UserObserver right now after a record is created
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function findAndSetPilotId(User $user): User
|
|
|
|
{
|
|
|
|
if ($user->pilot_id !== null && $user->pilot_id > 0) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
$user->pilot_id = $this->getNextAvailablePilotId();
|
2019-07-17 01:54:14 +08:00
|
|
|
$user->save();
|
|
|
|
|
2019-07-17 02:06:13 +08:00
|
|
|
Log::info('Set pilot ID for user '.$user->id.' to '.$user->pilot_id);
|
2019-07-17 01:54:14 +08:00
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2020-02-24 08:48:28 +08:00
|
|
|
/**
|
|
|
|
* Return true or false if a pilot ID already exists
|
|
|
|
*
|
|
|
|
* @param int $pilot_id
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-07-17 21:48:20 +08:00
|
|
|
public function isPilotIdAlreadyUsed(int $pilot_id): bool
|
|
|
|
{
|
|
|
|
return User::where('pilot_id', '=', $pilot_id)->exists();
|
|
|
|
}
|
|
|
|
|
2019-07-17 01:54:14 +08:00
|
|
|
/**
|
|
|
|
* Change a user's pilot ID
|
|
|
|
*
|
|
|
|
* @param User $user
|
2019-07-17 02:06:13 +08:00
|
|
|
* @param int $pilot_id
|
2019-07-17 01:54:14 +08:00
|
|
|
*
|
|
|
|
* @throws UserPilotIdExists
|
|
|
|
*
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function changePilotId(User $user, int $pilot_id): User
|
|
|
|
{
|
2019-07-17 21:48:20 +08:00
|
|
|
if ($user->pilot_id === $pilot_id) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isPilotIdAlreadyUsed($pilot_id)) {
|
2019-07-17 01:54:14 +08:00
|
|
|
Log::error('User with id '.$pilot_id.' already exists');
|
2019-07-17 02:06:13 +08:00
|
|
|
|
2019-08-21 20:17:44 +08:00
|
|
|
throw new UserPilotIdExists($user);
|
2019-07-17 01:54:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$old_id = $user->pilot_id;
|
|
|
|
$user->pilot_id = $pilot_id;
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
Log::info('Changed pilot ID for user '.$user->id.' from '.$old_id.' to '.$user->pilot_id);
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2020-05-16 23:46:25 +08:00
|
|
|
/**
|
|
|
|
* Split a given pilot ID into an airline and ID portions
|
|
|
|
*
|
|
|
|
* @param string $pilot_id
|
2020-07-15 10:02:35 +08:00
|
|
|
*
|
|
|
|
* @return User
|
2020-05-16 23:46:25 +08:00
|
|
|
*/
|
2020-07-15 10:02:35 +08:00
|
|
|
public function findUserByPilotId(string $pilot_id): User
|
2020-05-16 23:46:25 +08:00
|
|
|
{
|
2020-07-15 10:02:35 +08:00
|
|
|
$pilot_id = trim($pilot_id);
|
|
|
|
if (empty($pilot_id)) {
|
|
|
|
throw new PilotIdNotFound('');
|
|
|
|
}
|
|
|
|
|
2020-05-16 23:46:25 +08:00
|
|
|
$airlines = $this->airlineRepo->all(['id', 'icao', 'iata']);
|
|
|
|
|
|
|
|
$ident_str = null;
|
|
|
|
$pilot_id = strtoupper($pilot_id);
|
2020-07-15 10:02:35 +08:00
|
|
|
|
|
|
|
/** @var Airline $airline */
|
2020-05-16 23:46:25 +08:00
|
|
|
foreach ($airlines as $airline) {
|
|
|
|
if (strpos($pilot_id, $airline->icao) !== false) {
|
|
|
|
$ident_str = $airline->icao;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-15 10:02:35 +08:00
|
|
|
if (!empty($airline->iata)) {
|
|
|
|
if (strpos($pilot_id, $airline->iata) !== false) {
|
|
|
|
$ident_str = $airline->iata;
|
|
|
|
break;
|
|
|
|
}
|
2020-05-16 23:46:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($ident_str)) {
|
|
|
|
throw new PilotIdNotFound($pilot_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsed_pilot_id = str_replace($ident_str, '', $pilot_id);
|
|
|
|
$user = User::where(['airline_id' => $airline->id, 'pilot_id' => $parsed_pilot_id])->first();
|
|
|
|
if (empty($user)) {
|
|
|
|
throw new PilotIdNotFound($pilot_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2021-02-18 07:54:18 +08:00
|
|
|
/**
|
|
|
|
* Return all of the users that are determined to be on leave. Only goes through the
|
|
|
|
* currently active users. If the user doesn't have a PIREP, then the creation date
|
|
|
|
* of the user record is used to determine the difference
|
|
|
|
*/
|
|
|
|
public function findUsersOnLeave(): array
|
|
|
|
{
|
|
|
|
$leave_days = setting('pilots.auto_leave_days');
|
|
|
|
if ($leave_days === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_users = [];
|
|
|
|
|
|
|
|
$date = Carbon::now('UTC');
|
2021-02-23 00:23:50 +08:00
|
|
|
$users = User::with(['last_pirep'])->where('state', UserState::ACTIVE)->get();
|
2021-02-18 07:54:18 +08:00
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
foreach ($users as $user) {
|
|
|
|
// If they haven't submitted a PIREP, use the date that the user was created
|
|
|
|
if (!$user->last_pirep) {
|
|
|
|
$diff_date = $user->created_at;
|
|
|
|
} else {
|
|
|
|
$diff_date = $user->last_pirep->submitted_at;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if the difference is larger than what the setting calls for
|
2021-03-20 01:09:29 +08:00
|
|
|
if ($date->diffInDays($diff_date) <= $leave_days) {
|
|
|
|
continue;
|
2021-02-18 07:54:18 +08:00
|
|
|
}
|
2021-03-20 01:09:29 +08:00
|
|
|
|
|
|
|
$skip = false;
|
|
|
|
// If any role for this user has the "disable_activity_check" feature activated, skip this user
|
|
|
|
foreach ($user->roles()->get() as $role) {
|
|
|
|
/** @var Role $role */
|
|
|
|
if ($role->disable_activity_checks) {
|
|
|
|
$skip = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($skip) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$return_users[] = $user;
|
2021-02-18 07:54:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $return_users;
|
|
|
|
}
|
|
|
|
|
2018-01-11 08:40:01 +08:00
|
|
|
/**
|
|
|
|
* Return the subfleets this user is allowed access to,
|
|
|
|
* based on their current rank
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-11 08:40:01 +08:00
|
|
|
* @param $user
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-11 08:40:01 +08:00
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getAllowableSubfleets($user)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($user === null || setting('pireps.restrict_aircraft_to_rank') === false) {
|
2020-09-30 01:21:24 +08:00
|
|
|
/** @var Collection $subfleets */
|
|
|
|
$subfleets = $this->subfleetRepo->with('aircraft')->all();
|
|
|
|
} else {
|
|
|
|
/** @var Collection $subfleets */
|
|
|
|
$subfleets = $user->rank->subfleets()->with('aircraft')->get();
|
2018-01-20 01:46:30 +08:00
|
|
|
}
|
|
|
|
|
2020-09-30 01:21:24 +08:00
|
|
|
// Map the subfleets with the proper fare information
|
|
|
|
return $subfleets->transform(function ($sf, $key) {
|
|
|
|
$sf->fares = $this->fareSvc->getForSubfleet($sf);
|
|
|
|
return $sf;
|
|
|
|
});
|
2018-01-11 08:40:01 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 02:59:49 +08:00
|
|
|
/**
|
|
|
|
* Return a bool if a user is allowed to fly the current aircraft
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 02:59:49 +08:00
|
|
|
* @param $user
|
|
|
|
* @param $aircraft_id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 02:59:49 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function aircraftAllowed($user, $aircraft_id)
|
|
|
|
{
|
|
|
|
$aircraft = $this->aircraftRepo->find($aircraft_id, ['subfleet_id']);
|
|
|
|
$subfleets = $this->getAllowableSubfleets($user);
|
|
|
|
$subfleet_ids = $subfleets->pluck('id')->toArray();
|
|
|
|
|
|
|
|
return \in_array($aircraft->subfleet_id, $subfleet_ids, true);
|
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
/**
|
|
|
|
* Change the user's state. PENDING to ACCEPTED, etc
|
|
|
|
* Send out an email
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @param User $user
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $old_state
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function changeUserState(User $user, $old_state): User
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($user->state === $old_state) {
|
2017-12-23 06:55:47 +08:00
|
|
|
return $user;
|
2017-12-23 06:32:21 +08:00
|
|
|
}
|
|
|
|
|
2019-07-17 01:54:14 +08:00
|
|
|
Log::info('User '.$user->ident.' state changing from '
|
2018-03-20 09:50:40 +08:00
|
|
|
.UserState::label($old_state).' to '
|
|
|
|
.UserState::label($user->state));
|
2017-12-23 06:32:21 +08:00
|
|
|
|
|
|
|
event(new UserStateChanged($user, $old_state));
|
2018-01-04 05:41:21 +08:00
|
|
|
|
|
|
|
return $user;
|
2017-12-23 06:32:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjust the number of flights a user has. Triggers
|
|
|
|
* UserStatsChanged event
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @param User $user
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param int $count
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @return User
|
|
|
|
*/
|
2017-12-13 06:58:27 +08:00
|
|
|
public function adjustFlightCount(User $user, int $count): User
|
2017-07-04 14:05:37 +08:00
|
|
|
{
|
2017-12-13 06:58:27 +08:00
|
|
|
$user->refresh();
|
2017-12-23 06:32:21 +08:00
|
|
|
$old_value = $user->flights;
|
2017-12-13 06:58:27 +08:00
|
|
|
$user->flights += $count;
|
|
|
|
$user->save();
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
event(new UserStatsChanged($user, 'flights', $old_value));
|
2017-12-03 00:55:17 +08:00
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
return $user;
|
2017-07-04 14:05:37 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
/**
|
|
|
|
* Update a user's flight times
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @param User $user
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param int $minutes
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @return User
|
|
|
|
*/
|
2017-12-14 00:56:26 +08:00
|
|
|
public function adjustFlightTime(User $user, int $minutes): User
|
2017-07-04 14:05:37 +08:00
|
|
|
{
|
2017-12-13 06:58:27 +08:00
|
|
|
$user->refresh();
|
2017-12-14 00:56:26 +08:00
|
|
|
$user->flight_time += $minutes;
|
2017-12-13 06:58:27 +08:00
|
|
|
$user->save();
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
return $user;
|
2017-07-04 14:05:37 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
/**
|
|
|
|
* See if a pilot's rank has change. Triggers the UserStatsChanged event
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @param User $user
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @return User
|
|
|
|
*/
|
2017-12-13 06:58:27 +08:00
|
|
|
public function calculatePilotRank(User $user): User
|
2017-07-04 14:05:37 +08:00
|
|
|
{
|
2017-12-13 06:58:27 +08:00
|
|
|
$user->refresh();
|
2018-02-11 07:53:25 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// If their current rank is one they were assigned, then
|
|
|
|
// don't change away from it automatically.
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($user->rank && $user->rank->auto_promote === false) {
|
2018-02-11 07:53:25 +08:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2019-08-09 00:50:43 +08:00
|
|
|
// If we should count their transfer hours?
|
2019-08-11 08:42:35 +08:00
|
|
|
if (setting('pilots.count_transfer_hours', false) === true) {
|
2019-08-09 00:50:43 +08:00
|
|
|
$pilot_hours = new Time($user->flight_time + $user->transfer_time);
|
|
|
|
} else {
|
|
|
|
$pilot_hours = new Time($user->flight_time);
|
|
|
|
}
|
2018-03-01 05:01:23 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// The current rank's hours are over the pilot's current hours,
|
|
|
|
// so assume that they were "placed" here by an admin so don't
|
|
|
|
// bother with updating it
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($user->rank && $user->rank->hours > $pilot_hours->hours) {
|
2018-03-01 05:01:23 +08:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
$old_rank = $user->rank;
|
|
|
|
$original_rank_id = $user->rank_id;
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
$ranks = Rank::where('auto_promote', true)
|
2018-03-20 09:50:40 +08:00
|
|
|
->orderBy('hours', 'asc')->get();
|
2017-07-04 14:05:37 +08:00
|
|
|
|
|
|
|
foreach ($ranks as $rank) {
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($rank->hours > $pilot_hours->hours) {
|
2017-07-04 14:05:37 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-08-20 22:48:39 +08:00
|
|
|
|
|
|
|
$user->rank_id = $rank->id;
|
2017-07-04 14:05:37 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
// Only trigger the event/update if there's been a change
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($user->rank_id !== $original_rank_id) {
|
2017-12-23 06:32:21 +08:00
|
|
|
$user->save();
|
|
|
|
$user->refresh();
|
|
|
|
event(new UserStatsChanged($user, 'rank', $old_rank));
|
|
|
|
}
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
return $user;
|
|
|
|
}
|
2017-12-03 00:55:17 +08:00
|
|
|
|
2018-03-31 11:28:19 +08:00
|
|
|
/**
|
|
|
|
* Set the user's status to being on leave
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-31 11:28:19 +08:00
|
|
|
* @param User $user
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-31 11:28:19 +08:00
|
|
|
* @return User
|
|
|
|
*/
|
2018-03-31 11:36:01 +08:00
|
|
|
public function setStatusOnLeave(User $user): User
|
2018-03-31 11:28:19 +08:00
|
|
|
{
|
|
|
|
$user->refresh();
|
|
|
|
$user->state = UserState::ON_LEAVE;
|
|
|
|
$user->save();
|
|
|
|
|
2021-02-18 07:57:16 +08:00
|
|
|
event(new UserStateChanged($user, UserState::ON_LEAVE));
|
2018-03-31 11:28:19 +08:00
|
|
|
|
|
|
|
$user->refresh();
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2019-08-11 08:42:35 +08:00
|
|
|
/**
|
|
|
|
* Recalculate the stats for all active users
|
|
|
|
*/
|
|
|
|
public function recalculateAllUserStats(): void
|
|
|
|
{
|
|
|
|
$w = [
|
|
|
|
['state', '!=', UserState::REJECTED],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->userRepo
|
|
|
|
->findWhere($w, ['id', 'name', 'airline_id'])
|
|
|
|
->each(function ($user, $_) {
|
|
|
|
return $this->recalculateStats($user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
/**
|
|
|
|
* Recount/update all of the stats for a user
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @param User $user
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function recalculateStats(User $user): User
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
// Recalc their hours
|
2018-08-20 22:42:54 +08:00
|
|
|
$w = [
|
|
|
|
'user_id' => $user->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'state' => PirepState::ACCEPTED,
|
2018-08-20 22:42:54 +08:00
|
|
|
];
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
$pirep_count = Pirep::where($w)->count();
|
|
|
|
$user->flights = $pirep_count;
|
2019-08-02 03:23:59 +08:00
|
|
|
|
2018-08-20 22:42:54 +08:00
|
|
|
$flight_time = Pirep::where($w)->sum('flight_time');
|
|
|
|
$user->flight_time = $flight_time;
|
|
|
|
|
2019-08-11 08:42:35 +08:00
|
|
|
$user->save();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Recalc the rank
|
2018-08-20 22:42:54 +08:00
|
|
|
$this->calculatePilotRank($user);
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
Log::info('User '.$user->ident.' updated; pirep count='.$pirep_count
|
2019-08-11 08:42:35 +08:00
|
|
|
.', rank='.$user->rank->name
|
|
|
|
.', flight_time='.$user->flight_time.' minutes');
|
2018-08-20 22:42:54 +08:00
|
|
|
|
|
|
|
$user->save();
|
2017-12-13 06:58:27 +08:00
|
|
|
return $user;
|
2017-07-04 14:05:37 +08:00
|
|
|
}
|
|
|
|
}
|