#126 - vacation request reminders (#130)

* #126 - vacation request reminders

* #126 - fix workdays

* #126 - changes

* #126 - cs fix

* #5 - bump codestyle

* #126 - fix

* #126 - fix

* #126 - fix

* #126 - fix

* #126 - tests

* #126 - fix

* #126 - fix

* #126 - fix seeders

* #126 - fix

* #126 - tests

Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
This commit is contained in:
Adrian Hopek
2022-04-27 15:26:55 +02:00
committed by GitHub
parent c69866bb52
commit 6b2556c1da
22 changed files with 407 additions and 69 deletions

View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Toby\Domain\Enums\Role;
use Toby\Domain\Notifications\VacationRequestsSummaryNotification;
use Toby\Domain\VacationRequestStatesRetriever;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationRequest;
class SendVacationRequestSummariesToApprovers extends Command
{
protected $signature = "toby:send-vacation-request-reminders";
protected $description = "Sends vacation request reminders to approvers if they didn't approve";
public function handle(): void
{
$users = User::query()
->whereIn("role", [Role::AdministrativeApprover, Role::TechnicalApprover, Role::Administrator])
->get();
foreach ($users as $user) {
$vacationRequests = VacationRequest::query()
->states(VacationRequestStatesRetriever::waitingForUserActionStates($user))
->get();
if ($vacationRequests->isNotEmpty()) {
$user->notify(new VacationRequestsSummaryNotification(Carbon::today(), $vacationRequests));
}
}
}
}

View File

@@ -6,13 +6,13 @@ namespace Toby\Infrastructure\Http\Controllers\Api;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Carbon;
use Toby\Domain\VacationDaysCalculator;
use Toby\Domain\WorkDaysCalculator;
use Toby\Infrastructure\Http\Controllers\Controller;
use Toby\Infrastructure\Http\Requests\Api\CalculateVacationDaysRequest;
class CalculateVacationDaysController extends Controller
{
public function __invoke(CalculateVacationDaysRequest $request, VacationDaysCalculator $calculator): JsonResponse
public function __invoke(CalculateVacationDaysRequest $request, WorkDaysCalculator $calculator): JsonResponse
{
$days = $calculator->calculateDays($request->from(), $request->to());

View File

@@ -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

View 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(),
];
}
}

View File

@@ -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})";
});
}
}