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)
{
$res = parent::toArray($request);
$res['flight'] = new Flight($this->flight);
$res['flight'] = new BidFlight($this->flight);
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
*/
private function setFields()
protected function setFields()
{
/** @var \Illuminate\Support\Collection $field_values */
$return_values = new stdClass();

View File

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

View File

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

View File

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

View File

@ -135,6 +135,9 @@ class FlightService extends Service
*/
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 */
$subfleets = $flight->subfleets;

View File

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