From 594d0eb22299459f10c7ad396074b0cc8d7396dd Mon Sep 17 00:00:00 2001 From: "B.Fatih KOZ" <74361521+FatihKoz@users.noreply.github.com> Date: Thu, 13 May 2021 22:26:53 +0300 Subject: [PATCH] Check Aircraft Availability (#1177) * Check Aircraft Availability before Prefile Check if the aircraft is available for flight (State : Parked / On Ground). If not throw new exception AircraftNotAvailable * Add Exception AircraftNotAvailable exception, used by PirepService during prefile checks. --- app/Exceptions/AircraftNotAvailable.php | 38 +++++++++++++++++++++++++ app/Services/PirepService.php | 12 +++++++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 app/Exceptions/AircraftNotAvailable.php diff --git a/app/Exceptions/AircraftNotAvailable.php b/app/Exceptions/AircraftNotAvailable.php new file mode 100644 index 00000000..81aad838 --- /dev/null +++ b/app/Exceptions/AircraftNotAvailable.php @@ -0,0 +1,38 @@ +aircraft = $aircraft; + parent::__construct( + 400, + static::MESSAGE + ); + } + + public function getErrorType(): string + { + return 'aircraft-not-available'; + } + + public function getErrorDetails(): string + { + return $this->getMessage(); + } + + public function getErrorMetadata(): array + { + return [ + 'aircraft_id' => $this->aircraft->id, + ]; + } +} diff --git a/app/Services/PirepService.php b/app/Services/PirepService.php index 19b61bdb..e43b680e 100644 --- a/app/Services/PirepService.php +++ b/app/Services/PirepService.php @@ -11,6 +11,7 @@ use App\Events\PirepRejected; use App\Events\UserStatsChanged; use App\Exceptions\AircraftInvalid; use App\Exceptions\AircraftNotAtAirport; +use App\Exceptions\AircraftNotAvailable; use App\Exceptions\AircraftPermissionDenied; use App\Exceptions\AirportNotFound; use App\Exceptions\PirepCancelNotAllowed; @@ -19,6 +20,7 @@ use App\Exceptions\UserNotAtAirport; use App\Models\Acars; use App\Models\Aircraft; use App\Models\Enums\AcarsType; +use App\Models\Enums\AircraftState; use App\Models\Enums\FlightType; use App\Models\Enums\PirepSource; use App\Models\Enums\PirepState; @@ -132,13 +134,21 @@ class PirepService extends Service throw new AircraftPermissionDenied($user, $pirep->aircraft); } - // See if this aircraft is at the departure airport + // See if this aircraft is valid /** @var Aircraft $aircraft */ $aircraft = $this->aircraftRepo->findWithoutFail($pirep->aircraft_id); if ($aircraft === null) { throw new AircraftInvalid($aircraft); } + // See if this aircraft is available for flight + /** @var Aircraft $aircraft */ + $aircraft = $this->aircraftRepo->where('id', $pirep->aircraft_id)->where('state', AircraftState::PARKED)->first(); + if ($aircraft === null) { + throw new AircraftNotAvailable($pirep->aircraft); + } + + // See if this aircraft is at the departure airport /* @noinspection NotOptimalIfConditionsInspection */ if (setting('pireps.only_aircraft_at_dpt_airport') && $aircraft->airport_id !== $pirep->dpt_airport_id) { throw new AircraftNotAtAirport($pirep->aircraft);