#41 - wip
This commit is contained in:
67
app/Domain/Notifications/VacationRequestNotification.php
Normal file
67
app/Domain/Notifications/VacationRequestNotification.php
Normal 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);
|
||||
}
|
||||
}
|
31
app/Domain/VacationRequestNotificationSender.php
Normal file
31
app/Domain/VacationRequestNotificationSender.php
Normal 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();
|
||||
}
|
||||
}
|
@@ -8,6 +8,7 @@ use Illuminate\Contracts\Auth\Factory as Auth;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
use Toby\Domain\Enums\VacationRequestState;
|
||||
use Toby\Domain\Events\VacationRequestStateChanged;
|
||||
use Toby\Domain\VacationRequestNotificationSender;
|
||||
use Toby\Eloquent\Models\User;
|
||||
use Toby\Eloquent\Models\VacationRequest;
|
||||
|
||||
@@ -16,6 +17,7 @@ class VacationRequestObserver
|
||||
public function __construct(
|
||||
protected Auth $auth,
|
||||
protected Dispatcher $dispatcher,
|
||||
protected VacationRequestNotificationSender $vacationRequestNotificationSender,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -39,6 +41,13 @@ class VacationRequestObserver
|
||||
}
|
||||
}
|
||||
|
||||
public function updated(VacationRequest $vacationRequest): void
|
||||
{
|
||||
if ($vacationRequest->state !== VacationRequestState::CREATED) {
|
||||
$this->vacationRequestNotificationSender->sendVacationRequestNotification($vacationRequest);
|
||||
}
|
||||
}
|
||||
|
||||
protected function fireStateChangedEvent(
|
||||
VacationRequest $vacationRequest,
|
||||
?VacationRequestState $from,
|
||||
|
Reference in New Issue
Block a user