Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#337 - add dates to medical exams and ohs #338

Merged
merged 11 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Toby\Domain\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;

class UpcomingAndOverdueMedicalExamsNotification extends QueuedNotification
{
use Queueable;

protected Collection $usersUpcomingMedicalExams;
protected Collection $usersOverdueMedicalExams;

public function __construct(
$usersUpcomingMedicalExams,
$usersOverdueMedicalExams,
) {
parent::__construct();

$this->usersUpcomingMedicalExams = $usersUpcomingMedicalExams;
$this->usersOverdueMedicalExams = $usersOverdueMedicalExams;
}

public function via(): array
{
return [Channels::MAIL];
}

public function toMail(Notifiable $notifiable): MailMessage
{
$url = route(
"users.index",
);

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,
]),
)
->subject(__("Occupational medicine examinations for employees"));

if ($this->usersUpcomingMedicalExams->isEmpty() && $this->usersOverdueMedicalExams->isEmpty()) {
$message->line(__("During the next two months, none of the employees have to go for medical examinations."));
}

if ($this->usersUpcomingMedicalExams->isNotEmpty()) {
$message->line(__("The deadline for occupational health examinations for some employees is about to expire."))
->line(__("Below is a list of employees with upcoming health examinations:"));

foreach ($this->usersUpcomingMedicalExams as $user) {
$message->line(__(":user - :date (in :difference days)", [
"user" => $user->profile->full_name,
"date" => $user->profile->next_medical_exam_date->toDisplayString(),
"difference" => $user->profile->next_medical_exam_date->diffInDays(Carbon::today()),
]));
}
}

if ($this->usersOverdueMedicalExams->isNotEmpty()) {
$message->line(__("The deadline for medical examinations for some employees has passed."))
->line(__("Below is a list of employees with overdue examinations:"));

foreach ($this->usersOverdueMedicalExams as $user) {
$message->line(__(":user - :date (overdue :difference days)", [
"user" => $user->profile->full_name,
"date" => $user->profile->next_medical_exam_date->toDisplayString(),
"difference" => $user->profile->next_medical_exam_date->diffInDays(Carbon::today()),
]));
}
}

return $message
->action(__("See list of users"), $url);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Toby\Domain\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;

class UpcomingAndOverdueOhsTrainingNotification extends QueuedNotification
{
use Queueable;

protected Collection $usersForUpcomingOhsTraining;
protected Collection $usersForOverdueOhsTraining;

public function __construct(
$usersForUpcomingOhsTraining,
$usersForOverdueOhsTraining,
) {
parent::__construct();

$this->usersForUpcomingOhsTraining = $usersForUpcomingOhsTraining;
$this->usersForOverdueOhsTraining = $usersForOverdueOhsTraining;
}

public function via(): array
{
return [Channels::MAIL];
}

public function toMail(Notifiable $notifiable): MailMessage
{
$url = route(
"users.index",
);

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,
]),
)
->subject(__("Health and safety training for employees"));

if ($this->usersForUpcomingOhsTraining->isEmpty() && $this->usersForOverdueOhsTraining->isEmpty()) {
$message->line(__("During the next two months, none of the employees have to go for OHS training."));
}

if ($this->usersForUpcomingOhsTraining->isNotEmpty()) {
$message->line(__("The deadline for OHS training for some employees is about to expire."))
->line(__("Below is a list of employees with upcoming OHS training:"));

foreach ($this->usersForUpcomingOhsTraining as $user) {
$message->line(__(":user - :date (in :difference days)", [
"user" => $user->profile->full_name,
"date" => $user->profile->next_ohs_training_date->toDisplayString(),
"difference" => $user->profile->next_ohs_training_date->diffInDays(Carbon::today()),
]));
}
}

