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 dpt_airport_id: KJFK
arr_airport_id: KAUS arr_airport_id: KAUS
flight_time: 180 # 6 hours flight_time: 180 # 6 hours
state: 1 # pending state: 5 # draft
source: 0 # manual source: 0 # manual
flight_type: J flight_type: J
route: PLMMR2 SPA Q22 BEARI FAK PHLBO3 route: PLMMR2 SPA Q22 BEARI FAK PHLBO3

View File

@@ -26,6 +26,7 @@ use Flash;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Log; use Log;
use PirepStatus;
/** /**
* Class PirepController * Class PirepController
@@ -292,6 +293,17 @@ class PirepController extends Controller
$this->saveFares($pirep, $request); $this->saveFares($pirep, $request);
$this->pirepSvc->saveRoute($pirep); $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])); return redirect(route('frontend.pireps.show', ['id' => $pirep->id]));
} }
@@ -344,15 +356,12 @@ class PirepController extends Controller
public function update($id, UpdatePirepRequest $request) public function update($id, UpdatePirepRequest $request)
{ {
$pirep = $this->pirepRepo->findWithoutFail($id); $pirep = $this->pirepRepo->findWithoutFail($id);
if (empty($pirep)) { if (empty($pirep)) {
Flash::error('Pirep not found'); Flash::error('Pirep not found');
return redirect(route('admin.pireps.index')); return redirect(route('admin.pireps.index'));
} }
$orig_route = $pirep->route; $orig_route = $pirep->route;
$attrs = $request->all(); $attrs = $request->all();
# Fix the time # Fix the time
@@ -370,8 +379,39 @@ class PirepController extends Controller
$this->saveCustomFields($pirep, $request); $this->saveCustomFields($pirep, $request);
$this->saveFares($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])); 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', 'score' => 'integer',
'source' => 'integer', 'source' => 'integer',
'state' => '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 = [ public static $rules = [
'airline_id' => 'required|exists:airlines,id', 'airline_id' => 'required|exists:airlines,id',
'aircraft_id' => 'required|exists:aircraft,id', 'aircraft_id' => 'required|exists:aircraft,id',
@@ -133,10 +121,15 @@ class Pirep extends Model
'route' => 'nullable', 'route' => 'nullable',
]; ];
/*public static $sanitize = [ /**
'dpt_airport_id' => 'trim|uppercase', * If a PIREP is in these states, then it can't be changed.
'arr_airport_id' => 'trim|uppercase', */
];*/ public static $read_only_states = [
PirepState::ACCEPTED,
PirepState::REJECTED,
PirepState::PENDING,
PirepState::CANCELLED,
];
/** /**
* Get the flight ident, e.,g JBU1900 * Get the flight ident, e.,g JBU1900
@@ -240,8 +233,7 @@ class Pirep extends Model
*/ */
public function getReadOnlyAttribute(): bool public function getReadOnlyAttribute(): bool
{ {
return $this->state !== PirepState::PENDING return \in_array($this->state, static::$read_only_states, true);
&& $this->state != PirepState::IN_PROGRESS;
} }
/** /**
@@ -375,11 +367,7 @@ class Pirep extends Model
*/ */
public function allowedUpdates(): bool public function allowedUpdates(): bool
{ {
if ($this->state === PirepState::CANCELLED) { return ! $this->getReadOnlyAttribute();
return false;
}
return true;
} }
/** /**

View File

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

View File

@@ -172,19 +172,6 @@ class PirepService extends Service
$field_values = []; $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 # Check the block times. If a block on (arrival) time isn't
# specified, then use the time that it was submitted. It won't # specified, then use the time that it was submitted. It won't
# be the most accurate, but that might be OK # be the most accurate, but that might be OK
@@ -214,6 +201,28 @@ class PirepService extends Service
$this->updateCustomFields($pirep->id, $field_values); $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]); Log::info('New PIREP filed', [$pirep]);
event(new PirepFiled($pirep)); event(new PirepFiled($pirep));
@@ -224,15 +233,13 @@ class PirepService extends Service
} }
# 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;
$pirep->user->state = UserState::ACTIVE; $pirep->user->state = UserState::ACTIVE;
$pirep->user->save(); $pirep->user->save();
event(new UserStateChanged($pirep->user, $old_state)); event(new UserStateChanged($pirep->user, $old_state));
} }
return $pirep;
} }
/** /**

View File

@@ -62,7 +62,7 @@ return [
], ],
'state' => [ 'state' => [
'accepted' => 'Accepted', 'accepted' => 'Accepted',
'pending' => 'Pending', 'pending' => 'Pending Approval',
'rejected' => 'Rejected', 'rejected' => 'Rejected',
'in_progress' => 'In Progress', 'in_progress' => 'In Progress',
'cancelled' => 'Cancelled', 'cancelled' => 'Cancelled',

View File

@@ -308,8 +308,31 @@ flight reports that have been filed. You've been warned!
<div class="col-sm-12"> <div class="col-sm-12">
<div class="float-right"> <div class="float-right">
<div class="form-group"> <div class="form-group">
{{--{{ Form::button('Save Draft', ['class' => 'btn btn-primary']) }}--}}
{{ Form::submit('Save PIREP', ['class' => 'btn btn-info']) }} @if(isset($pirep) && !$pirep->read_only)
{{ Form::button('Delete PIREP', [
'name' => 'submit',
'value' => 'cancel',
'class' => 'btn btn-warning',
'type' => 'submit'])
}}
@endif
@if(!isset($pirep) || (filled($pirep) && !$pirep->read_only))
{{ Form::button('Save PIREP', [
'name' => 'submit',
'value' => 'save',
'class' => 'btn btn-info',
'type' => 'submit'])
}}
{{ Form::button('Submit PIREP', [
'name' => 'submit',
'value' => 'submit',
'class' => 'btn btn-success',
'type' => 'submit'])
}}
@endif
</div> </div>
</div> </div>
</div> </div>

View File

@@ -2,13 +2,12 @@
@section('title', 'PIREP '.$pirep->ident) @section('title', 'PIREP '.$pirep->ident)
@section('content') @section('content')
<div class="row"> <div class="row">
<div class="col-8"> <div class="col-8">
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
<p> <p>
<h2 style="margin-bottom: 5px;">{{$pirep->airline->code}}{{ $pirep->ident }}</h2> <h2 style="margin-bottom: 5px;">{{$pirep->airline->code}}{{ $pirep->ident }}</h2>
<p> <p>
@if($pirep->state === PirepState::IN_PROGRESS) @if($pirep->state === PirepState::IN_PROGRESS)
@@ -17,7 +16,6 @@
@endif @endif
</p> </p>
</p> </p>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
@@ -62,9 +60,6 @@
<div class="progress-bar progress-bar-success" role="progressbar" <div class="progress-bar progress-bar-success" role="progressbar"
aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100"
style="width: {{$pirep->progress_percent}}%;"> style="width: {{$pirep->progress_percent}}%;">
{{--<p style="padding: 10px">
{{ Utils::minutesToTimeString($pirep->flight_time) }}
</p>--}}
</div> </div>
</div> </div>
</div> </div>
@@ -86,26 +81,26 @@
<div class="col-4"> <div class="col-4">
<h2>&nbsp;</h2> <h2>&nbsp;</h2>
<table class="table table-striped">
{{--<tr>
<td width="30%">Status</td>
<td>
@php
if($pirep->state === PirepState::PENDING)
$badge = 'warning';
elseif ($pirep->state === PirepState::ACCEPTED)
$badge = 'success';
elseif ($pirep->state === PirepState::REJECTED)
$badge = 'danger';
else
$badge = 'info';
@endphp
<div class="badge badge-{{$badge}}">
{{ PirepState::label($pirep->state) }}
</div>
</td>
</tr>--}}
{{-- Show the link to edit if it can be edited --}}
@if(!$pirep->read_only)
<div class="float-right" style="margin-bottom: 10px;">
<form method="get"
action="{{ route('frontend.pireps.edit', ['id' => $pirep->id]) }}"
style="display: inline">
@csrf
<button class="btn btn-info">Edit</button>
</form>
&nbsp;
<form method="post"
action="{{ route('frontend.pireps.submit', ['id' => $pirep->id]) }}"
style="display: inline">
@csrf
<button class="btn btn-success">Submit</button>
</form>
</div>
@endif
<table class="table table-striped">
<tr> <tr>
<td width="30%">State</td> <td width="30%">State</td>
<td> <td>
@@ -119,20 +114,6 @@
<td>Source</td> <td>Source</td>
<td>{{ PirepSource::label($pirep->source) }}</td> <td>{{ PirepSource::label($pirep->source) }}</td>
</tr> </tr>
{{--<tr>--}}
{{--<td>Departure/Arrival</td>--}}
{{--<td>--}}
{{--{{ $pirep->dpt_airport->name }}--}}
{{--(<a href="{{route('frontend.airports.show', [--}}
{{--'id' => $pirep->dpt_airport->icao--}}
{{--])}}">{{$pirep->dpt_airport->icao}}</a>)--}}
{{--<span class="description">to</span>--}}
{{--{{ $pirep->arr_airport->name }}--}}
{{--(<a href="{{route('frontend.airports.show', [--}}
{{--'id' => $pirep->arr_airport->icao--}}
{{--])}}">{{$pirep->arr_airport->icao}}</a>)--}}
{{--</td>--}}
{{--</tr>--}}
<tr> <tr>
<td>Flight Type</td> <td>Flight Type</td>

View File

@@ -231,6 +231,7 @@ class PIREPTest extends TestCase
# Pilot should be at rank 2, where accept should be automatic # Pilot should be at rank 2, where accept should be automatic
$this->pirepSvc->create($pirep); $this->pirepSvc->create($pirep);
$this->pirepSvc->submit($pirep);
$pilot->refresh(); $pilot->refresh();
$latest_pirep = Pirep::where('id', $pilot->last_pirep_id)->first(); $latest_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();