From cbb7d6e27424252ad200467428444d10c0911813 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Wed, 17 Feb 2021 18:54:18 -0500 Subject: [PATCH] Fix pilot leave calculation #1022 (#1035) * Fix pilot leave calculation #1022 * Remove unused imports * Add logging where fares are being saved --- app/Cron/Nightly/PilotLeave.php | 15 ++---- .../Frontend/SimBriefController.php | 1 + app/Models/PirepFare.php | 8 ++++ app/Services/FareService.php | 6 +-- app/Services/UserService.php | 36 ++++++++++++++ tests/TestData.php | 25 ++++++---- tests/UserTest.php | 47 +++++++++++++++++++ 7 files changed, 114 insertions(+), 24 deletions(-) diff --git a/app/Cron/Nightly/PilotLeave.php b/app/Cron/Nightly/PilotLeave.php index 36973a3c..f49606c1 100644 --- a/app/Cron/Nightly/PilotLeave.php +++ b/app/Cron/Nightly/PilotLeave.php @@ -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); diff --git a/app/Http/Controllers/Frontend/SimBriefController.php b/app/Http/Controllers/Frontend/SimBriefController.php index b9be0d52..f087f065 100644 --- a/app/Http/Controllers/Frontend/SimBriefController.php +++ b/app/Http/Controllers/Frontend/SimBriefController.php @@ -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) { diff --git a/app/Models/PirepFare.php b/app/Models/PirepFare.php index 6a4b9c4d..d488ab07 100644 --- a/app/Models/PirepFare.php +++ b/app/Models/PirepFare.php @@ -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'; diff --git a/app/Services/FareService.php b/app/Services/FareService.php index 2c06d3ee..6be9d3ec 100644 --- a/app/Services/FareService.php +++ b/app/Services/FareService.php @@ -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(); } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 44f284ff..f341d280 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -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 diff --git a/tests/TestData.php b/tests/TestData.php index f5522eea..d25e9bba 100644 --- a/tests/TestData.php +++ b/tests/TestData.php @@ -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)); } /** diff --git a/tests/UserTest.php b/tests/UserTest.php index 925bdcdf..18978eec 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -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)); + } }