Fix issue where user stats aren't incremented on PIREP auto accept (#335)

* Fix issue where user stats aren't incremented on PIREP auto accept

* Formatting
This commit is contained in:
Nabeel S 2019-08-01 15:23:59 -04:00 committed by GitHub
parent 63485d5a0f
commit 95147e31bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 79 additions and 18 deletions

View File

@ -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

View File

@ -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'

View File

@ -0,0 +1,53 @@
<?php
namespace App\Listeners;
use App\Contracts\Listener;
use App\Events\PirepAccepted;
use App\Services\PirepService;
use Illuminate\Contracts\Events\Dispatcher;
/**
* Do stuff with bids - like if a PIREP is accepted, then remove the bid
*/
class BidEvents extends Listener
{
private $pirepSvc;
/**
* FinanceEvents constructor.
*
* @param PirepService $pirepSvc
*/
public function __construct(
PirepService $pirepSvc
) {
$this->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);
}
}

View File

@ -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',

View File

@ -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,
];

View File

@ -7,9 +7,6 @@ use App\Models\Flight;
use App\Models\Rank;
use App\Models\Subfleet;
/**
* Class FleetService
*/
class FleetService extends Service
{
/**

View File

@ -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));

View File

@ -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;

View File

@ -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