Skip to content

Commit

Permalink
#465 - feat: added employee notification about upcoming medical exam …
Browse files Browse the repository at this point in the history
…or ohs training
  • Loading branch information
kamilpiech97 committed Jul 16, 2024
1 parent ace38c4 commit 092ecc9
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
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));
}
}
}
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 SendNotificationAboutUpcomingOhsTrainingForEmployees extends Command
{
protected $signature = "toby:send-notification-about-ohs-training-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 app/Notifications/UpcomingMedicalExamForEmployeeNotification.php
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::MAIL];
}

public function toSlack(): SlackMessage
{
$lastMedicalExamDate = $this->user->lastMedicalExam();

return (new SlackMessage())
->text(__("The deadline for occupational health examinations for you is about to expire - :date (overdue :difference days)", [
"date" => $lastMedicalExamDate->to->toDisplayString(),
"difference" => (int)$lastMedicalExamDate->to->diffInDays(Carbon::today(), true),
]));
}
}
37 changes: 37 additions & 0 deletions app/Notifications/UpcomingOhsTrainingForEmployeeNotification.php
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::MAIL];
}

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 (overdue :difference days)", [
"date" => $lastOhsTrainingDate->to->toDisplayString(),
"difference" => (int)$lastOhsTrainingDate->to->diffInDays(Carbon::today(), true),
]));
}
}
2 changes: 2 additions & 0 deletions lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
"The request :title is waiting for your administrative approval.\nUser: :requester\nDate: :date (number of days: :days)": "Wniosek :title czeka na Twoją akceptację administracyjną.\nPracownik: :requester\nData: :date (liczba dni: :days)",
"The request :title has been approved by you as a technical approver.\nUser: :requester\nDate: :date (number of days: :days)": "Wniosek :title został zaakceptowany przez Ciebie jako przełożonego technicznego.\nPracownik: :requester\nData: :date (liczba dni: :days)",
"The request :title has been approved by you as an administrative approver.\nUser: :requester\nDate: :date (number of days: :days)": "Wniosek :title został zaakceptowany przez Ciebie jako przełożonego administracyjnego.\nPracownik: :requester\nData: :date (liczba dni: :days)",
"The deadline for occupational ohs training for you is about to expire - :date (overdue :difference days)": "Niedługo mija termin szkolenia BHP dla Ciebie - :date (przeterminowane za :difference dni)",
"The deadline for occupational health examinations for you is about to expire - :date (overdue :difference days)": "Niedługo mija termin badań lekarskich z medycyny pracy dla Ciebie - :date (przeterminowane za :difference dni)",
"Request :title has been :status": "Wniosek :title został :status",
"Request :title in :application": "Wniosek :title w aplikacji :application",
"The request :title from user :requester has been :status.": "Wniosek :title użytkownika :requester został :status.",
Expand Down

0 comments on commit 092ecc9

Please sign in to comment.