diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php index 879765a9..7c6edd49 100644 --- a/app/Http/Controllers/Api/PirepController.php +++ b/app/Http/Controllers/Api/PirepController.php @@ -2,8 +2,9 @@ namespace App\Http\Controllers\Api; -use Auth; use Log; +use Auth; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\Request; use App\Models\Acars; @@ -22,6 +23,7 @@ use App\Http\Resources\Acars as AcarsResource; use App\Http\Resources\Pirep as PirepResource; use App\Http\Controllers\AppBaseController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; class PirepController extends AppBaseController { @@ -73,7 +75,7 @@ class PirepController extends AppBaseController */ public function prefile(Request $request) { - Log::info('PIREP Prefile, user '. Auth::user()->pilot_id, $request->toArray()); + Log::info('PIREP Prefile, user '.Auth::user()->id, $request->toArray()); $attrs = [ 'user_id' => Auth::user()->id, @@ -111,11 +113,23 @@ class PirepController extends AppBaseController * @param $id * @param Request $request * @return PirepResource + * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public function file($id, Request $request) { Log::info('PIREP Prefile, user ' . Auth::user()->pilot_id, $request->toArray()); + $pirep = $this->pirepRepo->find($id); + if (empty($pirep)) { + throw new ModelNotFoundException('PIREP not found'); + } + + # Check if the status is cancelled... + if($pirep->state === PirepState::CANCELLED) { + throw new BadRequestHttpException('PIREP has been cancelled, updates can\'t be posted'); + } + $attrs = [ 'state' => PirepState::PENDING, 'status' => PirepStatus::ARRIVED, @@ -127,6 +141,7 @@ class PirepController extends AppBaseController } } + $pirep_fields = []; if($request->filled('fields')) { $pirep_fields = $request->get('fields'); } @@ -142,6 +157,30 @@ class PirepController extends AppBaseController return new PirepResource($pirep); } + /** + * Cancel the PIREP + * @param $id + * @param Request $request + * @return PirepResource + */ + public function cancel($id, Request $request) + { + Log::info('PIREP Cancel, user ' . Auth::user()->pilot_id, $request->toArray()); + + $attrs = [ + 'state' => PirepState::CANCELLED, + ]; + + try { + $pirep = $this->pirepRepo->update($attrs, $id); + } catch (\Exception $e) { + Log::error($e); + } + + PirepResource::withoutWrapping(); + return new PirepResource($pirep); + } + /** * Return the GeoJSON for the ACARS line * @param $id @@ -163,11 +202,17 @@ class PirepController extends AppBaseController * @param $id * @param Request $request * @return AcarsResource + * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException */ public function acars_store($id, Request $request) { $pirep = $this->pirepRepo->find($id); + # Check if the status is cancelled... + if ($pirep->state === PirepState::CANCELLED) { + throw new BadRequestHttpException('PIREP has been cancelled, updates can\'t be posted'); + } + Log::info('Posting ACARS update', $request->toArray()); $attrs = $request->toArray(); diff --git a/app/Routes/api.php b/app/Routes/api.php index a80353f5..fdb41621 100755 --- a/app/Routes/api.php +++ b/app/Routes/api.php @@ -27,6 +27,7 @@ Route::group(['middleware' => ['api.auth']], function () Route::get('pireps/{id}', 'PirepController@get'); Route::post('pireps/prefile', 'PirepController@prefile'); Route::post('pireps/{id}/file', 'PirepController@file'); + Route::post('pireps/{id}/cancel', 'PirepController@cancel'); Route::post('pireps/{id}/acars', 'PirepController@acars_store'); diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index 27321e5c..d11c6908 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -4,18 +4,12 @@ use Carbon\Carbon; use App\Models\Acars; use App\Models\Enums\AcarsType; -use App\Models\Navdata; use App\Models\Pirep; use App\Models\User; use App\Models\Enums\PirepState; -use Illuminate\Foundation\Testing\WithoutMiddleware; - - class PIREPTest extends TestCase { - use WithoutMiddleware; - protected $pirepSvc; public function setUp() @@ -191,4 +185,28 @@ class PIREPTest extends TestCase $dupe_pirep = $this->pirepSvc->findDuplicate($pirep); $this->assertFalse($dupe_pirep); } + + public function testCancelViaAPI() + { + $pirep = factory(App\Models\Pirep::class)->make(['id'=>''])->toArray(); + $uri = '/api/pireps/prefile'; + $response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep); + $pirep_id = $response->json()['id']; + + $uri = '/api/pireps/' . $pirep_id . '/acars'; + $acars = factory(App\Models\Acars::class)->make()->toArray(); + $response = $this->withHeaders($this->apiHeaders())->post($uri, $acars); + $response->assertStatus(201); + + # Cancel it + $uri = '/api/pireps/' . $pirep_id . '/cancel'; + $response = $this->withHeaders($this->apiHeaders())->post($uri, $acars); + $response->assertStatus(200); + + # Should get a 400 when posting an ACARS update + $uri = '/api/pireps/' . $pirep_id . '/acars'; + $acars = factory(App\Models\Acars::class)->make()->toArray(); + $response = $this->withHeaders($this->apiHeaders())->post($uri, $acars); + $response->assertStatus(400); + } }