From 9c319e73f968b695da321c9ea210a8107970e6ed Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Tue, 2 Jan 2018 13:17:22 -0600 Subject: [PATCH] Check whether a duplicate PIREP has been submitted --- ...017_06_07_014930_create_settings_table.php | 6 ++- app/Http/Controllers/Api/PirepController.php | 17 ++++--- app/Services/PIREPService.php | 44 ++++++++++++++++++- app/helpers.php | 8 ++-- tests/PIREPTest.php | 32 ++++++++++++++ 5 files changed, 96 insertions(+), 11 deletions(-) diff --git a/app/Database/migrations/2017_06_07_014930_create_settings_table.php b/app/Database/migrations/2017_06_07_014930_create_settings_table.php index bc84c578..cb2db5d0 100644 --- a/app/Database/migrations/2017_06_07_014930_create_settings_table.php +++ b/app/Database/migrations/2017_06_07_014930_create_settings_table.php @@ -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', ], diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php index dcfc9b22..19d77e2c 100644 --- a/app/Http/Controllers/Api/PirepController.php +++ b/app/Http/Controllers/Api/PirepController.php @@ -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); diff --git a/app/Services/PIREPService.php b/app/Services/PIREPService.php index 500cb4a6..89a9ea47 100644 --- a/app/Services/PIREPService.php +++ b/app/Services/PIREPService.php @@ -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 diff --git a/app/helpers.php b/app/helpers.php index 70ab3394..5726e0e8 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -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; } } diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index f06bb0e3..ad2c25dc 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -1,5 +1,7 @@ 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); + } }