Bids are missing subfleets in API response #867 (#868)

Bids are missing subfleets in API response #867
This commit is contained in:
Nabeel S
2020-10-12 12:49:11 -04:00
committed by GitHub
parent 193fbd369d
commit dc007f6d9e
7 changed files with 100 additions and 23 deletions
+1 -1
View File
@@ -105,7 +105,7 @@ class UserController extends Controller
*/ */
public function bids(Request $request) public function bids(Request $request)
{ {
$user = $this->userRepo->find($this->getUserId($request)); $user = $this->userSvc->getUser($this->getUserId($request));
// Add a bid // Add a bid
if ($request->isMethod('PUT') || $request->isMethod('POST')) { if ($request->isMethod('PUT') || $request->isMethod('POST')) {
+21 -1
View File
@@ -13,6 +13,14 @@ use Illuminate\Support\Facades\Log;
class BidService extends Service class BidService extends Service
{ {
/** @var FlightService */
private $flightSvc;
public function __construct(FlightService $flightSvc)
{
$this->flightSvc = $flightSvc;
}
/** /**
* Get a specific bid for a user * Get a specific bid for a user
* *
@@ -35,8 +43,20 @@ class BidService extends Service
*/ */
public function findBidsForUser(User $user) 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(); ->where(['user_id' => $user->id])->get();
foreach ($bids as $bid) {
$bid->flight = $this->flightSvc->filterSubfleets($user, $bid->flight);
}
return $bids;
} }
/** /**
+4 -3
View File
@@ -8,6 +8,7 @@ use App\Models\Bid;
use App\Models\Enums\Days; use App\Models\Enums\Days;
use App\Models\Flight; use App\Models\Flight;
use App\Models\FlightFieldValue; use App\Models\FlightFieldValue;
use App\Models\User;
use App\Repositories\FlightRepository; use App\Repositories\FlightRepository;
use App\Repositories\NavdataRepository; use App\Repositories\NavdataRepository;
use App\Support\Units\Time; use App\Support\Units\Time;
@@ -123,12 +124,12 @@ class FlightService extends Service
/** /**
* Filter out subfleets to only include aircraft that a user has access to * Filter out subfleets to only include aircraft that a user has access to
* *
* @param $user * @param User $user
* @param $flight * @param Flight $flight
* *
* @return mixed * @return mixed
*/ */
public function filterSubfleets($user, $flight) public function filterSubfleets(User $user, Flight $flight)
{ {
/** @var \Illuminate\Support\Collection $subfleets */ /** @var \Illuminate\Support\Collection $subfleets */
$subfleets = $flight->subfleets; $subfleets = $flight->subfleets;
+1 -1
View File
@@ -85,7 +85,7 @@ class UserService extends Service
* *
* @return 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 = User::create($attrs);
$user->api_key = Utils::generateApiKey(); $user->api_key = Utils::generateApiKey();
+44 -10
View File
@@ -4,17 +4,24 @@ namespace Tests;
use App\Exceptions\BidExistsForFlight; use App\Exceptions\BidExistsForFlight;
use App\Models\Bid; use App\Models\Bid;
use App\Models\Fare;
use App\Models\Flight; use App\Models\Flight;
use App\Models\Subfleet; use App\Models\Subfleet;
use App\Models\User; use App\Models\User;
use App\Repositories\SettingRepository; use App\Repositories\SettingRepository;
use App\Services\BidService; use App\Services\BidService;
use App\Services\FareService;
use App\Services\FlightService; use App\Services\FlightService;
class BidTest extends TestCase class BidTest extends TestCase
{ {
/** @var BidService */
protected $bidSvc; protected $bidSvc;
/** @var FlightService */
protected $flightSvc; protected $flightSvc;
/** @var SettingRepository */
protected $settingsRepo; protected $settingsRepo;
public function setUp(): void public function setUp(): void
@@ -27,17 +34,20 @@ class BidTest extends TestCase
$this->settingsRepo = app(SettingRepository::class); $this->settingsRepo = app(SettingRepository::class);
} }
public function addFlight($user) public function addFlight($user, $subfleet = null)
{ {
$flight = factory(Flight::class)->create([ $flight = factory(Flight::class)->create([
'airline_id' => $user->airline_id, 'airline_id' => $user->airline_id,
]); ]);
$flight->subfleets()->syncWithoutDetaching([ if ($subfleet === null) {
factory(Subfleet::class)->create([ /** @var Subfleet $subfleet */
$subfleet = factory(Subfleet::class)->create([
'airline_id' => $user->airline_id, 'airline_id' => $user->airline_id,
])->id, ])->id;
]); }
$flight->subfleets()->syncWithoutDetaching([$subfleet]);
return $flight; return $flight;
} }
@@ -45,18 +55,35 @@ class BidTest extends TestCase
/** /**
* Add/remove a bid, test the API, etc * Add/remove a bid, test the API, etc
* *
* @throws \App\Services\Exception * @throws Exception|\Exception
*/ */
public function testBids() public function testBids()
{ {
$this->settingsRepo->store('bids.allow_multiple_bids', true); $this->settingsRepo->store('bids.allow_multiple_bids', true);
$this->settingsRepo->store('bids.disable_flight_on_bid', false); $this->settingsRepo->store('bids.disable_flight_on_bid', false);
$user = factory(User::class)->create(); $subfleet = $this->createSubfleetWithAircraft(2);
$user2 = factory(User::class)->create(); $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); $headers = $this->headers($user);
$flight = $this->addFlight($user); /** @var Flight $flight */
$flight = $this->addFlight($user, $subfleet['subfleet']->id);
$bid = $this->bidSvc->addBid($flight, $user); $bid = $this->bidSvc->addBid($flight, $user);
$this->assertEquals($user->id, $bid->user_id); $this->assertEquals($user->id, $bid->user_id);
@@ -78,12 +105,15 @@ class BidTest extends TestCase
// Query the API and see that the user has the bids // Query the API and see that the user has the bids
// And pull the flight details for the user/bids // And pull the flight details for the user/bids
$req = $this->get('/api/user/bids', $headers); $req = $this->get('/api/user/bids', $headers);
$req->assertStatus(200);
$body = $req->json()['data']; $body = $req->json()['data'];
$req->assertStatus(200); $req->assertStatus(200);
$this->assertEquals($flight->id, $body[0]['flight_id']); $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); $req = $this->get('/api/users/'.$user->id.'/bids', $headers);
$body = $req->json()['data']; $body = $req->json()['data'];
@@ -91,6 +121,10 @@ class BidTest extends TestCase
$this->assertEquals($flight->id, $body[0]['flight_id']); $this->assertEquals($flight->id, $body[0]['flight_id']);
// have a second user bid on it // 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); $bid_user2 = $this->bidSvc->addBid($flight, $user2);
$this->assertNotNull($bid_user2); $this->assertNotNull($bid_user2);
$this->assertNotEquals($bid_retrieved->id, $bid_user2->id); $this->assertNotEquals($bid_retrieved->id, $bid_user2->id);
+5 -5
View File
@@ -47,7 +47,7 @@ class SimBriefTest extends TestCase
*/ */
public function testReadSimbrief() public function testReadSimbrief()
{ {
$this->user = factory(User::class)->create(); $this->user = $this->createUser();
$briefing = $this->loadSimBrief($this->user); $briefing = $this->loadSimBrief($this->user);
$this->assertNotEmpty($briefing->ofp_xml); $this->assertNotEmpty($briefing->ofp_xml);
@@ -86,7 +86,7 @@ class SimBriefTest extends TestCase
*/ */
public function testApiCalls() public function testApiCalls()
{ {
$this->user = factory(User::class)->create(); $this->user = $this->createUser();
$briefing = $this->loadSimBrief($this->user); $briefing = $this->loadSimBrief($this->user);
// Check the flight API response // Check the flight API response
@@ -120,7 +120,7 @@ class SimBriefTest extends TestCase
*/ */
public function testUserBidSimbrief() public function testUserBidSimbrief()
{ {
$this->user = factory(User::class)->create(); $this->user = $this->createUser();
$this->loadSimBrief($this->user); $this->loadSimBrief($this->user);
// Find the flight // Find the flight
@@ -136,7 +136,7 @@ class SimBriefTest extends TestCase
public function testAttachToPirep() public function testAttachToPirep()
{ {
$user = factory(User::class)->create(); $user = $this->createUser();
$pirep = factory(Pirep::class)->create([ $pirep = factory(Pirep::class)->create([
'user_id' => $user->id, 'user_id' => $user->id,
'dpt_airport_id' => 'OMAA', 'dpt_airport_id' => 'OMAA',
@@ -172,7 +172,7 @@ class SimBriefTest extends TestCase
*/ */
public function testClearExpiredBriefs() public function testClearExpiredBriefs()
{ {
$user = factory(User::class)->create(); $user = $this->createUser();
$sb_ignored = factory(SimBrief::class)->create([ $sb_ignored = factory(SimBrief::class)->create([
'user_id' => $user->id, 'user_id' => $user->id,
'flight_id' => 'a_flight_id', 'flight_id' => 'a_flight_id',
+24 -2
View File
@@ -4,9 +4,30 @@ namespace Tests;
use App\Models\Aircraft; use App\Models\Aircraft;
use App\Models\Subfleet; use App\Models\Subfleet;
use App\Models\User;
use Exception;
trait TestData 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 * Create a new PIREP with a proper subfleet/rank/user and an
* aircraft that the user is allowed to fly * aircraft that the user is allowed to fly
@@ -37,7 +58,7 @@ trait TestData
* *
* @return mixed * @return mixed
*/ */
public function createRank($hours, array $subfleet_ids) public function createRank(int $hours, array $subfleet_ids)
{ {
$attrs = []; $attrs = [];
@@ -59,12 +80,13 @@ trait TestData
* @param null $aircraft_count * @param null $aircraft_count
* @param null $airport_id * @param null $airport_id
* *
* @throws \Exception * @throws Exception
* *
* @return mixed * @return mixed
*/ */
public function createSubfleetWithAircraft($aircraft_count = null, $airport_id = null) public function createSubfleetWithAircraft($aircraft_count = null, $airport_id = null)
{ {
/** @var Subfleet $subfleet */
$subfleet = factory(Subfleet::class)->create([ $subfleet = factory(Subfleet::class)->create([
'ground_handling_multiplier' => '100', 'ground_handling_multiplier' => '100',
]); ]);