Don't allow cancels from certain states (#396)

* Don't allow cancels from certain states

* Unused imports

* Don't reset the state doubly

* Move SetUserActive into listener; code cleanup

* Unused imports

* Add missing files into htaccess

* Move Command contract to correct folder
This commit is contained in:
Nabeel S
2019-09-16 13:08:26 -04:00
committed by GitHub
parent 41baefbf4a
commit aedb1f22b6
61 changed files with 231 additions and 138 deletions

View File

@@ -6,20 +6,19 @@ use App\Contracts\Service;
use App\Events\PirepAccepted;
use App\Events\PirepFiled;
use App\Events\PirepRejected;
use App\Events\UserStateChanged;
use App\Events\UserStatsChanged;
use App\Exceptions\PirepCancelNotAllowed;
use App\Models\Acars;
use App\Models\Aircraft;
use App\Models\Bid;
use App\Models\Enums\AcarsType;
use App\Models\Enums\PirepSource;
use App\Models\Enums\PirepState;
use App\Models\Enums\PirepStatus;
use App\Models\Enums\UserState;
use App\Models\Navdata;
use App\Models\Pirep;
use App\Models\PirepFieldValue;
use App\Models\User;
use App\Repositories\PirepRepository;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Log;
@@ -31,19 +30,23 @@ class PirepService extends Service
{
private $geoSvc;
private $pilotSvc;
private $pirepRepo;
/**
* PirepService constructor.
*
* @param GeoService $geoSvc
* @param UserService $pilotSvc
* @param GeoService $geoSvc
* @param PirepRepository $pirepRepo
* @param UserService $pilotSvc
*/
public function __construct(
GeoService $geoSvc,
PirepRepository $pirepRepo,
UserService $pilotSvc
) {
$this->geoSvc = $geoSvc;
$this->pilotSvc = $pilotSvc;
$this->pirepRepo = $pirepRepo;
}
/**
@@ -207,11 +210,11 @@ class PirepService extends Service
// behavior from the rank that the pilot is assigned to
$default_state = PirepState::PENDING;
if ($pirep->source === PirepSource::ACARS) {
if ($pirep->pilot->rank->auto_approve_acars) {
if ($pirep->user->rank->auto_approve_acars) {
$default_state = PirepState::ACCEPTED;
}
} else {
if ($pirep->pilot->rank->auto_approve_manual) {
if ($pirep->user->rank->auto_approve_manual) {
$default_state = PirepState::ACCEPTED;
}
}
@@ -222,21 +225,36 @@ class PirepService extends Service
// only update the pilot last state if they are accepted
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;
$pirep->user->state = UserState::ACTIVE;
$pirep->user->save();
/**
* Cancel a PIREP
*
* @param Pirep $pirep
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
*
* @return Pirep
*/
public function cancel(Pirep $pirep): Pirep
{
if (in_array($pirep->state, Pirep::$cancel_states, true)) {
Log::info('PIREP '.$pirep->id.' can\'t be cancelled, state='.$pirep->state);
event(new UserStateChanged($pirep->user, $old_state));
throw new PirepCancelNotAllowed($pirep);
}
$pirep = $this->pirepRepo->update([
'state' => PirepState::CANCELLED,
'status' => PirepStatus::CANCELLED,
], $pirep->id);
return $pirep;
}
/**
@@ -324,12 +342,12 @@ class PirepService extends Service
}
$ft = $pirep->flight_time;
$pilot = $pirep->pilot;
$pilot = $pirep->user;
$this->pilotSvc->adjustFlightTime($pilot, $ft);
$this->pilotSvc->adjustFlightCount($pilot, +1);
$this->pilotSvc->calculatePilotRank($pilot);
$pirep->pilot->refresh();
$pirep->user->refresh();
// Change the status
$pirep->state = PirepState::ACCEPTED;
@@ -362,13 +380,13 @@ class PirepService extends Service
// If this was previously ACCEPTED, then reconcile the flight hours
// that have already been counted, etc
if ($pirep->state === PirepState::ACCEPTED) {
$pilot = $pirep->pilot;
$user = $pirep->user;
$ft = $pirep->flight_time * -1;
$this->pilotSvc->adjustFlightTime($pilot, $ft);
$this->pilotSvc->adjustFlightCount($pilot, -1);
$this->pilotSvc->calculatePilotRank($pilot);
$pirep->pilot->refresh();
$this->pilotSvc->adjustFlightTime($user, $ft);
$this->pilotSvc->adjustFlightCount($user, -1);
$this->pilotSvc->calculatePilotRank($user);
$pirep->user->refresh();
}
// Change the status
@@ -387,6 +405,7 @@ class PirepService extends Service
}
/**
* @param User $pilot
* @param Pirep $pirep
*/
public function setPilotState(User $pilot, Pirep $pirep)