Skip to content

Commit 37f5c2c

Browse files
committed
Add email queue
1 parent 2ebbfb5 commit 37f5c2c

File tree

10 files changed

+142
-8
lines changed

10 files changed

+142
-8
lines changed

app/Http/Controllers/Api/V1_0/VolunteerAuthController.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
use App\Http\Requests\Api\V1_0\VolunteerRegistrationRequest;
66
use App\Http\Controllers\Controller;
77
use Dingo\Api\Routing\Helpers;
8-
use App\Vounteer;
8+
use App\Volunteer;
99
use App\City;
10+
use App\VerificationCode;
11+
use App\Jobs\SendVerificationEmail;
12+
use App\Utils\StringUtil;
1013

1114
class VolunteerAuthController extends Controller
1215
{
@@ -31,14 +34,20 @@ public function register(VolunteerRegistrationRequest $request)
3134
// Get volunteer data, except city object
3235
$volunteerInput = $request->except(['city']);
3336
// Get city ID
34-
//$cityId = $request->input('city.id');
37+
$cityId = $request->input('city.id');
38+
$verificationCodeString = StringUtil::generateHashToken();
3539

3640
// Create a new volunteer
37-
//$volunteer = Volunteer::create($volunteerInput);
38-
//$city = City::find($cityId);
39-
//$volunteer->cities()->save($city);
41+
$volunteer = Volunteer::create($volunteerInput);
42+
$city = City::find($cityId);
43+
$volunteer->cities()->save($city);
44+
45+
// Save verification code into the volunteer
46+
$verificationCode = new VerificationCode(['code' => $verificationCodeString]);
47+
$volunteer->verificationCode()->save($verificationCode);
4048

41-
// Send verification email
49+
// Send verification email to an queue
50+
$this->dispatch(new SendVerificationEmail($volunteer, $verificationCodeString, 'Verification email'));
4251

4352
// Get authentication token
4453

app/Jobs/SendVerificationEmail.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Jobs\Job;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Queue\InteractsWithQueue;
8+
use Illuminate\Contracts\Bus\SelfHandling;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Contracts\Mail\Mailer;
11+
use App\Volunteer;
12+
13+
class SendVerificationEmail extends Job implements SelfHandling, ShouldQueue
14+
{
15+
use InteractsWithQueue, SerializesModels;
16+
17+
protected $volunteer;
18+
protected $verificationCode;
19+
protected $subject;
20+
21+
/**
22+
* Create a new job instance.
23+
*
24+
* @return void
25+
*/
26+
public function __construct(Volunteer $volunteer, $verificationCode, $subject)
27+
{
28+
$this->volunteer = $volunteer;
29+
$this->verificationCode = $verificationCode;
30+
$this->subject = $subject;
31+
}
32+
33+
/**
34+
* Execute the job.
35+
*
36+
* @return void
37+
*/
38+
public function handle(Mailer $mailer)
39+
{
40+
if ($this->attempts() < 10 ) {
41+
$data = [
42+
'name' => $this->volunteer->last_name,
43+
'verifcationUrl' => $this->verificationCode
44+
];
45+
$lastName = $this->volunteer->last_name;
46+
$emailAddress = $this->volunteer->email;
47+
$subject = $this->subject;
48+
49+
$mailer->send('emails.verify',
50+
$data,
51+
function ($message) use ($lastName, $emailAddress, $subject) {
52+
$message->to($emailAddress, $lastName)->subject($subject);
53+
});
54+
}
55+
}
56+
}

app/Utils/StringUtil.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Utils;
4+
5+
use Hash;
6+
7+
class StringUtil
8+
{
9+
public static function generateHashToken()
10+
{
11+
// generate
12+
$randomString = str_random(100) . time();
13+
return Hash::make($randomString);
14+
}
15+
}

app/VerificationCode.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class VerificationCode extends Model
8+
{
9+
protected $table = 'verification_codes';
10+
11+
public function volunteer()
12+
{
13+
return $this->belongsTo('App\Volunteer');
14+
}
15+
}

app/Volunteer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ public function cities()
2525
{
2626
return $this->belongsToMany('App\City');
2727
}
28+
29+
public function verificationToken()
30+
{
31+
return $this->hasOne('App\VerificationCode');
32+
}
2833
}

config/mail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
|
5555
*/
5656

57-
'from' => ['address' => null, 'name' => null],
57+
'from' => ['address' => env('MAIL_FROM', null), 'name' => env('MAIL_NAME', null)],
5858

5959
/*
6060
|--------------------------------------------------------------------------

database/migrations/2015_09_15_055905_create_volunteers_table.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class CreateVolunteersTable extends Migration
77
{
88
/**
99
* Run the migrations.
10-
*
1110
* @return void
1211
*/
1312
public function up()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateVerificationCodesTable extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('verification_codes', function (Blueprint $table) {
16+
$table->increments('id');
17+
$table->string('code')->unique();
18+
$table->integer('volunteer_id')->unique();
19+
$table->dateTime('created_at');
20+
$table->dateTime('updated_at');
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::drop('verification_codes');
32+
}
33+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hi, {{ $name }}
2+
Link: {{ $verifcationUrl }}

storage/database.sqlite

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)