Fix pilot leave calculation #1022 (#1035)

* Fix pilot leave calculation #1022

* Remove unused imports

* Add logging where fares are being saved
This commit is contained in:
Nabeel S
2021-02-17 18:54:18 -05:00
committed by GitHub
parent 8907527872
commit cbb7d6e274
7 changed files with 114 additions and 24 deletions

View File

@@ -4,10 +4,8 @@ namespace App\Cron\Nightly;
use App\Contracts\Listener;
use App\Events\CronNightly;
use App\Models\Enums\UserState;
use App\Models\User;
use App\Services\UserService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
/**
* Determine if any pilots should be set to ON LEAVE status
@@ -18,6 +16,8 @@ class PilotLeave extends Listener
/**
* PilotLeave constructor.
*
* @param UserService $userSvc
*/
public function __construct(UserService $userSvc)
{
@@ -34,14 +34,7 @@ class PilotLeave extends Listener
*/
public function handle(CronNightly $event): void
{
if (setting('pilots.auto_leave_days') === 0) {
return;
}
$date = Carbon::now()->subDay(setting('pilots.auto_leave_days'));
$users = User::where('status', UserState::ACTIVE)
->whereDate('updated_at', '<', $date);
$users = $this->userSvc->findUsersOnLeave();
foreach ($users as $user) {
Log::info('Setting user '.$user->ident.' to ON LEAVE status');
$this->userSvc->setStatusOnLeave($user);

View File

@@ -109,6 +109,7 @@ class SimBriefController
$loadmax = $lfactor + $lfactorv;
$loadmax = $loadmax > 100 ? 100 : $loadmax;
// Failsafe for admins not defining load values for their flights
// and also leave the general settings empty, set loadmax to 100
if ($loadmax === 0) {

View File

@@ -4,6 +4,14 @@ namespace App\Models;
use App\Contracts\Model;
/**
* @property int id
* @property string pirep_id
* @property int fare_id
* @property int count
* @property Pirep pirep
* @property Fare fare
*/
class PirepFare extends Model
{
public $table = 'pirep_fares';

View File

@@ -286,9 +286,7 @@ class FareService extends Service
*/
public function getForPirep(Pirep $pirep)
{
$found_fares = PirepFare::where('pirep_id', $pirep->id)->get();
return $found_fares;
return PirepFare::where('pirep_id', $pirep->id)->get();
}
/**
@@ -313,6 +311,8 @@ class FareService extends Service
$fare['pirep_id'] = $pirep->id;
// other fields: ['fare_id', 'count']
Log::info('Saving fare pirep='.$pirep->id.', fare='.$fare['count']);
$field = new PirepFare($fare);
$field->save();
}

View File

@@ -21,6 +21,7 @@ use App\Repositories\SubfleetRepository;
use App\Repositories\UserRepository;
use App\Support\Units\Time;
use App\Support\Utils;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use function is_array;
@@ -254,6 +255,41 @@ class UserService extends Service
return $user;
}
/**
* 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');
$users = User::with(['last_pirep'])->where('status', UserState::ACTIVE)->get();
/** @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
if ($date->diffInDays($diff_date) > $leave_days) {
$return_users[] = $user;
}
}
return $return_users;
}
/**
* Return the subfleets this user is allowed access to,
* based on their current rank

View File

@@ -3,7 +3,9 @@
namespace Tests;
use App\Models\Aircraft;
use App\Models\Enums\UserState;
use App\Models\Flight;
use App\Models\Pirep;
use App\Models\Subfleet;
use App\Models\User;
use Exception;
@@ -21,34 +23,37 @@ trait TestData
{
$subfleet = $this->createSubfleetWithAircraft(1);
$rank = $this->createRank(2, [$subfleet['subfleet']->id]);
$user = factory(User::class)->create(array_merge([
return factory(User::class)->create(array_merge([
'flight_time' => 1000,
'rank_id' => $rank->id,
'state' => UserState::ACTIVE,
], $attrs));
return $user;
}
/**
* Create a new PIREP with a proper subfleet/rank/user and an
* aircraft that the user is allowed to fly
*
* @param array $user_attrs Additional attributes for the user
* @param array $pirep_attrs Additional attributes for the PIREP
*
* @throws \Exception
*
* @return \App\Models\Pirep
*/
protected function createPirep()
protected function createPirep(array $user_attrs = [], array $pirep_attrs = [])
{
$subfleet = $this->createSubfleetWithAircraft(2);
$rank = $this->createRank(10, [$subfleet['subfleet']->id]);
$this->user = factory(\App\Models\User::class)->create([
$this->user = factory(\App\Models\User::class)->create(array_merge([
'rank_id' => $rank->id,
]);
], $user_attrs));
// Return a Pirep model
$pirep = factory(\App\Models\Pirep::class)->make([
return factory(Pirep::class)->make(array_merge([
'aircraft_id' => $subfleet['aircraft']->random()->id,
]);
return $pirep;
], $pirep_attrs));
}
/**

View File

@@ -5,11 +5,14 @@ namespace Tests;
use App\Exceptions\PilotIdNotFound;
use App\Exceptions\UserPilotIdExists;
use App\Models\Airline;
use App\Models\Enums\UserState;
use App\Models\Fare;
use App\Models\Pirep;
use App\Models\User;
use App\Repositories\SettingRepository;
use App\Services\FareService;
use App\Services\UserService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
class UserTest extends TestCase
@@ -324,4 +327,48 @@ class UserTest extends TestCase
$this->assertEquals($expected, $user->name_private);
}
}
/**
* @throws \Exception
*/
public function testUserLeave(): void
{
$this->createUser(['status' => UserState::ACTIVE]);
$users_on_leave = $this->userSvc->findUsersOnLeave();
$this->assertEquals(0, count($users_on_leave));
$this->updateSetting('pilots.auto_leave_days', 1);
$user = $this->createUser([
'status' => UserState::ACTIVE,
'created_at' => Carbon::now('UTC')->subDays(5),
]);
$users_on_leave = $this->userSvc->findUsersOnLeave();
$this->assertEquals(1, count($users_on_leave));
$this->assertEquals($user->id, $users_on_leave[0]->id);
// Give that user a new PIREP, still old
/** @var \App\Models\Pirep $pirep */
$pirep = factory(Pirep::class)->create(['submitted_at' => Carbon::now('UTC')->subDays(5)]);
$user->last_pirep_id = $pirep->id;
$user->save();
$user->refresh();
$users_on_leave = $this->userSvc->findUsersOnLeave();
$this->assertEquals(1, count($users_on_leave));
$this->assertEquals($user->id, $users_on_leave[0]->id);
// Create a new PIREP
/** @var \App\Models\Pirep $pirep */
$pirep = factory(Pirep::class)->create(['submitted_at' => Carbon::now('UTC')]);
$user->last_pirep_id = $pirep->id;
$user->save();
$user->refresh();
$users_on_leave = $this->userSvc->findUsersOnLeave();
$this->assertEquals(0, count($users_on_leave));
}
}