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:
@@ -7,7 +7,7 @@ use App\Events\CronNightly;
|
|||||||
use App\Models\Enums\UserState;
|
use App\Models\Enums\UserState;
|
||||||
use App\Repositories\UserRepository;
|
use App\Repositories\UserRepository;
|
||||||
use App\Services\UserService;
|
use App\Services\UserService;
|
||||||
use Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This recalculates the balances on all of the journals
|
* This recalculates the balances on all of the journals
|
||||||
|
|||||||
@@ -279,7 +279,7 @@ class PirepController extends Controller
|
|||||||
// Can they fly this aircraft?
|
// Can they fly this aircraft?
|
||||||
if (setting('pireps.restrict_aircraft_to_rank', false)
|
if (setting('pireps.restrict_aircraft_to_rank', false)
|
||||||
&& !$this->userSvc->aircraftAllowed(Auth::user(), $pirep->aircraft_id)) {
|
&& !$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(
|
return $this->flashError(
|
||||||
'You are not allowed to fly this aircraft!',
|
'You are not allowed to fly this aircraft!',
|
||||||
'frontend.pireps.create'
|
'frontend.pireps.create'
|
||||||
@@ -290,7 +290,7 @@ class PirepController extends Controller
|
|||||||
/* @noinspection NotOptimalIfConditionsInspection */
|
/* @noinspection NotOptimalIfConditionsInspection */
|
||||||
if (setting('pireps.only_aircraft_at_dpt_airport')
|
if (setting('pireps.only_aircraft_at_dpt_airport')
|
||||||
&& $pirep->aircraft_id !== $pirep->dpt_airport_id) {
|
&& $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(
|
return $this->flashError(
|
||||||
'This aircraft is not positioned at the departure airport!',
|
'This aircraft is not positioned at the departure airport!',
|
||||||
'frontend.pireps.create'
|
'frontend.pireps.create'
|
||||||
|
|||||||
53
app/Listeners/BidEvents.php
Normal file
53
app/Listeners/BidEvents.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,9 +7,10 @@ use App\Contracts\Model;
|
|||||||
/**
|
/**
|
||||||
* Class Rank
|
* Class Rank
|
||||||
*
|
*
|
||||||
* @property int hours
|
* @property string name
|
||||||
* @property float manual_base_pay_rate
|
* @property int hours
|
||||||
* @property float acars_base_pay_rate
|
* @property float manual_base_pay_rate
|
||||||
|
* @property float acars_base_pay_rate
|
||||||
*/
|
*/
|
||||||
class Rank extends Model
|
class Rank extends Model
|
||||||
{
|
{
|
||||||
@@ -28,7 +29,6 @@ class Rank extends Model
|
|||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'hours' => 'integer',
|
'hours' => 'integer',
|
||||||
'base_pay_rate' => 'float',
|
|
||||||
'auto_approve_acars' => 'bool',
|
'auto_approve_acars' => 'bool',
|
||||||
'auto_approve_manual' => 'bool',
|
'auto_approve_manual' => 'bool',
|
||||||
'auto_promote' => 'bool',
|
'auto_promote' => 'bool',
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace App\Providers;
|
|||||||
use App\Events\Expenses;
|
use App\Events\Expenses;
|
||||||
use App\Events\UserStatsChanged;
|
use App\Events\UserStatsChanged;
|
||||||
use App\Listeners\AwardListener;
|
use App\Listeners\AwardListener;
|
||||||
|
use App\Listeners\BidEvents;
|
||||||
use App\Listeners\ExpenseListener;
|
use App\Listeners\ExpenseListener;
|
||||||
use App\Listeners\FinanceEvents;
|
use App\Listeners\FinanceEvents;
|
||||||
use App\Listeners\NotificationEvents;
|
use App\Listeners\NotificationEvents;
|
||||||
@@ -29,6 +30,7 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
];
|
];
|
||||||
|
|
||||||
protected $subscribe = [
|
protected $subscribe = [
|
||||||
|
BidEvents::class,
|
||||||
FinanceEvents::class,
|
FinanceEvents::class,
|
||||||
NotificationEvents::class,
|
NotificationEvents::class,
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -7,9 +7,6 @@ use App\Models\Flight;
|
|||||||
use App\Models\Rank;
|
use App\Models\Rank;
|
||||||
use App\Models\Subfleet;
|
use App\Models\Subfleet;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class FleetService
|
|
||||||
*/
|
|
||||||
class FleetService extends Service
|
class FleetService extends Service
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ use App\Models\PirepFieldValue;
|
|||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
use Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class PirepService
|
* Class PirepService
|
||||||
@@ -181,6 +181,8 @@ class PirepService extends Service
|
|||||||
$pirep->submitted_at = Carbon::now('UTC');
|
$pirep->submitted_at = Carbon::now('UTC');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$pirep->status = PirepStatus::ARRIVED;
|
||||||
|
|
||||||
$pirep->save();
|
$pirep->save();
|
||||||
$pirep->refresh();
|
$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]);
|
Log::info('New PIREP filed', [$pirep]);
|
||||||
event(new PirepFiled($pirep));
|
event(new PirepFiled($pirep));
|
||||||
|
|
||||||
@@ -224,8 +222,12 @@ class PirepService extends Service
|
|||||||
if ($default_state === PirepState::ACCEPTED) {
|
if ($default_state === PirepState::ACCEPTED) {
|
||||||
$pirep = $this->accept($pirep);
|
$pirep = $this->accept($pirep);
|
||||||
$this->setPilotState($pirep->pilot, $pirep);
|
$this->setPilotState($pirep->pilot, $pirep);
|
||||||
|
} else {
|
||||||
|
$pirep->state = $default_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$pirep->save();
|
||||||
|
|
||||||
// Check the user state, set them to ACTIVE if on leave
|
// Check the user state, set them to ACTIVE if on leave
|
||||||
if ($pirep->user->state !== UserState::ACTIVE) {
|
if ($pirep->user->state !== UserState::ACTIVE) {
|
||||||
$old_state = $pirep->user->state;
|
$old_state = $pirep->user->state;
|
||||||
@@ -260,6 +262,8 @@ class PirepService extends Service
|
|||||||
* @param Pirep $pirep
|
* @param Pirep $pirep
|
||||||
* @param int $new_state
|
* @param int $new_state
|
||||||
*
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*
|
||||||
* @return Pirep
|
* @return Pirep
|
||||||
*/
|
*/
|
||||||
public function changeState(Pirep $pirep, int $new_state)
|
public function changeState(Pirep $pirep, int $new_state)
|
||||||
@@ -341,9 +345,6 @@ class PirepService extends Service
|
|||||||
|
|
||||||
$pirep->refresh();
|
$pirep->refresh();
|
||||||
|
|
||||||
// Any ancillary tasks before an event is dispatched
|
|
||||||
$this->removeBid($pirep);
|
|
||||||
|
|
||||||
$this->setPilotState($pilot, $pirep);
|
$this->setPilotState($pilot, $pirep);
|
||||||
event(new PirepAccepted($pirep));
|
event(new PirepAccepted($pirep));
|
||||||
|
|
||||||
|
|||||||
@@ -325,6 +325,9 @@ class UserService extends Service
|
|||||||
'state' => PirepState::ACCEPTED,
|
'state' => PirepState::ACCEPTED,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
$flight_count = Pirep::where($w)->count();
|
||||||
|
$user->flights = $flight_count;
|
||||||
|
|
||||||
$flight_time = Pirep::where($w)->sum('flight_time');
|
$flight_time = Pirep::where($w)->sum('flight_time');
|
||||||
$user->flight_time = $flight_time;
|
$user->flight_time = $flight_time;
|
||||||
|
|
||||||
|
|||||||
@@ -221,6 +221,8 @@ class PIREPTest extends TestCase
|
|||||||
$this->assertGreaterThan($user->rank_id, $pilot->rank_id);
|
$this->assertGreaterThan($user->rank_id, $pilot->rank_id);
|
||||||
$this->assertEquals($last_pirep->arr_airport_id, $pilot->curr_airport_id);
|
$this->assertEquals($last_pirep->arr_airport_id, $pilot->curr_airport_id);
|
||||||
|
|
||||||
|
$this->assertEquals(2, $pilot->flights);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Submit another PIREP, adding another 6 hours
|
// Submit another PIREP, adding another 6 hours
|
||||||
// it should automatically be accepted
|
// it should automatically be accepted
|
||||||
@@ -238,6 +240,9 @@ class PIREPTest extends TestCase
|
|||||||
$this->pirepSvc->submit($pirep);
|
$this->pirepSvc->submit($pirep);
|
||||||
|
|
||||||
$pilot->refresh();
|
$pilot->refresh();
|
||||||
|
|
||||||
|
$this->assertEquals(3, $pilot->flights);
|
||||||
|
|
||||||
$latest_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
|
$latest_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
|
||||||
|
|
||||||
// Make sure PIREP was auto updated
|
// Make sure PIREP was auto updated
|
||||||
|
|||||||
Reference in New Issue
Block a user