diff --git a/app/Database/factories/AwardsFactory.php b/app/Database/factories/AwardsFactory.php new file mode 100644 index 00000000..48cbaba0 --- /dev/null +++ b/app/Database/factories/AwardsFactory.php @@ -0,0 +1,13 @@ +define(App\Models\Award::class, function (Faker $faker) { + return [ + 'id' => null, + 'name' => $faker->name, + 'description' => $faker->text(10), + 'ref_class' => null, + 'ref_class_params' => null, + ]; +}); diff --git a/app/Database/migrations/2018_01_28_180522_create_awards_table.php b/app/Database/migrations/2018_01_28_180522_create_awards_table.php index e8645762..6d43d6e3 100644 --- a/app/Database/migrations/2018_01_28_180522_create_awards_table.php +++ b/app/Database/migrations/2018_01_28_180522_create_awards_table.php @@ -29,6 +29,16 @@ class CreateAwardsTable extends Migration $table->index(['ref_class']); }); + + + Schema::create('user_awards', function (Blueprint $table) { + $table->increments('id'); + $table->unsignedInteger('user_id'); + $table->unsignedInteger('award_id'); + $table->timestamps(); + + $table->index(['user_id', 'award_id']); + }); } /** @@ -39,5 +49,6 @@ class CreateAwardsTable extends Migration public function down() { Schema::dropIfExists('awards'); + Schema::dropIfExists('user_awards'); } } diff --git a/app/Listeners/AwardListener.php b/app/Listeners/AwardListener.php index 5e0f73f4..d2cdee09 100644 --- a/app/Listeners/AwardListener.php +++ b/app/Listeners/AwardListener.php @@ -2,7 +2,7 @@ namespace App\Listeners; -use App\Events\UserStateChanged; +use App\Events\UserStatsChanged; use App\Models\Award; /** @@ -13,9 +13,9 @@ class AwardListener { /** * Call all of the awards - * @param UserStateChanged $event + * @param UserStatsChanged $event */ - public function handle(UserStateChanged $event) + public function handle(UserStatsChanged $event) { $awards = Award::all(); foreach($awards as $award) { diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 6273d505..7496b143 100755 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -6,7 +6,7 @@ use App\Events\CronMonthly; use App\Events\CronNightly; use App\Events\CronWeekly; use App\Events\Expenses; -use App\Events\UserStateChanged; +use App\Events\UserStatsChanged; use App\Listeners\AwardListener; use App\Listeners\Cron\Nightly\RecalculateBalances; use App\Listeners\ExpenseListener; @@ -17,11 +17,6 @@ use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvi class EventServiceProvider extends ServiceProvider { - /** - * The event listener mappings for the application. - * - * @var array - */ protected $listen = [ Expenses::class => [ @@ -41,7 +36,7 @@ class EventServiceProvider extends ServiceProvider \App\Listeners\Cron\Monthly\ApplyExpenses::class ], - UserStateChanged::class => [ + UserStatsChanged::class => [ AwardListener::class, ], ]; diff --git a/app/Services/PirepService.php b/app/Services/PirepService.php index 4bf7f3c8..93db1f68 100644 --- a/app/Services/PirepService.php +++ b/app/Services/PirepService.php @@ -285,8 +285,6 @@ class PirepService extends BaseService $pirep->save(); $pirep->refresh(); - $this->setPilotState($pilot, $pirep); - Log::info('PIREP ' . $pirep->id . ' state change to ACCEPTED'); # Update the aircraft @@ -300,6 +298,7 @@ class PirepService extends BaseService # Any ancillary tasks before an event is dispatched $this->removeBid($pirep); + $this->setPilotState($pilot, $pirep); event(new PirepAccepted($pirep)); return $pirep; diff --git a/tests/AwardsTest.php b/tests/AwardsTest.php new file mode 100644 index 00000000..37385fd0 --- /dev/null +++ b/tests/AwardsTest.php @@ -0,0 +1,60 @@ +awardSvc = app(\App\Services\AwardService::class); + $this->pirepSvc = app(\App\Services\PirepService::class); + } + + /** + * Make sure the awards classes are returned + */ + public function testGetAwardsClasses() + { + $classes = $this->awardSvc->findAllAwardClasses(); + $this->assertCount(2, $classes); + } + + /** + * Test to make sure that the award is actually given out + */ + public function testAwardsGiven() + { + // Create one award that's given out with one flight + $award = factory(App\Models\Award::class)->create([ + 'ref_class' => App\Awards\PilotFlightAwards::class, + 'ref_class_params' => 1, + ]); + + $user = factory(App\Models\User::class)->create([ + 'flights' => 0, + ]); + + $pirep = factory(App\Models\Pirep::class)->create([ + 'airline_id' => $user->airline->id, + 'user_id' => $user->id, + ]); + + $this->pirepSvc->create($pirep); + $this->pirepSvc->accept($pirep); + + $w = [ + 'user_id' => $user->id, + 'award_id' => $award->id, + ]; + + # Make sure only one is awarded + $this->assertEquals(1, UserAward::where($w)->count(['id'])); + + $found_award = UserAward::where($w)->first(); + $this->assertNotNull($found_award); + } +}