Allow the fares to be submitted via API for ACARS #125

This commit is contained in:
Nabeel Shahzad
2018-03-14 10:34:41 -05:00
parent 7148f27d33
commit afd7aebe20
5 changed files with 213 additions and 56 deletions

View File

@@ -26,6 +26,7 @@ use App\Models\PirepComment;
use App\Repositories\AcarsRepository; use App\Repositories\AcarsRepository;
use App\Repositories\JournalRepository; use App\Repositories\JournalRepository;
use App\Repositories\PirepRepository; use App\Repositories\PirepRepository;
use App\Services\FareService;
use App\Services\Finance\PirepFinanceService; use App\Services\Finance\PirepFinanceService;
use App\Services\GeoService; use App\Services\GeoService;
use App\Services\PirepService; use App\Services\PirepService;
@@ -37,6 +38,7 @@ use Log;
class PirepController extends RestController class PirepController extends RestController
{ {
private $acarsRepo, private $acarsRepo,
$fareSvc,
$financeSvc, $financeSvc,
$geoSvc, $geoSvc,
$journalRepo, $journalRepo,
@@ -56,6 +58,7 @@ class PirepController extends RestController
*/ */
public function __construct( public function __construct(
AcarsRepository $acarsRepo, AcarsRepository $acarsRepo,
FareService $fareSvc,
PirepFinanceService $financeSvc, PirepFinanceService $financeSvc,
GeoService $geoSvc, GeoService $geoSvc,
JournalRepository $journalRepo, JournalRepository $journalRepo,
@@ -64,6 +67,7 @@ class PirepController extends RestController
UserService $userSvc UserService $userSvc
) { ) {
$this->acarsRepo = $acarsRepo; $this->acarsRepo = $acarsRepo;
$this->fareSvc = $fareSvc;
$this->financeSvc = $financeSvc; $this->financeSvc = $financeSvc;
$this->geoSvc = $geoSvc; $this->geoSvc = $geoSvc;
$this->journalRepo = $journalRepo; $this->journalRepo = $journalRepo;
@@ -115,6 +119,29 @@ class PirepController extends RestController
$this->pirepSvc->updateCustomFields($pirep->id, $pirep_fields); $this->pirepSvc->updateCustomFields($pirep->id, $pirep_fields);
} }
/**
* Save the fares
* @param $pirep
* @param Request $request
* @throws \Exception
*/
protected function updateFares($pirep, Request $request)
{
if(!$request->filled('fares')) {
return;
}
$fares = [];
foreach($request->post('fares') as $fare) {
$fares[] = [
'fare_id' => $fare['id'],
'count' => $fare['count'],
];
}
$this->fareSvc->saveForPirep($pirep, $fares);
}
/** /**
* Create a new PIREP and place it in a "inprogress" and "prefile" state * Create a new PIREP and place it in a "inprogress" and "prefile" state
* Once ACARS updates are being processed, then it can go into an 'ENROUTE' * Once ACARS updates are being processed, then it can go into an 'ENROUTE'
@@ -124,6 +151,7 @@ class PirepController extends RestController
* @return PirepResource * @return PirepResource
* @throws \App\Exceptions\PirepCancelled * @throws \App\Exceptions\PirepCancelled
* @throws \App\Exceptions\AircraftPermissionDenied * @throws \App\Exceptions\AircraftPermissionDenied
* @throws \Exception
*/ */
public function prefile(PrefileRequest $request) public function prefile(PrefileRequest $request)
{ {
@@ -160,6 +188,7 @@ class PirepController extends RestController
Log::info($pirep->id); Log::info($pirep->id);
$this->updateFields($pirep, $request); $this->updateFields($pirep, $request);
$this->updateFares($pirep, $request);
return new PirepResource($pirep); return new PirepResource($pirep);
} }
@@ -175,6 +204,7 @@ class PirepController extends RestController
* @throws \App\Exceptions\PirepCancelled * @throws \App\Exceptions\PirepCancelled
* @throws \App\Exceptions\AircraftPermissionDenied * @throws \App\Exceptions\AircraftPermissionDenied
* @throws \Prettus\Validator\Exceptions\ValidatorException * @throws \Prettus\Validator\Exceptions\ValidatorException
* @throws \Exception
*/ */
public function update($id, UpdateRequest $request) public function update($id, UpdateRequest $request)
{ {
@@ -199,6 +229,7 @@ class PirepController extends RestController
$pirep = $this->pirepRepo->update($attrs, $id); $pirep = $this->pirepRepo->update($attrs, $id);
$this->updateFields($pirep, $request); $this->updateFields($pirep, $request);
$this->updateFares($pirep, $request);
return new PirepResource($pirep); return new PirepResource($pirep);
} }
@@ -242,6 +273,7 @@ class PirepController extends RestController
$pirep = $this->pirepRepo->update($attrs, $id); $pirep = $this->pirepRepo->update($attrs, $id);
$pirep = $this->pirepSvc->create($pirep); $pirep = $this->pirepSvc->create($pirep);
$this->updateFields($pirep, $request); $this->updateFields($pirep, $request);
$this->updateFares($pirep, $request);
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error($e); Log::error($e);
} }

