Skip to content

Commit

Permalink
#354 - employees milestones page (#364)
Browse files Browse the repository at this point in the history
* #354 - wip

* #354 - wip

* #354 - wip

* #354 - fix

* #354 - fix

* #354 - fix

* #354 - fix

* #354 - cr fixes

* #354 - fix

* #354 - fix
  • Loading branch information
EwelinaSkrzypacz authored Oct 30, 2023
1 parent e3dcec1 commit 8019143
Show file tree
Hide file tree
Showing 13 changed files with 510 additions and 20 deletions.
15 changes: 0 additions & 15 deletions app/Domain/DailySummaryRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,6 @@ public function getUpcomingRemoteDays(Carbon $date): Collection
->get();
}

/**
* @return Collection<User>
*/
public function getBirthdays(Carbon $date): Collection
{
return User::query()
->whereRelation(
"profile",
fn(Builder $query): Builder => $query
->whereMonth("birthday", $date->month)
->whereDay("birthday", $date->day),
)
->get();
}

/**
* @return Collection<User>
*/
Expand Down
48 changes: 48 additions & 0 deletions app/Domain/EmployeesMilestonesRetriever.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Toby\Domain;

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

class EmployeesMilestonesRetriever
{
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()
->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();
}
}
36 changes: 33 additions & 3 deletions app/Eloquent/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public function scopeSearch(Builder $query, ?string $text): Builder
);
}

public function scopeOrderByProfileField(Builder $query, string $field): Builder
public function scopeOrderByProfileField(Builder $query, string $field, string $direction = "asc"): Builder
{
$profileQuery = Profile::query()->select($field)->whereColumn("users.id", "profiles.user_id");

return $query->orderBy($profileQuery);
return $query->orderBy($profileQuery, $direction);
}

public function scopeWithVacationLimitIn(Builder $query, YearPeriod $yearPeriod): Builder
Expand All @@ -135,8 +135,12 @@ public function scopeStatus(Builder $query, ?string $status): Builder
};
}

public function upcomingBirthday(): Carbon
public function upcomingBirthday(): ?Carbon
{
if (!$this->profile->birthday) {
return null;
}

$today = Carbon::today();

$birthday = $this->profile->birthday->setYear($today->year);
Expand All @@ -148,11 +152,37 @@ public function upcomingBirthday(): Carbon
return $birthday;
}

public function seniority(): ?string
{
$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 isWorkAnniversaryToday(): bool
{
$today = Carbon::now();

$employmentDate = $this->profile->employment_date;

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

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

return $workAnniversary->isToday();
}

protected static function newFactory(): UserFactory
{
return UserFactory::new();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Toby\Infrastructure\Http\Controllers;

use Illuminate\Http\Request;
use Inertia\Response;
use Toby\Domain\EmployeesMilestonesRetriever;
use Toby\Infrastructure\Http\Resources\EmployeeMilestoneResource;

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

$users = $employeesMilestoneRetriever->getResults($searchText, $sort);

return inertia("EmployeesMilestones", [
"users" => EmployeeMilestoneResource::collection($users),
"filters" => [
"search" => $searchText,
"sort" => $sort,
],
]);
}
}
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" => ["required", "date_format:Y-m-d", "before:today"],
"slackId" => [],
"nextMedicalExamDate" => ["nullable", "after:lastMedicalExamDate"],
"nextOhsTrainingDate" => ["nullable", "after:lastOhsTrainingDate"],
Expand Down
36 changes: 36 additions & 0 deletions app/Infrastructure/Http/Resources/EmployeeMilestoneResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Toby\Infrastructure\Http\Resources;

use Carbon\CarbonInterface;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;

class EmployeeMilestoneResource extends JsonResource
{
public static $wrap = null;

public function toArray($request): array
{
$upcomingBirthday = $this->upcomingBirthday();
$seniority = $this->seniority();
$isSeniorityAnniversaryToday = $this->isWorkAnniversaryToday();

return [
"user" => new SimpleUserResource($this->resource),
"birthdayDisplayDate" => $upcomingBirthday?->toDisplayString(),
"birthdayRelativeDate" => $upcomingBirthday?->isToday()
? __("today")
: $upcomingBirthday?->diffForHumans(
Carbon::today(),
["options" => CarbonInterface::ONE_DAY_WORDS, "syntax" => CarbonInterface::DIFF_RELATIVE_TO_NOW],
),
"isBirthdayToday" => (bool)$upcomingBirthday?->isToday(),
"seniorityDisplayDate" => $seniority,
"isWorkAnniversaryToday" => $isSeniorityAnniversaryToday,
"employmentDate" => $this->profile->employment_date->toDisplayString(),
];
}
}
2 changes: 1 addition & 1 deletion app/Infrastructure/Http/Resources/UserFormDataResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function toArray($request): array
"position" => $this->profile->position,
"employmentForm" => $this->profile->employment_form,
"employmentDate" => $this->profile->employment_date->toDateString(),
"birthday" => $this->profile->birthday?->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(),
Expand Down
4 changes: 4 additions & 0 deletions lang/pl/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,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 @@ -196,5 +199,6 @@
"isMobile" => "mobilny",
"assignee" => "przydzielona osoba",
"assignedAt" => "data przydzielenia",
"birthday" => "data urodzenia",
],
];
Loading

0 comments on commit 8019143

Please sign in to comment.