#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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 407 additions and 69 deletions

View File

@ -60,3 +60,8 @@ GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT=http://localhost/login/google/end
GOOGLE_CALENDAR_ID=
LOCAL_EMAIL_FOR_LOGIN_VIA_GOOGLE=
SLACK_URL=https://slack.com/api
SLACK_CLIENT_TOKEN=
SLACK_SIGNING_SECRET=
SLACK_DEFAULT_CHANNEL="#general"

View File

@ -6,10 +6,10 @@ namespace Toby\Domain\Actions\VacationRequest;
use Illuminate\Validation\ValidationException;
use Toby\Domain\Notifications\VacationRequestCreatedNotification;
use Toby\Domain\VacationDaysCalculator;
use Toby\Domain\VacationRequestStateManager;
use Toby\Domain\VacationTypeConfigRetriever;
use Toby\Domain\Validation\VacationRequestValidator;
use Toby\Domain\WorkDaysCalculator;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationRequest;
@ -19,7 +19,7 @@ class CreateAction
protected VacationRequestStateManager $stateManager,
protected VacationRequestValidator $vacationRequestValidator,
protected VacationTypeConfigRetriever $configRetriever,
protected VacationDaysCalculator $vacationDaysCalculator,
protected WorkDaysCalculator $workDaysCalculator,
protected WaitForTechApprovalAction $waitForTechApprovalAction,
protected WaitForAdminApprovalAction $waitForAdminApprovalAction,
protected ApproveAction $approveAction,
@ -52,10 +52,7 @@ class CreateAction
$vacationRequest->save();
$days = $this->vacationDaysCalculator->calculateDays(
$vacationRequest->from,
$vacationRequest->to,
);
$days = $this->workDaysCalculator->calculateDays($vacationRequest->from, $vacationRequest->to);
foreach ($days as $day) {
$vacationRequest->vacations()->create([

View File

@ -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", [
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

View File

@ -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", [
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

View File

@ -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!", [
->greeting(
__("Hi :user!", [
"user" => $user,
]))
]),
)
->subject($this->buildSubject())
->line($this->buildDescription())
->line(__("Vacation type: :type", [
->line(
__("Vacation type: :type", [
"type" => $type,
]))
->line(__("From :from to :to (number of days: :days)", [
]),
)
->line(
__("From :from to :to (number of days: :days)", [
"from" => $from,
"to" => $to,
"days" => $days,
]))
]),
)
->action(__("Click here for details"), $url);
}

View File

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

View File

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

View File

@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Toby\Domain\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Toby\Infrastructure\Slack\Elements\SlackMessage;
use Toby\Infrastructure\Slack\Elements\VacationRequestsAttachment;
class VacationRequestsSummaryNotification extends Notification
{
use Queueable;
public function __construct(
protected Carbon $day,
protected Collection $vacationRequests,
) {}
public function via(): array
{
return [Channels::MAIL, Channels::SLACK];
}
public function toSlack(): SlackMessage
{
return (new SlackMessage())
->text("Wnioski oczekujące na Twoją akcję - stan na dzień {$this->day->toDisplayString()}:")
->withAttachment(new VacationRequestsAttachment($this->vacationRequests));
}
public function toMail(Notifiable $notifiable): MailMessage
{
$url = route(
"vacation.requests.indexForApprovers",
[
"status" => "waiting_for_action",
],
);
return $this->buildMailMessage($notifiable, $url);
}
protected function buildMailMessage(Notifiable $notifiable, string $url): MailMessage
{
$user = $notifiable->profile->first_name;
$message = (new MailMessage())
->greeting(
__("Hi :user!", [
"user" => $user,
]),
)
->line("Lista wniosków oczekujących na Twoją akcję - stan na dzień {$this->day->toDisplayString()}:")
->subject("Wnioski oczekujące na akcje - stan na dzień {$this->day->toDisplayString()}");
foreach ($this->vacationRequests as $request) {
$message->line(
"Wniosek nr {$request->name} użytkownika {$request->user->profile->full_name} ({$request->from->toDisplayString()} - {$request->to->toDisplayString()})",
);
}
return $message
->action("Przejdź do wniosków", $url);
}
}

View File

