Block refiling of PIREPs (#1167)

* Block refiling of PIREPs

* Style fixes

* Package updates
This commit is contained in:
Nabeel S 2021-05-03 10:00:10 -04:00 committed by GitHub
parent 5094375e13
commit fe0ef60b6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 518 additions and 383 deletions

View File

@ -0,0 +1,44 @@
<?php
namespace App\Exceptions;
use App\Models\Pirep;
class PirepError extends AbstractHttpException
{
private $pirep;
private $error;
public function __construct(Pirep $pirep, string $error)
{
$this->error = $error;
$this->pirep = $pirep;
parent::__construct(400, $error);
}
/**
* Return the RFC 7807 error type (without the URL root)
*/
public function getErrorType(): string
{
return 'pirep-error';
}
/**
* 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 [
'pirep_id' => $this->pirep->id,
];
}
}

View File

@ -308,6 +308,8 @@ class PirepController extends Controller
$this->updateFares($pirep, $request);
} catch (\Exception $e) {
Log::error($e);
throw $e;
}
// See if there there is any route data posted

View File

@ -44,8 +44,9 @@ use Illuminate\Support\Collection;
* @property Flight|null flight
* @property Collection fields
* @property string status
* @property bool state
* @property string source
* @property PirepState state
* @property int source
* @property string source_name
* @property Carbon submitted_at
* @property Carbon created_at
* @property Carbon updated_at

View File

@ -48,6 +48,10 @@ class AviationWeather extends Metar
$res = $this->httpClient->get($url, []);
$xml = simplexml_load_string($res);
if ($xml->errors && count($xml->errors->children()) > 0) {
return '';
}
$attrs = $xml->data->attributes();
if (!isset($attrs['num_results'])) {
return '';

View File

@ -14,6 +14,7 @@ use App\Exceptions\AircraftNotAtAirport;
use App\Exceptions\AircraftPermissionDenied;
use App\Exceptions\AirportNotFound;
use App\Exceptions\PirepCancelNotAllowed;
use App\Exceptions\PirepError;
use App\Exceptions\UserNotAtAirport;
use App\Models\Acars;
use App\Models\Aircraft;
@ -231,6 +232,18 @@ class PirepService extends Service
$field_values = [];
}
// Check if the PIREP has already been submitted
$is_already_submitted = in_array($pirep->state, [
PirepState::PENDING,
PirepState::ACCEPTED,
PirepState::CANCELLED,
PirepState::REJECTED,
], true);
if ($is_already_submitted) {
throw new PirepError($pirep, 'PIREP has already been submitted');
}
$attrs['state'] = PirepState::PENDING;
$attrs['status'] = PirepStatus::ARRIVED;
$attrs['submitted_at'] = Carbon::now('UTC');

818
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -604,8 +604,13 @@ class AcarsTest extends TestCase
'rank_id' => $rank->id,
]);
/** @var Airport $airport */
$airport = factory(Airport::class)->create();
/** @var Airline $airline */
$airline = factory(Airline::class)->create();
/** @var Aircraft $aircraft */
$aircraft = $subfleet['aircraft']->random();
$uri = '/api/pireps/prefile';
@ -639,8 +644,18 @@ class AcarsTest extends TestCase
// Check the block_off_time and block_on_time being set
$body = $this->get('/api/pireps/'.$pirep_id)->json('data');
$this->assertEquals(PirepState::PENDING, $body['state']);
$this->assertNotNull($body['block_off_time']);
$this->assertNotNull($body['block_on_time']);
// Try to refile, should be blocked
$response = $this->post($uri, [
'flight_time' => 130,
'fuel_used' => 8000.19,
'distance' => 400,
]);
$response->assertStatus(400);
}
/**