Skip to content

Commit

Permalink
#454 - reminder refactor (#455)
Browse files Browse the repository at this point in the history
* #443 - added tests for user history

* #443 - added console command for migrating data

* #443 - cr fixes

* #443 - fix: code review fixes

* #443 - fix: fixes after code review - fixed validation and edit view

* #443 - fix: fixes

* #454 - refactor: refactored ohs and medical reminder

* #454 - fix: fixes after review

* #454 - fix: code review fix
  • Loading branch information
kamilpiech97 authored Jul 2, 2024
1 parent 957b2dc commit 9966342
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Spatie\Permission\Models\Permission;
use Toby\Enums\UserHistoryType;
use Toby\Models\User;
use Toby\Notifications\UpcomingAndOverdueMedicalExamsNotification;

Expand All @@ -20,15 +21,20 @@ public function handle(): void
$usersToNotify = Permission::findByName("receiveUpcomingAndOverdueMedicalExamsNotification")->users()->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();
->whereRelation("histories", function ($query): void {
$query->where("type", UserHistoryType::MedicalExam)
->where("to", ">", Carbon::now())
->where("to", "<=", Carbon::now()->addMonths(2));
})->get();

$usersOverdueMedicalExams = User::query()
->whereRelation("profile", "next_medical_exam_date", "<=", Carbon::now())
->orderByProfileField("next_medical_exam_date", "desc")
->get();
->whereDoesntHave("histories", function ($query): void {
$query->where("type", UserHistoryType::MedicalExam)
->where("to", ">", Carbon::now());
})->withWhereHas("histories", function ($query): void {
$query->where("type", UserHistoryType::MedicalExam)
->where("to", "<", Carbon::now());
})->get();