View File

@@ -43,6 +43,11 @@ class FileRequest extends FormRequest
'landing_rate' => 'nullable|numeric', 'landing_rate' => 'nullable|numeric',
'flight_type' => 'nullable|integer', 'flight_type' => 'nullable|integer',
'created_at' => 'nullable|date', 'created_at' => 'nullable|date',
# See if the fare objects are included and formatted properly
'fares' => 'nullable|array',
'fares.*.id' => 'required',
'fares.*.count' => 'required|numeric',
]; ];
return $rules; return $rules;

View File

@@ -38,6 +38,11 @@ class PrefileRequest extends FormRequest
'notes' => 'nullable', 'notes' => 'nullable',
'flight_type' => 'nullable|integer', 'flight_type' => 'nullable|integer',
'created_at' => 'nullable|date', 'created_at' => 'nullable|date',
# See if the fare objects are included and formatted properly
'fares' => 'nullable|array',
'fares.*.id' => 'required',
'fares.*.count' => 'required|numeric',
]; ];
return $rules; return $rules;

View File

@@ -42,6 +42,11 @@ class UpdateRequest extends FormRequest
'landing_rate' => 'nullable|numeric', 'landing_rate' => 'nullable|numeric',
'flight_type' => 'nullable|integer', 'flight_type' => 'nullable|integer',
'created_at' => 'nullable|date', 'created_at' => 'nullable|date',
# See if the fare objects are included and formatted properly
'fares' => 'nullable|array',
'fares.*.id' => 'required',
'fares.*.count' => 'required|numeric',
]; ];
return $rules; return $rules;

View File