if ($this->usersForOverdueOhsTraining->isNotEmpty()) {
$message->line(__("The deadline for OHS training for some employees has passed."))
->line(__("Below is a list of employees with overdue OHS training:"));

foreach ($this->usersForOverdueOhsTraining as $user) {
$message->line(__(":user - :date (overdue :difference days)", [
"user" => $user->profile->full_name,
"date" => $user->profile->next_ohs_training_date->toDisplayString(),
"difference" => $user->profile->next_ohs_training_date->diffInDays(Carbon::today()),
]));
}
}

return $message
->action(__("See list of users"), $url);
}
}
8 changes: 8 additions & 0 deletions app/Eloquent/Models/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
* @property EmploymentForm $employment_form
* @property Carbon $employment_date
* @property Carbon $birthday
* @property ?Carbon $last_medical_exam_date
* @property ?Carbon $next_medical_exam_date
* @property ?Carbon $last_ohs_training_date
* @property ?Carbon $next_ohs_training_date
*/
class Profile extends Model
{
Expand All @@ -33,6 +37,10 @@ class Profile extends Model
"employment_form" => EmploymentForm::class,
"employment_date" => "date",
"birthday" => "date",
"last_medical_exam_date" => "date",
"next_medical_exam_date" => "date",
"last_ohs_training_date" => "date",
"next_ohs_training_date" => "date",
];

public function user(): BelongsTo
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\UpcomingAndOverdueMedicalExamsNotification;
use Toby\Eloquent\Models\User;

class SendNotificationAboutUpcomingAndOverdueMedicalExams extends Command
{
protected $signature = "toby:send-notification-about-medical-exams";
protected $description = "Send notifications about upcoming and overdue medical exams.";

public function handle(): void
{
$usersToNotify = User::query()
->whereIn("role", [Role::AdministrativeApprover])
->get();

$usersUpcomingMedicalExams = User::query()
->whereRelation("profile", "next_medical_exam_date", ">", Carbon::now())
->whereRelation("profile", "next_medical_exam_date", "<=", Carbon::now()->addMonths(2))
->orderByProfileField("next_medical_exam_date", "desc")
->get();

$usersOverdueMedicalExams = User::query()
->whereRelation("profile", "next_medical_exam_date", "<=", Carbon::now())
->orderByProfileField("next_medical_exam_date", "desc")
->get();

if ($usersUpcomingMedicalExams->isEmpty() && $usersOverdueMedicalExams->isEmpty()) {
return;
}

foreach ($usersToNotify as $user) {
$user->notify(new UpcomingAndOverdueMedicalExamsNotification($usersUpcomingMedicalExams, $usersOverdueMedicalExams));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\UpcomingAndOverdueOhsTrainingNotification;
use Toby\Eloquent\Models\User;

class SendNotificationAboutUpcomingAndOverdueOhsTraining extends Command
{
protected $signature = "toby:send-notification-about-ohs-training";
protected $description = "Send notifications about upcoming and overdue OHS training.";

public function handle(): void
{
$usersToNotify = User::query()
->whereIn("role", [Role::AdministrativeApprover])
->get();

$usersForUpcomingOhsTraining = User::query()
->whereRelation("profile", "next_ohs_training_date", ">", Carbon::now())
->whereRelation("profile", "next_ohs_training_date", "<=", Carbon::now()->addMonths(2))
->orderByProfileField("next_ohs_training_date", "desc")
->get();

$usersForOverdueOhsTraining = User::query()
->whereRelation("profile", "next_ohs_training_date", "<=", Carbon::now())
->orderByProfileField("next_ohs_training_date", "desc")
->get();

if ($usersForUpcomingOhsTraining->isEmpty() && $usersForOverdueOhsTraining->isEmpty()) {
return;
}

foreach ($usersToNotify as $user) {
$user->notify(new UpcomingAndOverdueOhsTrainingNotification($usersForUpcomingOhsTraining, $usersForOverdueOhsTraining));
}
}
}
23 changes: 19 additions & 4 deletions app/Infrastructure/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Psr\Container\NotFoundExceptionInterface;
use Toby\Infrastructure\Console\Commands\Database\BackupPostgresDatabase;
use Toby\Infrastructure\Console\Commands\SendDailySummaryToSlack;
use Toby\Infrastructure\Console\Commands\SendNotificationAboutUpcomingAndOverdueMedicalExams;
use Toby\Infrastructure\Console\Commands\SendNotificationAboutUpcomingAndOverdueOhsTraining;
use Toby\Infrastructure\Console\Commands\SendVacationRequestSummariesToApprovers;
use Toby\Infrastructure\Jobs\CheckYearPeriod;

Expand All @@ -30,16 +32,29 @@ protected function schedule(Schedule $schedule): void
$schedule->command(SendDailySummaryToSlack::class)
->when(config("services.slack.enabled"))
->weekdays()
->dailyAt("08:00");
->dailyAt("08:00")
->onOneServer();

$schedule->command(SendVacationRequestSummariesToApprovers::class)
->weekdays()
->dailyAt("08:30");
->dailyAt("08:30")
->onOneServer();

$schedule->command(SendNotificationAboutUpcomingAndOverdueMedicalExams::class)
->monthlyOn(1, "08:00")
->onOneServer();

$schedule->command(SendNotificationAboutUpcomingAndOverdueOhsTraining::class)
->monthlyOn(1, "08:00")
->onOneServer();

$schedule->job(CheckYearPeriod::class)
->yearlyOn(1, 1, "01:00");
->yearlyOn(1, 1, "01:00")
->onOneServer();

$schedule->command(PruneStaleTagsCommand::class)->hourly();
$schedule->command(PruneStaleTagsCommand::class)
->hourly()
->onOneServer();

$this->scheduleDatabaseBackup($schedule);
}
Expand Down
6 changes: 6 additions & 0 deletions app/Infrastructure/Http/Requests/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function rules(): array
"employmentDate" => ["required", "date_format:Y-m-d"],
"birthday" => ["nullable", "date_format:Y-m-d"],
"slackId" => [],
"nextMedicalExamDate" => ["nullable", "after:lastMedicalExamDate"],
"nextOhsTrainingDate" => ["nullable", "after:lastOhsTrainingDate"],
];
}

