Fixes for emails; send user registered/pending depending on state

This commit is contained in:
Nabeel Shahzad 2017-12-22 12:46:46 -06:00
parent 74316218bc
commit 161623c85e
42 changed files with 215 additions and 153 deletions

View File

@ -0,0 +1,17 @@
<?php
use Faker\Generator as Faker;
/**
* Add any number of airports. Don't really care if they're real or not
*/
$factory->define(App\Models\Airline::class, function (Faker $faker) {
return [
'id' => $faker->unique()->numberBetween(10, 10000),
'icao' => function(array $apt) { return substr($apt['id'],0, 4); },
'iata' => function (array $apt) { return $apt['id']; },
'name' => $faker->sentence(3),
'country' => $faker->country,
'active' => 1
];
});

View File

@ -12,7 +12,9 @@ $factory->define(App\Models\User::class, function (Faker $faker)
'email' => $faker->safeEmail,
'password' => $password ?: $password = Hash::make('secret'),
'api_key' => $faker->sha1,
'airline_id' => 1,
'airline_id' => function () {
return factory(App\Models\Airline::class)->create()->id;
},
'rank_id' => 1,
'flights' => $faker->numberBetween(0, 1000),
'flight_time' => $faker->numberBetween(0, 10000),

View File

@ -2,6 +2,9 @@
namespace App\Http\Controllers\Auth;
use App\Facades\Utils;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Validator;
use App\Models\Airport;
use App\Models\Airline;
@ -61,12 +64,12 @@ class RegisterController extends Controller
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
* @throws \RuntimeException
*/
protected function create(array $data)
{
# First, validate the posted data
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email',
@ -75,13 +78,19 @@ class RegisterController extends Controller
'password' => 'required|confirmed'
]);
# Let's tell the service to create the pilot
if($p = $this->userService->createPilot($data))
{
//return $this->view('auth.registered');
return $p;
}
$opts = [
'name' => $data['name'],
'email' => $data['email'],
'api_key' => Utils::generateApiKey(),
'airline_id' => $data['airline'],
'home_airport_id' => $data['home_airport'],
'curr_airport_id' => $data['home_airport'],
'password' => Hash::make($data['password'])
];
# I'm not sure if we really need to add the error something if createPilot fails?
$user = User::create($opts);
$user = $this->userService->createPilot($user);
return $user;
}
}

View File

@ -1,16 +1,12 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: nabeelshahzad
* Date: 12/22/17
* Time: 11:44 AM
*/
namespace App\Listeners;
use Log;
use \App\Events\UserRegistered;
use Illuminate\Support\Facades\Mail;
use App\Models\Enums\PilotState;
use \App\Events\UserRegistered;
/**
* Handle sending emails on different events
@ -26,9 +22,22 @@ class EmailEventListener
);
}
/**
* Send an email when the user registered
* @param UserRegistered $event
*/
public function onUserRegister(UserRegistered $event)
{
Log::info($event->user->toArray());
Log::info('onUserRegister: '
. $event->user->pilot_id . ' is '
. PilotState::label($event->user->state)
. ', sending active email');
if($event->user->state === PilotState::ACTIVE) {
Mail::to($event->user->email)->send(new \App\Mail\UserRegistered($event->user));
} else if($event->user->state === PilotState::PENDING) {
Mail::to($event->user->email)->send(new \App\Mail\UserPending($event->user));
}
}
}

32
app/Mail/UserPending.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace App\Mail;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class UserPending extends Mailable
{
use Queueable, SerializesModels;
private $user;
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.user.pending')
->with(['user' => $this->user]);
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Mail;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class UserRegistered extends Mailable
{
use Queueable, SerializesModels;
private $user;
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.user.registered')
->with([
'user' => $this->user,
]);
}
}

View File

@ -0,0 +1,25 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: nabeelshahzad
* Date: 12/22/17
* Time: 12:14 PM
*/
namespace App\Models\Enums;
class PilotState extends EnumBase
{
const PENDING = 1;
const ACTIVE = 2;
const ON_LEAVE = 3;
const SUSPENDED = 4;
protected static $labels = [
PilotState::PENDING => 'Pending',
PilotState::ACTIVE => 'Active',
PilotState::ON_LEAVE => 'On Leave',
PilotState::SUSPENDED => 'Suspended',
];
}

View File

