Skip to content

Commit

Permalink
Fix aircraft retrieval for Simbrief (#1089)
Browse files Browse the repository at this point in the history
* Fix full aircraft retrieval for simbriefs

* F off StyleCI
  • Loading branch information
nabeelio committed Mar 19, 2021
1 parent 11824c9 commit 1287766
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 21 deletions.
2 changes: 2 additions & 0 deletions app/Database/seeds/dev/simbrief.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/Http/Resources/Bid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
36 changes: 36 additions & 0 deletions app/Http/Resources/BidFlight.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
41 changes: 41 additions & 0 deletions app/Http/Resources/BidSubfleet.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion app/Http/Resources/Flight.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 7 additions & 7 deletions app/Http/Resources/SimBrief.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ class SimBrief extends Resource
public function toArray($request)
{
$data = [
'id' => $this->id,
'url' => url(route('api.flights.briefing', ['id' => $this->id])),
'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;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/SimBrief.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 9 additions & 5 deletions app/Services/BidService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,26 @@ public function getBid($bid_id)
*/
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) {
$bid->flight = $this->flightSvc->filterSubfleets($user, $bid->flight);
$bid->flight = $this->fareSvc->getReconciledFaresForFlight($bid->flight);
if (empty($bid->flight->simbrief)) {
$bid->flight = $this->flightSvc->filterSubfleets($user, $bid->flight);
$bid->flight = $this->fareSvc->getReconciledFaresForFlight($bid->flight);
}
}

return $bids;
Expand Down
3 changes: 3 additions & 0 deletions app/Services/FlightService.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ protected function transformFlightFields($fields)
*/
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;

Expand Down
21 changes: 15 additions & 6 deletions tests/SimBriefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -141,7 +141,8 @@ public function testApiCalls()
{
$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',
Expand Down Expand Up @@ -196,22 +197,30 @@ public function testUserBidSimbrief()

$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']);
}

/**
Expand Down

0 comments on commit 1287766

Please sign in to comment.