Add setting to automatically remove bid on PIREP accept #200

This commit is contained in:
Nabeel Shahzad 2018-03-15 18:20:07 -05:00
parent 58fbbd98a4
commit ccbc109db2
7 changed files with 120 additions and 39 deletions

View File

@ -177,6 +177,14 @@ class CreateSettingsTable extends Migration
'description' => 'Only allow aircraft that are at the departure airport', 'description' => 'Only allow aircraft that are at the departure airport',
]); ]);
$this->addSetting('pireps.remove_bid_on_accept', [
'name' => 'Remove bid on accept',
'group' => 'pireps',
'value' => false,
'type' => 'boolean',
'description' => 'When a PIREP is accepted, remove the bid, if it exists',
]);
/** /**
* PILOTS * PILOTS
*/ */

View File

@ -23,6 +23,8 @@ use PhpUnitsOfMeasure\Exception\NonStringUnitName;
* @property Airport dep_airport * @property Airport dep_airport
* @property integer flight_time In minutes * @property integer flight_time In minutes
* @property User user * @property User user
* @property Flight|null flight
* @property int user_id
* @package App\Models * @package App\Models
*/ */
class Pirep extends BaseModel class Pirep extends BaseModel
@ -190,6 +192,29 @@ class Pirep extends BaseModel
} }
} }
/**
* Look up the flight, based on the PIREP flight info
* @return Flight|null
*/
public function getFlightAttribute()
{
$where = [
'airline_id' => $this->airline_id,
'flight_number' => $this->flight_number,
'active' => true,
];
if (filled($this->route_code)) {
$where['route_code'] = $this->route_code;
}
if (filled($this->route_leg)) {
$where['route_leg'] = $this->route_leg;
}
return Flight::where($where)->first();
}
/** /**
* Set the amount of fuel used * Set the amount of fuel used
* @param $value * @param $value

View File

@ -22,6 +22,7 @@ use Laratrust\Traits\LaratrustUserTrait;
* @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $updated_at
* @property Rank rank * @property Rank rank
* @property Journal journal * @property Journal journal
* @property string pilot_id
* @mixin \Illuminate\Notifications\Notifiable * @mixin \Illuminate\Notifications\Notifiable
* @mixin \Laratrust\Traits\LaratrustUserTrait * @mixin \Laratrust\Traits\LaratrustUserTrait
*/ */

View File

