Fix subfleets not returning proper fare counts #831 (#846)

Fix subfleets not returning proper fare counts #831
This commit is contained in:
Nabeel S
2020-09-29 13:21:24 -04:00
committed by GitHub
parent b6603bd178
commit 98aa362935
3 changed files with 43 additions and 13 deletions

View File

@@ -29,25 +29,27 @@ class UserService extends Service
{ {
private $aircraftRepo; private $aircraftRepo;
private $airlineRepo; private $airlineRepo;
private $fareSvc;
private $subfleetRepo; private $subfleetRepo;
private $userRepo; private $userRepo;
/** /**
* UserService constructor.
*
* @param AircraftRepository $aircraftRepo * @param AircraftRepository $aircraftRepo
* @param AirlineRepository $airlineRepo * @param AirlineRepository $airlineRepo
* @param FareService $fareSvc
* @param SubfleetRepository $subfleetRepo * @param SubfleetRepository $subfleetRepo
* @param UserRepository $userRepo * @param UserRepository $userRepo
*/ */
public function __construct( public function __construct(
AircraftRepository $aircraftRepo, AircraftRepository $aircraftRepo,
AirlineRepository $airlineRepo, AirlineRepository $airlineRepo,
FareService $fareSvc,
SubfleetRepository $subfleetRepo, SubfleetRepository $subfleetRepo,
UserRepository $userRepo UserRepository $userRepo
) { ) {
$this->aircraftRepo = $aircraftRepo; $this->aircraftRepo = $aircraftRepo;
$this->airlineRepo = $airlineRepo; $this->airlineRepo = $airlineRepo;
$this->fareSvc = $fareSvc;
$this->subfleetRepo = $subfleetRepo; $this->subfleetRepo = $subfleetRepo;
$this->userRepo = $userRepo; $this->userRepo = $userRepo;
} }
@@ -243,11 +245,18 @@ class UserService extends Service
public function getAllowableSubfleets($user) public function getAllowableSubfleets($user)
{ {
if ($user === null || setting('pireps.restrict_aircraft_to_rank') === false) { if ($user === null || setting('pireps.restrict_aircraft_to_rank') === false) {
return $this->subfleetRepo->with('aircraft')->all(); /** @var Collection $subfleets */
$subfleets = $this->subfleetRepo->with('aircraft')->all();
} else {
/** @var Collection $subfleets */
$subfleets = $user->rank->subfleets()->with('aircraft')->get();
} }
$subfleets = $user->rank->subfleets(); // Map the subfleets with the proper fare information
return $subfleets->with('aircraft')->get(); return $subfleets->transform(function ($sf, $key) {
$sf->fares = $this->fareSvc->getForSubfleet($sf);
return $sf;
});
} }
/** /**

View File

@@ -2,6 +2,8 @@
namespace Tests; namespace Tests;
use App\Models\Subfleet;
trait TestData trait TestData
{ {
/** /**
@@ -62,7 +64,7 @@ trait TestData
*/ */
public function createSubfleetWithAircraft($aircraft_count = null, $airport_id = null) public function createSubfleetWithAircraft($aircraft_count = null, $airport_id = null)
{ {
$subfleet = factory(\App\Models\Subfleet::class)->create([ $subfleet = factory(Subfleet::class)->create([
'ground_handling_multiplier' => '100', 'ground_handling_multiplier' => '100',
]); ]);

View File

@@ -5,8 +5,10 @@ namespace Tests;
use App\Exceptions\PilotIdNotFound; use App\Exceptions\PilotIdNotFound;
use App\Exceptions\UserPilotIdExists; use App\Exceptions\UserPilotIdExists;
use App\Models\Airline; use App\Models\Airline;
use App\Models\Fare;
use App\Models\User; use App\Models\User;
use App\Repositories\SettingRepository; use App\Repositories\SettingRepository;
use App\Services\FareService;
use App\Services\UserService; use App\Services\UserService;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
@@ -86,11 +88,25 @@ class UserTest extends TestCase
*/ */
public function testGetAllAircraft() public function testGetAllAircraft()
{ {
$fare_svc = app(FareService::class);
// Add subfleets and aircraft, but also add another // Add subfleets and aircraft, but also add another
// set of subfleets // set of subfleets
$subfleetA = $this->createSubfleetWithAircraft(); $subfleetA = $this->createSubfleetWithAircraft();
$subfleetB = $this->createSubfleetWithAircraft(); $subfleetB = $this->createSubfleetWithAircraft();
$fare = factory(Fare::class)->create([
'price' => 20,
'capacity' => 200,
]);
$overrides = [
'price' => 50,
'capacity' => 400,
];
$fare_svc->setForSubfleet($subfleetA['subfleet'], $fare, $overrides);
$added_aircraft = array_merge( $added_aircraft = array_merge(
$subfleetA['aircraft']->pluck('id')->toArray(), $subfleetA['aircraft']->pluck('id')->toArray(),
$subfleetB['aircraft']->pluck('id')->toArray() $subfleetB['aircraft']->pluck('id')->toArray()
@@ -114,19 +130,22 @@ class UserTest extends TestCase
$this->assertEquals($added_aircraft, $all_aircraft); $this->assertEquals($added_aircraft, $all_aircraft);
$subfleetACalled = collect($subfleets)->firstWhere('id', $subfleetA['subfleet']->id);
$this->assertEquals($subfleetACalled->fares[0]['price'], $overrides['price']);
$this->assertEquals($subfleetACalled->fares[0]['capacity'], $overrides['capacity']);
/** /**
* Check via API * Check via API
*/ */
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', true);
$resp = $this->get('/api/user/fleet', [], $user)->assertStatus(200); $resp = $this->get('/api/user/fleet', [], $user)->assertStatus(200);
// Get all the aircraft from that subfleet // Get all the aircraft from that subfleet, check the fares
$body = $resp->json()['data']; $body = $resp->json()['data'];
$aircraft_from_api = array_merge( $subfleetAFromApi = collect($body)->firstWhere('id', $subfleetA['subfleet']->id);
collect($body[0]['aircraft'])->pluck('id')->toArray(), $this->assertEquals($subfleetAFromApi['fares'][0]['price'], $overrides['price']);
collect($body[1]['aircraft'])->pluck('id')->toArray() $this->assertEquals($subfleetAFromApi['fares'][0]['capacity'], $overrides['capacity']);
);
$this->assertEquals($added_aircraft, $aircraft_from_api);
} }
/** /**