diff --git a/.htaccess b/.htaccess index d8b312ce..b0112ed5 100755 --- a/.htaccess +++ b/.htaccess @@ -20,6 +20,7 @@ RedirectMatch 403 ^/storage/.*?$ RedirectMatch 403 ^/tests/.*?$ RedirectMatch 403 ^/vendor/.*?$ RedirectMatch 403 ^/.bowerrc$ +RedirectMatch 403 ^/.env RedirectMatch 403 ^/artisan$ RedirectMatch 403 ^/composer.json RedirectMatch 403 ^/composer.lock @@ -32,6 +33,7 @@ RedirectMatch 403 ^/package.json RedirectMatch 403 ^/package-lock.json RedirectMatch 403 ^/phpunit.xml RedirectMatch 403 ^/webpack.mix.js +RedirectMatch 403 ^/yarn.lock # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d diff --git a/app/Console/Commands/AcarsReplay.php b/app/Console/Commands/AcarsReplay.php index e52e2f27..0fef140f 100644 --- a/app/Console/Commands/AcarsReplay.php +++ b/app/Console/Commands/AcarsReplay.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; use App\Facades\Utils; use GuzzleHttp\Client; use Illuminate\Database\Eloquent\Collection; diff --git a/app/Console/Commands/ComposerCommand.php b/app/Console/Commands/ComposerCommand.php index 167d9959..cbebb7ba 100644 --- a/app/Console/Commands/ComposerCommand.php +++ b/app/Console/Commands/ComposerCommand.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; use Illuminate\Support\Facades\Artisan; class ComposerCommand extends Command diff --git a/app/Console/Commands/CreateDatabase.php b/app/Console/Commands/CreateDatabase.php index 6f269ff4..03402c82 100644 --- a/app/Console/Commands/CreateDatabase.php +++ b/app/Console/Commands/CreateDatabase.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; use Illuminate\Support\Facades\Log; use Tivie\OS\Detector; diff --git a/app/Console/Commands/DevCommands.php b/app/Console/Commands/DevCommands.php index 6ac20857..836f2324 100644 --- a/app/Console/Commands/DevCommands.php +++ b/app/Console/Commands/DevCommands.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; use App\Models\Acars; use App\Models\Airline; use App\Models\Pirep; diff --git a/app/Console/Commands/DevInstall.php b/app/Console/Commands/DevInstall.php index 489c7799..5856899c 100644 --- a/app/Console/Commands/DevInstall.php +++ b/app/Console/Commands/DevInstall.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; use Modules\Installer\Services\ConfigService; /** diff --git a/app/Console/Commands/ImportCsv.php b/app/Console/Commands/ImportCsv.php index 10cdbe60..feb18501 100644 --- a/app/Console/Commands/ImportCsv.php +++ b/app/Console/Commands/ImportCsv.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; use App\Services\ImportService; class ImportCsv extends Command diff --git a/app/Console/Commands/ImportFromClassic.php b/app/Console/Commands/ImportFromClassic.php index 35183f78..fbad3f50 100644 --- a/app/Console/Commands/ImportFromClassic.php +++ b/app/Console/Commands/ImportFromClassic.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; class ImportFromClassic extends Command { diff --git a/app/Console/Commands/NavdataImport.php b/app/Console/Commands/NavdataImport.php index dae59169..58e54220 100644 --- a/app/Console/Commands/NavdataImport.php +++ b/app/Console/Commands/NavdataImport.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; use App\Models\Enums\NavaidType; use App\Models\Navdata; diff --git a/app/Console/Commands/TestApi.php b/app/Console/Commands/TestApi.php index 66d841ca..9f80d81a 100644 --- a/app/Console/Commands/TestApi.php +++ b/app/Console/Commands/TestApi.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; use GuzzleHttp\Client; class TestApi extends Command diff --git a/app/Console/Commands/Version.php b/app/Console/Commands/Version.php index 89b7066e..623c5426 100644 --- a/app/Console/Commands/Version.php +++ b/app/Console/Commands/Version.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; use App\Services\VersionService; use Symfony\Component\Yaml\Yaml; diff --git a/app/Console/Commands/YamlExport.php b/app/Console/Commands/YamlExport.php index 0182bf09..f15acb5b 100644 --- a/app/Console/Commands/YamlExport.php +++ b/app/Console/Commands/YamlExport.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; use DB; use Symfony\Component\Yaml\Yaml; diff --git a/app/Console/Commands/YamlImport.php b/app/Console/Commands/YamlImport.php index 552edb5d..13ac1751 100644 --- a/app/Console/Commands/YamlImport.php +++ b/app/Console/Commands/YamlImport.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Console\Command; +use App\Contracts\Command; use App\Services\DatabaseService; /** diff --git a/app/Console/Cron/Hourly.php b/app/Console/Cron/Hourly.php index f1f5bc45..f0c894c2 100644 --- a/app/Console/Cron/Hourly.php +++ b/app/Console/Cron/Hourly.php @@ -2,7 +2,7 @@ namespace App\Console\Cron; -use App\Console\Command; +use App\Contracts\Command; use App\Events\CronHourly; /** diff --git a/app/Console/Cron/Monthly.php b/app/Console/Cron/Monthly.php index ddf2fa81..33b21768 100644 --- a/app/Console/Cron/Monthly.php +++ b/app/Console/Cron/Monthly.php @@ -2,7 +2,7 @@ namespace App\Console\Cron; -use App\Console\Command; +use App\Contracts\Command; use App\Events\CronMonthly; /** diff --git a/app/Console/Cron/Nightly.php b/app/Console/Cron/Nightly.php index c6e88161..368949f5 100644 --- a/app/Console/Cron/Nightly.php +++ b/app/Console/Cron/Nightly.php @@ -2,7 +2,7 @@ namespace App\Console\Cron; -use App\Console\Command; +use App\Contracts\Command; use App\Events\CronNightly; /** diff --git a/app/Console/Cron/Weekly.php b/app/Console/Cron/Weekly.php index e3a998a9..3ae11ca8 100644 --- a/app/Console/Cron/Weekly.php +++ b/app/Console/Cron/Weekly.php @@ -2,7 +2,7 @@ namespace App\Console\Cron; -use App\Console\Command; +use App\Contracts\Command; use App\Events\CronWeekly; /** diff --git a/app/Console/Command.php b/app/Contracts/Command.php similarity index 96% rename from app/Console/Command.php rename to app/Contracts/Command.php index 4b7c101a..ec3a9c8f 100644 --- a/app/Console/Command.php +++ b/app/Contracts/Command.php @@ -1,6 +1,6 @@ unsignedSmallInteger('state')->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + } +} diff --git a/app/Exceptions/PirepCancelNotAllowed.php b/app/Exceptions/PirepCancelNotAllowed.php new file mode 100644 index 00000000..a84728ca --- /dev/null +++ b/app/Exceptions/PirepCancelNotAllowed.php @@ -0,0 +1,40 @@ +pirep = $pirep; + parent::__construct( + 400, + 'This PIREP can\'t be cancelled' + ); + } + + /** + * Return the RFC 7807 error type (without the URL root) + */ + public function getErrorType(): string + { + return 'pirep-cancel-not-allowed'; + } + + public function getErrorDetails(): string + { + return $this->getMessage(); + } + + public function getErrorMetadata(): array + { + return [ + 'pirep_id' => $this->pirep->id, + 'state' => $this->pirep->state, + ]; + } +} diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php index 4f39c8b1..445b1f1c 100644 --- a/app/Http/Controllers/Api/PirepController.php +++ b/app/Http/Controllers/Api/PirepController.php @@ -363,10 +363,8 @@ class PirepController extends Controller { Log::info('PIREP Cancel, user '.Auth::id(), $request->post()); - $pirep = $this->pirepRepo->update([ - 'state' => PirepState::CANCELLED, - 'status' => PirepStatus::CANCELLED, - ], $pirep_id); + $pirep = Pirep::find($pirep_id); + $this->pirepSvc->cancel($pirep); return new PirepResource($pirep); } diff --git a/app/Http/Routes/api.php b/app/Http/Routes/api.php index adf6ee2b..6da61a84 100755 --- a/app/Http/Routes/api.php +++ b/app/Http/Routes/api.php @@ -46,6 +46,7 @@ Route::group(['middleware' => ['api.auth']], function () { Route::post('pireps/{pirep_id}/update', 'PirepController@update'); Route::post('pireps/{pirep_id}/file', 'PirepController@file'); Route::post('pireps/{pirep_id}/comments', 'PirepController@comments_post'); + Route::put('pireps/{pirep_id}/cancel', 'PirepController@cancel'); Route::delete('pireps/{pirep_id}/cancel', 'PirepController@cancel'); Route::get('pireps/{pirep_id}/fields', 'PirepController@fields_get'); diff --git a/app/Listeners/SetUserActive.php b/app/Listeners/SetUserActive.php new file mode 100644 index 00000000..2a484bf9 --- /dev/null +++ b/app/Listeners/SetUserActive.php @@ -0,0 +1,23 @@ +pirep->user->state !== UserState::ACTIVE) { + $old_state = $event->pirep->user->state; + $event->pirep->user->state = UserState::ACTIVE; + $event->pirep->user->save(); + + event(new UserStateChanged($event->pirep->user, $old_state)); + } + } +} diff --git a/app/Models/Aircraft.php b/app/Models/Aircraft.php index c2d75f4d..d8bfd8cf 100644 --- a/app/Models/Aircraft.php +++ b/app/Models/Aircraft.php @@ -10,14 +10,17 @@ use App\Models\Traits\FilesTrait; /** * @property int id * @property mixed subfleet_id + * @property string airport_id The apt where the aircraft is * @property string name * @property string icao * @property string registration + * @property int flight_time * @property string hex_code * @property Airport airport * @property Subfleet subfleet * @property int status * @property int state + * @property Carbon landing_time */ class Aircraft extends Model { diff --git a/app/Models/Enums/AcarsType.php b/app/Models/Enums/AcarsType.php index 980e57c2..13b37d29 100644 --- a/app/Models/Enums/AcarsType.php +++ b/app/Models/Enums/AcarsType.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class AcarsType - */ class AcarsType extends Enum { public const FLIGHT_PATH = 0; diff --git a/app/Models/Enums/ActiveState.php b/app/Models/Enums/ActiveState.php index 031ee2a2..a8f323e2 100644 --- a/app/Models/Enums/ActiveState.php +++ b/app/Models/Enums/ActiveState.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class ActiveState - */ class ActiveState extends Enum { public const INACTIVE = 0; diff --git a/app/Models/Enums/AircraftState.php b/app/Models/Enums/AircraftState.php index 2179a5ed..9020a33d 100644 --- a/app/Models/Enums/AircraftState.php +++ b/app/Models/Enums/AircraftState.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class AircraftState - */ class AircraftState extends Enum { public const PARKED = 0; diff --git a/app/Models/Enums/AircraftStatus.php b/app/Models/Enums/AircraftStatus.php index 9c60765c..60eec997 100644 --- a/app/Models/Enums/AircraftStatus.php +++ b/app/Models/Enums/AircraftStatus.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class AircraftState - */ class AircraftStatus extends Enum { public const ACTIVE = 'A'; diff --git a/app/Models/Enums/AnalyticsDimensions.php b/app/Models/Enums/AnalyticsDimensions.php index 506fc06f..6966d790 100644 --- a/app/Models/Enums/AnalyticsDimensions.php +++ b/app/Models/Enums/AnalyticsDimensions.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class AnalyticsDimensions - */ class AnalyticsDimensions extends Enum { public const PHP_VERSION = 1; diff --git a/app/Models/Enums/AnalyticsMetrics.php b/app/Models/Enums/AnalyticsMetrics.php index 550f2e6c..d212c5db 100644 --- a/app/Models/Enums/AnalyticsMetrics.php +++ b/app/Models/Enums/AnalyticsMetrics.php @@ -5,7 +5,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; /** - * Class AnalyticsMetrics * Metrics IDs used in Google Analytics */ class AnalyticsMetrics extends Enum diff --git a/app/Models/Enums/Days.php b/app/Models/Enums/Days.php index e0ff5986..04013a1c 100644 --- a/app/Models/Enums/Days.php +++ b/app/Models/Enums/Days.php @@ -5,7 +5,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; /** - * Class Days * Start on Monday - ISO8601 */ class Days extends Enum diff --git a/app/Models/Enums/ExpenseType.php b/app/Models/Enums/ExpenseType.php index 5154885e..7d424540 100644 --- a/app/Models/Enums/ExpenseType.php +++ b/app/Models/Enums/ExpenseType.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class ExpenseType - */ class ExpenseType extends Enum { public const FLIGHT = 'F'; diff --git a/app/Models/Enums/FlightType.php b/app/Models/Enums/FlightType.php index c3254e34..aaaa2b87 100644 --- a/app/Models/Enums/FlightType.php +++ b/app/Models/Enums/FlightType.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class FlightType - */ class FlightType extends Enum { public const SCHED_PAX = 'J'; diff --git a/app/Models/Enums/FuelType.php b/app/Models/Enums/FuelType.php index d5d1c3ae..afd52ef8 100644 --- a/app/Models/Enums/FuelType.php +++ b/app/Models/Enums/FuelType.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class FuelType - */ class FuelType extends Enum { public const LOW_LEAD = 0; diff --git a/app/Models/Enums/JournalType.php b/app/Models/Enums/JournalType.php index 6321cb49..ff46c8d4 100644 --- a/app/Models/Enums/JournalType.php +++ b/app/Models/Enums/JournalType.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class JournalType - */ class JournalType extends Enum { public const AIRLINE = 0; diff --git a/app/Models/Enums/PirepFieldSource.php b/app/Models/Enums/PirepFieldSource.php index 7270fd97..fc09f14a 100644 --- a/app/Models/Enums/PirepFieldSource.php +++ b/app/Models/Enums/PirepFieldSource.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class AcarsType - */ class PirepFieldSource extends Enum { public const MANUAL = 0; diff --git a/app/Models/Enums/PirepSource.php b/app/Models/Enums/PirepSource.php index 9b837ba6..bbcd003e 100644 --- a/app/Models/Enums/PirepSource.php +++ b/app/Models/Enums/PirepSource.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class PirepSource - */ class PirepSource extends Enum { public const MANUAL = 0; diff --git a/app/Models/Enums/PirepState.php b/app/Models/Enums/PirepState.php index 50f7ec05..0d35d7b3 100644 --- a/app/Models/Enums/PirepState.php +++ b/app/Models/Enums/PirepState.php @@ -9,21 +9,21 @@ use App\Contracts\Enum; */ class PirepState extends Enum { - public const REJECTED = -1; public const IN_PROGRESS = 0; // flight is ongoing public const PENDING = 1; // waiting admin approval public const ACCEPTED = 2; public const CANCELLED = 3; public const DELETED = 4; public const DRAFT = 5; + public const REJECTED = 6; protected static $labels = [ - self::REJECTED => 'pireps.state.rejected', self::IN_PROGRESS => 'pireps.state.in_progress', self::PENDING => 'pireps.state.pending', self::ACCEPTED => 'pireps.state.accepted', self::CANCELLED => 'pireps.state.cancelled', self::DELETED => 'pireps.state.deleted', self::DRAFT => 'pireps.state.draft', + self::REJECTED => 'pireps.state.rejected', ]; } diff --git a/app/Models/Enums/UserState.php b/app/Models/Enums/UserState.php index 516b4399..5f8ef478 100644 --- a/app/Models/Enums/UserState.php +++ b/app/Models/Enums/UserState.php @@ -4,9 +4,6 @@ namespace App\Models\Enums; use App\Contracts\Enum; -/** - * Class UserState - */ class UserState extends Enum { public const PENDING = 0; diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 305509af..f3de2467 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -42,6 +42,7 @@ use Illuminate\Support\Collection; * @property Collection fields * @property int status * @property bool state + * @property string source * @property Carbon submitted_at * @property Carbon created_at * @property Carbon updated_at @@ -130,7 +131,7 @@ class Pirep extends Model 'route' => 'nullable', ]; - /** + /* * If a PIREP is in these states, then it can't be changed. */ public static $read_only_states = [ @@ -139,6 +140,16 @@ class Pirep extends Model PirepState::CANCELLED, ]; + /* + * If a PIREP is in one of these states, it can't be cancelled + */ + public static $cancel_states = [ + PirepState::ACCEPTED, + PirepState::REJECTED, + PirepState::CANCELLED, + PirepState::DELETED, + ]; + /** * Get the flight ident, e.,g JBU1900 * diff --git a/app/Models/PirepFieldValue.php b/app/Models/PirepFieldValue.php index 76a3ebe1..cf885b22 100644 --- a/app/Models/PirepFieldValue.php +++ b/app/Models/PirepFieldValue.php @@ -11,6 +11,8 @@ use App\Models\Enums\PirepFieldSource; * @property string slug * @property string value * @property string source + * + * @method static updateOrCreate(array $array, array $array1) */ class PirepFieldValue extends Model { diff --git a/app/Models/Rank.php b/app/Models/Rank.php index cb434051..f49d271d 100644 --- a/app/Models/Rank.php +++ b/app/Models/Rank.php @@ -10,6 +10,8 @@ use App\Contracts\Model; * @property float manual_base_pay_rate * @property float acars_base_pay_rate * @property bool auto_promote + * @property bool auto_approve_acars + * @property bool auto_approve_manual */ class Rank extends Model { diff --git a/app/Models/User.php b/app/Models/User.php index 66f790ef..0aeef63b 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -32,6 +32,7 @@ use Laratrust\Traits\LaratrustUserTrait; * @property int rank_id * @property int state * @property bool opt_in + * @property string last_pirep_id * @mixin \Illuminate\Notifications\Notifiable * @mixin \Laratrust\Traits\LaratrustUserTrait */ diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 6e28cee2..f2758991 100755 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -3,12 +3,14 @@ namespace App\Providers; use App\Events\Expenses; +use App\Events\PirepFiled; use App\Events\UserStatsChanged; use App\Listeners\AwardListener; use App\Listeners\BidEvents; use App\Listeners\ExpenseListener; use App\Listeners\FinanceEvents; use App\Listeners\NotificationEvents; +use App\Listeners\SetUserActive; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -20,6 +22,10 @@ class EventServiceProvider extends ServiceProvider ExpenseListener::class, ], + PirepFiled::class => [ + SetUserActive::class, + ], + Registered::class => [ SendEmailVerificationNotification::class, ], diff --git a/app/Services/AirportService.php b/app/Services/AirportService.php index 156b83e0..2d644e4c 100644 --- a/app/Services/AirportService.php +++ b/app/Services/AirportService.php @@ -15,9 +15,6 @@ use League\Geotools\Geotools; use PhpUnitsOfMeasure\Exception\NonNumericValue; use PhpUnitsOfMeasure\Exception\NonStringUnitName; -/** - * Class AnalyticsService - */ class AirportService extends Service { private $airportRepo; diff --git a/app/Services/AnalyticsService.php b/app/Services/AnalyticsService.php index 05dd47ed..b25c6119 100644 --- a/app/Services/AnalyticsService.php +++ b/app/Services/AnalyticsService.php @@ -4,14 +4,11 @@ namespace App\Services; use App\Contracts\Service; use App\Models\Enums\AnalyticsDimensions; -use DB; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; use Irazasyed\LaravelGAMP\Facades\GAMP; -use Log; use PDO; -/** - * Class AnalyticsService - */ class AnalyticsService extends Service { /** diff --git a/app/Services/AwardService.php b/app/Services/AwardService.php index d42a1ad8..6fad9316 100644 --- a/app/Services/AwardService.php +++ b/app/Services/AwardService.php @@ -4,11 +4,8 @@ namespace App\Services; use App\Contracts\Service; use App\Support\ClassLoader; -use Module; +use Nwidart\Modules\Facades\Module; -/** - * Class AwardService - */ class AwardService extends Service { /** diff --git a/app/Services/DatabaseService.php b/app/Services/DatabaseService.php index 0168e06e..c0fdc626 100644 --- a/app/Services/DatabaseService.php +++ b/app/Services/DatabaseService.php @@ -7,9 +7,6 @@ use App\Support\Database; use Carbon\Carbon; use Webpatser\Uuid\Uuid; -/** - * Class DatabaseService - */ class DatabaseService extends Service { protected $time_fields = [ diff --git a/app/Services/ExportService.php b/app/Services/ExportService.php index 87a5366d..e41b429d 100644 --- a/app/Services/ExportService.php +++ b/app/Services/ExportService.php @@ -11,14 +11,11 @@ use App\Services\ImportExport\FareExporter; use App\Services\ImportExport\FlightExporter; use App\Services\ImportExport\SubfleetExporter; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use League\Csv\CharsetConverter; use League\Csv\Writer; -use Log; -/** - * Class ExportService - */ class ExportService extends Service { /** diff --git a/app/Services/FareService.php b/app/Services/FareService.php index 12a29919..00bbade7 100644 --- a/app/Services/FareService.php +++ b/app/Services/FareService.php @@ -11,9 +11,6 @@ use App\Models\Subfleet; use App\Support\Math; use Illuminate\Support\Collection; -/** - * Class FareService - */ class FareService extends Service { /** diff --git a/app/Services/FlightService.php b/app/Services/FlightService.php index 3b058771..5ca53650 100644 --- a/app/Services/FlightService.php +++ b/app/Services/FlightService.php @@ -15,9 +15,6 @@ use App\Repositories\NavdataRepository; use App\Support\Units\Time; use Illuminate\Support\Facades\Log; -/** - * Class FlightService - */ class FlightService extends Service { private $airportSvc; diff --git a/app/Services/GeoService.php b/app/Services/GeoService.php index ce555fbc..41a43f95 100644 --- a/app/Services/GeoService.php +++ b/app/Services/GeoService.php @@ -16,9 +16,6 @@ use Illuminate\Support\Facades\Log; use League\Geotools\Coordinate\Coordinate; use League\Geotools\Geotools; -/** - * Class GeoService - */ class GeoService extends Service { private $acarsRepo; diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index 956a94dd..4f68311b 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -19,9 +19,6 @@ use Illuminate\Validation\ValidationException; use League\Csv\Exception; use League\Csv\Reader; -/** - * Class ImportService - */ class ImportService extends Service { protected $flightRepo; diff --git a/app/Services/ModuleService.php b/app/Services/ModuleService.php index b1e9cf22..61f69f23 100644 --- a/app/Services/ModuleService.php +++ b/app/Services/ModuleService.php @@ -4,9 +4,6 @@ namespace App\Services; use App\Contracts\Service; -/** - * Class ModuleService - */ class ModuleService extends Service { protected static $adminLinks = []; diff --git a/app/Services/PirepService.php b/app/Services/PirepService.php index 87b30ff3..1a41b78c 100644 --- a/app/Services/PirepService.php +++ b/app/Services/PirepService.php @@ -6,20 +6,19 @@ use App\Contracts\Service; use App\Events\PirepAccepted; use App\Events\PirepFiled; use App\Events\PirepRejected; -use App\Events\UserStateChanged; use App\Events\UserStatsChanged; +use App\Exceptions\PirepCancelNotAllowed; use App\Models\Acars; -use App\Models\Aircraft; use App\Models\Bid; use App\Models\Enums\AcarsType; use App\Models\Enums\PirepSource; use App\Models\Enums\PirepState; use App\Models\Enums\PirepStatus; -use App\Models\Enums\UserState; use App\Models\Navdata; use App\Models\Pirep; use App\Models\PirepFieldValue; use App\Models\User; +use App\Repositories\PirepRepository; use Carbon\Carbon; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\Log; @@ -31,19 +30,23 @@ class PirepService extends Service { private $geoSvc; private $pilotSvc; + private $pirepRepo; /** * PirepService constructor. * - * @param GeoService $geoSvc - * @param UserService $pilotSvc + * @param GeoService $geoSvc + * @param PirepRepository $pirepRepo + * @param UserService $pilotSvc */ public function __construct( GeoService $geoSvc, + PirepRepository $pirepRepo, UserService $pilotSvc ) { $this->geoSvc = $geoSvc; $this->pilotSvc = $pilotSvc; + $this->pirepRepo = $pirepRepo; } /** @@ -207,11 +210,11 @@ class PirepService extends Service // 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) { + if ($pirep->user->rank->auto_approve_acars) { $default_state = PirepState::ACCEPTED; } } else { - if ($pirep->pilot->rank->auto_approve_manual) { + if ($pirep->user->rank->auto_approve_manual) { $default_state = PirepState::ACCEPTED; } } @@ -222,21 +225,36 @@ class PirepService extends Service // only update the pilot last state if they are accepted if ($default_state === PirepState::ACCEPTED) { $pirep = $this->accept($pirep); - $this->setPilotState($pirep->pilot, $pirep); } else { $pirep->state = $default_state; } $pirep->save(); + } - // Check the user state, set them to ACTIVE if on leave - if ($pirep->user->state !== UserState::ACTIVE) { - $old_state = $pirep->user->state; - $pirep->user->state = UserState::ACTIVE; - $pirep->user->save(); + /** + * Cancel a PIREP + * + * @param Pirep $pirep + * + * @throws \Prettus\Validator\Exceptions\ValidatorException + * + * @return Pirep + */ + public function cancel(Pirep $pirep): Pirep + { + if (in_array($pirep->state, Pirep::$cancel_states, true)) { + Log::info('PIREP '.$pirep->id.' can\'t be cancelled, state='.$pirep->state); - event(new UserStateChanged($pirep->user, $old_state)); + throw new PirepCancelNotAllowed($pirep); } + + $pirep = $this->pirepRepo->update([ + 'state' => PirepState::CANCELLED, + 'status' => PirepStatus::CANCELLED, + ], $pirep->id); + + return $pirep; } /** @@ -324,12 +342,12 @@ class PirepService extends Service } $ft = $pirep->flight_time; - $pilot = $pirep->pilot; + $pilot = $pirep->user; $this->pilotSvc->adjustFlightTime($pilot, $ft); $this->pilotSvc->adjustFlightCount($pilot, +1); $this->pilotSvc->calculatePilotRank($pilot); - $pirep->pilot->refresh(); + $pirep->user->refresh(); // Change the status $pirep->state = PirepState::ACCEPTED; @@ -362,13 +380,13 @@ class PirepService extends Service // If this was previously ACCEPTED, then reconcile the flight hours // that have already been counted, etc if ($pirep->state === PirepState::ACCEPTED) { - $pilot = $pirep->pilot; + $user = $pirep->user; $ft = $pirep->flight_time * -1; - $this->pilotSvc->adjustFlightTime($pilot, $ft); - $this->pilotSvc->adjustFlightCount($pilot, -1); - $this->pilotSvc->calculatePilotRank($pilot); - $pirep->pilot->refresh(); + $this->pilotSvc->adjustFlightTime($user, $ft); + $this->pilotSvc->adjustFlightCount($user, -1); + $this->pilotSvc->calculatePilotRank($user); + $pirep->user->refresh(); } // Change the status @@ -387,6 +405,7 @@ class PirepService extends Service } /** + * @param User $pilot * @param Pirep $pirep */ public function setPilotState(User $pilot, Pirep $pirep) diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 1e2dcc53..bc233a7a 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -21,9 +21,6 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; use function is_array; -/** - * Class UserService - */ class UserService extends Service { private $aircraftRepo; diff --git a/tests/AcarsTest.php b/tests/AcarsTest.php index f4640817..635ecdc3 100644 --- a/tests/AcarsTest.php +++ b/tests/AcarsTest.php @@ -2,6 +2,7 @@ use App\Models\Enums\PirepState; use App\Models\Enums\PirepStatus; +use App\Models\PirepFare; use App\Repositories\SettingRepository; /** @@ -217,7 +218,7 @@ class AcarsTest extends TestCase $pirep = $response->json('data'); // See that the fields and fares were set - $fares = \App\Models\PirepFare::where('pirep_id', $pirep['id'])->get(); + $fares = PirepFare::where('pirep_id', $pirep['id'])->get(); $this->assertCount(1, $fares); $saved_fare = $fares->first(); @@ -246,16 +247,30 @@ class AcarsTest extends TestCase ]; $response = $this->post($uri, $update); - $response->assertStatus(200); - $updated_pirep = $response->json('data'); + $response->assertOk(); // Make sure there are no duplicates - $fares = \App\Models\PirepFare::where('pirep_id', $pirep['id'])->get(); + $fares = PirepFare::where('pirep_id', $pirep['id'])->get(); $this->assertCount(1, $fares); $saved_fare = $fares->first(); $this->assertEquals($fare->id, $saved_fare['fare_id']); $this->assertEquals($fare->capacity, $saved_fare['count']); + + /* + * Try cancelling the PIREP now + */ + $uri = '/api/pireps/'.$pirep['id'].'/cancel'; + $response = $this->put($uri, []); + $response->assertOk(); + + // Read it + $uri = '/api/pireps/'.$pirep['id']; + $response = $this->get($uri); + $response->assertOk(); + $body = $response->json('data'); + + $this->assertEquals($body['state'], PirepState::CANCELLED); } /** @@ -460,11 +475,6 @@ class AcarsTest extends TestCase $body = $this->get('/api/pireps/'.$pirep_id)->json('data'); $this->assertNotNull($body['block_off_time']); $this->assertNotNull($body['block_on_time']); - - // make sure the time matches up - /*$block_on = new Carbon($body['block_on_time'], 'UTC'); - $block_off = new Carbon($body['block_off_time'], 'UTC'); - $this->assertEquals($block_on->subMinutes($body['flight_time']), $block_off);*/ } /** diff --git a/tests/ImporterTest.php b/tests/ImporterTest.php index 8c598b71..ac6a5b65 100644 --- a/tests/ImporterTest.php +++ b/tests/ImporterTest.php @@ -17,6 +17,7 @@ use App\Services\ImportExport\AircraftExporter; use App\Services\ImportExport\AirportExporter; use App\Services\ImportExport\FlightExporter; use App\Services\ImportService; +use Illuminate\Validation\ValidationException; /** * Class ImporterTest @@ -358,11 +359,10 @@ class ImporterTest extends TestCase /** * Try importing the aicraft in the airports. Should fail - * - * @expectedException \Illuminate\Validation\ValidationException */ public function testInvalidFileImport(): void { + $this->expectException(ValidationException::class); $file_path = base_path('tests/data/aircraft.csv'); $this->importSvc->importAirports($file_path); } diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index bdf90d1f..771a1054 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -95,6 +95,11 @@ class PIREPTest extends TestCase $this->get('/api/fleet/aircraft/'.$pirep->aircraft_id, [], $user) ->assertJson(['data' => ['airport_id' => $pirep->arr_airport_id]]); + // Try cancelling it + $uri = '/api/pireps/'.$pirep->id.'/cancel'; + $response = $this->put($uri, [], [], $user); + $response->assertStatus(400); + /** * Now go from ACCEPTED to REJECTED */ diff --git a/tests/UserTest.php b/tests/UserTest.php index 374f49f6..c25c67ec 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -1,5 +1,6 @@ expectException(UserPilotIdExists::class); $user1 = factory(App\Models\User::class)->create(['id' => 1]); $user2 = factory(App\Models\User::class)->create(['id' => 2]);