Allow draft for PIREPs, separate out the save/submit and delete functionality

This commit is contained in:
Nabeel Shahzad
2018-05-10 10:35:10 -05:00
parent 5230fc9600
commit df88cb141a
9 changed files with 127 additions and 86 deletions

View File

@@ -461,7 +461,7 @@ pireps:
dpt_airport_id: KJFK
arr_airport_id: KAUS
flight_time: 180 # 6 hours
state: 1 # pending
state: 5 # draft
source: 0 # manual
flight_type: J
route: PLMMR2 SPA Q22 BEARI FAK PHLBO3

View File

@@ -26,6 +26,7 @@ use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Log;
use PirepStatus;
/**
* Class PirepController
@@ -292,6 +293,17 @@ class PirepController extends Controller
$this->saveFares($pirep, $request);
$this->pirepSvc->saveRoute($pirep);
// Depending on the button they selected, set an initial state
// Can be saved as a draft or just submitted
if ($attrs['submit'] === 'save') {
$pirep->status = PirepState::DRAFT;
$pirep->save();
Flash::success('PIREP saved successfully.');
} else if ($attrs['submit'] === 'submit') {
$this->pirepSvc->submit($pirep);
Flash::success('PIREP submitted!');
}
return redirect(route('frontend.pireps.show', ['id' => $pirep->id]));
}
@@ -344,15 +356,12 @@ class PirepController extends Controller
public function update($id, UpdatePirepRequest $request)
{
$pirep = $this->pirepRepo->findWithoutFail($id);
if (empty($pirep)) {
Flash::error('Pirep not found');
return redirect(route('admin.pireps.index'));
}
$orig_route = $pirep->route;
$attrs = $request->all();
# Fix the time
@@ -370,8 +379,39 @@ class PirepController extends Controller
$this->saveCustomFields($pirep, $request);
$this->saveFares($pirep, $request);
Flash::success('Pirep updated successfully.');
if($attrs['submit'] === 'save') {
Flash::success('PIREP saved successfully.');
} else if($attrs['submit'] === 'submit') {
$this->pirepSvc->submit($pirep);
Flash::success('PIREP submitted!');
} else if($attrs['submit'] === 'cancel') {
$this->pirepRepo->update([
'state' => PirepState::CANCELLED,
'status' => PirepStatus::CANCELLED,
], $pirep->id);
Flash::success('PIREP cancelled!');
return redirect(route('frontend.pireps.index'));
}
return redirect(route('frontend.pireps.show', ['id' => $pirep->id]));
}
/**
* Submit the PIREP
* @param $id
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function submit($id, Request $request)
{
$pirep = $this->pirepRepo->findWithoutFail($id);
if (empty($pirep)) {
Flash::error('PIREP not found');
return redirect(route('admin.pireps.index'));
}
$this->pirepSvc->submit($pirep);
return redirect(route('frontend.pireps.show', ['id' => $pirep->id]));
}
}

View File

@@ -109,20 +109,8 @@ class Pirep extends Model
'score' => 'integer',
'source' => 'integer',
'state' => 'integer',
#'block_off_time' => 'datetime',
#'block_on_time' => 'datetime',
#'submitted_at' => 'datetime',
];
/*protected $dates = [
'block_off_time',
'block_on_time',
'submitted_at',
'created_at',
'updated_at',
'deleted_at'
];*/
public static $rules = [
'airline_id' => 'required|exists:airlines,id',
'aircraft_id' => 'required|exists:aircraft,id',
@@ -133,10 +121,15 @@ class Pirep extends Model
'route' => 'nullable',
];
/*public static $sanitize = [
'dpt_airport_id' => 'trim|uppercase',
'arr_airport_id' => 'trim|uppercase',
];*/
/**
* If a PIREP is in these states, then it can't be changed.
*/
public static $read_only_states = [
PirepState::ACCEPTED,
PirepState::REJECTED,
PirepState::PENDING,
PirepState::CANCELLED,
];
/**
* Get the flight ident, e.,g JBU1900
@@ -240,8 +233,7 @@ class Pirep extends Model
*/
public function getReadOnlyAttribute(): bool
{
return $this->state !== PirepState::PENDING
&& $this->state != PirepState::IN_PROGRESS;
return \in_array($this->state, static::$read_only_states, true);
}
/**
@@ -375,11 +367,7 @@ class Pirep extends Model
*/
public function allowedUpdates(): bool
{
if ($this->state === PirepState::CANCELLED) {
return false;
}
return true;
return ! $this->getReadOnlyAttribute();
}
/**

View File

@@ -37,6 +37,7 @@ Route::group([
Route::get('pireps/fares', 'PirepController@fares');
Route::resource('pireps', 'PirepController');
Route::post('pireps/{id}/submit', 'PirepController@submit')->name('pireps.submit');
Route::get('profile/regen_apikey', 'ProfileController@regen_apikey')
->name('profile.regen_apikey');

View File

@@ -172,19 +172,6 @@ class PirepService extends Service
$field_values = [];
}
# Figure out what default state should be. Look at the default
# 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) {
$default_state = PirepState::ACCEPTED;
}
} else {
if ($pirep->pilot->rank->auto_approve_manual) {
$default_state = PirepState::ACCEPTED;
}
}
# Check the block times. If a block on (arrival) time isn't
# specified, then use the time that it was submitted. It won't
# be the most accurate, but that might be OK
@@ -214,6 +201,28 @@ class PirepService extends Service
$this->updateCustomFields($pirep->id, $field_values);
}
return $pirep;
}
/**
* Submit the PIREP. Figure out its default state
* @param Pirep $pirep
*/
public function submit(Pirep $pirep)
{
# Figure out what default state should be. Look at the default
# 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) {
$default_state = PirepState::ACCEPTED;
}
} else {
if ($pirep->pilot->rank->auto_approve_manual) {
$default_state = PirepState::ACCEPTED;
}
}
Log::info('New PIREP filed', [$pirep]);
event(new PirepFiled($pirep));
@@ -224,15 +233,13 @@ class PirepService extends Service
}
# 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;
$pirep->user->state = UserState::ACTIVE;
$pirep->user->save();
event(new UserStateChanged($pirep->user, $old_state));
}
return $pirep;
}
/**