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

@@ -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));
}
}