@ -7,9 +7,9 @@ namespace Toby\Domain\Validation\Rules;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Toby\Domain\Enums\VacationType;
use Toby\Domain\VacationDaysCalculator;
use Toby\Domain\VacationRequestStatesRetriever;
use Toby\Domain\VacationTypeConfigRetriever;
use Toby\Domain\WorkDaysCalculator;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationRequest;
use Toby\Eloquent\Models\YearPeriod;
@ -18,7 +18,7 @@ class DoesNotExceedLimitRule implements VacationRequestRule
{
public function __construct(
protected VacationTypeConfigRetriever $configRetriever,
protected VacationDaysCalculator $vacationDaysCalculator,
protected WorkDaysCalculator $workDaysCalculator,
) {}
public function check(VacationRequest $vacationRequest): bool
@ -29,7 +29,9 @@ class DoesNotExceedLimitRule implements VacationRequestRule
$limit = $this->getUserVacationLimit($vacationRequest->user, $vacationRequest->yearPeriod);
$vacationDays = $this->getVacationDaysWithLimit($vacationRequest->user, $vacationRequest->yearPeriod);
$estimatedDays = $this->vacationDaysCalculator->calculateDays($vacationRequest->from, $vacationRequest->to)->count();
$estimatedDays = $this->workDaysCalculator
->calculateDays($vacationRequest->from, $vacationRequest->to)
->count();
return $limit >= ($vacationDays + $estimatedDays);
}

View File

