This commit is contained in:
EwelinaLasowy
2022-02-11 13:02:35 +01:00
parent d026d41715
commit a0b64e6cb3
9 changed files with 500 additions and 3 deletions

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace Toby\Domain\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use InvalidArgumentException;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationRequest;
class VacationRequestNotification extends Notification
{
use Queueable;
protected User $user;
protected VacationRequest $vacationRequest;
public function __construct(User $user, VacationRequest $vacationRequest)
{
$this->user = $user;
$this->vacationRequest = $vacationRequest;
}
public function via(): array
{
return ["mail"];
}
/**
* @throws InvalidArgumentException
*/
public function toMail(): MailMessage
{
$url = route(
"vacation.requests.show",
[
"vacationRequest" => $this->vacationRequest,
],
);
return $this->buildMailMessage($url);
}
protected function buildMailMessage(string $url): MailMessage
{
$title = $this->vacationRequest->name;
$state = $this->vacationRequest->state->label();
$user = $this->user->getFullNameAttribute();
return (new MailMessage())
->greeting(__("Hi :user!", [
"user" => $user,
]))
->subject(__("Vacation request :title", [
"title" => $title,
]))
->line(__("The vacation request :title has changed state to :state.", [
"title" => $title,
"state" => $state,
]))
->action(__("Show vacation request"), $url);
}
}

View File

@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Toby\Domain;
use Illuminate\Support\Collection;
use Toby\Domain\Enums\Role;
use Toby\Domain\Notifications\VacationRequestNotification;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationRequest;
class VacationRequestNotificationSender
{
public function sendVacationRequestNotification(VacationRequest $vacationRequest): void
{
foreach ($this->getUsersForNotifications() as $user) {
$user->notify(new VacationRequestNotification($user, $vacationRequest));
}
$vacationRequest->user->notify(new VacationRequestNotification($vacationRequest->user, $vacationRequest));
}
protected function getUsersForNotifications(): Collection
{
return User::query()
->where("role", Role::TECHNICAL_APPROVER)
->orWhere("role", Role::ADMINISTRATIVE_APPROVER)
->get();
}
}