Setting which filters out aircraft not at the departure airport #171
This commit is contained in:
@@ -132,7 +132,6 @@ class CreateSettingsTable extends Migration
|
|||||||
'name' => 'PIREP duplicate time check',
|
'name' => 'PIREP duplicate time check',
|
||||||
'group' => 'pireps',
|
'group' => 'pireps',
|
||||||
'value' => 10,
|
'value' => 10,
|
||||||
'default' => 10,
|
|
||||||
'type' => 'int',
|
'type' => 'int',
|
||||||
'description' => 'The time in minutes to check for a duplicate PIREP',
|
'description' => 'The time in minutes to check for a duplicate PIREP',
|
||||||
]);
|
]);
|
||||||
@@ -141,7 +140,6 @@ class CreateSettingsTable extends Migration
|
|||||||
'name' => 'Hide Cancelled PIREPs',
|
'name' => 'Hide Cancelled PIREPs',
|
||||||
'group' => 'pireps',
|
'group' => 'pireps',
|
||||||
'value' => true,
|
'value' => true,
|
||||||
'default' => true,
|
|
||||||
'type' => 'boolean',
|
'type' => 'boolean',
|
||||||
'description' => 'Hide any cancelled PIREPs in the front-end',
|
'description' => 'Hide any cancelled PIREPs in the front-end',
|
||||||
]);
|
]);
|
||||||
@@ -150,11 +148,18 @@ class CreateSettingsTable extends Migration
|
|||||||
'name' => 'Restrict Aircraft to Ranks',
|
'name' => 'Restrict Aircraft to Ranks',
|
||||||
'group' => 'pireps',
|
'group' => 'pireps',
|
||||||
'value' => true,
|
'value' => true,
|
||||||
'default' => true,
|
|
||||||
'type' => 'boolean',
|
'type' => 'boolean',
|
||||||
'description' => 'Aircraft that can be flown are restricted to a user\'s rank',
|
'description' => 'Aircraft that can be flown are restricted to a user\'s rank',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$this->addSetting('pireps.only_aircraft_at_dep_airport', [
|
||||||
|
'name' => 'Restrict Aircraft At Departure',
|
||||||
|
'group' => 'pireps',
|
||||||
|
'value' => false,
|
||||||
|
'type' => 'boolean',
|
||||||
|
'description' => 'Only allow aircraft that are at the departure airport',
|
||||||
|
]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PILOTS
|
* PILOTS
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ class Migration extends MigrationBase
|
|||||||
'name' => $attrs['name'],
|
'name' => $attrs['name'],
|
||||||
'group' => $group,
|
'group' => $group,
|
||||||
'value' => $attrs['value'],
|
'value' => $attrs['value'],
|
||||||
|
'default' => $attrs['value'],
|
||||||
'type' => $attrs['type'],
|
'type' => $attrs['type'],
|
||||||
'description' => $attrs['description'],
|
'description' => $attrs['description'],
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
|
use function foo\func;
|
||||||
use Log;
|
use Log;
|
||||||
|
|
||||||
use App\Models\Flight;
|
use App\Models\Flight;
|
||||||
@@ -31,16 +32,38 @@ class FlightService extends BaseService
|
|||||||
*/
|
*/
|
||||||
public function filterSubfleets($user, $flight)
|
public function filterSubfleets($user, $flight)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
$subfleets = $flight->subfleets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Only allow aircraft that the user has access to in their rank
|
||||||
|
*/
|
||||||
if (setting('pireps.restrict_aircraft_to_rank', false)) {
|
if (setting('pireps.restrict_aircraft_to_rank', false)) {
|
||||||
$allowed_subfleets = $this->userSvc->getAllowableSubfleets($user)->pluck('id');
|
$allowed_subfleets = $this->userSvc->getAllowableSubfleets($user)->pluck('id');
|
||||||
$flight->subfleets = $flight->subfleets->filter(
|
$subfleets = $subfleets->filter(function ($subfleet, $i) use ($allowed_subfleets) {
|
||||||
function ($subfleet, $item) use ($allowed_subfleets) {
|
if ($allowed_subfleets->contains($subfleet->id)) {
|
||||||
if ($allowed_subfleets->contains($subfleet->id)) {
|
return true;
|
||||||
return true;
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Only allow aircraft that are at the current departure airport
|
||||||
|
*/
|
||||||
|
if(setting('pireps.only_aircraft_at_dep_airport', false)) {
|
||||||
|
foreach($subfleets as $subfleet) {
|
||||||
|
$subfleet->aircraft = $subfleet->aircraft->filter(
|
||||||
|
function ($aircraft, $i) use ($flight) {
|
||||||
|
if ($aircraft->airport_id === $flight->dpt_airport_id) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$flight->subfleets = $subfleets;
|
||||||
|
|
||||||
return $flight;
|
return $flight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -34,7 +34,7 @@ class TestData
|
|||||||
* @param null $aircraft_count
|
* @param null $aircraft_count
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function createSubfleetWithAircraft($aircraft_count = null)
|
public static function createSubfleetWithAircraft($aircraft_count = null, $airport_id=null)
|
||||||
{
|
{
|
||||||
$subfleet = factory(\App\Models\Subfleet::class)->create();
|
$subfleet = factory(\App\Models\Subfleet::class)->create();
|
||||||
|
|
||||||
@@ -43,7 +43,8 @@ class TestData
|
|||||||
}
|
}
|
||||||
|
|
||||||
$aircraft = factory(\App\Models\Aircraft::class, $aircraft_count)->create([
|
$aircraft = factory(\App\Models\Aircraft::class, $aircraft_count)->create([
|
||||||
'subfleet_id' => $subfleet->id
|
'subfleet_id' => $subfleet->id,
|
||||||
|
'airport_id' => $airport_id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|||||||
+12
-3
@@ -133,13 +133,21 @@ class UserTest extends TestCase
|
|||||||
{
|
{
|
||||||
# Add subfleets and aircraft, but also add another
|
# Add subfleets and aircraft, but also add another
|
||||||
# set of subfleets
|
# set of subfleets
|
||||||
$subfleetA = TestData::createSubfleetWithAircraft(2);
|
$airport = factory(App\Models\Airport::class)->create();
|
||||||
|
$subfleetA = TestData::createSubfleetWithAircraft(2, $airport->id);
|
||||||
$subfleetB = TestData::createSubfleetWithAircraft(2);
|
$subfleetB = TestData::createSubfleetWithAircraft(2);
|
||||||
|
|
||||||
$rank = TestData::createRank(10, [$subfleetA['subfleet']->id]);
|
$rank = TestData::createRank(10, [$subfleetA['subfleet']->id]);
|
||||||
$user = factory(App\Models\User::class)->create(['rank_id' => $rank->id,]);
|
$user = factory(App\Models\User::class)->create([
|
||||||
|
'curr_airport_id' => $airport->id,
|
||||||
|
'rank_id' => $rank->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$flight = factory(App\Models\Flight::class)->create([
|
||||||
|
'airline_id' => $user->airline_id,
|
||||||
|
'dpt_airport_id' => $airport->id,
|
||||||
|
]);
|
||||||
|
|
||||||
$flight = factory(App\Models\Flight::class)->create(['airline_id' => $user->airline_id]);
|
|
||||||
$flight->subfleets()->syncWithoutDetaching([
|
$flight->subfleets()->syncWithoutDetaching([
|
||||||
$subfleetA['subfleet']->id,
|
$subfleetA['subfleet']->id,
|
||||||
$subfleetB['subfleet']->id
|
$subfleetB['subfleet']->id
|
||||||
@@ -155,6 +163,7 @@ class UserTest extends TestCase
|
|||||||
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false);
|
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false);
|
||||||
|
|
||||||
$response = $this->get('/api/flights/' . $flight->id, [], $user);
|
$response = $this->get('/api/flights/' . $flight->id, [], $user);
|
||||||
|
$body = $response->json();
|
||||||
$response->assertStatus(200);
|
$response->assertStatus(200);
|
||||||
$this->assertCount(2, $response->json()['subfleets']);
|
$this->assertCount(2, $response->json()['subfleets']);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user