Check whether a duplicate PIREP has been submitted
This commit is contained in:
@@ -17,6 +17,7 @@ class CreateSettingsTable extends Migration
|
|||||||
$table->unsignedInteger('order')->default(99);
|
$table->unsignedInteger('order')->default(99);
|
||||||
$table->string('name');
|
$table->string('name');
|
||||||
$table->string('value');
|
$table->string('value');
|
||||||
|
$table->string('default')->nullable();
|
||||||
$table->string('group')->nullable();
|
$table->string('group')->nullable();
|
||||||
$table->string('type')->nullable();
|
$table->string('type')->nullable();
|
||||||
$table->string('options')->nullable();
|
$table->string('options')->nullable();
|
||||||
@@ -98,7 +99,8 @@ class CreateSettingsTable extends Migration
|
|||||||
'order' => $this->getNextOrderNumber('pireps'),
|
'order' => $this->getNextOrderNumber('pireps'),
|
||||||
'name' => 'PIREP duplicate time check',
|
'name' => 'PIREP duplicate time check',
|
||||||
'group' => 'pireps',
|
'group' => 'pireps',
|
||||||
'value' => 4,
|
'value' => 10,
|
||||||
|
'default' => 10,
|
||||||
'type' => 'int',
|
'type' => 'int',
|
||||||
'description' => 'The time in minutes to check for a duplicate PIREP',
|
'description' => 'The time in minutes to check for a duplicate PIREP',
|
||||||
],
|
],
|
||||||
@@ -108,6 +110,7 @@ class CreateSettingsTable extends Migration
|
|||||||
'name' => 'Pilot ID Length',
|
'name' => 'Pilot ID Length',
|
||||||
'group' => 'pilots',
|
'group' => 'pilots',
|
||||||
'value' => 4,
|
'value' => 4,
|
||||||
|
'default' => 4,
|
||||||
'type' => 'int',
|
'type' => 'int',
|
||||||
'description' => 'The length of a pilot\'s ID',
|
'description' => 'The length of a pilot\'s ID',
|
||||||
],
|
],
|
||||||
@@ -126,6 +129,7 @@ class CreateSettingsTable extends Migration
|
|||||||
'name' => 'Pilot to ON LEAVE days',
|
'name' => 'Pilot to ON LEAVE days',
|
||||||
'group' => 'pilots',
|
'group' => 'pilots',
|
||||||
'value' => 30,
|
'value' => 30,
|
||||||
|
'default' => 30,
|
||||||
'type' => 'int',
|
'type' => 'int',
|
||||||
'description' => 'Automatically set a pilot to ON LEAVE status after N days of no activity',
|
'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 Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
use App\Models\Acars;
|
use App\Models\Acars;
|
||||||
|
use App\Models\Pirep;
|
||||||
|
|
||||||
use App\Models\Enums\AcarsType;
|
use App\Models\Enums\AcarsType;
|
||||||
use App\Models\Enums\PirepState;
|
use App\Models\Enums\PirepState;
|
||||||
use App\Models\Enums\PirepStatus;
|
use App\Models\Enums\PirepStatus;
|
||||||
@@ -85,13 +87,18 @@ class PirepController extends AppBaseController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
$pirep = new Pirep($attrs);
|
||||||
$pirep = $this->pirepRepo->create($attrs);
|
|
||||||
$this->pirepSvc->saveRoute($pirep);
|
# Find if there's a duplicate, if so, let's work on that
|
||||||
} catch(\Exception $e) {
|
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
||||||
Log::error($e);
|
if($dupe_pirep !== false) {
|
||||||
|
$pirep = $dupe_pirep;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$pirep->save();
|
||||||
|
|
||||||
|
$this->pirepSvc->saveRoute($pirep);
|
||||||
|
|
||||||
Log::info('PIREP PREFILED');
|
Log::info('PIREP PREFILED');
|
||||||
Log::info($pirep->id);
|
Log::info($pirep->id);
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use App\Repositories\AcarsRepository;
|
|
||||||
use Log;
|
use Log;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use App\Repositories\AcarsRepository;
|
||||||
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
|
|
||||||
use App\Models\Acars;
|
use App\Models\Acars;
|
||||||
use App\Models\Navdata;
|
use App\Models\Navdata;
|
||||||
@@ -53,6 +55,46 @@ class PIREPService extends BaseService
|
|||||||
$this->pirepRepo = $pirepRepo;
|
$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
|
* Save the route into the ACARS table with AcarsType::ROUTE
|
||||||
* @param Pirep $pirep
|
* @param Pirep $pirep
|
||||||
|
|||||||
+4
-4
@@ -4,14 +4,14 @@
|
|||||||
* Shortcut for retrieving a setting value
|
* Shortcut for retrieving a setting value
|
||||||
*/
|
*/
|
||||||
if (!function_exists('setting')) {
|
if (!function_exists('setting')) {
|
||||||
function setting($key, $value = null)
|
function setting($key, $default = null)
|
||||||
{
|
{
|
||||||
$settingRepo = app('setting'); // defined in AppServiceProvider
|
$settingRepo = app('setting'); // defined in AppServiceProvider
|
||||||
if($value !== null) {
|
/*if($value !== null) {
|
||||||
return $settingRepo->store($key, $value);
|
return $settingRepo->store($key, $value);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
return $settingRepo->retrieve($key);
|
return $settingRepo->retrieve($key) ?: $default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
|
||||||
use App\Models\Acars;
|
use App\Models\Acars;
|
||||||
use App\Models\Enums\AcarsType;
|
use App\Models\Enums\AcarsType;
|
||||||
use App\Models\Navdata;
|
use App\Models\Navdata;
|
||||||
@@ -163,4 +165,34 @@ class PIREPTest extends TestCase
|
|||||||
# Make sure latest PIREP was updated
|
# Make sure latest PIREP was updated
|
||||||
$this->assertNotEquals($last_pirep->id, $latest_pirep->id);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user