From 2ddfb62f17cd49fe28b3424315f8a766b117b751 Mon Sep 17 00:00:00 2001 From: miguel456 Date: Sun, 3 Apr 2022 22:25:44 +0100 Subject: [PATCH] feat: add absence notifications --- app/Notifications/AbsenceRequestApproved.php | 68 ++++++++++++++++++ app/Notifications/AbsenceRequestCancelled.php | 69 +++++++++++++++++++ app/Notifications/AbsenceRequestDeclined.php | 69 +++++++++++++++++++ app/Notifications/AbsenceRequestEnded.php | 69 +++++++++++++++++++ app/Notifications/NewAbsenceRequest.php | 68 ++++++++++++++++++ app/Services/AbsenceService.php | 28 ++++++-- 6 files changed, 366 insertions(+), 5 deletions(-) create mode 100644 app/Notifications/AbsenceRequestApproved.php create mode 100644 app/Notifications/AbsenceRequestCancelled.php create mode 100644 app/Notifications/AbsenceRequestDeclined.php create mode 100644 app/Notifications/AbsenceRequestEnded.php create mode 100644 app/Notifications/NewAbsenceRequest.php diff --git a/app/Notifications/AbsenceRequestApproved.php b/app/Notifications/AbsenceRequestApproved.php new file mode 100644 index 0000000..4da1094 --- /dev/null +++ b/app/Notifications/AbsenceRequestApproved.php @@ -0,0 +1,68 @@ +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 [ + // + ]; + } +} diff --git a/app/Notifications/AbsenceRequestCancelled.php b/app/Notifications/AbsenceRequestCancelled.php new file mode 100644 index 0000000..1cf25ee --- /dev/null +++ b/app/Notifications/AbsenceRequestCancelled.php @@ -0,0 +1,69 @@ +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 [ + // + ]; + } +} diff --git a/app/Notifications/AbsenceRequestDeclined.php b/app/Notifications/AbsenceRequestDeclined.php new file mode 100644 index 0000000..8986bdc --- /dev/null +++ b/app/Notifications/AbsenceRequestDeclined.php @@ -0,0 +1,69 @@ +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 [ + // + ]; + } +} diff --git a/app/Notifications/AbsenceRequestEnded.php b/app/Notifications/AbsenceRequestEnded.php new file mode 100644 index 0000000..362a352 --- /dev/null +++ b/app/Notifications/AbsenceRequestEnded.php @@ -0,0 +1,69 @@ +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 [ + // + ]; + } +} diff --git a/app/Notifications/NewAbsenceRequest.php b/app/Notifications/NewAbsenceRequest.php new file mode 100644 index 0000000..51add0b --- /dev/null +++ b/app/Notifications/NewAbsenceRequest.php @@ -0,0 +1,68 @@ +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 [ + // + ]; + } +} diff --git a/app/Services/AbsenceService.php b/app/Services/AbsenceService.php index f3e7376..df80f21 100644 --- a/app/Services/AbsenceService.php +++ b/app/Services/AbsenceService.php @@ -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); } } }