-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #465 - feat: added employee notification about upcoming medical exam or ohs training * #465 - fix: fixed channel * #465 - fix: fixed slack messages * #465 - feat: added some tests * #465 - fix: code review fixes
- Loading branch information
1 parent
ace38c4
commit 54ad119
Showing
8 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
app/Console/Commands/SendNotificationAboutUpcomingMedicalExamsForEmployees.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Toby\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Carbon; | ||
use Toby\Enums\UserHistoryType; | ||
use Toby\Models\User; | ||
use Toby\Notifications\UpcomingMedicalExamForEmployeeNotification; | ||
|
||
class SendNotificationAboutUpcomingMedicalExamsForEmployees extends Command | ||
{ | ||
protected $signature = "toby:send-notification-about-medical-exams-for-employees"; | ||
protected $description = "Send notifications about upcoming medical exams."; | ||
|
||
public function handle(): void | ||
{ | ||
$usersUpcomingMedicalExams = User::query() | ||
->whereRelation("histories", function ($query): void { | ||
$query->where("type", UserHistoryType::MedicalExam) | ||
->where("to", ">", Carbon::now()) | ||
->where("to", "<=", Carbon::now()->addMonth()); | ||
})->get(); | ||
|
||
if ($usersUpcomingMedicalExams->isEmpty()) { | ||
return; | ||
} | ||
|
||
foreach ($usersUpcomingMedicalExams as $user) { | ||
$user->notify(new UpcomingMedicalExamForEmployeeNotification($user)); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/Console/Commands/SendNotificationAboutUpcomingOhsTrainingsForEmployees.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Toby\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Carbon; | ||
use Toby\Enums\UserHistoryType; | ||
use Toby\Models\User; | ||
use Toby\Notifications\UpcomingOhsTrainingForEmployeeNotification; | ||
|
||
class SendNotificationAboutUpcomingOhsTrainingsForEmployees extends Command | ||
{ | ||
protected $signature = "toby:send-notification-about-ohs-trainings-for-employees"; | ||
protected $description = "Send notifications about upcoming ohs trainings."; | ||
|
||
public function handle(): void | ||
{ | ||
$usersUpcomingOhsTrainings = User::query() | ||
->whereRelation("histories", function ($query): void { | ||
$query->where("type", UserHistoryType::OhsTraining) | ||
->where("to", ">", Carbon::now()) | ||
->where("to", "<=", Carbon::now()->addMonth()); | ||
})->get(); | ||
|
||
if ($usersUpcomingOhsTrainings->isEmpty()) { | ||
return; | ||
} | ||
|
||
foreach ($usersUpcomingOhsTrainings as $user) { | ||
$user->notify(new UpcomingOhsTrainingForEmployeeNotification($user)); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
app/Notifications/UpcomingMedicalExamForEmployeeNotification.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Toby\Notifications; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Support\Carbon; | ||
use Toby\Models\User; | ||
use Toby\Slack\Elements\SlackMessage; | ||
|
||
class UpcomingMedicalExamForEmployeeNotification extends QueuedNotification | ||
{ | ||
use Queueable; | ||
|
||
public function __construct( | ||
protected User $user, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public function via(): array | ||
{ | ||
return [Channels::SLACK]; | ||
} | ||
|
||
public function toSlack(): SlackMessage | ||
{ | ||
$lastMedicalExamDate = $this->user->lastMedicalExam(); | ||
|
||
return (new SlackMessage()) | ||
->text(__("The deadline for occupational medical examinations for you is about to expire - :date (:difference days)", [ | ||
"date" => $lastMedicalExamDate->to->toDisplayString(), | ||
"difference" => (int)$lastMedicalExamDate->to->diffInDays(Carbon::today(), true), | ||
])); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
app/Notifications/UpcomingOhsTrainingForEmployeeNotification.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Toby\Notifications; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Support\Carbon; | ||
use Toby\Models\User; | ||
use Toby\Slack\Elements\SlackMessage; | ||
|
||
class UpcomingOhsTrainingForEmployeeNotification extends QueuedNotification | ||
{ | ||
use Queueable; | ||
|
||
public function __construct( | ||
protected User $user, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public function via(): array | ||
{ | ||
return [Channels::SLACK]; | ||
} | ||
|
||
public function toSlack(): SlackMessage | ||
{ | ||
$lastOhsTrainingDate = $this->user->lastOhsTraining(); | ||
|
||
return (new SlackMessage()) | ||
->text(__("The deadline for occupational ohs training for you is about to expire - :date (:difference days)", [ | ||
"date" => $lastOhsTrainingDate->to->toDisplayString(), | ||
"difference" => (int)$lastOhsTrainingDate->to->diffInDays(Carbon::today(), true), | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
tests/Unit/SendNotificationAboutUpcomingMedicalExamsToSlackTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Unit; | ||
|
||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Facades\Notification; | ||
use Tests\TestCase; | ||
use Tests\Traits\InteractsWithYearPeriods; | ||
use Toby\Console\Commands\SendNotificationAboutUpcomingMedicalExamsForEmployees; | ||
use Toby\Models\User; | ||
use Toby\Notifications\UpcomingMedicalExamForEmployeeNotification; | ||
|
||
class SendNotificationAboutUpcomingMedicalExamsToSlackTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
use InteractsWithYearPeriods; | ||
|
||
protected User $user; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Notification::fake(); | ||
$this->createCurrentYearPeriod(); | ||
$this->user = User::factory()->employee()->create(); | ||
} | ||
|
||
public function testNotificationIsSentToUserWithUpcomingMedicalExams(): void | ||
{ | ||
$this->user->histories()->create([ | ||
"from" => Carbon::createFromDate(2022, 1, 1), | ||
"to" => Carbon::now()->addDays(14), | ||
"type" => "medical_exam", | ||
]); | ||
|
||
$this->artisan(SendNotificationAboutUpcomingMedicalExamsForEmployees::class) | ||
->execute(); | ||
|
||
Notification::assertSentTo($this->user, UpcomingMedicalExamForEmployeeNotification::class); | ||
} | ||
|
||
public function testNotificationIsNotSentToUserWithoutUpcomingMedicalExams(): void | ||
{ | ||
$this->user->histories()->create([ | ||
"from" => Carbon::createFromDate(2022, 1, 1), | ||
"to" => Carbon::now()->addYear(), | ||
"type" => "medical_exam", | ||
]); | ||
|
||
$this->artisan(SendNotificationAboutUpcomingMedicalExamsForEmployees::class) | ||
->execute(); | ||
|
||
Notification::assertNothingSent(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/Unit/SendNotificationAboutUpcomingOhsTrainingsToSlackTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Unit; | ||
|
||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Support\Facades\Notification; | ||
use Illuminate\Support\Str; | ||
use Tests\TestCase; | ||
use Tests\Traits\InteractsWithYearPeriods; | ||
use Toby\Console\Commands\SendNotificationAboutUpcomingOhsTrainingsForEmployees; | ||
use Toby\Models\User; | ||
use Toby\Notifications\UpcomingOhsTrainingForEmployeeNotification; | ||
|
||
class SendNotificationAboutUpcomingOhsTrainingsToSlackTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
use InteractsWithYearPeriods; | ||
|
||
protected User $user; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Notification::fake(); | ||
$this->createCurrentYearPeriod(); | ||
Http::fake(fn(): array => [ | ||
"channel" => Str::random(8), | ||
"message" => ["ts" => Carbon::now()->toDateTimeString()], | ||
]); | ||
|
||
$this->user = User::factory()->employee()->create(); | ||
} | ||
|
||
public function testNotificationIsSentToUserWithUpcomingMedicalExams(): void | ||
{ | ||
$this->user->histories()->create([ | ||
"from" => Carbon::createFromDate(2022, 1, 1), | ||
"to" => Carbon::now()->addDays(14), | ||
"type" => "ohs_training", | ||
]); | ||
|
||
$this->artisan(SendNotificationAboutUpcomingOhsTrainingsForEmployees::class) | ||
->execute(); | ||
|
||
Notification::assertSentTo($this->user, UpcomingOhsTrainingForEmployeeNotification::class); | ||
} | ||
|
||
public function testNotificationIsNotSentToUserWithoutUpcomingMedicalExams(): void | ||
{ | ||
$this->user->histories()->create([ | ||
"from" => Carbon::createFromDate(2022, 1, 1), | ||
"to" => Carbon::now()->addYear(), | ||
"type" => "ohs_training", | ||
]); | ||
|
||
$this->artisan(SendNotificationAboutUpcomingOhsTrainingsForEmployees::class) | ||
->execute(); | ||
|
||
Notification::assertNothingSent(); | ||
} | ||
} |