diff --git a/app/Database/migrations/2017_06_07_014930_create_settings_table.php b/app/Database/migrations/2017_06_07_014930_create_settings_table.php index 96a0c22a..80b17713 100644 --- a/app/Database/migrations/2017_06_07_014930_create_settings_table.php +++ b/app/Database/migrations/2017_06_07_014930_create_settings_table.php @@ -177,6 +177,14 @@ class CreateSettingsTable extends Migration '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 */ diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index e8459f09..24705dad 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -23,6 +23,8 @@ use PhpUnitsOfMeasure\Exception\NonStringUnitName; * @property Airport dep_airport * @property integer flight_time In minutes * @property User user + * @property Flight|null flight + * @property int user_id * @package App\Models */ 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 * @param $value diff --git a/app/Models/User.php b/app/Models/User.php index 6061cf5b..d733adc3 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -22,6 +22,7 @@ use Laratrust\Traits\LaratrustUserTrait; * @property \Carbon\Carbon $updated_at * @property Rank rank * @property Journal journal + * @property string pilot_id * @mixin \Illuminate\Notifications\Notifiable * @mixin \Laratrust\Traits\LaratrustUserTrait */ diff --git a/app/Services/FareService.php b/app/Services/FareService.php index dd94c763..92271d5a 100644 --- a/app/Services/FareService.php +++ b/app/Services/FareService.php @@ -198,11 +198,6 @@ class FareService extends BaseService $fares = []; $found_fares = PirepFare::where('pirep_id', $pirep->id)->get(); return $found_fares; - /*foreach($found_fares as $fare) { - $fares[] = $fare->toArray(); - } - - return collect($fares);*/ } /** diff --git a/app/Services/Finance/PirepFinanceService.php b/app/Services/Finance/PirepFinanceService.php index acac6f46..d5ad0c20 100644 --- a/app/Services/Finance/PirepFinanceService.php +++ b/app/Services/Finance/PirepFinanceService.php @@ -323,13 +323,11 @@ class PirepFinanceService extends BaseService */ public function getReconciledFaresForPirep($pirep) { - $flight = $this->pirepSvc->findFlight($pirep); - # Collect all of the fares and prices $flight_fares = $this->fareSvc->getForPirep($pirep); 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) { diff --git a/app/Services/PirepService.php b/app/Services/PirepService.php index b4d9f91d..4bf7f3c8 100644 --- a/app/Services/PirepService.php +++ b/app/Services/PirepService.php @@ -7,6 +7,7 @@ use App\Events\PirepFiled; use App\Events\PirepRejected; use App\Events\UserStatsChanged; use App\Models\Acars; +use App\Models\Bid; use App\Models\Enums\AcarsType; use App\Models\Enums\PirepSource; use App\Models\Enums\PirepState; @@ -29,11 +30,11 @@ use Log; class PirepService extends BaseService { private $acarsRepo, - $flightRepo, - $geoSvc, - $navRepo, - $pilotSvc, - $pirepRepo; + $flightRepo, + $geoSvc, + $navRepo, + $pilotSvc, + $pirepRepo; /** * 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 * in PIREP hasn't been saved or gone through the create() method * @param Pirep $pirep @@ -88,26 +74,26 @@ class PirepService extends BaseService $time_limit = Carbon::now()->subMinutes($minutes)->toDateTimeString(); $where = [ - 'user_id' => $pirep->user_id, - 'airline_id' => $pirep->airline_id, + 'user_id' => $pirep->user_id, + 'airline_id' => $pirep->airline_id, 'flight_number' => $pirep->flight_number, ]; - if(filled($pirep->route_code)) { + if (filled($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; } try { $found_pireps = Pirep::where($where) - ->where('state', '!=', PirepState::CANCELLED) - ->where('created_at', '>=', $time_limit) - ->get(); + ->where('state', '!=', PirepState::CANCELLED) + ->where('created_at', '>=', $time_limit) + ->get(); - if($found_pireps->count() === 0) { + if ($found_pireps->count() === 0) { return false; } @@ -199,7 +185,7 @@ class PirepService extends BaseService $pirep->save(); $pirep->refresh(); - if(\count($field_values) > 0) { + if (\count($field_values) > 0) { $this->updateCustomFields($pirep->id, $field_values); } @@ -224,10 +210,10 @@ class PirepService extends BaseService { foreach ($field_values as $fv) { PirepFieldValues::updateOrCreate( - [ 'pirep_id' => $pirep_id, + ['pirep_id' => $pirep_id, 'name' => $fv['name'] ], - [ 'value' => $fv['value'], + ['value' => $fv['value'], 'source' => $fv['source'] ] ); @@ -311,6 +297,9 @@ class PirepService extends BaseService $pirep->refresh(); + # Any ancillary tasks before an event is dispatched + $this->removeBid($pirep); + event(new PirepAccepted($pirep)); return $pirep; @@ -365,4 +354,31 @@ class PirepService extends BaseService 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(); + } + } } diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index 38a18e96..291e842f 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -1,11 +1,13 @@ post($uri, $acars); $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); + } }