@ -198,11 +198,6 @@ class FareService extends BaseService
$fares = []; $fares = [];
$found_fares = PirepFare::where('pirep_id', $pirep->id)->get(); $found_fares = PirepFare::where('pirep_id', $pirep->id)->get();
return $found_fares; return $found_fares;
/*foreach($found_fares as $fare) {
$fares[] = $fare->toArray();
}
return collect($fares);*/
} }
/** /**

View File

@ -323,13 +323,11 @@ class PirepFinanceService extends BaseService
*/ */
public function getReconciledFaresForPirep($pirep) public function getReconciledFaresForPirep($pirep)
{ {
$flight = $this->pirepSvc->findFlight($pirep);
# Collect all of the fares and prices # Collect all of the fares and prices
$flight_fares = $this->fareSvc->getForPirep($pirep); $flight_fares = $this->fareSvc->getForPirep($pirep);
Log::info('Finance: PIREP: ' . $pirep->id . ', flight fares: ', $flight_fares->toArray()); Log::info('Finance: PIREP: ' . $pirep->id . ', flight fares: ', $flight_fares->toArray());
$all_fares = $this->fareSvc->getAllFares($flight, $pirep->aircraft->subfleet); $all_fares = $this->fareSvc->getAllFares($pirep->flight, $pirep->aircraft->subfleet);
$fares = $all_fares->map(function($fare, $i) use ($flight_fares, $pirep) { $fares = $all_fares->map(function($fare, $i) use ($flight_fares, $pirep) {

View File

@ -7,6 +7,7 @@ use App\Events\PirepFiled;
use App\Events\PirepRejected; use App\Events\PirepRejected;
use App\Events\UserStatsChanged; use App\Events\UserStatsChanged;
use App\Models\Acars; use App\Models\Acars;
use App\Models\Bid;
use App\Models\Enums\AcarsType; use App\Models\Enums\AcarsType;
use App\Models\Enums\PirepSource; use App\Models\Enums\PirepSource;
use App\Models\Enums\PirepState; use App\Models\Enums\PirepState;
@ -29,11 +30,11 @@ use Log;
class PirepService extends BaseService class PirepService extends BaseService
{ {
private $acarsRepo, private $acarsRepo,
$flightRepo, $flightRepo,
$geoSvc, $geoSvc,
$navRepo, $navRepo,
$pilotSvc, $pilotSvc,
$pirepRepo; $pirepRepo;
/** /**
* PirepService constructor. * PirepService constructor.
@ -62,21 +63,6 @@ class PirepService extends BaseService
} }
/** /**
* Find the flight that a PIREP is based on
* @param Pirep $pirep
* @return mixed
*/
public function findFlight(Pirep $pirep)
{
return $this->flightRepo->findFlight(
$pirep->airline_id,
$pirep->flight_number,
$pirep->route_code,
$pirep->route_leg
)->first();
}
/**π
* Find if there are duplicates to a given PIREP. Ideally, the passed * Find if there are duplicates to a given PIREP. Ideally, the passed
* in PIREP hasn't been saved or gone through the create() method * in PIREP hasn't been saved or gone through the create() method
* @param Pirep $pirep * @param Pirep $pirep
@ -88,26 +74,26 @@ class PirepService extends BaseService
$time_limit = Carbon::now()->subMinutes($minutes)->toDateTimeString(); $time_limit = Carbon::now()->subMinutes($minutes)->toDateTimeString();
$where = [ $where = [
'user_id' => $pirep->user_id, 'user_id' => $pirep->user_id,
'airline_id' => $pirep->airline_id, 'airline_id' => $pirep->airline_id,
'flight_number' => $pirep->flight_number, 'flight_number' => $pirep->flight_number,
]; ];
if(filled($pirep->route_code)) { if (filled($pirep->route_code)) {
$where['route_code'] = $pirep->route_code; $where['route_code'] = $pirep->route_code;
} }
if(filled($pirep->route_leg)) { if (filled($pirep->route_leg)) {
$where['route_leg'] = $pirep->route_leg; $where['route_leg'] = $pirep->route_leg;
} }
try { try {
$found_pireps = Pirep::where($where) $found_pireps = Pirep::where($where)
->where('state', '!=', PirepState::CANCELLED) ->where('state', '!=', PirepState::CANCELLED)
->where('created_at', '>=', $time_limit) ->where('created_at', '>=', $time_limit)
->get(); ->get();
if($found_pireps->count() === 0) { if ($found_pireps->count() === 0) {
return false; return false;
} }
@ -199,7 +185,7 @@ class PirepService extends BaseService
$pirep->save(); $pirep->save();
$pirep->refresh(); $pirep->refresh();
if(\count($field_values) > 0) { if (\count($field_values) > 0) {
$this->updateCustomFields($pirep->id, $field_values); $this->updateCustomFields($pirep->id, $field_values);
} }
@ -224,10 +210,10 @@ class PirepService extends BaseService
{ {
foreach ($field_values as $fv) { foreach ($field_values as $fv) {
PirepFieldValues::updateOrCreate( PirepFieldValues::updateOrCreate(
[ 'pirep_id' => $pirep_id, ['pirep_id' => $pirep_id,
'name' => $fv['name'] 'name' => $fv['name']
], ],
[ 'value' => $fv['value'], ['value' => $fv['value'],
'source' => $fv['source'] 'source' => $fv['source']
] ]
); );
@ -311,6 +297,9 @@ class PirepService extends BaseService
$pirep->refresh(); $pirep->refresh();
# Any ancillary tasks before an event is dispatched
$this->removeBid($pirep);
event(new PirepAccepted($pirep)); event(new PirepAccepted($pirep));
return $pirep; return $pirep;
@ -365,4 +354,31 @@ class PirepService extends BaseService
event(new UserStatsChanged($pilot, 'airport', $previous_airport)); event(new UserStatsChanged($pilot, 'airport', $previous_airport));
} }
/**
* If the setting is enabled, remove the bid
* @param Pirep $pirep
* @throws \Exception
*/
public function removeBid(Pirep $pirep)
{
if (!setting('pireps.remove_bid_on_accept')) {
return;
}
$flight = $pirep->flight;
if(!$flight) {
return;
}
$bid = Bid::where([
'user_id' => $pirep->user->id,
'flight_id' => $flight->id,
]);
if($bid) {
Log::info('Bid for user: '.$pirep->user->pilot_id.' on flight '.$flight->ident);
$bid->delete();
}
}
} }

View File

@ -1,11 +1,13 @@
<?php <?php
use App\Models\Acars; use App\Models\Acars;
use App\Models\Bid;
use App\Models\Enums\AcarsType; use App\Models\Enums\AcarsType;
use App\Models\Enums\PirepState; use App\Models\Enums\PirepState;
use App\Models\Pirep; use App\Models\Pirep;
use App\Models\User; use App\Models\User;
use App\Repositories\SettingRepository; use App\Repositories\SettingRepository;
use App\Services\FlightService;
use Carbon\Carbon; use Carbon\Carbon;
class PIREPTest extends TestCase class PIREPTest extends TestCase
@ -300,4 +302,40 @@ class PIREPTest extends TestCase
$response = $this->post($uri, $acars); $response = $this->post($uri, $acars);
$response->assertStatus(400); $response->assertStatus(400);
} }
/**
* When a PIREP is accepted, ensure that the bid is removed
*/
public function testPirepBidRemoved()
{
$flightSvc = app(FlightService::class);
$this->settingsRepo->store('pireps.remove_bid_on_accept', true);
$user = factory(App\Models\User::class)->create([
'flight_time' => 0,
]);
$flight = factory(App\Models\Flight::class)->create([
'route_code' => null,
'route_leg' => null,
]);
$flightSvc->addBid($flight, $user);
$pirep = factory(App\Models\Pirep::class)->create([
'user_id' => $user->id,
'airline_id' => $flight->airline_id,
'flight_number' => $flight->flight_number,
]);
$pirep = $this->pirepSvc->create($pirep, []);
$this->pirepSvc->changeState($pirep, PirepState::ACCEPTED);
$user_bid = Bid::where([
'user_id' => $user->id,
'flight_id' => $flight->id,
])->first();
$this->assertNull($user_bid);
}
} }