Check whether a duplicate PIREP has been submitted
This commit is contained in:
parent
6b63214154
commit
9c319e73f9
@ -17,6 +17,7 @@ class CreateSettingsTable extends Migration
|
||||
$table->unsignedInteger('order')->default(99);
|
||||
$table->string('name');
|
||||
$table->string('value');
|
||||
$table->string('default')->nullable();
|
||||
$table->string('group')->nullable();
|
||||
$table->string('type')->nullable();
|
||||
$table->string('options')->nullable();
|
||||
@ -98,7 +99,8 @@ class CreateSettingsTable extends Migration
|
||||
'order' => $this->getNextOrderNumber('pireps'),
|
||||
'name' => 'PIREP duplicate time check',
|
||||
'group' => 'pireps',
|
||||
'value' => 4,
|
||||
'value' => 10,
|
||||
'default' => 10,
|
||||
'type' => 'int',
|
||||
'description' => 'The time in minutes to check for a duplicate PIREP',
|
||||
],
|
||||
@ -108,6 +110,7 @@ class CreateSettingsTable extends Migration
|
||||
'name' => 'Pilot ID Length',
|
||||
'group' => 'pilots',
|
||||
'value' => 4,
|
||||
'default' => 4,
|
||||
'type' => 'int',
|
||||
'description' => 'The length of a pilot\'s ID',
|
||||
],
|
||||
@ -126,6 +129,7 @@ class CreateSettingsTable extends Migration
|
||||
'name' => 'Pilot to ON LEAVE days',
|
||||
'group' => 'pilots',
|
||||
'value' => 30,
|
||||
'default' => 30,
|
||||
'type' => 'int',
|
||||
'description' => 'Automatically set a pilot to ON LEAVE status after N days of no activity',
|
||||
],
|
||||
|
@ -7,6 +7,8 @@ use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
use App\Models\Acars;
|
||||
use App\Models\Pirep;
|
||||
|
||||
use App\Models\Enums\AcarsType;
|
||||
use App\Models\Enums\PirepState;
|
||||
use App\Models\Enums\PirepStatus;
|
||||
@ -85,13 +87,18 @@ class PirepController extends AppBaseController
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$pirep = $this->pirepRepo->create($attrs);
|
||||
$this->pirepSvc->saveRoute($pirep);
|
||||
} catch(\Exception $e) {
|
||||
Log::error($e);
|
||||
$pirep = new Pirep($attrs);
|
||||
|
||||
# Find if there's a duplicate, if so, let's work on that
|
||||
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
||||
if($dupe_pirep !== false) {
|
||||
$pirep = $dupe_pirep;
|
||||
}
|
||||
|
||||
$pirep->save();
|
||||
|
||||
$this->pirepSvc->saveRoute($pirep);
|
||||
|
||||
Log::info('PIREP PREFILED');
|
||||
Log::info($pirep->id);
|
||||
|
||||
|
@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Repositories\AcarsRepository;
|
||||
use Log;
|
||||
use Carbon\Carbon;
|
||||
use App\Repositories\AcarsRepository;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
use App\Models\Acars;
|
||||
use App\Models\Navdata;
|
||||
@ -53,6 +55,46 @@ class PIREPService extends BaseService
|
||||
$this->pirepRepo = $pirepRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find if there are duplicates to a given PIREP. Ideally, the passed
|
||||
* in PIREP hasn't been saved or gone through the create() method
|
||||
* @param Pirep $pirep
|
||||
* @return bool|Pirep
|
||||
*/
|
||||
public function findDuplicate(Pirep $pirep)
|
||||
{
|
||||
$minutes = setting('pireps.duplicate_check_time', 10);
|
||||
$time_limit = Carbon::now()->subMinutes($minutes)->toDateTimeString();
|
||||
|
||||
$where = [
|
||||
'user_id' => $pirep->user_id,
|
||||
'airline_id' => $pirep->airline_id,
|
||||
'flight_number' => $pirep->flight_number,
|
||||
];
|
||||
|
||||
if(!empty($pirep->route_code)) {
|
||||
$where['route_code'] = $pirep->route_code;
|
||||
}
|
||||
|
||||
if(!empty($pirep->route_leg)) {
|
||||
$where['route_leg'] = $pirep->route_leg;
|
||||
}
|
||||
|
||||
try {
|
||||
$found_pireps = Pirep::where($where)
|
||||
->where('created_at', '>=', $time_limit)
|
||||
->get();
|
||||
|
||||
if($found_pireps->count() === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $found_pireps[0];
|
||||
} catch (ModelNotFoundException $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the route into the ACARS table with AcarsType::ROUTE
|
||||
* @param Pirep $pirep
|
||||
|
@ -4,14 +4,14 @@
|
||||
* Shortcut for retrieving a setting value
|
||||
*/
|
||||
if (!function_exists('setting')) {
|
||||
function setting($key, $value = null)
|
||||
function setting($key, $default = null)
|
||||
{
|
||||
$settingRepo = app('setting'); // defined in AppServiceProvider
|
||||
if($value !== null) {
|
||||
/*if($value !== null) {
|
||||
return $settingRepo->store($key, $value);
|
||||
}
|
||||
}*/
|
||||
|
||||
return $settingRepo->retrieve($key);
|
||||
return $settingRepo->retrieve($key) ?: $default;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
use App\Models\Acars;
|
||||
use App\Models\Enums\AcarsType;
|
||||
use App\Models\Navdata;
|
||||
@ -163,4 +165,34 @@ class PIREPTest extends TestCase
|
||||
# Make sure latest PIREP was updated
|
||||
$this->assertNotEquals($last_pirep->id, $latest_pirep->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and check for any duplicate PIREPs by a user
|
||||
*/
|
||||
public function testDuplicatePireps()
|
||||
{
|
||||
$pirep = factory(Pirep::class)->create([
|
||||
'created_at' => Carbon::now()->toDateTimeString(),
|
||||
'updated_at' => Carbon::now()->toDateTimeString()
|
||||
]);
|
||||
|
||||
# This should find itself...
|
||||
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
||||
$this->assertNotFalse($dupe_pirep);
|
||||
$this->assertEquals($pirep->id, $dupe_pirep->id);
|
||||
|
||||
/**
|
||||
* Create a PIREP outside of the check time interval
|
||||
*/
|
||||
|
||||
$minutes = setting('pireps.duplicate_check_time') + 1;
|
||||
$pirep = factory(Pirep::class)->create([
|
||||
'created_at' => Carbon::now()->subMinutes($minutes)->toDateTimeString(),
|
||||
'updated_at' => Carbon::now()->subMinutes($minutes)->toDateTimeString()
|
||||
]);
|
||||
|
||||
# This should find itself...
|
||||
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
||||
$this->assertFalse($dupe_pirep);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user