Skip to content

Commit

Permalink
#354 - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
EwelinaSkrzypacz committed Oct 10, 2023
1 parent 1e9cab5 commit 2f3739c
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 28 deletions.
50 changes: 50 additions & 0 deletions app/Domain/EmployeesMilestoneRetriever.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Toby\Domain;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Toby\Eloquent\Models\User;

class EmployeesMilestoneRetriever
{
public function __construct(
protected VacationTypeConfigRetriever $configRetriever,
) {}

public function getResults(?string $searchText, ?string $sort): Collection
{
return match ($sort) {
"birthday-asc" => $this->getUpcomingBirthdays($searchText),
"birthday-desc" => $this->getUpcomingBirthdays($searchText, "desc"),
"seniority-asc" => $this->getSeniority($searchText),
"seniority-desc" => $this->getSeniority($searchText, "desc"),
default => User::query()
->search($searchText)
->orderByProfileField("last_name")
->orderByProfileField("first_name")
->get(),
};
}

public function getUpcomingBirthdays(?string $searchText, string $direction = "asc"): Collection
{
$users = User::query()
->whereRelation("profile", fn(Builder $query): Builder => $query->whereNotNull("birthday"))
->search($searchText)
->get();

return $users->sortBy(fn(User $user): int => $user->upcomingBirthday()->diffInDays(Carbon::today()), descending: $direction !== "asc");
}

public function getSeniority(?string $searchText, string $direction = "asc"): Collection
{
return User::query()
->search($searchText)
->orderByProfileField("employment_date", $direction)
->get();
}
}
31 changes: 15 additions & 16 deletions app/Eloquent/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,33 @@ public function upcomingBirthday(): ?Carbon
return $birthday;
}

public function seniority(): string
public function seniority(): ?string
{
return $this->profile->employment_date->longAbsoluteDiffForHumans(Carbon::today(), 2);
$employmentDate = $this->profile->employment_date;

if ($employmentDate->isFuture() || $employmentDate->isToday()) {
return null;
}

return $employmentDate->longAbsoluteDiffForHumans(Carbon::today(), 2);
}

public function routeNotificationForSlack()
{
return $this->profile->slack_id;
}

public function scopeSortForEmployeesMilestones(Builder $query, ?string $sort): Builder
{
return match ($sort) {
// "birthday-asc" TO DO
// "birthday-desc" TO DO
"seniority-asc" => $query->orderByProfileField("employment_date", "asc"),
"seniority-desc" => $query->orderByProfileField("employment_date", "desc"),
default => $query
->orderByProfileField("last_name")
->orderByProfileField("first_name"),
};
}

