diff --git a/app/Database/migrations/2017_06_09_010621_create_aircrafts_table.php b/app/Database/migrations/2017_06_09_010621_create_aircrafts_table.php index ab903bd3..a25e13cc 100644 --- a/app/Database/migrations/2017_06_09_010621_create_aircrafts_table.php +++ b/app/Database/migrations/2017_06_09_010621_create_aircrafts_table.php @@ -1,5 +1,7 @@ unsignedInteger('subfleet_id'); $table->string('icao', 4)->nullable(); $table->string('airport_id', 5)->nullable(); + $table->timestamp('landing_time')->nullable(); $table->string('hex_code', 10)->nullable(); $table->string('name', 50); $table->string('registration', 10)->nullable(); $table->string('tail_number', 10)->nullable(); $table->boolean('active')->default(true); + $table->unsignedTinyInteger('state')->default(AircraftState::PARKED); $table->timestamps(); $table->unique('registration'); + $table->index('airport_id'); }); } diff --git a/app/Models/Enums/AircraftState.php b/app/Models/Enums/AircraftState.php new file mode 100644 index 00000000..f3d1b9a2 --- /dev/null +++ b/app/Models/Enums/AircraftState.php @@ -0,0 +1,20 @@ + 'On Ground', + AircraftState::IN_USE => 'In Use', + AircraftState::IN_AIR => 'In Air', + ]; +} diff --git a/app/Services/PIREPService.php b/app/Services/PIREPService.php index 9d29470b..fb0aaea1 100644 --- a/app/Services/PIREPService.php +++ b/app/Services/PIREPService.php @@ -267,6 +267,11 @@ class PIREPService extends BaseService Log::info('PIREP ' . $pirep->id . ' state change to ACCEPTED'); + # Update the aircraft + $pirep->aircraft->airport_id = $pirep->arr_airport_id; + $pirep->aircraft->landing_time = $pirep->updated_at; + $pirep->aircraft->save(); + event(new PirepAccepted($pirep)); return $pirep; diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index 8bf7083b..97aa488f 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -48,8 +48,10 @@ class PIREPTest extends TestCase */ public function testAddPirep() { + $user = factory(App\Models\User::class)->create(); $route = $this->createNewRoute(); $pirep = factory(App\Models\Pirep::class)->create([ + 'user_id' => $user->id, 'route' => implode(' ', $route) ]); @@ -73,6 +75,13 @@ class PIREPTest extends TestCase $this->assertEquals($new_flight_time, $pirep->pilot->flight_time); $this->assertEquals($pirep->arr_airport_id, $pirep->pilot->curr_airport_id); + # Check the location of the current aircraft + $this->assertEquals($pirep->aircraft->airport_id, $pirep->arr_airport_id); + + # Also check via API: + $this->get('/api/fleet/aircraft/' . $pirep->aircraft_id, [], $user) + ->assertJson(['airport_id' => $pirep->arr_airport_id]); + /** * Now go from ACCEPTED to REJECTED */ diff --git a/tests/TestCase.php b/tests/TestCase.php index d4e7e763..fec3c3fb 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -111,14 +111,17 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase * Override the GET call to inject the user API key * @param string $uri * @param array $headers + * @param null $user * @return \Illuminate\Foundation\Testing\TestResponse */ - public function get($uri, array $headers=[]): \Illuminate\Foundation\Testing\TestResponse + public function get($uri, array $headers=[], $user=null): \Illuminate\Foundation\Testing\TestResponse { - if(empty($headers)) { - if($this->user !== null) { - $headers = $this->headers($this->user); - } + if($this->user !== null) { + $headers = $this->headers($this->user); + } + + if($user !== null) { + $headers['x-api-key'] = $user->api_key; } return parent::get($uri, $headers);