Fix subfleets not returning proper fare counts #831
This commit is contained in:
@@ -29,25 +29,27 @@ class UserService extends Service
|
||||
{
|
||||
private $aircraftRepo;
|
||||
private $airlineRepo;
|
||||
private $fareSvc;
|
||||
private $subfleetRepo;
|
||||
private $userRepo;
|
||||
|
||||
/**
|
||||
* UserService constructor.
|
||||
*
|
||||
* @param AircraftRepository $aircraftRepo
|
||||
* @param AirlineRepository $airlineRepo
|
||||
* @param FareService $fareSvc
|
||||
* @param SubfleetRepository $subfleetRepo
|
||||
* @param UserRepository $userRepo
|
||||
*/
|
||||
public function __construct(
|
||||
AircraftRepository $aircraftRepo,
|
||||
AirlineRepository $airlineRepo,
|
||||
FareService $fareSvc,
|
||||
SubfleetRepository $subfleetRepo,
|
||||
UserRepository $userRepo
|
||||
) {
|
||||
$this->aircraftRepo = $aircraftRepo;
|
||||
$this->airlineRepo = $airlineRepo;
|
||||
$this->fareSvc = $fareSvc;
|
||||
$this->subfleetRepo = $subfleetRepo;
|
||||
$this->userRepo = $userRepo;
|
||||
}
|
||||
@@ -243,11 +245,18 @@ class UserService extends Service
|
||||
public function getAllowableSubfleets($user)
|
||||
{
|
||||
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();
|
||||
return $subfleets->with('aircraft')->get();
|
||||
// Map the subfleets with the proper fare information
|
||||
return $subfleets->transform(function ($sf, $key) {
|
||||
$sf->fares = $this->fareSvc->getForSubfleet($sf);
|
||||
return $sf;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use App\Models\Subfleet;
|
||||
|
||||
trait TestData
|
||||
{
|
||||
/**
|
||||
@@ -62,7 +64,7 @@ trait TestData
|
||||
*/
|
||||
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',
|
||||
]);
|
||||
|
||||
|
||||
@@ -5,8 +5,10 @@ namespace Tests;
|
||||
use App\Exceptions\PilotIdNotFound;
|
||||
use App\Exceptions\UserPilotIdExists;
|
||||
use App\Models\Airline;
|
||||
use App\Models\Fare;
|
||||
use App\Models\User;
|
||||
use App\Repositories\SettingRepository;
|
||||
use App\Services\FareService;
|
||||
use App\Services\UserService;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
@@ -86,11 +88,25 @@ class UserTest extends TestCase
|
||||
*/
|
||||
public function testGetAllAircraft()
|
||||
{
|
||||
$fare_svc = app(FareService::class);
|
||||
|
||||
// Add subfleets and aircraft, but also add another
|
||||
// set of subfleets
|
||||
$subfleetA = $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(
|
||||
$subfleetA['aircraft']->pluck('id')->toArray(),
|
||||
$subfleetB['aircraft']->pluck('id')->toArray()
|
||||
@@ -114,19 +130,22 @@ class UserTest extends TestCase
|
||||
|
||||
$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
|
||||
*/
|
||||
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', true);
|
||||
|
||||
$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'];
|
||||
$aircraft_from_api = array_merge(
|
||||
collect($body[0]['aircraft'])->pluck('id')->toArray(),
|
||||
collect($body[1]['aircraft'])->pluck('id')->toArray()
|
||||
);
|
||||
|
||||
$this->assertEquals($added_aircraft, $aircraft_from_api);
|
||||
$subfleetAFromApi = collect($body)->firstWhere('id', $subfleetA['subfleet']->id);
|
||||
$this->assertEquals($subfleetAFromApi['fares'][0]['price'], $overrides['price']);
|
||||
$this->assertEquals($subfleetAFromApi['fares'][0]['capacity'], $overrides['capacity']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user