Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bids are missing subfleets in API response #867 #868

Merged
merged 3 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function get($id)
*/
public function bids(Request $request)
{
$user = $this->userRepo->find($this->getUserId($request));
$user = $this->userSvc->getUser($this->getUserId($request));

// Add a bid
if ($request->isMethod('PUT') || $request->isMethod('POST')) {
Expand Down
22 changes: 21 additions & 1 deletion app/Services/BidService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

class BidService extends Service
{
/** @var FlightService */
private $flightSvc;

public function __construct(FlightService $flightSvc)
{
$this->flightSvc = $flightSvc;
}

/**
* Get a specific bid for a user
*
Expand All @@ -35,8 +43,20 @@ public function getBid($bid_id)
*/
public function findBidsForUser(User $user)
{
return Bid::with(['flight', 'flight.simbrief'])
$bids = Bid::with([
'flight',
'flight.simbrief',
'flight.subfleets',
'flight.subfleets.aircraft',
'flight.subfleets.fares',
])
->where(['user_id' => $user->id])->get();

foreach ($bids as $bid) {
$bid->flight = $this->flightSvc->filterSubfleets($user, $bid->flight);
}

return $bids;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions app/Services/FlightService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Enums\Days;
use App\Models\Flight;
use App\Models\FlightFieldValue;
use App\Models\User;
use App\Repositories\FlightRepository;
use App\Repositories\NavdataRepository;
use App\Support\Units\Time;
Expand Down Expand Up @@ -123,12 +124,12 @@ protected function transformFlightFields($fields)
/**
* Filter out subfleets to only include aircraft that a user has access to
*
* @param $user
* @param $flight
* @param User $user
* @param Flight $flight
*
* @return mixed
*/
public function filterSubfleets($user, $flight)
public function filterSubfleets(User $user, Flight $flight)
{
/** @var \Illuminate\Support\Collection $subfleets */
$subfleets = $flight->subfleets;
Expand Down
2 changes: 1 addition & 1 deletion app/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getUser($user_id): User
*
* @return User
*/
public function createUser(array $attrs, array $roles = null): User
public function createUser(array $attrs, array $roles = []): User
{
$user = User::create($attrs);
$user->api_key = Utils::generateApiKey();
Expand Down
54 changes: 44 additions & 10 deletions tests/BidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@

use App\Exceptions\BidExistsForFlight;
use App\Models\Bid;
use App\Models\Fare;
use App\Models\Flight;
use App\Models\Subfleet;
use App\Models\User;
use App\Repositories\SettingRepository;
use App\Services\BidService;
use App\Services\FareService;
use App\Services\FlightService;

class BidTest extends TestCase
{
/** @var BidService */
protected $bidSvc;

/** @var FlightService */
protected $flightSvc;

/** @var SettingRepository */
protected $settingsRepo;

public function setUp(): void
Expand All @@ -27,36 +34,56 @@ public function setUp(): void
$this->settingsRepo = app(SettingRepository::class);
}

public function addFlight($user)
public function addFlight($user, $subfleet = null)
{
$flight = factory(Flight::class)->create([
'airline_id' => $user->airline_id,
]);

$flight->subfleets()->syncWithoutDetaching([
factory(Subfleet::class)->create([
if ($subfleet === null) {
/** @var Subfleet $subfleet */
$subfleet = factory(Subfleet::class)->create([
'airline_id' => $user->airline_id,
])->id,
]);
])->id;
}

$flight->subfleets()->syncWithoutDetaching([$subfleet]);

return $flight;
}

/**
* Add/remove a bid, test the API, etc
*
* @throws \App\Services\Exception
* @throws Exception|\Exception
*/
public function testBids()
{
$this->settingsRepo->store('bids.allow_multiple_bids', true);
$this->settingsRepo->store('bids.disable_flight_on_bid', false);

$user = factory(User::class)->create();
$user2 = factory(User::class)->create();
$subfleet = $this->createSubfleetWithAircraft(2);
$rank = $this->createRank(2, [$subfleet['subfleet']->id]);

/** @var FareService $fare_svc */
$fare_svc = app(FareService::class);

/** @var Fare $fare */
$fare = factory(Fare::class)->create();
$fare_svc->setForSubfleet($subfleet['subfleet'], $fare, [
'price' => 50, 'capacity' => 400,
]);

/** @var User $user */
$user = factory(User::class)->create([
'flight_time' => 1000,
'rank_id' => $rank->id,
]);

$headers = $this->headers($user);

$flight = $this->addFlight($user);
/** @var Flight $flight */
$flight = $this->addFlight($user, $subfleet['subfleet']->id);

$bid = $this->bidSvc->addBid($flight, $user);
$this->assertEquals($user->id, $bid->user_id);
Expand All @@ -78,19 +105,26 @@ public function testBids()
// Query the API and see that the user has the bids
// And pull the flight details for the user/bids
$req = $this->get('/api/user/bids', $headers);
$req->assertStatus(200);

$body = $req->json()['data'];
$req->assertStatus(200);
$this->assertEquals($flight->id, $body[0]['flight_id']);

// Make sure subfleets and fares are included
$this->assertNotNull($body[0]['flight']['subfleets']);
$this->assertNotNull($body[0]['flight']['subfleets'][0]['fares']);

$req = $this->get('/api/users/'.$user->id.'/bids', $headers);

$body = $req->json()['data'];
$req->assertStatus(200);
$this->assertEquals($flight->id, $body[0]['flight_id']);

// have a second user bid on it

/** @var User $user */
$user2 = factory(User::class)->create(['rank_id' => $rank->id]);

$bid_user2 = $this->bidSvc->addBid($flight, $user2);
$this->assertNotNull($bid_user2);
$this->assertNotEquals($bid_retrieved->id, $bid_user2->id);
Expand Down
10 changes: 5 additions & 5 deletions tests/SimBriefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function loadSimBrief($user): SimBrief
*/
public function testReadSimbrief()
{
$this->user = factory(User::class)->create();
$this->user = $this->createUser();
$briefing = $this->loadSimBrief($this->user);

$this->assertNotEmpty($briefing->ofp_xml);
Expand Down Expand Up @@ -86,7 +86,7 @@ public function testReadSimbrief()
*/
public function testApiCalls()
{
$this->user = factory(User::class)->create();
$this->user = $this->createUser();
$briefing = $this->loadSimBrief($this->user);

// Check the flight API response
Expand Down Expand Up @@ -120,7 +120,7 @@ public function testApiCalls()
*/
public function testUserBidSimbrief()
{
$this->user = factory(User::class)->create();
$this->user = $this->createUser();
$this->loadSimBrief($this->user);

// Find the flight
Expand All @@ -136,7 +136,7 @@ public function testUserBidSimbrief()

public function testAttachToPirep()
{
$user = factory(User::class)->create();
$user = $this->createUser();
$pirep = factory(Pirep::class)->create([
'user_id' => $user->id,
'dpt_airport_id' => 'OMAA',
Expand Down Expand Up @@ -172,7 +172,7 @@ public function testAttachToPirep()
*/
public function testClearExpiredBriefs()
{
$user = factory(User::class)->create();
$user = $this->createUser();
$sb_ignored = factory(SimBrief::class)->create([
'user_id' => $user->id,
'flight_id' => 'a_flight_id',
Expand Down
26 changes: 24 additions & 2 deletions tests/TestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,30 @@

use App\Models\Aircraft;
use App\Models\Subfleet;
use App\Models\User;
use Exception;

trait TestData
{
/**
* @param array $attrs Additional user attributes
*
* @throws Exception
*
* @return User
*/
public function createUser(array $attrs = []): User
{
$subfleet = $this->createSubfleetWithAircraft(1);
$rank = $this->createRank(2, [$subfleet['subfleet']->id]);
$user = factory(User::class)->create(array_merge([
'flight_time' => 1000,
'rank_id' => $rank->id,
], $attrs));

return $user;
}

/**
* Create a new PIREP with a proper subfleet/rank/user and an
* aircraft that the user is allowed to fly
Expand Down Expand Up @@ -37,7 +58,7 @@ protected function createPirep()
*
* @return mixed
*/
public function createRank($hours, array $subfleet_ids)
public function createRank(int $hours, array $subfleet_ids)
{
$attrs = [];

Expand All @@ -59,12 +80,13 @@ public function createRank($hours, array $subfleet_ids)
* @param null $aircraft_count
* @param null $airport_id
*
* @throws \Exception
* @throws Exception
*
* @return mixed
*/
public function createSubfleetWithAircraft($aircraft_count = null, $airport_id = null)
{
/** @var Subfleet $subfleet */
$subfleet = factory(Subfleet::class)->create([
'ground_handling_multiplier' => '100',
]);
Expand Down