Expand All @@ -45,6 +47,10 @@ public function profileData(): array
"employment_date" => $this->get("employmentDate"),
"birthday" => $this->get("birthday"),
"slack_id" => $this->get("slackId"),
"last_medical_exam_date" => $this->get("lastMedicalExamDate"),
"next_medical_exam_date" => $this->get("nextMedicalExamDate"),
"last_ohs_training_date" => $this->get("lastOhsTrainingDate"),
"next_ohs_training_date" => $this->get("nextOhsTrainingDate"),
];
}
}
4 changes: 4 additions & 0 deletions app/Infrastructure/Http/Resources/UserFormDataResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function toArray($request): array
"employmentDate" => $this->profile->employment_date->toDateString(),
"birthday" => $this->profile->birthday?->toDateString(),
"slackId" => $this->profile->slack_id,
"lastMedicalExamDate" => $this->profile->last_medical_exam_date?->toDateString(),
"nextMedicalExamDate" => $this->profile->next_medical_exam_date?->toDateString(),
"lastOhsTrainingDate" => $this->profile->last_ohs_training_date?->toDateString(),
"nextOhsTrainingDate" => $this->profile->next_ohs_training_date?->toDateString(),
];
}
}
4 changes: 4 additions & 0 deletions app/Infrastructure/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function toArray($request): array
"lastActiveAt" => $this->last_active_at?->toDateTimeString(),
"employmentForm" => $this->profile->employment_form->label(),
"employmentDate" => $this->profile->employment_date->toDisplayString(),
"lastMedicalExamDate" => $this->profile->last_medical_exam_date?->toDisplayString(),
"nextMedicalExamDate" => $this->profile->next_medical_exam_date?->toDisplayString(),
"lastOhsTrainingDate" => $this->profile->last_ohs_training_date?->toDisplayString(),
"nextOhsTrainingDate" => $this->profile->next_ohs_training_date?->toDisplayString(),
];
}
}
Loading