feat: add absence notifications

This commit is contained in:
Miguel Nogueira 2022-04-03 22:25:44 +01:00
parent a9c2617713
commit 2ddfb62f17
6 changed files with 366 additions and 5 deletions

View File

@ -0,0 +1,68 @@
<?php
namespace App\Notifications;
use App\Absence;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class AbsenceRequestApproved extends Notification implements ShouldQueue
{
use Queueable;
public Absence $absence;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Absence $absence)
{
$this->absence = $absence;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hi ' . $notifiable->name . ',')
->from(config('notification.sender.address'), config('notification.sender.name'))
->subject(config('app.name').' - absence request approved')
->line("Your recent Leave of Absence request from {$this->absence->created_at} has just been approved by an admin.")
->line('Your inactivity during the period you selected won\'t be counted. You will receive another email notification when your request ends, or if you decide to cancel it.')
->action('View your request', url(route('absences.show', ['absence' => $this->absence->id])))
->salutation('The team at ' . config('app.name'));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace App\Notifications;
use App\Absence;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class AbsenceRequestCancelled extends Notification implements ShouldQueue
{
use Queueable;
public Absence $absence;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Absence $absence)
{
$this->absence = $absence;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hi ' . $notifiable->name . ',')
->from(config('notification.sender.address'), config('notification.sender.name'))
->subject(config('app.name').' - absence request cancelled')
->line("This notification confirms that your recent Leave of Absence from {$this->absence->created_at} has just been cancelled by you.")
->line('Please note that any inactivity will be counted in our activity metrics. You may also make a new request if you wish.')
->action('View your request', url(route('absences.show', ['absence' => $this->absence->id])))
->action('Send new request', url(route('absences.create')))
->salutation('The team at ' . config('app.name'));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace App\Notifications;
use App\Absence;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class AbsenceRequestDeclined extends Notification implements ShouldQueue
{
use Queueable;
public Absence $absence;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Absence $absence)
{
$this->absence = $absence;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hi ' . $notifiable->name . ',')
->from(config('notification.sender.address'), config('notification.sender.name'))
->subject(config('app.name').' - absence request declined')
->line("Your recent Leave of Absence request from {$this->absence->created_at} has just been declined by an admin.")
->line('Please note that any inactivity will be counted in our activity metrics. You may make a new request, but we recommend you ask your team lead regarding your declined request.')
->action('View your request', url(route('absences.show', ['absence' => $this->absence->id])))
->action('Send new request', url(route('absences.create')))
->salutation('The team at ' . config('app.name'));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace App\Notifications;
use App\Absence;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class AbsenceRequestEnded extends Notification implements ShouldQueue
{
use Queueable;
public Absence $absence;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Absence $absence)
{
$this->absence = $absence;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hi ' . $notifiable->name . ',')
->from(config('notification.sender.address'), config('notification.sender.name'))
->subject(config('app.name').' - absence request expired')
->line("Your Leave of Absence request from {$this->absence->created_at} (until {$this->absence->predicted_end}) has expired today.")
->line('Please note that any inactivity will be counted in our activity metrics. You may now make a new request if you still need more time.')
->action('View expired request', url(route('absences.show', ['absence' => $this->absence->id])))
->action('Send new request', url(route('absences.create')))
->salutation('The team at ' . config('app.name'));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace App\Notifications;
use App\Absence;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class NewAbsenceRequest extends Notification implements ShouldQueue
{
use Queueable;
public Absence $absence;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Absence $absence)
{
$this->absence = $absence;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hi ' . $notifiable->name . ',')
->from(config('notification.sender.address'), config('notification.sender.name'))
->subject(config('app.name').' - new absence request pending review')
->line("A new absence request has just been submitted, scheduled to end {$this->absence->predicted_end}. Please review this request and take the appropriate action(s). The requester will be notified of your decision by email.")
->line("You are receiving this email because you're a site admin.")
->action('Review request', url(route('absences.show', ['absence' => $this->absence->id])))
->salutation('The team at ' . config('app.name'));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

View File

@ -4,12 +4,18 @@ namespace App\Services;
use App\Absence;
use App\Exceptions\AbsenceNotActionableException;
use App\Notifications\AbsenceRequestApproved;
use App\Notifications\AbsenceRequestCancelled;
use App\Notifications\AbsenceRequestDeclined;
use App\Notifications\AbsenceRequestEnded;
use App\Notifications\NewAbsenceRequest;
use App\User;
use Carbon\Carbon;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Spatie\Permission\Models\Role;
class AbsenceService
@ -51,6 +57,10 @@ class AbsenceService
'status' => 'PENDING',
]);
foreach(User::role('admin')->get() as $admin) {
$admin->notify(new NewAbsenceRequest($absence));
}
Log::info('Processing new leave of absence request.', [
'requesting_user' => $user->email,
'absenceid' => $absence->id,
@ -76,7 +86,9 @@ class AbsenceService
'new_status' => 'APPROVED'
]);
return $absence->setApproved();
return $absence
->setApproved()
->requester->notify(new AbsenceRequestApproved($absence));
}
@ -95,7 +107,9 @@ class AbsenceService
'new_status' => 'DECLINED'
]);
return $absence->setDeclined();
return $absence
->setDeclined()
->requester->notify(new AbsenceRequestDeclined($absence));
}
@ -113,7 +127,9 @@ class AbsenceService
'new_status' => 'CANCELLED'
]);
return $absence->setCancelled();
return $absence
->setCancelled()
->requester->notify(new AbsenceRequestCancelled($absence));
}
/**
@ -129,7 +145,9 @@ class AbsenceService
'new_status' => 'ENDED'
]);
return $absence->setEnded();
return $absence
->setEnded()
->requester->notify(new AbsenceRequestEnded($absence));
}
/**
@ -154,7 +172,7 @@ class AbsenceService
foreach (Absence::all() as $absence)
{
if (!Carbon::parse($absence->predicted_end)->isFuture()) {
$absence->setEnded();
$this->endAbsence($absence);
}
}
}