diff --git a/app/Cron/Nightly/RecalculateStats.php b/app/Cron/Nightly/RecalculateStats.php index 9ad9bdf8..fe939598 100644 --- a/app/Cron/Nightly/RecalculateStats.php +++ b/app/Cron/Nightly/RecalculateStats.php @@ -7,7 +7,7 @@ use App\Events\CronNightly; use App\Models\Enums\UserState; use App\Repositories\UserRepository; use App\Services\UserService; -use Log; +use Illuminate\Support\Facades\Log; /** * This recalculates the balances on all of the journals diff --git a/app/Http/Controllers/Frontend/PirepController.php b/app/Http/Controllers/Frontend/PirepController.php index b389f316..7b459438 100644 --- a/app/Http/Controllers/Frontend/PirepController.php +++ b/app/Http/Controllers/Frontend/PirepController.php @@ -279,7 +279,7 @@ class PirepController extends Controller // Can they fly this aircraft? if (setting('pireps.restrict_aircraft_to_rank', false) && !$this->userSvc->aircraftAllowed(Auth::user(), $pirep->aircraft_id)) { - Log::info('Pilot ' . Auth::user()->id . ' not allowed to fly aircraft'); + Log::info('Pilot '.Auth::user()->id.' not allowed to fly aircraft'); return $this->flashError( 'You are not allowed to fly this aircraft!', 'frontend.pireps.create' @@ -290,7 +290,7 @@ class PirepController extends Controller /* @noinspection NotOptimalIfConditionsInspection */ if (setting('pireps.only_aircraft_at_dpt_airport') && $pirep->aircraft_id !== $pirep->dpt_airport_id) { - Log::info('Aircraft ' . $pirep->aircraft_id . ' not at departure airport '.$pirep->dpt_airport_id.', erroring out'); + Log::info('Aircraft '.$pirep->aircraft_id.' not at departure airport '.$pirep->dpt_airport_id.', erroring out'); return $this->flashError( 'This aircraft is not positioned at the departure airport!', 'frontend.pireps.create' diff --git a/app/Listeners/BidEvents.php b/app/Listeners/BidEvents.php new file mode 100644 index 00000000..9e208f3e --- /dev/null +++ b/app/Listeners/BidEvents.php @@ -0,0 +1,53 @@ +pirepSvc = $pirepSvc; + } + + /** + * @param $events + */ + public function subscribe(Dispatcher $events): void + { + $events->listen( + PirepAccepted::class, + 'App\Listeners\BidEvents@onPirepAccept' + ); + } + + /** + * When a PIREP is accepted, remove any bids + * + * @param PirepAccepted $event + * + * @throws \UnexpectedValueException + * @throws \InvalidArgumentException + * @throws \Exception + * @throws \Prettus\Validator\Exceptions\ValidatorException + */ + public function onPirepAccept(PirepAccepted $event): void + { + $this->pirepSvc->removeBid($event->pirep); + } +} diff --git a/app/Models/Rank.php b/app/Models/Rank.php index 8cb685ae..d5d7d399 100644 --- a/app/Models/Rank.php +++ b/app/Models/Rank.php @@ -7,9 +7,10 @@ use App\Contracts\Model; /** * Class Rank * - * @property int hours - * @property float manual_base_pay_rate - * @property float acars_base_pay_rate + * @property string name + * @property int hours + * @property float manual_base_pay_rate + * @property float acars_base_pay_rate */ class Rank extends Model { @@ -28,7 +29,6 @@ class Rank extends Model protected $casts = [ 'hours' => 'integer', - 'base_pay_rate' => 'float', 'auto_approve_acars' => 'bool', 'auto_approve_manual' => 'bool', 'auto_promote' => 'bool', diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index a202f469..6e28cee2 100755 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -5,6 +5,7 @@ namespace App\Providers; use App\Events\Expenses; use App\Events\UserStatsChanged; use App\Listeners\AwardListener; +use App\Listeners\BidEvents; use App\Listeners\ExpenseListener; use App\Listeners\FinanceEvents; use App\Listeners\NotificationEvents; @@ -29,6 +30,7 @@ class EventServiceProvider extends ServiceProvider ]; protected $subscribe = [ + BidEvents::class, FinanceEvents::class, NotificationEvents::class, ]; diff --git a/app/Services/FleetService.php b/app/Services/FleetService.php index 3ef3e033..2b07a938 100644 --- a/app/Services/FleetService.php +++ b/app/Services/FleetService.php @@ -7,9 +7,6 @@ use App\Models\Flight; use App\Models\Rank; use App\Models\Subfleet; -/** - * Class FleetService - */ class FleetService extends Service { /** diff --git a/app/Services/PirepService.php b/app/Services/PirepService.php index 24f9cc87..ce3c7937 100644 --- a/app/Services/PirepService.php +++ b/app/Services/PirepService.php @@ -21,7 +21,7 @@ use App\Models\PirepFieldValue; use App\Models\User; use Carbon\Carbon; use Illuminate\Database\Eloquent\ModelNotFoundException; -use Log; +use Illuminate\Support\Facades\Log; /** * Class PirepService @@ -181,6 +181,8 @@ class PirepService extends Service $pirep->submitted_at = Carbon::now('UTC'); } + $pirep->status = PirepStatus::ARRIVED; + $pirep->save(); $pirep->refresh(); @@ -213,10 +215,6 @@ class PirepService extends Service } } - $pirep->state = $default_state; - $pirep->status = PirepStatus::ARRIVED; - $pirep->save(); - Log::info('New PIREP filed', [$pirep]); event(new PirepFiled($pirep)); @@ -224,8 +222,12 @@ class PirepService extends Service if ($default_state === PirepState::ACCEPTED) { $pirep = $this->accept($pirep); $this->setPilotState($pirep->pilot, $pirep); + } else { + $pirep->state = $default_state; } + $pirep->save(); + // Check the user state, set them to ACTIVE if on leave if ($pirep->user->state !== UserState::ACTIVE) { $old_state = $pirep->user->state; @@ -260,6 +262,8 @@ class PirepService extends Service * @param Pirep $pirep * @param int $new_state * + * @throws \Exception + * * @return Pirep */ public function changeState(Pirep $pirep, int $new_state) @@ -341,9 +345,6 @@ class PirepService extends Service $pirep->refresh(); - // Any ancillary tasks before an event is dispatched - $this->removeBid($pirep); - $this->setPilotState($pilot, $pirep); event(new PirepAccepted($pirep)); diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 96bd20b1..f968027a 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -325,6 +325,9 @@ class UserService extends Service 'state' => PirepState::ACCEPTED, ]; + $flight_count = Pirep::where($w)->count(); + $user->flights = $flight_count; + $flight_time = Pirep::where($w)->sum('flight_time'); $user->flight_time = $flight_time; diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index 992282bf..f61f8a7a 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -221,6 +221,8 @@ class PIREPTest extends TestCase $this->assertGreaterThan($user->rank_id, $pilot->rank_id); $this->assertEquals($last_pirep->arr_airport_id, $pilot->curr_airport_id); + $this->assertEquals(2, $pilot->flights); + // // Submit another PIREP, adding another 6 hours // it should automatically be accepted @@ -238,6 +240,9 @@ class PIREPTest extends TestCase $this->pirepSvc->submit($pirep); $pilot->refresh(); + + $this->assertEquals(3, $pilot->flights); + $latest_pirep = Pirep::where('id', $pilot->last_pirep_id)->first(); // Make sure PIREP was auto updated