From 95a40d3565a4a73c219b2b26b9b836656b481d51 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Wed, 30 Sep 2020 12:58:45 -0400 Subject: [PATCH] Make sure proper subfleets are returned as part of the user profile #848 (#849) * Make sure proper subfleets are returned as part of the user profile #848 * Formatting --- app/Http/Controllers/Api/UserController.php | 5 +---- app/Http/Resources/User.php | 1 + app/Services/UserService.php | 20 ++++++++++++++++++++ tests/TestData.php | 3 ++- tests/UserTest.php | 16 +++++++++++++++- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index 60fc0b15..a4f3aeca 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -89,10 +89,7 @@ class UserController extends Controller */ public function get($id) { - $user = $this->userRepo - ->with(['airline', 'bids', 'rank']) - ->find($id); - + $user = $this->userSvc->getUser($id); return new UserResource($user); } diff --git a/app/Http/Resources/User.php b/app/Http/Resources/User.php index 147dec2d..141cd5d1 100644 --- a/app/Http/Resources/User.php +++ b/app/Http/Resources/User.php @@ -31,6 +31,7 @@ class User extends Resource $res['airline'] = Airline::make($this->whenLoaded('airline')); $res['bids'] = UserBid::collection($this->whenLoaded('bids')); $res['rank'] = Rank::make($this->whenLoaded('rank')); + $res['subfleets'] = Subfleet::make($this->whenLoaded('subfleets')); return $res; } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index b7e00a9d..aede36bf 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -54,6 +54,26 @@ class UserService extends Service $this->userRepo = $userRepo; } + /** + * Find the user and return them with all of the data properly attached + * + * @param $user_id + * + * @return User + */ + public function getUser($user_id): User + { + $user = $this->userRepo + ->with(['airline', 'bids', 'rank']) + ->find($user_id); + + // Load the proper subfleets to the rank + $user->rank->subfleets = $this->getAllowableSubfleets($user); + $user->subfleets = $user->rank->subfleets; + + return $user; + } + /** * Register a pilot. Also attaches the initial roles * required, and then triggers the UserRegistered event diff --git a/tests/TestData.php b/tests/TestData.php index c8cdf2db..e807f4b4 100644 --- a/tests/TestData.php +++ b/tests/TestData.php @@ -2,6 +2,7 @@ namespace Tests; +use App\Models\Aircraft; use App\Models\Subfleet; trait TestData @@ -72,7 +73,7 @@ trait TestData $aircraft_count = \random_int(2, 10); } - $aircraft = factory(\App\Models\Aircraft::class, $aircraft_count)->create([ + $aircraft = factory(Aircraft::class, $aircraft_count)->create([ 'subfleet_id' => $subfleet->id, 'airport_id' => $airport_id, ]); diff --git a/tests/UserTest.php b/tests/UserTest.php index cb97de9d..925bdcdf 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -85,6 +85,8 @@ class UserTest extends TestCase /** * Flip the setting for getting all of the user's aircraft restricted * by rank. Make sure that they're all returned + * + * @throws \Exception */ public function testGetAllAircraft() { @@ -135,7 +137,7 @@ class UserTest extends TestCase $this->assertEquals($subfleetACalled->fares[0]['capacity'], $overrides['capacity']); /** - * Check via API + * Check via API, but should only show the single subfleet being returned */ $this->settingsRepo->store('pireps.restrict_aircraft_to_rank', true); @@ -146,6 +148,18 @@ class UserTest extends TestCase $subfleetAFromApi = collect($body)->firstWhere('id', $subfleetA['subfleet']->id); $this->assertEquals($subfleetAFromApi['fares'][0]['price'], $overrides['price']); $this->assertEquals($subfleetAFromApi['fares'][0]['capacity'], $overrides['capacity']); + + // Read the user's profile and make sure that subfleet C is not part of this + // Should only return a single subfleet (subfleet A) + $resp = $this->get('/api/user', [], $user); + $resp->assertStatus(200); + + $body = $resp->json('data'); + $subfleets = $body['rank']['subfleets']; + + $this->assertEquals(1, count($subfleets)); + $this->assertEquals($subfleets[0]['fares'][0]['price'], $overrides['price']); + $this->assertEquals($subfleets[0]['fares'][0]['capacity'], $overrides['capacity']); } /**