Fix aircraft retrieval for Simbrief (#1089)

* Fix full aircraft retrieval for simbriefs

* F off StyleCI
This commit is contained in:
Nabeel S 2021-03-19 18:25:19 -04:00 committed by GitHub
parent 11824c9f8b
commit 1287766a46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 116 additions and 21 deletions

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,7 @@ class Bid extends Resource
public function toArray($request) public function toArray($request)
{ {
$res = parent::toArray($request); $res = parent::toArray($request);
$res['flight'] = new Flight($this->flight); $res['flight'] = new BidFlight($this->flight);
return $res; return $res;
} }

View File

@ -0,0 +1,36 @@
<?php
namespace App\Http\Resources;
use App\Http\Resources\SimBrief as SimbriefResource;
/**
* @mixin \App\Models\Flight
*/
class BidFlight extends Flight
{
/**
* @param \Illuminate\Http\Request $request
*
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
*
* @return array
*/
public function toArray($request)
{
$res = parent::toArray($request);
if ($this->whenLoaded('simbrief')) {
unset($res['subfleets']);
$res['simbrief'] = new SimbriefResource($this->simbrief);
} else {
unset($res['simbrief']);
$res['subfleets'] = Subfleet::collection($this->whenLoaded('subfleets'));
}
$res['fields'] = $this->setFields();
return $res;
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace App\Http\Resources;
class BidSubfleet extends Subfleet
{
protected $aircraft;
protected $fares;
public function __construct($resource, $aircraft, $fares)
{
parent::__construct($resource);
$this->aircraft = $aircraft;
$this->fares = $fares;
}
public function toArray($request)
{
$res = [];
$res['airline_id'] = $this->airline_id;
$res['hub_id'] = $this->hub_id;
$res['type'] = $this->type;
$res['simbrief_type'] = $this->simbrief_type;
$res['name'] = $this->name;
$res['fuel_type'] = $this->fuel_type;
$res['cost_block_hour'] = $this->cost_block_hour;
$res['cost_delay_minute'] = $this->cost_delay_minute;
$res['ground_handling_multiplier'] = $this->ground_handling_multiplier;
$res['cargo_capacity'] = $this->cargo_capacity;
$res['fuel_capacity'] = $this->fuel_capacity;
$res['gross_weight'] = $this->gross_weight;
$res['fares'] = Fare::collection($this->fares);
// There should only be one aircraft tied to a bid subfleet, wrap in a collection
$res['aircraft'] = Aircraft::collection([$this->aircraft]);
return $res;
}
}

View File

@ -15,7 +15,7 @@ class Flight extends Resource
/** /**
* Set the fields on the flight object * Set the fields on the flight object
*/ */
private function setFields() protected function setFields()
{ {
/** @var \Illuminate\Support\Collection $field_values */ /** @var \Illuminate\Support\Collection $field_values */
$return_values = new stdClass(); $return_values = new stdClass();

View File

@ -12,27 +12,27 @@ class SimBrief extends Resource
public function toArray($request) public function toArray($request)
{ {
$data = [ $data = [
'id' => $this->id, 'id' => $this->id,
'url' => url(route('api.flights.briefing', ['id' => $this->id])), 'aircraft_id' => $this->aircraft_id,
'url' => url(route('api.flights.briefing', ['id' => $this->id])),
]; ];
$fares = [];
try { try {
if (!empty($this->fare_data)) { if (!empty($this->fare_data)) {
$fares = [];
$fare_data = json_decode($this->fare_data, true); $fare_data = json_decode($this->fare_data, true);
foreach ($fare_data as $fare) { foreach ($fare_data as $fare) {
$fares[] = new \App\Models\Fare($fare); $fares[] = new \App\Models\Fare($fare);
} }
$this->aircraft->subfleet->fares = collect($fares); $fares = collect($fares);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
// Invalid fare data // Invalid fare data
} }
if ($this->aircraft->subfleet) { $data['subfleet'] = new BidSubfleet($this->aircraft->subfleet, $this->aircraft, $fares);
$data['subfleet'] = new Subfleet($this->aircraft->subfleet);
}
return $data; return $data;
} }

View File

@ -49,7 +49,7 @@ class SimBrief extends Model
* *
* @return \App\Models\SimBriefXML|null * @return \App\Models\SimBriefXML|null
*/ */
public function getXmlAttribute(): SimBriefXML public function getXmlAttribute(): ?SimBriefXML
{ {
if (empty($this->attributes['ofp_xml'])) { if (empty($this->attributes['ofp_xml'])) {
return null; return null;

View File

@ -47,22 +47,26 @@ class BidService extends Service
*/ */
public function findBidsForUser(User $user) public function findBidsForUser(User $user)
{ {
$bids = Bid::with([ $with = [
'flight', 'flight',
'flight.fares', 'flight.fares',
'flight.simbrief' => function ($query) use ($user) { 'flight.simbrief' => function ($query) use ($user) {
$query->where('user_id', $user->id); $query->where('user_id', $user->id);
}, },
'flight.simbrief.aircraft', 'flight.simbrief.aircraft',
'flight.simbrief.aircraft.subfleet',
'flight.subfleets', 'flight.subfleets',
'flight.subfleets.aircraft', 'flight.subfleets.aircraft',
'flight.subfleets.fares', 'flight.subfleets.fares',
]) ];
->where(['user_id' => $user->id])->get();
$bids = Bid::with($with)->where(['user_id' => $user->id])->get();
foreach ($bids as $bid) { foreach ($bids as $bid) {
$bid->flight = $this->flightSvc->filterSubfleets($user, $bid->flight); if (empty($bid->flight->simbrief)) {
$bid->flight = $this->fareSvc->getReconciledFaresForFlight($bid->flight); $bid->flight = $this->flightSvc->filterSubfleets($user, $bid->flight);
$bid->flight = $this->fareSvc->getReconciledFaresForFlight($bid->flight);
}
} }
return $bids; return $bids;

View File

@ -135,6 +135,9 @@ class FlightService extends Service
*/ */
public function filterSubfleets(User $user, Flight $flight) public function filterSubfleets(User $user, Flight $flight)
{ {
// Eager load some of the relationships needed
//$flight->load(['flight.subfleets', 'flight.subfleets.aircraft', 'flight.subfleets.fares']);
/** @var \Illuminate\Support\Collection $subfleets */ /** @var \Illuminate\Support\Collection $subfleets */
$subfleets = $flight->subfleets; $subfleets = $flight->subfleets;

View File

@ -28,7 +28,7 @@ class SimBriefTest extends TestCase
*/ */
public function createUserData(array $attrs = []): array public function createUserData(array $attrs = []): array
{ {
$subfleet = $this->createSubfleetWithAircraft(1); $subfleet = $this->createSubfleetWithAircraft(2);
$rank = $this->createRank(2, [$subfleet['subfleet']->id]); $rank = $this->createRank(2, [$subfleet['subfleet']->id]);
/** @var User $user */ /** @var User $user */
@ -141,7 +141,8 @@ class SimBriefTest extends TestCase
{ {
$userinfo = $this->createUserData(); $userinfo = $this->createUserData();
$this->user = $userinfo['user']; $this->user = $userinfo['user'];
$briefing = $this->loadSimBrief($this->user, $userinfo['aircraft']->first(), [ $aircraft = $userinfo['aircraft']->random();
$briefing = $this->loadSimBrief($this->user, $aircraft, [
[ [
'id' => 100, 'id' => 100,
'code' => 'F', 'code' => 'F',
@ -196,22 +197,30 @@ class SimBriefTest extends TestCase
$userinfo = $this->createUserData(); $userinfo = $this->createUserData();
$this->user = $userinfo['user']; $this->user = $userinfo['user'];
$this->loadSimBrief($this->user, $userinfo['aircraft']->first(), $fares); $aircraft = $userinfo['aircraft']->random();
$this->loadSimBrief($this->user, $aircraft, $fares);
// Find the flight // Add the flight to the bid and then
$uri = '/api/user/bids'; $uri = '/api/user/bids';
$data = ['flight_id' => self::$simbrief_flight_id]; $data = ['flight_id' => self::$simbrief_flight_id];
$body = $this->put($uri, $data); $this->put($uri, $data);
$body = $body->json('data');
// Retrieve it
$body = $this->get($uri);
$body = $body->json('data')[0];
// Make sure Simbrief is there // Make sure Simbrief is there
$this->assertNotNull($body['flight']['simbrief']['id']); $this->assertNotNull($body['flight']['simbrief']['id']);
$this->assertNotNull($body['flight']['simbrief']['id']);
$this->assertNotNull($body['flight']['simbrief']['subfleet']['fares']); $this->assertNotNull($body['flight']['simbrief']['subfleet']['fares']);
$subfleet = $body['flight']['simbrief']['subfleet']; $subfleet = $body['flight']['simbrief']['subfleet'];
$this->assertEquals($fares[0]['id'], $subfleet['fares'][0]['id']); $this->assertEquals($fares[0]['id'], $subfleet['fares'][0]['id']);
$this->assertEquals($fares[0]['count'], $subfleet['fares'][0]['count']); $this->assertEquals($fares[0]['count'], $subfleet['fares'][0]['count']);
$this->assertCount(1, $subfleet['aircraft']);
$this->assertEquals($aircraft->id, $subfleet['aircraft'][0]['id']);
} }
/** /**