Skip to content
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
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions laravel/app/Console/Commands/Notifications/Send.php
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();
}
}
4 changes: 3 additions & 1 deletion laravel/app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Kernel extends ConsoleKernel
*/
protected $commands =
[
\App\Console\Commands\Notifications\Send::class,
\App\Console\Commands\PopulateScheduleDates::class,
\App\Console\Commands\FixDuplicateRoles::class,
];
Expand All @@ -26,6 +27,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
//$schedule->command('inspire')->hourly();
$schedule->command('notifications:send')->daily();
}
}
4 changes: 4 additions & 0 deletions laravel/app/Http/Controllers/SlotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ private function userAllowed(Slot $slot)
$allowed = true;
}
}
if($slotRoles->isEmpty())
{
$allowed = true;
Copy link
Contributor

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.

}

return $allowed;
}
Expand Down
21 changes: 0 additions & 21 deletions laravel/app/Jobs/Job.php

This file was deleted.

64 changes: 64 additions & 0 deletions laravel/app/Jobs/SendUserMailJob.php
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']);
}
}
}
30 changes: 30 additions & 0 deletions laravel/app/Listeners/QueueUserMessage.php
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)
{
//
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

}
}
40 changes: 26 additions & 14 deletions laravel/app/Listeners/SendUserShiftConfirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Listeners;

use Mail;
use App\Models\Notification;
use App\Models\User;
use App\Events\SlotChanged;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -29,27 +31,37 @@ public function handle(SlotChanged $event)
{
if ($event->change['status'] === 'taken')
{
$slot = $event->slot;
$user_email = $event->change['email'];
$user_name = $event->change['name'];
$event_name = $event->slot->schedule->shift->event->name;
$shift_name = $event->slot->schedule->shift->name;
$start_date = $event->slot->start_date;
$start_time = $event->slot->start_date;
$end_time = $event->slot->end_time;

$admin_assigned = false;
if (isset($event->change['admin_assigned']))
{
$admin_assigned = $event->change['admin_assigned'];
}

$event_data = compact('slot', 'user_email', 'user_name', 'event_name', 'shift_name', 'start_date', 'start_time', 'end_time', 'admin_assigned');
$user = User::where('name', $event->change['name'])->first();

Mail::send('emails/user-shift-confirmation', $event_data, function ($message) use ($user_email, $user_name, $shift_name)
{
$message->to($user_email, $user_name)->subject('Confirmation Email - ' . $shift_name . ' shift!');
});
Notification::queue($user, 'email', 'user-shift-confirmation', [
'event' => 'slot_taken',
'slot_id' => $event->slot->id,
'user_email' => $event->change['email'],
'user_name' => $event->change['name'],
'event_name' => $event->slot->schedule->shift->event->name,
'shift_name' => $event->slot->schedule->shift->name,
'start_date' => $event->slot->start_date,
'start_time' => $event->slot->start_date,
'end_time' => $event->slot->end_time,
'admin_assigned' => $admin_assigned,
]);
}
else
{
// Get the most recently created notification for the slot
$notification = Notification::where('user_to', $event->slot->user->id)
->where('metadata->slot_id', $event->slot->id)
->orderBy('created_at', 'desc')
->first();

$notification->status = 'canceled';
$notification->save();
}
}
}
74 changes: 74 additions & 0 deletions laravel/app/Models/Notification.php
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;
}
}
25 changes: 25 additions & 0 deletions laravel/database/factories/NotificationFactory.php
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();
},
];
});
Loading