#126 - fix
This commit is contained in:
		@@ -7,6 +7,7 @@ namespace Toby\Domain\Notifications;
 | 
			
		||||
use Illuminate\Bus\Queueable;
 | 
			
		||||
use Illuminate\Notifications\Notification;
 | 
			
		||||
use Toby\Eloquent\Models\User;
 | 
			
		||||
use Toby\Infrastructure\Slack\Elements\SlackMessage;
 | 
			
		||||
 | 
			
		||||
class KeyHasBeenGivenNotification extends Notification
 | 
			
		||||
{
 | 
			
		||||
@@ -22,13 +23,14 @@ class KeyHasBeenGivenNotification extends Notification
 | 
			
		||||
        return [Channels::SLACK];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function toSlack(Notifiable $notifiable): string
 | 
			
		||||
    public function toSlack(Notifiable $notifiable): SlackMessage
 | 
			
		||||
    {
 | 
			
		||||
        return __(":sender gives key no :key to :recipient", [
 | 
			
		||||
            "sender" => $this->getName($this->sender),
 | 
			
		||||
            "recipient" => $this->getName($this->recipient),
 | 
			
		||||
            "key" => $notifiable->id,
 | 
			
		||||
        ]);
 | 
			
		||||
        return (new SlackMessage())
 | 
			
		||||
            ->text(__(":sender gives key no :key to :recipient", [
 | 
			
		||||
                "sender" => $this->getName($this->sender),
 | 
			
		||||
                "recipient" => $this->getName($this->recipient),
 | 
			
		||||
                "key" => $notifiable->id,
 | 
			
		||||
            ]));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function getName(User $user): string
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@ namespace Toby\Domain\Notifications;
 | 
			
		||||
use Illuminate\Bus\Queueable;
 | 
			
		||||
use Illuminate\Notifications\Notification;
 | 
			
		||||
use Toby\Eloquent\Models\User;
 | 
			
		||||
use Toby\Infrastructure\Slack\Elements\SlackMessage;
 | 
			
		||||
 | 
			
		||||
class KeyHasBeenTakenNotification extends Notification
 | 
			
		||||
{
 | 
			
		||||
@@ -22,13 +23,14 @@ class KeyHasBeenTakenNotification extends Notification
 | 
			
		||||
        return [Channels::SLACK];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function toSlack(Notifiable $notifiable): string
 | 
			
		||||
    public function toSlack(Notifiable $notifiable): SlackMessage
 | 
			
		||||
    {
 | 
			
		||||
        return __(":recipient takes key no :key from :sender", [
 | 
			
		||||
            "recipient" => $this->getName($this->recipient),
 | 
			
		||||
            "sender" => $this->getName($this->sender),
 | 
			
		||||
            "key" => $notifiable->id,
 | 
			
		||||
        ]);
 | 
			
		||||
        return (new SlackMessage())
 | 
			
		||||
            ->text(__(":recipient takes key no :key from :sender", [
 | 
			
		||||
                "recipient" => $this->getName($this->recipient),
 | 
			
		||||
                "sender" => $this->getName($this->sender),
 | 
			
		||||
                "key" => $notifiable->id,
 | 
			
		||||
            ]));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function getName(User $user): string
 | 
			
		||||
 
 | 
			
		||||
@@ -9,6 +9,7 @@ use Illuminate\Notifications\Messages\MailMessage;
 | 
			
		||||
use Illuminate\Notifications\Notification;
 | 
			
		||||
use InvalidArgumentException;
 | 
			
		||||
use Toby\Eloquent\Models\VacationRequest;
 | 
			
		||||
use Toby\Infrastructure\Slack\Elements\SlackMessage;
 | 
			
		||||
 | 
			
		||||
class VacationRequestCreatedNotification extends Notification
 | 
			
		||||
{
 | 
			
		||||
@@ -23,14 +24,12 @@ class VacationRequestCreatedNotification extends Notification
 | 
			
		||||
        return [Channels::MAIL, Channels::SLACK];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function toSlack(): string
 | 
			
		||||
    public function toSlack(): SlackMessage
 | 
			
		||||
    {
 | 
			
		||||
        $url = route("vacation.requests.show", ["vacationRequest" => $this->vacationRequest->id]);
 | 
			
		||||
 | 
			
		||||
        return implode("\n", [
 | 
			
		||||
            $this->buildDescription(),
 | 
			
		||||
            "<${url}|Zobacz szczegóły>",
 | 
			
		||||
        ]);
 | 
			
		||||
        return (new SlackMessage())
 | 
			
		||||
            ->text("{$this->buildDescription()}\n <${url}|Zobacz szczegóły>");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -56,19 +55,25 @@ class VacationRequestCreatedNotification extends Notification
 | 
			
		||||
        $days = $this->vacationRequest->vacations()->count();
 | 
			
		||||
 | 
			
		||||
        return (new MailMessage())
 | 
			
		||||
            ->greeting(__("Hi :user!", [
 | 
			
		||||
                "user" => $user,
 | 
			
		||||
            ]))
 | 
			
		||||
            ->greeting(
 | 
			
		||||
                __("Hi :user!", [
 | 
			
		||||
                    "user" => $user,
 | 
			
		||||
                ]),
 | 
			
		||||
            )
 | 
			
		||||
            ->subject($this->buildSubject())
 | 
			
		||||
            ->line($this->buildDescription())
 | 
			
		||||
            ->line(__("Vacation type: :type", [
 | 
			
		||||
                "type" => $type,
 | 
			
		||||
            ]))
 | 
			
		||||
            ->line(__("From :from to :to (number of days: :days)", [
 | 
			
		||||
                "from" => $from,
 | 
			
		||||
                "to" => $to,
 | 
			
		||||
                "days" => $days,
 | 
			
		||||
            ]))
 | 
			
		||||
            ->line(
 | 
			
		||||
                __("Vacation type: :type", [
 | 
			
		||||
                    "type" => $type,
 | 
			
		||||
                ]),
 | 
			
		||||
            )
 | 
			
		||||
            ->line(
 | 
			
		||||
                __("From :from to :to (number of days: :days)", [
 | 
			
		||||
                    "from" => $from,
 | 
			
		||||
                    "to" => $to,
 | 
			
		||||
                    "days" => $days,
 | 
			
		||||
                ]),
 | 
			
		||||
            )
 | 
			
		||||
            ->action(__("Click here for details"), $url);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@ use Illuminate\Notifications\Notification;
 | 
			
		||||
use InvalidArgumentException;
 | 
			
		||||
use Toby\Eloquent\Models\User;
 | 
			
		||||
use Toby\Eloquent\Models\VacationRequest;
 | 
			
		||||
use Toby\Infrastructure\Slack\Elements\SlackMessage;
 | 
			
		||||
 | 
			
		||||
class VacationRequestStatusChangedNotification extends Notification
 | 
			
		||||
{
 | 
			
		||||
@@ -25,14 +26,12 @@ class VacationRequestStatusChangedNotification extends Notification
 | 
			
		||||
        return [Channels::MAIL, Channels::SLACK];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function toSlack(): string
 | 
			
		||||
    public function toSlack(): SlackMessage
 | 
			
		||||
    {
 | 
			
		||||
        $url = route("vacation.requests.show", ["vacationRequest" => $this->vacationRequest->id]);
 | 
			
		||||
 | 
			
		||||
        return implode("\n", [
 | 
			
		||||
            $this->buildDescription(),
 | 
			
		||||
            "<${url}|Zobacz szczegóły>",
 | 
			
		||||
        ]);
 | 
			
		||||
        return (new SlackMessage())
 | 
			
		||||
            ->text("{$this->buildDescription()}\n <${url}|Zobacz szczegóły>");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -11,6 +11,7 @@ use InvalidArgumentException;
 | 
			
		||||
use Toby\Domain\States\VacationRequest\WaitingForTechnical;
 | 
			
		||||
use Toby\Eloquent\Models\User;
 | 
			
		||||
use Toby\Eloquent\Models\VacationRequest;
 | 
			
		||||
use Toby\Infrastructure\Slack\Elements\SlackMessage;
 | 
			
		||||
 | 
			
		||||
class VacationRequestWaitsForApprovalNotification extends Notification
 | 
			
		||||
{
 | 
			
		||||
@@ -26,14 +27,12 @@ class VacationRequestWaitsForApprovalNotification extends Notification
 | 
			
		||||
        return [Channels::MAIL, Channels::SLACK];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function toSlack(): string
 | 
			
		||||
    public function toSlack(): SlackMessage
 | 
			
		||||
    {
 | 
			
		||||
        $url = route("vacation.requests.show", ["vacationRequest" => $this->vacationRequest->id]);
 | 
			
		||||
 | 
			
		||||
        return implode("\n", [
 | 
			
		||||
            $this->buildDescription(),
 | 
			
		||||
            "<${url}|Zobacz szczegóły>",
 | 
			
		||||
        ]);
 | 
			
		||||
        return (new SlackMessage())
 | 
			
		||||
            ->text("{$this->buildDescription()}\n <${url}|Zobacz szczegóły>");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,8 @@ use Illuminate\Notifications\Notification;
 | 
			
		||||
use Illuminate\Support\Carbon;
 | 
			
		||||
use Illuminate\Support\Collection;
 | 
			
		||||
use Toby\Eloquent\Models\User;
 | 
			
		||||
use Toby\Infrastructure\Slack\Elements\SlackMessage;
 | 
			
		||||
use Toby\Infrastructure\Slack\Elements\VacationRequestsAttachment;
 | 
			
		||||
 | 
			
		||||
class VacationRequestsSummaryNotification extends Notification
 | 
			
		||||
{
 | 
			
		||||
@@ -22,7 +24,14 @@ class VacationRequestsSummaryNotification extends Notification
 | 
			
		||||
 | 
			
		||||
    public function via(): array
 | 
			
		||||
    {
 | 
			
		||||
        return ["mail"];
 | 
			
		||||
        return [Channels::MAIL, Channels::SLACK];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function toSlack(): SlackMessage
 | 
			
		||||
    {
 | 
			
		||||
        return (new SlackMessage())
 | 
			
		||||
            ->text("Lista wniosków oczekujących na Twoją akcję - stan na dzień {$this->day->toDisplayString()}:")
 | 
			
		||||
            ->withAttachment(new VacationRequestsAttachment($this->vacationRequests));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function toMail($notifiable): MailMessage
 | 
			
		||||
 
 | 
			
		||||
@@ -17,11 +17,12 @@ class SlackApiChannel
 | 
			
		||||
        $url = "{$baseUrl}/chat.postMessage";
 | 
			
		||||
        $channel = $notifiable->routeNotificationFor("slack", $notification);
 | 
			
		||||
 | 
			
		||||
        $message = $notification->toSlack($notifiable);
 | 
			
		||||
 | 
			
		||||
        return Http::withToken($this->getClientToken())
 | 
			
		||||
            ->post($url, [
 | 
			
		||||
            ->post($url, array_merge($message->getPayload(), [
 | 
			
		||||
                "channel" => $channel,
 | 
			
		||||
                "text" => $notification->toSlack($notifiable),
 | 
			
		||||
            ]);
 | 
			
		||||
            ]));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function getClientToken(): string
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										53
									
								
								app/Infrastructure/Slack/Elements/SlackMessage.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								app/Infrastructure/Slack/Elements/SlackMessage.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Toby\Infrastructure\Slack\Elements;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Support\Collection;
 | 
			
		||||
 | 
			
		||||
class SlackMessage
 | 
			
		||||
{
 | 
			
		||||
    protected string $text = "";
 | 
			
		||||
    protected Collection $attachments;
 | 
			
		||||
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        $this->attachments = new Collection();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function text(string $text): static
 | 
			
		||||
    {
 | 
			
		||||
        $this->text = $text;
 | 
			
		||||
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function withAttachment(Attachment $attachment): static
 | 
			
		||||
    {
 | 
			
		||||
        $this->attachments->push($attachment);
 | 
			
		||||
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function withAttachments(Collection $attachments): static
 | 
			
		||||
    {
 | 
			
		||||
        foreach ($attachments as $attachment) {
 | 
			
		||||
            $this->withAttachment($attachment);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getPayload(): array
 | 
			
		||||
    {
 | 
			
		||||
        return [
 | 
			
		||||
            "text" => $this->text,
 | 
			
		||||
            "link_names" => true,
 | 
			
		||||
            "unfurl_links" => true,
 | 
			
		||||
            "unfurl_media" => true,
 | 
			
		||||
            "mrkdwn" => true,
 | 
			
		||||
            "attachments" => $this->attachments->toArray(),
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,33 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Toby\Infrastructure\Slack\Elements;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Support\Collection;
 | 
			
		||||
use Toby\Eloquent\Models\VacationRequest;
 | 
			
		||||
 | 
			
		||||
class VacationRequestsAttachment extends ListAttachment
 | 
			
		||||
{
 | 
			
		||||
    public function __construct(Collection $vacationRequests)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct();
 | 
			
		||||
 | 
			
		||||
        $this
 | 
			
		||||
            ->setColor("#527aba")
 | 
			
		||||
            ->setItems($this->mapVacationRequests($vacationRequests));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function mapVacationRequests(Collection $vacationRequests): Collection
 | 
			
		||||
    {
 | 
			
		||||
        return $vacationRequests->map(function (VacationRequest $request): string {
 | 
			
		||||
            $url = route("vacation.requests.show", ["vacationRequest" => $request->id]);
 | 
			
		||||
 | 
			
		||||
            $date = $request->from->equalTo($request->to)
 | 
			
		||||
                ? "{$request->from->toDisplayString()}"
 | 
			
		||||
                : "{$request->from->toDisplayString()} - {$request->to->toDisplayString()}";
 | 
			
		||||
 | 
			
		||||
            return "<{$url}|Wniosek nr {$request->name}> użytkownika {$request->user->profile->full_name} ({$date})";
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user