if ($usersUpcomingMedicalExams->isEmpty() && $usersOverdueMedicalExams->isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Spatie\Permission\Models\Permission;
use Toby\Enums\UserHistoryType;
use Toby\Models\User;
use Toby\Notifications\UpcomingAndOverdueOhsTrainingNotification;

Expand All @@ -20,15 +21,20 @@ public function handle(): void
$usersToNotify = Permission::findByName("receiveUpcomingAndOverdueOhsTrainingNotification")->users()->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();
->whereRelation("histories", function ($query): void {
$query->where("type", UserHistoryType::OhsTraining)
->where("to", ">", Carbon::now())
->where("to", "<=", Carbon::now()->addMonths(2));
})->get();

$usersForOverdueOhsTraining = User::query()
->whereRelation("profile", "next_ohs_training_date", "<=", Carbon::now())
->orderByProfileField("next_ohs_training_date", "desc")
->get();
->whereDoesntHave("histories", function ($query): void {
$query->where("type", UserHistoryType::OhsTraining)
->where("to", ">", Carbon::now());
})->withWhereHas("histories", function ($query): void {
$query->where("type", UserHistoryType::OhsTraining)
->where("to", "<", Carbon::now());
})->get();

if ($usersForUpcomingOhsTraining->isEmpty() && $usersForOverdueOhsTraining->isEmpty()) {
return;
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/UserHistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function index(User $user): Response
$this->authorize("manageUsers");

$history = $user->histories()
->orderBy("from", "desc")
->orderBy("to", "desc")
->get();

return inertia("UserHistory/Index", [
Expand Down Expand Up @@ -56,7 +56,7 @@ public function edit(UserHistory $history): Response
$this->authorize("manageUsers");

return inertia("UserHistory/Edit", [
"history" => UserHistoryResource::make($history),
"history" => $history,
"types" => UserHistoryType::casesToSelect(),
"employmentForms" => EmploymentForm::casesToSelect(),
]);
Expand Down
4 changes: 2 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ public function lastMedicalExam(): ?UserHistory
{
return $this->histories()
->where("type", UserHistoryType::MedicalExam)
->orderBy("from", "desc")
->orderBy("to", "desc")
->first();
}

public function lastOhsTraining(): ?UserHistory
{
return $this->histories()
->where("type", UserHistoryType::OhsTraining)
->orderBy("from", "desc")
->orderBy("to", "desc")
->first();
}

Expand Down
6 changes: 3 additions & 3 deletions app/Models/UserHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @property int $user_id
* @property string $comment
* @property Carbon $from
* @property Carbon $to
* @property ?Carbon $to
* @property UserHistoryType $type
* @property EmploymentForm $employment_form
* @property User $user
Expand All @@ -27,8 +27,8 @@ class UserHistory extends Model

protected $guarded = [];
protected $casts = [
"from" => "date:Y-m-d",
"to" => "date:Y-m-d",
"from" => "date",
"to" => "date",
"type" => UserHistoryType::class,
"employment_form" => EmploymentForm::class,
];
Expand Down
10 changes: 6 additions & 4 deletions app/Notifications/UpcomingAndOverdueMedicalExamsNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ protected function buildMailMessage(Notifiable $notifiable, string $url): MailMe
->line(__("Below is a list of employees with upcoming health examinations:"));

foreach ($this->usersUpcomingMedicalExams as $user) {
$lastMedicalExamDate = $user->lastMedicalExam();
$message->line(__(":user - :date (in :difference days)", [
"user" => $user->profile->full_name,
"date" => $user->profile->next_medical_exam_date->toDisplayString(),
"difference" => (int)$user->profile->next_medical_exam_date->diffInDays(Carbon::today()),
"date" => $lastMedicalExamDate->to->toDisplayString(),
"difference" => (int)$lastMedicalExamDate->to->diffInDays(Carbon::today(), true),
]));
}
}
Expand All @@ -74,10 +75,11 @@ protected function buildMailMessage(Notifiable $notifiable, string $url): MailMe
->line(__("Below is a list of employees with overdue examinations:"));

foreach ($this->usersOverdueMedicalExams as $user) {
$lastMedicalExamDate = $user->lastMedicalExam();
$message->line(__(":user - :date (overdue :difference days)", [
"user" => $user->profile->full_name,
"date" => $user->profile->next_medical_exam_date->toDisplayString(),
"difference" => (int)$user->profile->next_medical_exam_date->diffInDays(Carbon::today()),
"date" => $lastMedicalExamDate->to->toDisplayString(),
"difference" => (int)$lastMedicalExamDate->to->diffInDays(Carbon::today(), true),
]));
}
}
Expand Down
10 changes: 6 additions & 4 deletions app/Notifications/UpcomingAndOverdueOhsTrainingNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ protected function buildMailMessage(Notifiable $notifiable, string $url): MailMe
->line(__("Below is a list of employees with upcoming OHS training:"));

foreach ($this->usersForUpcomingOhsTraining as $user) {
$lastOhsTraining = $user->lastOhsTraining();
$message->line(__(":user - :date (in :difference days)", [
"user" => $user->profile->full_name,
"date" => $user->profile->next_ohs_training_date->toDisplayString(),
"difference" => (int)$user->profile->next_ohs_training_date->diffInDays(Carbon::today()),
"date" => $lastOhsTraining->to->toDisplayString(),
"difference" => (int)$lastOhsTraining->to->diffInDays(Carbon::today(), true),
]));
}
}
Expand All @@ -74,10 +75,11 @@ protected function buildMailMessage(Notifiable $notifiable, string $url): MailMe
->line(__("Below is a list of employees with overdue OHS training:"));

foreach ($this->usersForOverdueOhsTraining as $user) {
$lastOhsTraining = $user->lastOhsTraining();
$message->line(__(":user - :date (overdue :difference days)", [
"user" => $user->profile->full_name,
"date" => $user->profile->next_ohs_training_date->toDisplayString(),
"difference" => (int)$user->profile->next_ohs_training_date->diffInDays(Carbon::today()),
"date" => $lastOhsTraining->to->toDisplayString(),
"difference" => (int)$lastOhsTraining->to->diffInDays(Carbon::today(), true),
]));
}
}
Expand Down
3 changes: 1 addition & 2 deletions resources/js/Pages/UserHistory/Create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Listbox, ListboxButton, ListboxLabel, ListboxOption, ListboxOptions } f
import { CheckIcon, ChevronUpDownIcon } from '@heroicons/vue/24/solid'
const props = defineProps({
auth: Object,
employmentForms: Array,
types: Array,
userId: Number,
Expand Down Expand Up @@ -255,7 +254,7 @@ function createForm() {
<div class="space-x-3">
<InertiaLink
class="py-2 px-4 text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 rounded-md border border-gray-300 focus:outline-none focus:ring-2 focus:ring-blumilk-500 focus:ring-offset-2 shadow-sm"
href="/vacation/requests"
:href="`/users/${props.userId}/history`"
>
Anuluj
</InertiaLink>
Expand Down
5 changes: 2 additions & 3 deletions resources/js/Pages/UserHistory/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Listbox, ListboxButton, ListboxLabel, ListboxOption, ListboxOptions } f
import { CheckIcon, ChevronUpDownIcon } from '@heroicons/vue/24/solid'
const props = defineProps({
auth: Object,
employmentForms: Array,
types: Array,
history: Object,
Expand All @@ -16,7 +15,7 @@ const form = useForm({
to: props.history.to,
comment: props.history.comment,
type: props.types.find(type => type.value === props.history.type),
employmentForm: props.employmentForms.find(employmentForm => employmentForm.value === props.history.employmentForm) ?? '',
employmentForm: props.employmentForms.find(employmentForm => employmentForm.value === props.history.employment_form) ?? '',
})
function updateForm() {
Expand Down Expand Up @@ -255,7 +254,7 @@ function updateForm() {
<div class="space-x-3">
<InertiaLink
class="py-2 px-4 text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 rounded-md border border-gray-300 focus:outline-none focus:ring-2 focus:ring-blumilk-500 focus:ring-offset-2 shadow-sm"
href="/vacation/requests"
:href="`/users/${history.user_id}/history`"
>
Anuluj
</InertiaLink>
Expand Down
47 changes: 30 additions & 17 deletions tests/Unit/UpcomingAndOverdueMedicalExamsNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
use Toby\Console\Commands\SendNotificationAboutUpcomingAndOverdueMedicalExams;
use Toby\Enums\UserHistoryType;
use Toby\Models\User;
use Toby\Notifications\UpcomingAndOverdueMedicalExamsNotification;

Expand All @@ -20,17 +21,26 @@ public function testSendingNotificationAboutUpcomingAndOverdueMedicalExams(): vo
{
Notification::fake();

$userWithUpcomingMedicalExam = User::factory()->employee()->hasProfile([
"next_medical_exam_date" => Carbon::now()->addMonth(),
])->create();

$userWithDistantMedicalExamDate = User::factory()->employee()->hasProfile([
"next_medical_exam_date" => Carbon::now()->addYear(),
])->create();

$userWithOverdueMedicalExam = User::factory()->employee()->hasProfile([
"next_medical_exam_date" => Carbon::now()->subMonth(),
])->create();
$userWithUpcomingMedicalExam = User::factory()->employee()->hasProfile()->create();
$userWithUpcomingMedicalExam->histories()->create([
"type" => UserHistoryType::MedicalExam,
"from" => Carbon::now()->subMonth(),
"to" => Carbon::now()->addMonth(),
]);

$userWithDistantMedicalExamDate = User::factory()->employee()->hasProfile()->create();
$userWithDistantMedicalExamDate->histories()->create([
"type" => UserHistoryType::MedicalExam,
"from" => Carbon::now()->subMonth(),
"to" => Carbon::now()->addYear(),
]);

$userWithOverdueMedicalExam = User::factory()->employee()->hasProfile()->create();
$userWithOverdueMedicalExam->histories()->create([
"type" => UserHistoryType::MedicalExam,
"from" => Carbon::now()->subMonth(),
"to" => Carbon::now()->subMonth(),
]);

$administrativeApprover = User::factory()->administrativeApprover()->create();

Expand All @@ -43,22 +53,25 @@ public function testSendingNotificationAboutUpcomingAndOverdueMedicalExams(): vo
function ($notification) use ($administrativeApprover, $userWithUpcomingMedicalExam, $userWithOverdueMedicalExam, $userWithDistantMedicalExamDate) {
$mailData = $notification->toMail($administrativeApprover)->toArray();

$lastMedicalExamDate = $userWithUpcomingMedicalExam->lastMedicalExam();
$this->assertContains(__(":user - :date (in :difference days)", [
"user" => $userWithUpcomingMedicalExam->profile->full_name,
"date" => $userWithUpcomingMedicalExam->profile->next_medical_exam_date->toDisplayString(),
"difference" => (int)$userWithUpcomingMedicalExam->profile->next_medical_exam_date->diffInDays(Carbon::today()),
"date" => $lastMedicalExamDate->to->toDisplayString(),
"difference" => (int)$lastMedicalExamDate->to->diffInDays(Carbon::today(), true),
]), $mailData["introLines"]);

$lastMedicalExamDate = $userWithOverdueMedicalExam->lastMedicalExam();
$this->assertContains(__(":user - :date (overdue :difference days)", [
"user" => $userWithOverdueMedicalExam->profile->full_name,
"date" => $userWithOverdueMedicalExam->profile->next_medical_exam_date->toDisplayString(),
"difference" => (int)$userWithOverdueMedicalExam->profile->next_medical_exam_date->diffInDays(Carbon::today()),
"date" => $lastMedicalExamDate->to->toDisplayString(),
"difference" => (int)$lastMedicalExamDate->to->diffInDays(Carbon::today(), true),
]), $mailData["introLines"]);

$lastMedicalExamDate = $userWithOverdueMedicalExam->lastMedicalExam();
$this->assertNotContains(__(":user - :date (in :difference days)", [
"user" => $userWithDistantMedicalExamDate->profile->full_name,
"date" => $userWithDistantMedicalExamDate->profile->next_medical_exam_date->toDisplayString(),
"difference" => (int)$userWithDistantMedicalExamDate->profile->next_medical_exam_date->diffInDays(Carbon::today()),
"date" => $lastMedicalExamDate->to->toDisplayString(),
"difference" => (int)$lastMedicalExamDate->to->diffInDays(Carbon::today(), true),
]), $mailData["introLines"]);

return true;
Expand Down
47 changes: 30 additions & 17 deletions tests/Unit/UpcomingAndOverdueOhsTrainingNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
use Toby\Console\Commands\SendNotificationAboutUpcomingAndOverdueOhsTraining;
use Toby\Enums\UserHistoryType;
use Toby\Models\User;
use Toby\Notifications\UpcomingAndOverdueMedicalExamsNotification;
use Toby\Notifications\UpcomingAndOverdueOhsTrainingNotification;
Expand All @@ -21,17 +22,26 @@ public function testSendingNotificationAboutUpcomingAndOverdueOhsTraining(): voi
{
Notification::fake();

$userWithUpcomingOhsTraining = User::factory()->employee()->hasProfile([
"next_ohs_training_date" => Carbon::now()->addMonth(),
])->create();

$userWithDistantOhsTrainingDate = User::factory()->employee()->hasProfile([
"next_ohs_training_date" => Carbon::now()->addYear(),
])->create();

$userWithOverdueOhsTraining = User::factory()->employee()->hasProfile([
"next_ohs_training_date" => Carbon::now()->subMonth(),
])->create();
$userWithUpcomingOhsTraining = User::factory()->employee()->hasProfile()->create();
$userWithUpcomingOhsTraining->histories()->create([
"type" => UserHistoryType::OhsTraining,
"from" => Carbon::now()->subMonth(),
"to" => Carbon::now()->addMonth(),
]);

$userWithDistantOhsTrainingDate = User::factory()->employee()->hasProfile()->create();
$userWithDistantOhsTrainingDate->histories()->create([
"type" => UserHistoryType::OhsTraining,
"from" => Carbon::now()->subMonth(),
"to" => Carbon::now()->addYear(),
]);

$userWithOverdueOhsTraining = User::factory()->employee()->hasProfile()->create();
$userWithOverdueOhsTraining->histories()->create([
"type" => UserHistoryType::OhsTraining,
"from" => Carbon::now()->subMonth(),
"to" => Carbon::now()->subMonth(),
]);

$administrativeApprover = User::factory()->administrativeApprover()->create();

Expand All @@ -44,22 +54,25 @@ public function testSendingNotificationAboutUpcomingAndOverdueOhsTraining(): voi
function ($notification) use ($administrativeApprover, $userWithUpcomingOhsTraining, $userWithDistantOhsTrainingDate, $userWithOverdueOhsTraining) {
$mailData = $notification->toMail($administrativeApprover)->toArray();

$lastOhsTraining = $userWithUpcomingOhsTraining->lastOhsTraining();
$this->assertContains(__(":user - :date (in :difference days)", [
"user" => $userWithUpcomingOhsTraining->profile->full_name,
"date" => $userWithUpcomingOhsTraining->profile->next_ohs_training_date->toDisplayString(),
"difference" => (int)$userWithUpcomingOhsTraining->profile->next_ohs_training_date->diffInDays(Carbon::today()),
"date" => $lastOhsTraining->to->toDisplayString(),
"difference" => (int)$lastOhsTraining->to->diffInDays(Carbon::today(), true),
]), $mailData["introLines"]);

$lastOhsTraining = $userWithOverdueOhsTraining->lastOhsTraining();
$this->assertContains(__(":user - :date (overdue :difference days)", [
"user" => $userWithOverdueOhsTraining->profile->full_name,
"date" => $userWithOverdueOhsTraining->profile->next_ohs_training_date->toDisplayString(),
"difference" => (int)$userWithOverdueOhsTraining->profile->next_ohs_training_date->diffInDays(Carbon::today()),
"date" => $lastOhsTraining->to->toDisplayString(),
"difference" => (int)$lastOhsTraining->to->diffInDays(Carbon::today(), true),
]), $mailData["introLines"]);

$lastOhsTraining = $userWithDistantOhsTrainingDate->lastOhsTraining();
$this->assertNotContains(__(":user - :date (in :difference days)", [
"user" => $userWithDistantOhsTrainingDate->profile->full_name,
"date" => $userWithDistantOhsTrainingDate->profile->next_ohs_training_date->toDisplayString(),
"difference" => (int)$userWithDistantOhsTrainingDate->profile->next_ohs_training_date->diffInDays(Carbon::today()),
"date" => $lastOhsTraining->to->toDisplayString(),
"difference" => (int)$lastOhsTraining->to->diffInDays(Carbon::today(), true),
]), $mailData["introLines"]);

return true;
Expand Down

0 comments on commit 9966342

Please sign in to comment.