public function isWorkAnniversaryToday(): bool
{
$today = Carbon::now();

$workAnniversary = $this->profile->employment_date->setYear($today->year);
$employmentDate = $this->profile->employment_date;

if ($employmentDate->isToday()) {
return false;
}

$workAnniversary = $employmentDate->setYear($today->year);

return $workAnniversary->isToday();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@

use Illuminate\Http\Request;
use Inertia\Response;
use Toby\Eloquent\Models\User;
use Toby\Domain\EmployeesMilestoneRetriever;
use Toby\Infrastructure\Http\Resources\EmployeeMilestoneResource;

class EmployeesMilestonesController extends Controller
{
public function index(Request $request): Response
public function index(Request $request, EmployeesMilestoneRetriever $employeesMilestoneRetriever): Response
{
$searchText = $request->query("search");
$sort = $request->query("sort");

$users = User::query()
->sortForEmployeesMilestones($sort)
->search($searchText)
->orderByProfileField("last_name")
->orderByProfileField("first_name")
->paginate()
->withQueryString();
$users = $employeesMilestoneRetriever->getResults($searchText, $sort);

return inertia("EmployeesMilestones", [
"users" => EmployeeMilestoneResource::collection($users),
"filters" => [
"search" => $searchText,
"sort" => $sort,
],
]);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Infrastructure/Http/Requests/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function rules(): array
"position" => ["required"],
"employmentForm" => ["required", new Enum(EmploymentForm::class)],
"employmentDate" => ["required", "date_format:Y-m-d"],
"birthday" => ["nullable", "date_format:Y-m-d"],
"birthday" => ["nullable", "date_format:Y-m-d", "before:today"],
"slackId" => [],
"nextMedicalExamDate" => ["nullable", "after:lastMedicalExamDate"],
"nextOhsTrainingDate" => ["nullable", "after:lastOhsTrainingDate"],
Expand Down
4 changes: 4 additions & 0 deletions lang/pl/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
"nextMedicalExamDate" => [
"after" => "Data następnego badania lekarskiego musi być późniejsza od daty ostatniego badania lekarskiego.",
],
"birthday" => [
"before" => "Data urodzenia musi być datą wcześniejszą od dzisiaj.",
],
],
"attributes" => [
"to" => "do",
Expand All @@ -182,5 +185,6 @@
"date" => "data",
"name" => "nazwa",
"password" => "hasło",
"birthday" => "data urodzenia",
],
];
3 changes: 1 addition & 2 deletions resources/js/Pages/EmployeesMilestones.vue
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ watch(form, debounce(() => {
</td>
<td class="p-4 text-sm text-gray-500 whitespace-nowrap">
<div class="flex gap-1">
{{ user.seniorityDisplayDate }}
{{ user.seniorityDisplayDate ? user.seniorityDisplayDate : '-'}}
<span
v-if="user.isWorkAnniversaryToday"
v-tooltip.right="'okrągła rocznica pracy'"
Expand Down Expand Up @@ -210,7 +210,6 @@ watch(form, debounce(() => {
</tbody>
</table>
</div>
<Pagination :pagination="users.meta"/>
</div>
</div>
</template>
102 changes: 102 additions & 0 deletions tests/Feature/EmployeesMilestonesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Tests\Feature;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Carbon;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\FeatureTestCase;
use Toby\Domain\EmployeesMilestoneRetriever;
use Toby\Eloquent\Models\User;

class EmployeesMilestonesTest extends FeatureTestCase
{
use DatabaseMigrations;

protected EmployeesMilestoneRetriever $employeesMilestoneRetriever;

protected function setUp(): void
{
parent::setUp();

$this->employeesMilestoneRetriever = $this->app->make(EmployeesMilestoneRetriever::class);
}

public function testUserCanSeeEmployeesMilestonesList(): void
{
$user = User::factory()->create();

User::factory()->count(9)->create();

$this->actingAs($user)
->get("/employees-milestones")
->assertOk()
->assertInertia(
fn(Assert $page) => $page
->component("EmployeesMilestones")
->has("users.data", 10),
);
}

public function testSortingByBirthdays(): void
{
$user1 = User::factory()
->hasProfile(["birthday" => Carbon::createFromDate(1998, 1, 1)])
->employee()
->create();

$user2 = User::factory()
->hasProfile(["birthday" => Carbon::createFromDate(2000, 12, 30)])
->employee()
->create();

$user3 = User::factory()
->hasProfile(["birthday" => Carbon::createFromDate(1997, 5, 22)])
->employee()
->create();

$sortedUsersByUpcomingBirthday = $this->employeesMilestoneRetriever->getResults(null, "birthday-asc")->values();

$this->assertEquals($user1->id, $sortedUsersByUpcomingBirthday[0]->id);
$this->assertEquals($user3->id, $sortedUsersByUpcomingBirthday[1]->id);
$this->assertEquals($user2->id, $sortedUsersByUpcomingBirthday[2]->id);

$sortedUsersByFurthestBirthday = $this->employeesMilestoneRetriever->getResults(null, "birthday-desc")->values();

$this->assertEquals($user2->id, $sortedUsersByFurthestBirthday[0]->id);
$this->assertEquals($user3->id, $sortedUsersByFurthestBirthday[1]->id);
$this->assertEquals($user1->id, $sortedUsersByFurthestBirthday[2]->id);
}

public function testSortingBySeniority(): void
{
$user1 = User::factory()
->hasProfile(["employment_date" => Carbon::createFromDate(2023, 1, 31)])
->employee()
->create();

$user2 = User::factory()
->hasProfile(["employment_date" => Carbon::createFromDate(2022, 1, 1)])
->employee()
->create();

$user3 = User::factory()
->hasProfile(["employment_date" => Carbon::createFromDate(2021, 10, 4)])
->employee()
->create();

$sortedUsersByLongestSeniority = $this->employeesMilestoneRetriever->getResults(null, "seniority-asc")->values();

$this->assertEquals($user3->id, $sortedUsersByLongestSeniority[0]->id);
$this->assertEquals($user2->id, $sortedUsersByLongestSeniority[1]->id);
$this->assertEquals($user1->id, $sortedUsersByLongestSeniority[2]->id);

$sortedUsersByShortestSeniority = $this->employeesMilestoneRetriever->getResults(null, "seniority-desc")->values();

$this->assertEquals($user1->id, $sortedUsersByShortestSeniority[0]->id);
$this->assertEquals($user2->id, $sortedUsersByShortestSeniority[1]->id);
$this->assertEquals($user3->id, $sortedUsersByShortestSeniority[2]->id);
}
}

0 comments on commit 2f3739c

Please sign in to comment.