-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Internal/batch notifications #168
Open
demogorgonzola
wants to merge
13
commits into
master
Choose a base branch
from
internal/batch-notifications
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
22ee9b0
[wip] moving grants notifications to volunteers
demogorgonzola c85cac9
[wip] all backend is hooked up, remaining general layout to go
demogorgonzola 22bb631
[feature] condensed notification emails to users
demogorgonzola b730e19
[fix] general user email layout points to correct partials folder
demogorgonzola 2a9fc5c
Merge branch 'master' into internal/batch-notifications
demogorgonzola ab5d66b
[feature] batch notifications for slot taken events
demogorgonzola 2978a2f
[mess] re-enabling dispatch handeler
demogorgonzola 92b132e
[tests] more testing, more editing
demogorgonzola f1facb9
[mess] fixing functionality after notification json switch
demogorgonzola cfd8b13
[cleanup] removed echo statements
demogorgonzola 86b6da7
[cleaup] split logic from command into a job
demogorgonzola 0b3905c
[fix] correct view times, factories, and migration
demogorgonzola 9b92632
[fix] incorrect format string for duration
demogorgonzola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands\Notifications; | ||
|
||
use Illuminate\Console\Command; | ||
use DB; | ||
use Mail; | ||
use App\Jobs\SendUserMailJob; | ||
use App\Models\Notification; | ||
use App\Models\User; | ||
use App\Models\Slot; | ||
|
||
class Send extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'notifications:send'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Send queued notifications'; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
SendUserMailJob::dispatchNow(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\Notification; | ||
use App\Models\User; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Log; | ||
use Mail; | ||
|
||
class SendUserMailJob implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Send a batch email to each user with pending email notifications | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
// Group all notification by the user they're sent to | ||
$user_notifications = Notification::where('type', 'email') | ||
->where('status', 'new') | ||
->get()->groupBy('user_to'); | ||
|
||
//send batched notifications to each user via email | ||
foreach ($user_notifications as $user_id => $notifications) | ||
{ | ||
$user = User::find($user_id); | ||
|
||
//grab all notification metadata with the layout stored in them | ||
$notification_metadata = $notifications->map(function($notification) { | ||
$metadata = $notification->metadata; //copy | ||
$metadata['layout'] = $notification->layout; | ||
return $metadata; | ||
}); | ||
|
||
Log::info("Sending daily email to user: {$user->email}"); | ||
Mail::send('emails/user-daily-digest', compact('notification_metadata'), function ($message) use ($user) | ||
{ | ||
$message->to($user->email, $user->name)->subject('Daily Volunteer Digest - Some things you may want to look over...'); | ||
}); | ||
|
||
// Update all notifications to sent | ||
$notification_ids = $notifications->pluck('id'); | ||
Notification::whereIn('id', $notification_ids)->update(['status' => 'sent']); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
|
||
class QueueUserMessage | ||
{ | ||
/** | ||
* Create the event listener. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Handle the event. | ||
* | ||
* @param object $event | ||
* @return void | ||
*/ | ||
public function handle($event) | ||
{ | ||
// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this listener created? i don't see anything being handled just yet. |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use App\Models\User; | ||
use Mail; | ||
|
||
class Notification extends Model | ||
{ | ||
protected $casts = [ | ||
'metadata' => 'array', | ||
]; | ||
|
||
public function from() | ||
{ | ||
return $this->belongsTo('App\Models\User', 'user_from'); | ||
} | ||
|
||
public function to() | ||
{ | ||
return $this->belongsTo('App\Models\User', 'user_to'); | ||
} | ||
|
||
public static function send(User $user_to, $type, $layout, $metadata, User $user_from = null) | ||
{ | ||
// Email template variables | ||
$templateVars = ['user' => $user_to]; | ||
|
||
// Check if any template variables were set in the metadata | ||
if(isset($metadata['template-vars'])) | ||
{ | ||
// Prevent template vars from being saved in the database | ||
$templateVars = array_merge($templateVars, $metadata['template-vars']); | ||
unset($metadata['template-vars']); | ||
} | ||
|
||
$notification = new Notification; | ||
$notification->type = $type; | ||
$notification->layout = $layout; | ||
$notification->status = 'new'; | ||
$notification->metadata = $metadata; | ||
$notification->user_to = $user_to->id; | ||
$notification->user_from = $user_from ? $user_from->id : null; | ||
$notification->save(); | ||
|
||
if($type == 'email') | ||
{ | ||
Mail::send($metadata['template'], $templateVars, function ($message) use ($user_to, $metadata) | ||
{ | ||
$message->to($user_to->email, $user_to->name)->subject($metadata['subject']); | ||
}); | ||
|
||
$notification->status = 'sent'; | ||
$notification->save(); | ||
} | ||
|
||
return $notification; | ||
} | ||
|
||
public static function queue(User $user_to, $type, $layout, $metadata, User $user_from = null) | ||
{ | ||
$notification = new Notification; | ||
$notification->type = $type; | ||
$notification->layout = $layout; | ||
$notification->status = 'new'; | ||
$notification->metadata = $metadata; | ||
$notification->user_to = $user_to->id; | ||
$notification->user_from = $user_from ? $user_from->id : null; | ||
$notification->save(); | ||
|
||
return $notification; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
use App\Models\Notification; | ||
use App\Models\User; | ||
use Faker\Generator as Faker; | ||
|
||
$factory->define(Notification::class, function (Faker $faker, array $data) { | ||
|
||
if(!isset($data['schedule_id'])) | ||
{ | ||
Log::warning("Using Factory[Notification] without setting user_to"); | ||
} | ||
|
||
return [ | ||
'type' => 'info', | ||
'status' => 'new', | ||
'layout' => 'notification-test', | ||
'metadata' => [ | ||
'event' => 'test_event', | ||
], | ||
'user_to' => function() { | ||
return factory(User::class)->create(); | ||
}, | ||
]; | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this allows for banned users to take shifts.