-
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.
- Loading branch information
1 parent
1e9cab5
commit 2f3739c
Showing
7 changed files
with
177 additions
and
28 deletions.
There are no files selected for viewing
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,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(); | ||
} | ||
} |
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
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
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
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); | ||
} | ||
} |