diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index a4f3aeca..16f87734 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -105,7 +105,7 @@ class UserController extends Controller */ 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')) { diff --git a/app/Services/BidService.php b/app/Services/BidService.php index c3f84f0b..a06b522c 100644 --- a/app/Services/BidService.php +++ b/app/Services/BidService.php @@ -13,6 +13,14 @@ use Illuminate\Support\Facades\Log; class BidService extends Service { + /** @var FlightService */ + private $flightSvc; + + public function __construct(FlightService $flightSvc) + { + $this->flightSvc = $flightSvc; + } + /** * Get a specific bid for a user * @@ -35,8 +43,20 @@ class BidService extends Service */ 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; } /** diff --git a/app/Services/FlightService.php b/app/Services/FlightService.php index f9cb19c3..c08b0e9e 100644 --- a/app/Services/FlightService.php +++ b/app/Services/FlightService.php @@ -8,6 +8,7 @@ use App\Models\Bid; 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; @@ -123,12 +124,12 @@ class FlightService extends Service /** * 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; diff --git a/app/Services/UserService.php b/app/Services/UserService.php index aede36bf..44f284ff 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -85,7 +85,7 @@ class UserService extends Service * * @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(); diff --git a/tests/BidTest.php b/tests/BidTest.php index 15a6512e..773d55bf 100644 --- a/tests/BidTest.php +++ b/tests/BidTest.php @@ -4,17 +4,24 @@ namespace Tests; 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 @@ -27,17 +34,20 @@ class BidTest extends TestCase $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; } @@ -45,18 +55,35 @@ class BidTest extends TestCase /** * 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); @@ -78,12 +105,15 @@ class BidTest extends TestCase // 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']; @@ -91,6 +121,10 @@ class BidTest extends TestCase $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); diff --git a/tests/SimBriefTest.php b/tests/SimBriefTest.php index 3b0c9e99..383fa0c1 100644 --- a/tests/SimBriefTest.php +++ b/tests/SimBriefTest.php @@ -47,7 +47,7 @@ class SimBriefTest extends TestCase */ public function testReadSimbrief() { - $this->user = factory(User::class)->create(); + $this->user = $this->createUser(); $briefing = $this->loadSimBrief($this->user); $this->assertNotEmpty($briefing->ofp_xml); @@ -86,7 +86,7 @@ class SimBriefTest extends TestCase */ public function testApiCalls() { - $this->user = factory(User::class)->create(); + $this->user = $this->createUser(); $briefing = $this->loadSimBrief($this->user); // Check the flight API response @@ -120,7 +120,7 @@ class SimBriefTest extends TestCase */ public function testUserBidSimbrief() { - $this->user = factory(User::class)->create(); + $this->user = $this->createUser(); $this->loadSimBrief($this->user); // Find the flight @@ -136,7 +136,7 @@ class SimBriefTest extends TestCase public function testAttachToPirep() { - $user = factory(User::class)->create(); + $user = $this->createUser(); $pirep = factory(Pirep::class)->create([ 'user_id' => $user->id, 'dpt_airport_id' => 'OMAA', @@ -172,7 +172,7 @@ class SimBriefTest extends TestCase */ 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', diff --git a/tests/TestData.php b/tests/TestData.php index e807f4b4..1da1feca 100644 --- a/tests/TestData.php +++ b/tests/TestData.php @@ -4,9 +4,30 @@ namespace Tests; 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 @@ -37,7 +58,7 @@ trait TestData * * @return mixed */ - public function createRank($hours, array $subfleet_ids) + public function createRank(int $hours, array $subfleet_ids) { $attrs = []; @@ -59,12 +80,13 @@ trait TestData * @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', ]);