Wniosek o urlop
+Wniosek
Proszę o {{ mb_strtolower($vacationRequest->type->label()) }} w okresie od dnia {{ $vacationRequest->from->format("d.m.Y") }} do dnia {{ $vacationRequest->to->format("d.m.Y") }} włącznie tj. {{ $vacationRequest->vacations()->count() }} dni roboczych za rok {{ $vacationRequest->yearPeriod->year }}. diff --git a/routes/api.php b/routes/api.php index d76094c..7fa9ef1 100644 --- a/routes/api.php +++ b/routes/api.php @@ -7,6 +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 SlackCommandController; + +Route::post("slack", [SlackCommandController::class, "getResponse"]); Route::middleware("auth:sanctum")->group(function (): void { Route::post("vacation/calculate-days", CalculateVacationDaysController::class); diff --git a/tests/Feature/KeyTest.php b/tests/Feature/KeyTest.php index e9c3ddc..2b1c329 100644 --- a/tests/Feature/KeyTest.php +++ b/tests/Feature/KeyTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Tests\Feature; use Illuminate\Foundation\Testing\DatabaseMigrations; +use Illuminate\Support\Facades\Notification; use Inertia\Testing\AssertableInertia as Assert; use Tests\FeatureTestCase; use Toby\Eloquent\Models\Key; @@ -14,6 +15,13 @@ class KeyTest extends FeatureTestCase { use DatabaseMigrations; + protected function setUp(): void + { + parent::setUp(); + + Notification::fake(); + } + public function testUserCanSeeKeyList(): void { Key::factory()->count(10)->create(); diff --git a/tests/Unit/SendDailySummaryToSlackTest.php b/tests/Unit/SendDailySummaryToSlackTest.php new file mode 100644 index 0000000..93be6bf --- /dev/null +++ b/tests/Unit/SendDailySummaryToSlackTest.php @@ -0,0 +1,82 @@ +createCurrentYearPeriod(); + } + + public function testCommandSendsMessageToSlackIfWeekday(): void + { + $weekDay = Carbon::create(2022, 4, 22); + $this->assertTrue($weekDay->isWeekday()); + + $this->travelTo($weekDay); + + $this->artisan(SendDailySummaryToSlack::class) + ->execute(); + + Http::assertSentCount(1); + } + + public function testCommandDoesntSendMessageIfWeekend(): void + { + $weekend = Carbon::create(2022, 4, 23); + $this->assertTrue($weekend->isWeekend()); + + $this->travelTo($weekend); + + $this->artisan(SendDailySummaryToSlack::class) + ->execute(); + + Http::assertNothingSent(); + } + + public function testCommandDoesntSendMessageIfHoliday(): void + { + $holiday = Holiday::factory(["date" => Carbon::create(2022, 4, 22)])->create(); + + $this->assertDatabaseHas("holidays", [ + "date" => $holiday->date->toDateString(), + ]); + + $this->travelTo(Carbon::create(2022, 4, 22)); + + $this->artisan(SendDailySummaryToSlack::class) + ->execute(); + + Http::assertNothingSent(); + } + + public function testCommandForceSendsMessageEvenIsWeekendOrHoliday(): void + { + $weekend = Carbon::create(2022, 4, 23); + $this->assertTrue($weekend->isWeekend()); + + $this->travelTo($weekend); + + $this->artisan(SendDailySummaryToSlack::class, ["--force" => true]) + ->execute(); + + Http::assertSentCount(1); + } +} diff --git a/tests/Unit/SendVacationRequestSummariesTest.php b/tests/Unit/SendVacationRequestSummariesTest.php new file mode 100644 index 0000000..97370bd --- /dev/null +++ b/tests/Unit/SendVacationRequestSummariesTest.php @@ -0,0 +1,130 @@ +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); + } +}