@@ -24,24 +24,27 @@ class AcarsTest extends TestCase
* @param $points * @param $points
* @param array $addtl_fields * @param array $addtl_fields
*/ */
protected function allPointsInRoute($route, $points, $addtl_fields=[]) protected function allPointsInRoute($route, $points, $addtl_fields = [])
{ {
if(empty($addtl_fields)) { if (empty($addtl_fields)) {
$addtl_fields = []; $addtl_fields = [];
} }
$fields = array_merge([ $fields = array_merge(
'name', [
'order', 'name',
'lat', 'order',
'lon' 'lat',
], $addtl_fields); 'lon',
],
$addtl_fields
);
$this->assertEquals(\count($route), \count($points)); $this->assertEquals(\count($route), \count($points));
foreach($route as $idx => $point) { foreach ($route as $idx => $point) {
$this->assertHasKeys($points[$idx], $fields); $this->assertHasKeys($points[$idx], $fields);
foreach($fields as $f) { foreach ($fields as $f) {
if($f === 'lat' || $f === 'lon') { if ($f === 'lat' || $f === 'lon') {
continue; continue;
} }
@@ -52,8 +55,9 @@ class AcarsTest extends TestCase
protected function getPirep($pirep_id) protected function getPirep($pirep_id)
{ {
$resp = $this ->get('/api/pireps/' . $pirep_id); $resp = $this->get('/api/pireps/'.$pirep_id);
$resp->assertStatus(200); $resp->assertStatus(200);
return $resp->json()['data']; return $resp->json()['data'];
} }
@@ -91,14 +95,17 @@ class AcarsTest extends TestCase
/** /**
* Post a PIREP into a PREFILE state and post ACARS * Post a PIREP into a PREFILE state and post ACARS
*/ */
public function testAcarsUpdates() public function testPrefileAndUpdates()
{ {
$subfleet = $this->createSubfleetWithAircraft(2); $subfleet = $this->createSubfleetWithAircraft(2);
$rank = $this->createRank(10, [$subfleet['subfleet']->id]); $rank = $this->createRank(10, [$subfleet['subfleet']->id]);
$fare = factory(App\Models\Fare::class)->create();
$this->user = factory(App\Models\User::class)->create([ $this->user = factory(App\Models\User::class)->create(
'rank_id' => $rank->id [
]); 'rank_id' => $rank->id,
]
);
$airport = factory(App\Models\Airport::class)->create(); $airport = factory(App\Models\Airport::class)->create();
$airline = factory(App\Models\Airline::class)->create(); $airline = factory(App\Models\Airline::class)->create();
@@ -118,7 +125,94 @@ class AcarsTest extends TestCase
'source_name' => 'UnitTest', 'source_name' => 'UnitTest',
'fields' => [ 'fields' => [
'custom_field' => 'custom_value', 'custom_field' => 'custom_value',
],
'fares' => [
[
'id' => $fare->id,
'count' => $fare->capacity,
],
],
];
$response = $this->post($uri, $pirep);
$response->assertStatus(201);
$pirep = $response->json('data');
# See that the fields and fares were set
$fares = \App\Models\PirepFare::where('pirep_id', $pirep['id'])->get();
$this->assertCount(1, $fares);
$saved_fare = $fares->first();
$this->assertEquals($fare->id, $saved_fare['fare_id']);
$this->assertEquals($fare->capacity, $saved_fare['count']);
# Check saved fields
$saved_fields = \App\Models\PirepFieldValues::where('pirep_id', $pirep['id'])->get();
$this->assertCount(1, $saved_fields);
$field = $saved_fields->first();
$this->assertEquals('custom_field', $field['name']);
$this->assertEquals('custom_value', $field['value']);
/**
* Try to update fields
*/
$uri = '/api/pireps/'.$pirep['id'].'/update';
$update = [
'fares' => [
[
'id' => $fare->id,
'count' => $fare->capacity,
],
],
];
$response = $this->post($uri, $update);
$response->assertStatus(200);
$updated_pirep = $response->json('data');
# Make sure there are no duplicates
$fares = \App\Models\PirepFare::where('pirep_id', $pirep['id'])->get();
$this->assertCount(1, $fares);
$saved_fare = $fares->first();
$this->assertEquals($fare->id, $saved_fare['fare_id']);
$this->assertEquals($fare->capacity, $saved_fare['count']);
}
/**
* Post a PIREP into a PREFILE state and post ACARS
*/
public function testAcarsUpdates()
{
$subfleet = $this->createSubfleetWithAircraft(2);
$rank = $this->createRank(10, [$subfleet['subfleet']->id]);
$this->user = factory(App\Models\User::class)->create(
[
'rank_id' => $rank->id,
] ]
);
$airport = factory(App\Models\Airport::class)->create();
$airline = factory(App\Models\Airline::class)->create();
$aircraft = $subfleet['aircraft']->random();
$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_distance' => 400,
'planned_flight_time' => 120,
'route' => 'POINTA POINTB',
'source_name' => 'UnitTest',
'fields' => [
'custom_field' => 'custom_value',
],
]; ];
$response = $this->post($uri, $pirep); $response = $this->post($uri, $pirep);
@@ -149,10 +243,15 @@ class AcarsTest extends TestCase
/** /**
* Update the custom field * Update the custom field
*/ */
$uri = '/api/pireps/' . $pirep_id . '/update'; $uri = '/api/pireps/'.$pirep_id.'/update';
$this->post($uri, ['fields' => [ $this->post(
'custom_field' => 'custom_value_changed', $uri,
]]); [
'fields' => [
'custom_field' => 'custom_value_changed',
],
]
);
$pirep = $this->getPirep($pirep_id); $pirep = $this->getPirep($pirep_id);
$this->assertEquals('custom_value_changed', $pirep['fields'][0]['value']); $this->assertEquals('custom_value_changed', $pirep['fields'][0]['value']);
@@ -161,7 +260,7 @@ class AcarsTest extends TestCase
* Add some position updates * Add some position updates
*/ */
$uri = '/api/pireps/' . $pirep_id . '/acars/position'; $uri = '/api/pireps/'.$pirep_id.'/acars/position';
# Test missing positions field # Test missing positions field
# Post an ACARS update # Post an ACARS update
@@ -170,9 +269,11 @@ class AcarsTest extends TestCase
$response->assertStatus(400); $response->assertStatus(400);
# Post an ACARS update # Post an ACARS update
$acars = factory(App\Models\Acars::class)->make([ $acars = factory(App\Models\Acars::class)->make(
'id' => null [
])->toArray(); 'id' => null,
]
)->toArray();
$update = ['positions' => [$acars]]; $update = ['positions' => [$acars]];
$response = $this->post($uri, $update); $response = $this->post($uri, $update);
@@ -206,11 +307,14 @@ class AcarsTest extends TestCase
$response = $this->post($uri, ['flight_time' => '1:30']); $response = $this->post($uri, ['flight_time' => '1:30']);
$response->assertStatus(400); // invalid flight time $response->assertStatus(400); // invalid flight time
$response = $this->post($uri, [ $response = $this->post(
'flight_time' => 130, $uri,
'fuel_used' => 8000.19, [
'distance' => 400, 'flight_time' => 130,
]); 'fuel_used' => 8000.19,
'distance' => 400,
]
);
$response->assertStatus(200); $response->assertStatus(200);
$body = $response->json(); $body = $response->json();
@@ -245,9 +349,11 @@ class AcarsTest extends TestCase
$rank = $this->createRank(10, [$subfleetA['subfleet']->id]); $rank = $this->createRank(10, [$subfleetA['subfleet']->id]);
$this->user = factory(App\Models\User::class)->create([ $this->user = factory(App\Models\User::class)->create(
'rank_id' => $rank->id, [
]); 'rank_id' => $rank->id,
]
);
$uri = '/api/pireps/prefile'; $uri = '/api/pireps/prefile';
$pirep = [ $pirep = [
@@ -259,7 +365,7 @@ class AcarsTest extends TestCase
'level' => 38000, 'level' => 38000,
'planned_flight_time' => 120, 'planned_flight_time' => 120,
'route' => 'POINTA POINTB', 'route' => 'POINTA POINTB',
'source_name' => 'Unit test' 'source_name' => 'Unit test',
]; ];
$response = $this->post($uri, $pirep); $response = $this->post($uri, $pirep);
@@ -289,9 +395,11 @@ class AcarsTest extends TestCase
$rank = $this->createRank(10, [$subfleetA['subfleet']->id]); $rank = $this->createRank(10, [$subfleetA['subfleet']->id]);
$this->user = factory(App\Models\User::class)->create([ $this->user = factory(App\Models\User::class)->create(
'rank_id' => $rank->id, [
]); 'rank_id' => $rank->id,
]
);
$uri = '/api/pireps/prefile'; $uri = '/api/pireps/prefile';
$pirep = [ $pirep = [
@@ -303,7 +411,7 @@ class AcarsTest extends TestCase
'level' => 38000, 'level' => 38000,
'planned_flight_time' => 120, 'planned_flight_time' => 120,
'route' => 'POINTA POINTB', 'route' => 'POINTA POINTB',
'source_name' => 'Unit test' 'source_name' => 'Unit test',
]; ];
$response = $this->post($uri, $pirep); $response = $this->post($uri, $pirep);
@@ -323,11 +431,11 @@ class AcarsTest extends TestCase
$pirep_id = $response->json()['data']['id']; $pirep_id = $response->json()['data']['id'];
$uri = '/api/pireps/' . $pirep_id . '/acars/position'; $uri = '/api/pireps/'.$pirep_id.'/acars/position';
# Post an ACARS update # Post an ACARS update
$acars_count = \random_int(2, 10); $acars_count = \random_int(2, 10);
$acars = factory(App\Models\Acars::class, $acars_count)->make(['id'=>''])->toArray(); $acars = factory(App\Models\Acars::class, $acars_count)->make(['id' => ''])->toArray();
$update = ['positions' => $acars]; $update = ['positions' => $acars];
$response = $this->post($uri, $update); $response = $this->post($uri, $update);
@@ -374,10 +482,12 @@ class AcarsTest extends TestCase
$pirep_id = $response->json()['data']['id']; $pirep_id = $response->json()['data']['id'];
$dt = date('c'); $dt = date('c');
$uri = '/api/pireps/' . $pirep_id . '/acars/position'; $uri = '/api/pireps/'.$pirep_id.'/acars/position';
$acars = factory(App\Models\Acars::class)->make([ $acars = factory(App\Models\Acars::class)->make(
'sim_time' => $dt [
])->toArray(); 'sim_time' => $dt,
]
)->toArray();
$update = ['positions' => [$acars]]; $update = ['positions' => [$acars]];
$response = $this->post($uri, $update); $response = $this->post($uri, $update);
@@ -396,15 +506,15 @@ class AcarsTest extends TestCase
$pirep_id = $response->json()['data']['id']; $pirep_id = $response->json()['data']['id'];
$post_route = ['order' => 1, 'name' => 'NAVPOINT']; $post_route = ['order' => 1, 'name' => 'NAVPOINT'];
$uri = '/api/pireps/' . $pirep_id . '/route'; $uri = '/api/pireps/'.$pirep_id.'/route';
$response = $this->post($uri, $post_route); $response = $this->post($uri, $post_route);
$response->assertStatus(400); $response->assertStatus(400);
$post_route = [ $post_route = [
['order' => 1, 'name' => 'NAVPOINT', 'lat' => 'notanumber', 'lon' => 34.11] ['order' => 1, 'name' => 'NAVPOINT', 'lat' => 'notanumber', 'lon' => 34.11],
]; ];
$uri = '/api/pireps/' . $pirep_id . '/route'; $uri = '/api/pireps/'.$pirep_id.'/route';
$response = $this->post($uri, $post_route); $response = $this->post($uri, $post_route);
$response->assertStatus(400); $response->assertStatus(400);
} }
@@ -420,11 +530,11 @@ class AcarsTest extends TestCase
$acars = factory(App\Models\Acars::class)->make(); $acars = factory(App\Models\Acars::class)->make();
$post_log = [ $post_log = [
'logs' => [ 'logs' => [
['log' => $acars->log] ['log' => $acars->log],
] ],
]; ];
$uri = '/api/pireps/' . $pirep_id . '/acars/logs'; $uri = '/api/pireps/'.$pirep_id.'/acars/logs';
$response = $this->post($uri, $post_log); $response = $this->post($uri, $post_log);
$response->assertStatus(200); $response->assertStatus(200);
$body = $response->json(); $body = $response->json();
@@ -434,11 +544,11 @@ class AcarsTest extends TestCase
$acars = factory(App\Models\Acars::class)->make(); $acars = factory(App\Models\Acars::class)->make();
$post_log = [ $post_log = [
'events' => [ 'events' => [
['event' => $acars->log] ['event' => $acars->log],
] ],
]; ];
$uri = '/api/pireps/' . $pirep_id . '/acars/events'; $uri = '/api/pireps/'.$pirep_id.'/acars/events';
$response = $this->post($uri, $post_log); $response = $this->post($uri, $post_log);
$response->assertStatus(200); $response->assertStatus(200);
$body = $response->json(); $body = $response->json();
@@ -462,7 +572,7 @@ class AcarsTest extends TestCase
$route_count = \random_int(2, 10); $route_count = \random_int(2, 10);
$route = factory(App\Models\Navdata::class, $route_count)->create(); $route = factory(App\Models\Navdata::class, $route_count)->create();
foreach($route as $position) { foreach ($route as $position) {
$post_route[] = [ $post_route[] = [
'order' => $order, 'order' => $order,
'id' => $position->id, 'id' => $position->id,
@@ -482,7 +592,7 @@ class AcarsTest extends TestCase
* Get * Get
*/ */
$uri = '/api/pireps/' . $pirep_id . '/route'; $uri = '/api/pireps/'.$pirep_id.'/route';
$response = $this->get($uri); $response = $this->get($uri);
$response->assertStatus(200)->assertJsonCount($route_count, 'data'); $response->assertStatus(200)->assertJsonCount($route_count, 'data');
@@ -492,11 +602,11 @@ class AcarsTest extends TestCase
/** /**
* Delete and then recheck * Delete and then recheck
*/ */
$uri = '/api/pireps/' . $pirep_id . '/route'; $uri = '/api/pireps/'.$pirep_id.'/route';
$response = $this->delete($uri); $response = $this->delete($uri);
$response->assertStatus(200); $response->assertStatus(200);
$uri = '/api/pireps/' . $pirep_id . '/route'; $uri = '/api/pireps/'.$pirep_id.'/route';
$response = $this->get($uri); $response = $this->get($uri);
$response->assertStatus(200)->assertJsonCount(0, 'data'); $response->assertStatus(200)->assertJsonCount(0, 'data');
} }