@ -39,7 +39,8 @@ class User extends Authenticatable
'curr_airport_id',
'rank_id',
'timezone',
'active',
'state',
'status',
];
/**

View File

@ -4,6 +4,7 @@ namespace App\Services;
use App\Events\UserRegistered;
use App\Facades\Utils;
use App\Models\Enums\PilotState;
use App\Models\User;
use App\Models\Rank;
use App\Models\Role;
@ -18,29 +19,19 @@ class UserService extends BaseService
/**
* Register a pilot
* @param array $data
* @param User $user
* @return mixed
*/
public function createPilot(array $data)
public function createPilot(User $user)
{
$opts = [
'name' => $data['name'],
'email' => $data['email'],
'api_key' => Utils::generateApiKey(),
'airline_id' => $data['airline'],
'home_airport_id' => $data['home_airport'],
'curr_airport_id' => $data['home_airport'],
'password' => Hash::make($data['password'])
];
# Determine if we want to auto accept
if(setting('pilot.auto_accept') === true) {
$opts['status'] = config('enums.states.ACTIVE');
$user->state = PilotState::ACTIVE;
} else {
$opts['status'] = config('enums.states.PENDING');
$user->state = PilotState::PENDING;
}
$user = User::create($opts);
$user->save();
# Attach the user roles
$role = Role::where('name', 'user')->first();
@ -49,6 +40,8 @@ class UserService extends BaseService
# Let's check their rank
$this->calculatePilotRank($user);
$user->refresh();
event(new UserRegistered($user));
return $user;

View File

@ -9,14 +9,6 @@ return [
'ENABLED' => 1,
],
# Pilot states
'states' => [
'PENDING' => 0,
'ACTIVE' => 1,
'ON_LEAVE' => 2,
'SUSPENDED' => 3,
],
'fuel_types' => [
'100LL' => 0,
'JETA' => 1,

View File

@ -57,7 +57,7 @@ return [
'from' => [
'name' => env('MAIL_FROM_NAME', 'phpVMS Admin'),
'address' => env('MAIL_FROM_ADDRESS', ''),
'address' => env('MAIL_FROM_ADDRESS', 'no-reply@phpvms.net'),
],
/*

View File

@ -0,0 +1,8 @@
@component('mail::message')
# Thanks for signing up, {!! $user->name !!}!
You will be notified as soon as your account is approved!
Thanks,<br>
{{ config('app.name') }}
@endcomponent

View File

@ -0,0 +1,12 @@
@component('mail::message')
# Thanks for signing up, {!! $user->name !!}!
Your account is ready to use.
@component('mail::button', ['url' => url('/login')])
Visit your account now!
@endcomponent
Thanks,<br>
{{ config('app.name') }}
@endcomponent

View File

@ -1,41 +0,0 @@
<?php
namespace Tests\Browser\Pages;
use Laravel\Dusk\Browser;
class HomePage extends Page
{
/**
* Get the URL for the page.
*
* @return string
*/
public function url()
{
return '/';
}
/**
* Assert that the browser is on the page.
*
* @param Browser $browser
* @return void
*/
public function assert(Browser $browser)
{
//
}
/**
* Get the element shortcuts for the page.
*
* @return array
*/
public function elements()
{
return [
'@element' => '#selector',
];
}
}

View File

@ -1,20 +0,0 @@
<?php
namespace Tests\Browser\Pages;
use Laravel\Dusk\Page as BasePage;
abstract class Page extends BasePage
{
/**
* Get the global element shortcuts for the site.
*
* @return array
*/
public static function siteElements()
{
return [
'@element' => '#selector',
];
}
}

View File

@ -1,21 +0,0 @@
<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class RegistrationTest extends DuskTestCase
{
/**
* Test the registration
*/
public function testBasicExample()
{
/*$this->browse(function (Browser $browser) {
$browser->visit('/register')
->assertSee('Register');
});*/
}
}

View File

@ -1,2 +0,0 @@
*
!.gitignore

View File

@ -1,2 +0,0 @@
*
!.gitignore

View File

@ -1,22 +0,0 @@
<?php
namespace Tests;
use Illuminate\Support\Facades\Hash;
use Illuminate\Contracts\Console\Kernel;
trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__ . '/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
//Hash::driver('bcrypt')->setRounds(4);
return $app;
}
}

View File

@ -0,0 +1,38 @@
<?php
use Illuminate\Support\Facades\Mail;
use \App\Models\Enums\PilotState;
class RegistrationTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testRegistration()
{
Event::fake();
Mail::fake();
$userSvc = app('App\Services\UserService');
setting('pilot.auto_accept', true);
$user = factory(App\Models\User::class)->create();
$user = $userSvc->createPilot($user);
$this->assertEquals(PilotState::ACTIVE, $user->state);
Event::assertDispatched(\App\Events\UserRegistered::class, function ($e) use ($user) {
return $e->user->id === $user->id
&& $e->user->state === $user->state;
});
/*Mail::assertSent(\App\Mail\UserRegistered::class, function ($mail) use ($user) {
return $mail->user->id === $user->id
&& $mail->user->state === $user->state;
});*/
}
}

View File

@ -39,8 +39,6 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
public function setUp() {
parent::setUp();
$this->reset_db();
Mail::fake();
}
/**