Aircraft not being checked/detected at departure airport #781
This commit is contained in:
47
app/Exceptions/AircraftInvalid.php
Normal file
47
app/Exceptions/AircraftInvalid.php
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use App\Models\Aircraft;
|
||||||
|
|
||||||
|
class AircraftInvalid extends AbstractHttpException
|
||||||
|
{
|
||||||
|
public const MESSAGE = 'The supplied aircraft is invalid';
|
||||||
|
|
||||||
|
private $aircraft;
|
||||||
|
|
||||||
|
public function __construct(Aircraft $aircraft)
|
||||||
|
{
|
||||||
|
$this->aircraft = $aircraft;
|
||||||
|
parent::__construct(
|
||||||
|
400,
|
||||||
|
static::MESSAGE
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the RFC 7807 error type (without the URL root)
|
||||||
|
*/
|
||||||
|
public function getErrorType(): string
|
||||||
|
{
|
||||||
|
return 'aircraft-invalid';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the detailed error string
|
||||||
|
*/
|
||||||
|
public function getErrorDetails(): string
|
||||||
|
{
|
||||||
|
return $this->getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array with the error details, merged with the RFC7807 response
|
||||||
|
*/
|
||||||
|
public function getErrorMetadata(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'aircraft_id' => optional($this->aircraft)->id,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -285,7 +285,7 @@ class PirepController extends Controller
|
|||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
|
|
||||||
$pirep = new Pirep($request->post());
|
$pirep = new Pirep($request->post());
|
||||||
$pirep->user_id = Auth::user()->id;
|
$pirep->user_id = $user->id;
|
||||||
|
|
||||||
$attrs = $request->all();
|
$attrs = $request->all();
|
||||||
$attrs['submit'] = strtolower($attrs['submit']);
|
$attrs['submit'] = strtolower($attrs['submit']);
|
||||||
@@ -295,7 +295,7 @@ class PirepController extends Controller
|
|||||||
if (setting('pilots.only_flights_from_current')
|
if (setting('pilots.only_flights_from_current')
|
||||||
&& $user->curr_airport_id !== $pirep->dpt_airport_id) {
|
&& $user->curr_airport_id !== $pirep->dpt_airport_id) {
|
||||||
Log::info(
|
Log::info(
|
||||||
'Pilot '.Auth::user()->id
|
'Pilot '.$user->id
|
||||||
.' not at departure airport (curr='.$user->curr_airport_id
|
.' not at departure airport (curr='.$user->curr_airport_id
|
||||||
.', dpt='.$pirep->dpt_airport_id.')'
|
.', dpt='.$pirep->dpt_airport_id.')'
|
||||||
);
|
);
|
||||||
@@ -309,7 +309,7 @@ class PirepController extends Controller
|
|||||||
// Can they fly this aircraft?
|
// Can they fly this aircraft?
|
||||||
if (setting('pireps.restrict_aircraft_to_rank', false)
|
if (setting('pireps.restrict_aircraft_to_rank', false)
|
||||||
&& !$this->userSvc->aircraftAllowed($user, $pirep->aircraft_id)) {
|
&& !$this->userSvc->aircraftAllowed($user, $pirep->aircraft_id)) {
|
||||||
Log::info('Pilot '.Auth::user()->id.' not allowed to fly aircraft');
|
Log::info('Pilot '.$user->id.' not allowed to fly aircraft');
|
||||||
return $this->flashError(
|
return $this->flashError(
|
||||||
'You are not allowed to fly this aircraft!',
|
'You are not allowed to fly this aircraft!',
|
||||||
'frontend.pireps.create'
|
'frontend.pireps.create'
|
||||||
@@ -318,9 +318,20 @@ class PirepController extends Controller
|
|||||||
|
|
||||||
// is the aircraft in the right place?
|
// is the aircraft in the right place?
|
||||||
/* @noinspection NotOptimalIfConditionsInspection */
|
/* @noinspection NotOptimalIfConditionsInspection */
|
||||||
|
// Get the aircraft
|
||||||
|
$aircraft = $this->aircraftRepo->findWithoutFail($pirep->aircraft_id);
|
||||||
|
if ($aircraft === null) {
|
||||||
|
Log::error('Aircraft for PIREP not found, id='.$pirep->aircraft_id);
|
||||||
|
return $this->flashError(
|
||||||
|
'The aircraft for the PIREP hasn\'t been found',
|
||||||
|
'frontend.pireps.create'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (setting('pireps.only_aircraft_at_dpt_airport')
|
if (setting('pireps.only_aircraft_at_dpt_airport')
|
||||||
&& $pirep->aircraft_id !== $pirep->dpt_airport_id) {
|
&& $aircraft->airport_id !== $pirep->dpt_airport_id
|
||||||
Log::info('Aircraft '.$pirep->aircraft_id.' not at departure airport '.$pirep->dpt_airport_id.', erroring out');
|
) {
|
||||||
|
Log::info('Aircraft '.$pirep->aircraft_id.' not at departure airport (curr='.$pirep->aircraft->airport_id.', apt='.$pirep->dpt_airport_id.')');
|
||||||
return $this->flashError(
|
return $this->flashError(
|
||||||
'This aircraft is not positioned at the departure airport!',
|
'This aircraft is not positioned at the departure airport!',
|
||||||
'frontend.pireps.create'
|
'frontend.pireps.create'
|
||||||
|
|||||||
@@ -8,11 +8,13 @@ use App\Events\PirepCancelled;
|
|||||||
use App\Events\PirepFiled;
|
use App\Events\PirepFiled;
|
||||||
use App\Events\PirepRejected;
|
use App\Events\PirepRejected;
|
||||||
use App\Events\UserStatsChanged;
|
use App\Events\UserStatsChanged;
|
||||||
|
use App\Exceptions\AircraftInvalid;
|
||||||
use App\Exceptions\AircraftNotAtAirport;
|
use App\Exceptions\AircraftNotAtAirport;
|
||||||
use App\Exceptions\AircraftPermissionDenied;
|
use App\Exceptions\AircraftPermissionDenied;
|
||||||
use App\Exceptions\PirepCancelNotAllowed;
|
use App\Exceptions\PirepCancelNotAllowed;
|
||||||
use App\Exceptions\UserNotAtAirport;
|
use App\Exceptions\UserNotAtAirport;
|
||||||
use App\Models\Acars;
|
use App\Models\Acars;
|
||||||
|
use App\Models\Aircraft;
|
||||||
use App\Models\Enums\AcarsType;
|
use App\Models\Enums\AcarsType;
|
||||||
use App\Models\Enums\FlightType;
|
use App\Models\Enums\FlightType;
|
||||||
use App\Models\Enums\PirepSource;
|
use App\Models\Enums\PirepSource;
|
||||||
@@ -22,6 +24,7 @@ use App\Models\Navdata;
|
|||||||
use App\Models\Pirep;
|
use App\Models\Pirep;
|
||||||
use App\Models\PirepFieldValue;
|
use App\Models\PirepFieldValue;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Repositories\AircraftRepository;
|
||||||
use App\Repositories\PirepRepository;
|
use App\Repositories\PirepRepository;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use function count;
|
use function count;
|
||||||
@@ -30,20 +33,24 @@ use Illuminate\Support\Facades\Log;
|
|||||||
|
|
||||||
class PirepService extends Service
|
class PirepService extends Service
|
||||||
{
|
{
|
||||||
|
private $aircraftRepo;
|
||||||
private $geoSvc;
|
private $geoSvc;
|
||||||
private $userSvc;
|
private $userSvc;
|
||||||
private $pirepRepo;
|
private $pirepRepo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param GeoService $geoSvc
|
* @param AircraftRepository $aircraftRepo
|
||||||
* @param PirepRepository $pirepRepo
|
* @param GeoService $geoSvc
|
||||||
* @param UserService $userSvc
|
* @param PirepRepository $pirepRepo
|
||||||
|
* @param UserService $userSvc
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
AircraftRepository $aircraftRepo,
|
||||||
GeoService $geoSvc,
|
GeoService $geoSvc,
|
||||||
PirepRepository $pirepRepo,
|
PirepRepository $pirepRepo,
|
||||||
UserService $userSvc
|
UserService $userSvc
|
||||||
) {
|
) {
|
||||||
|
$this->aircraftRepo = $aircraftRepo;
|
||||||
$this->geoSvc = $geoSvc;
|
$this->geoSvc = $geoSvc;
|
||||||
$this->userSvc = $userSvc;
|
$this->userSvc = $userSvc;
|
||||||
$this->pirepRepo = $pirepRepo;
|
$this->pirepRepo = $pirepRepo;
|
||||||
@@ -89,9 +96,14 @@ class PirepService extends Service
|
|||||||
}
|
}
|
||||||
|
|
||||||
// See if this aircraft is at the departure airport
|
// See if this aircraft is at the departure airport
|
||||||
|
/** @var Aircraft $aircraft */
|
||||||
|
$aircraft = $this->aircraftRepo->findWithoutFail($pirep->aircraft_id);
|
||||||
|
if ($aircraft === null) {
|
||||||
|
throw new AircraftInvalid($aircraft);
|
||||||
|
}
|
||||||
|
|
||||||
/* @noinspection NotOptimalIfConditionsInspection */
|
/* @noinspection NotOptimalIfConditionsInspection */
|
||||||
if (setting('pireps.only_aircraft_at_dpt_airport')
|
if (setting('pireps.only_aircraft_at_dpt_airport') && $aircraft->airport_id !== $pirep->dpt_airport_id) {
|
||||||
&& $pirep->aircraft_id !== $pirep->dpt_airport_id) {
|
|
||||||
throw new AircraftNotAtAirport($pirep->aircraft);
|
throw new AircraftNotAtAirport($pirep->aircraft);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -107,6 +107,50 @@ class AcarsTest extends TestCase
|
|||||||
$response->assertStatus(400);
|
$response->assertStatus(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testPrefileAircraftNotAtAirport()
|
||||||
|
{
|
||||||
|
$this->settingsRepo->store('pilots.only_flights_from_current', false);
|
||||||
|
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false);
|
||||||
|
$this->settingsRepo->store('pireps.only_aircraft_at_dpt_airport', true);
|
||||||
|
|
||||||
|
$this->user = factory(User::class)->create();
|
||||||
|
|
||||||
|
/** @var Airport $airport */
|
||||||
|
$airport = factory(Airport::class)->create();
|
||||||
|
|
||||||
|
/** @var Airport $airport */
|
||||||
|
$aircraft_airport = factory(Airport::class)->create();
|
||||||
|
|
||||||
|
/** @var Airline $airline */
|
||||||
|
$airline = factory(Airline::class)->create();
|
||||||
|
|
||||||
|
/** @var Aircraft $aircraft */
|
||||||
|
$aircraft = factory(Aircraft::class)->create(['airport_id' => $aircraft_airport->id]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* INVALID AIRLINE_ID FIELD
|
||||||
|
*/
|
||||||
|
$uri = '/api/pireps/prefile';
|
||||||
|
$pirep = [
|
||||||
|
'airline_id' => $airline->id,
|
||||||
|
'aircraft_id' => $aircraft->id,
|
||||||
|
'dpt_airport_id' => $airport->icao,
|
||||||
|
'arr_airport_id' => $airport->icao,
|
||||||
|
'flight_number' => '6000',
|
||||||
|
'level' => 38000,
|
||||||
|
'planned_flight_time' => 120,
|
||||||
|
'route' => 'POINTA POINTB',
|
||||||
|
'source_name' => 'Tests',
|
||||||
|
];
|
||||||
|
|
||||||
|
$response = $this->post($uri, $pirep);
|
||||||
|
$response->assertStatus(400);
|
||||||
|
$this->assertEquals(
|
||||||
|
'The aircraft is not at the departure airport',
|
||||||
|
$response->json('title')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testBlankAirport()
|
public function testBlankAirport()
|
||||||
{
|
{
|
||||||
$this->user = factory(User::class)->create();
|
$this->user = factory(User::class)->create();
|
||||||
|
|||||||
Reference in New Issue
Block a user