@ -4,18 +4,18 @@ declare(strict_types=1);
namespace Toby\Domain\Validation\Rules;
use Toby\Domain\VacationDaysCalculator;
use Toby\Domain\WorkDaysCalculator;
use Toby\Eloquent\Models\VacationRequest;
class MinimumOneVacationDayRule implements VacationRequestRule
{
public function __construct(
protected VacationDaysCalculator $vacationDaysCalculator,
protected WorkDaysCalculator $workDaysCalculator,
) {}
public function check(VacationRequest $vacationRequest): bool
{
return $this->vacationDaysCalculator
return $this->workDaysCalculator
->calculateDays($vacationRequest->from, $vacationRequest->to)
->isNotEmpty();
}

View File

@ -9,7 +9,7 @@ use Carbon\CarbonPeriod;
use Illuminate\Support\Collection;
use Toby\Eloquent\Models\YearPeriod;
class VacationDaysCalculator
class WorkDaysCalculator
{
public function calculateDays(CarbonInterface $from, CarbonInterface $to): Collection
{

View File

@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Spatie\ModelStates\HasStates;
@ -84,6 +85,13 @@ class VacationRequest extends Model
return $query->whereNotState("state", $states);
}
public function scopeType(Builder $query, VacationType|array $types): Builder
{
$types = Arr::wrap($types);
return $query->whereIn("type", $types);
}
public function scopeOverlapsWith(Builder $query, self $vacationRequest): Builder
{
return $query->where("from", "<=", $vacationRequest->to)

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

View File

@ -7,7 +7,7 @@ namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
use Toby\Domain\PolishHolidaysRetriever;
use Toby\Domain\VacationDaysCalculator;
use Toby\Domain\WorkDaysCalculator;
use Toby\Eloquent\Models\Key;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationLimit;
@ -70,8 +70,7 @@ class DatabaseSeeder extends Seeder
"year_period_id" => $yearPeriods->random()->id,
])
->afterCreating(function (VacationRequest $vacationRequest): void {
$days = app(VacationDaysCalculator::class)->calculateDays(
$vacationRequest->yearPeriod,
$days = app(WorkDaysCalculator::class)->calculateDays(
$vacationRequest->from,
$vacationRequest->to,
);

View File

@ -18,7 +18,7 @@ use Toby\Domain\States\VacationRequest\Created;
use Toby\Domain\States\VacationRequest\Rejected;
use Toby\Domain\States\VacationRequest\WaitingForAdministrative;
use Toby\Domain\States\VacationRequest\WaitingForTechnical;
use Toby\Domain\VacationDaysCalculator;
use Toby\Domain\WorkDaysCalculator;
use Toby\Eloquent\Models\Key;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationLimit;
@ -164,8 +164,7 @@ class DemoSeeder extends Seeder
->for($user, "creator")
->for($currentYearPeriod)
->afterCreating(function (VacationRequest $vacationRequest): void {
$days = app(VacationDaysCalculator::class)->calculateDays(
$vacationRequest->yearPeriod,
$days = app(WorkDaysCalculator::class)->calculateDays(
$vacationRequest->from,
$vacationRequest->to,
);
@ -234,8 +233,7 @@ class DemoSeeder extends Seeder
->for($user, "creator")
->for($currentYearPeriod)
->afterCreating(function (VacationRequest $vacationRequest): void {
$days = app(VacationDaysCalculator::class)->calculateDays(
$vacationRequest->yearPeriod,
$days = app(WorkDaysCalculator::class)->calculateDays(
$vacationRequest->from,
$vacationRequest->to,
);
@ -291,8 +289,7 @@ class DemoSeeder extends Seeder
->for($user, "creator")
->for($currentYearPeriod)
->afterCreating(function (VacationRequest $vacationRequest): void {
$days = app(VacationDaysCalculator::class)->calculateDays(
$vacationRequest->yearPeriod,
$days = app(WorkDaysCalculator::class)->calculateDays(
$vacationRequest->from,
$vacationRequest->to,
);

View File

@ -77,7 +77,7 @@
</dt>
<dd
v-if="request.comment != null"
class="mt-1 text-sm text-gray-900 sm:col-span-2 sm:mt-0"
class="mt-1 text-sm text-gray-900 break-all sm:col-span-2 sm:mt-0"
>
{{ request.comment }}
</dd>

View File

@ -7,9 +7,9 @@ use Toby\Infrastructure\Http\Controllers\Api\CalculateUserUnavailableDaysControl
use Toby\Infrastructure\Http\Controllers\Api\CalculateUserVacationStatsController;
use Toby\Infrastructure\Http\Controllers\Api\CalculateVacationDaysController;
use Toby\Infrastructure\Http\Controllers\Api\GetAvailableVacationTypesController;
use Toby\Infrastructure\Slack\Controller as SlackController;
use Toby\Infrastructure\Slack\Controller as SlackCommandController;
Route::post("slack", [SlackController::class, "getResponse"]);
Route::post("slack", [SlackCommandController::class, "getResponse"]);
Route::middleware("auth:sanctum")->group(function (): void {
Route::post("vacation/calculate-days", CalculateVacationDaysController::class);

View File

@ -0,0 +1,130 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
use Tests\Traits\InteractsWithYearPeriods;
use Toby\Domain\Enums\Role;
use Toby\Domain\Notifications\VacationRequestsSummaryNotification;
use Toby\Domain\States\VacationRequest\Approved;
use Toby\Domain\States\VacationRequest\Cancelled;
use Toby\Domain\States\VacationRequest\Created;
use Toby\Domain\States\VacationRequest\Rejected;
use Toby\Domain\States\VacationRequest\WaitingForTechnical;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationRequest;
use Toby\Eloquent\Models\YearPeriod;
use Toby\Infrastructure\Console\Commands\SendVacationRequestSummariesToApprovers;
class SendVacationRequestSummariesTest extends TestCase
{
use RefreshDatabase;
use InteractsWithYearPeriods;
protected function setUp(): void
{
parent::setUp();
Notification::fake();
$this->createCurrentYearPeriod();
}
public function testSummariesAreSentOnlyToProperApprovers(): void
{
$currentYearPeriod = YearPeriod::current();
$user = User::factory([
"role" => Role::Employee,
])->create();
$technicalApprover = User::factory([
"role" => Role::TechnicalApprover,
])->create();
$administrativeApprover = User::factory([
"role" => Role::AdministrativeApprover,
])->create();
$admin = User::factory([
"role" => Role::Administrator,
])->create();
VacationRequest::factory()
->for($user)
->for($currentYearPeriod)
->create(["state" => WaitingForTechnical::class]);
$this->artisan(SendVacationRequestSummariesToApprovers::class)
->execute();
Notification::assertSentTo([$technicalApprover, $admin], VacationRequestsSummaryNotification::class);
Notification::assertNotSentTo([$user, $administrativeApprover], VacationRequestsSummaryNotification::class);
}
public function testSummariesAreSentOnlyIfVacationRequestWaitingForActionExists(): void
{
$currentYearPeriod = YearPeriod::current();
$user = User::factory([
"role" => Role::Employee,
])->create();
$technicalApprover = User::factory([
"role" => Role::TechnicalApprover,
])->create();
$admin = User::factory([
"role" => Role::Administrator,
])->create();
VacationRequest::factory()
->for($user)
->for($currentYearPeriod)
->create(["state" => WaitingForTechnical::class]);
$this->artisan(SendVacationRequestSummariesToApprovers::class)
->execute();
Notification::assertSentTo([$technicalApprover, $admin], VacationRequestsSummaryNotification::class);
Notification::assertNotSentTo([$user], VacationRequestsSummaryNotification::class);
}
public function testSummariesAreNotSentIfThereAreNoWaitingForActionVacationRequests(): void
{
$currentYearPeriod = YearPeriod::current();
$user = User::factory([
"role" => Role::Employee,
])->create();
$technicalApprover = User::factory([
"role" => Role::TechnicalApprover,
])->create();
$admin = User::factory([
"role" => Role::Administrator,
])->create();
VacationRequest::factory()
->for($user)
->for($currentYearPeriod)
->create(["state" => Approved::class]);
VacationRequest::factory()
->for($user)
->for($currentYearPeriod)
->create(["state" => Cancelled::class]);
VacationRequest::factory()
->for($user)
->for($currentYearPeriod)
->create(["state" => Rejected::class]);
VacationRequest::factory()
->for($user)
->for($currentYearPeriod)
->create(["state" => Created::class]);
$this->artisan(SendVacationRequestSummariesToApprovers::class)
->execute();
Notification::assertNotSentTo([$user, $technicalApprover, $admin], VacationRequestsSummaryNotification::class);
}
}