From d40b4b22cc326402818e1dc481dfc04df4b1096e Mon Sep 17 00:00:00 2001 From: Kamil Piech Date: Thu, 4 Jul 2024 13:19:07 +0200 Subject: [PATCH] #445 - seniority calculation refactor (#458) * #445 - rafactor: changed way how seniority is calculated * #445 - refactor: added updating user employment data --- .../MigrateProfileDataIntoUserHistory.php | 14 +++++++ app/Domain/EmployeesMilestonesRetriever.php | 13 ++++++- app/Http/Requests/UserHistoryRequest.php | 1 + .../Resources/EmployeeMilestoneResource.php | 2 +- app/Http/Resources/UserHistoryResource.php | 1 + app/Models/User.php | 38 ++++++++++++++++++- app/Models/UserHistory.php | 2 + ...add_blumilk_flag_in_user_history_table.php | 23 +++++++++++ resources/js/Pages/UserHistory/Create.vue | 26 ++++++++++++- resources/js/Pages/UserHistory/Edit.vue | 25 +++++++++++- resources/js/Pages/UserHistory/Index.vue | 15 +++++++- tests/Feature/EmployeesMilestonesTest.php | 24 ++++++++++-- 12 files changed, 173 insertions(+), 11 deletions(-) create mode 100644 database/migrations/2024_07_03_120018_add_blumilk_flag_in_user_history_table.php diff --git a/app/Console/Commands/MigrateProfileDataIntoUserHistory.php b/app/Console/Commands/MigrateProfileDataIntoUserHistory.php index c46f23c3..76a32f72 100644 --- a/app/Console/Commands/MigrateProfileDataIntoUserHistory.php +++ b/app/Console/Commands/MigrateProfileDataIntoUserHistory.php @@ -22,6 +22,7 @@ public function handle(): void foreach ($users as $user) { $this->moveMedicalDataToHistory($user); $this->moveOhsDataToHistory($user); + $this->moveEmploymentDataToHistory($user); } } @@ -46,4 +47,17 @@ private function moveOhsDataToHistory(User $user): void ]); } } + + private function moveEmploymentDataToHistory(User $user): void + { + if ($user->profile->employment_date) { + $user->histories()->create([ + "from" => $user->profile->employment_date, + "to" => null, + "type" => UserHistoryType::Employment, + "employment_form" => $user->profile->employment_form, + "is_employed_at_current_company" => true, + ]); + } + } } diff --git a/app/Domain/EmployeesMilestonesRetriever.php b/app/Domain/EmployeesMilestonesRetriever.php index c8a1e3cf..8bd6c04d 100644 --- a/app/Domain/EmployeesMilestonesRetriever.php +++ b/app/Domain/EmployeesMilestonesRetriever.php @@ -6,7 +6,9 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Collection; +use Toby\Enums\UserHistoryType; use Toby\Models\User; +use Toby\Models\UserHistory; class EmployeesMilestonesRetriever { @@ -42,7 +44,16 @@ public function getSeniority(?string $searchText, string $direction = "asc"): Co { return User::query() ->search($searchText) - ->orderByProfileField("employment_date", $direction) + ->orderBy( + UserHistory::query() + ->select("from") + ->whereColumn("users.id", "user_histories.user_id") + ->where("type", UserHistoryType::Employment) + ->where("is_employed_at_current_company", true) + ->orderBy("from", $direction) + ->limit(1), + $direction, + ) ->get(); } } diff --git a/app/Http/Requests/UserHistoryRequest.php b/app/Http/Requests/UserHistoryRequest.php index c7e3f93f..973f876f 100644 --- a/app/Http/Requests/UserHistoryRequest.php +++ b/app/Http/Requests/UserHistoryRequest.php @@ -30,6 +30,7 @@ public function data(): array "type" => $this->get("type"), "employment_form" => $this->get("type") === UserHistoryType::Employment->value ? $this->get("employmentForm") : null, "comment" => $this->get("comment"), + "is_employed_at_current_company" => $this->get("type") === UserHistoryType::Employment->value ? $this->boolean("isEmployedAtCurrentCompany") : null, ]; } } diff --git a/app/Http/Resources/EmployeeMilestoneResource.php b/app/Http/Resources/EmployeeMilestoneResource.php index 2394ef32..c05f16e1 100644 --- a/app/Http/Resources/EmployeeMilestoneResource.php +++ b/app/Http/Resources/EmployeeMilestoneResource.php @@ -30,7 +30,7 @@ public function toArray($request): array "isBirthdayToday" => (bool)$upcomingBirthday?->isToday(), "seniorityDisplayDate" => $seniority, "isWorkAnniversaryToday" => $isSeniorityAnniversaryToday, - "employmentDate" => $this->profile->employment_date->toDisplayString(), + "employmentDate" => $this->startOfEmploymentInCurrentCompany()?->from->toDisplayString(), ]; } } diff --git a/app/Http/Resources/UserHistoryResource.php b/app/Http/Resources/UserHistoryResource.php index 52bb80b9..c2262800 100644 --- a/app/Http/Resources/UserHistoryResource.php +++ b/app/Http/Resources/UserHistoryResource.php @@ -20,6 +20,7 @@ public function toArray($request): array "typeLabel" => $this->type->label(), "employmentFormLabel" => $this->employment_form?->label(), "employmentForm" => $this->employment_form?->value, + "isEmployedAtCurrentCompany" => $this->is_employed_at_current_company, "comment" => $this->comment, "userId" => $this->user_id, ]; diff --git a/app/Models/User.php b/app/Models/User.php index 06e585ad..0259cf10 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -110,6 +110,24 @@ public function lastOhsTraining(): ?UserHistory ->first(); } + public function startOfEmploymentInCurrentCompany(): ?UserHistory + { + return $this->histories() + ->where("type", UserHistoryType::Employment) + ->where("is_employed_at_current_company", true) + ->orderBy("from", "asc") + ->first(); + } + + public function endOfEmploymentInCurrentCompany(): ?UserHistory + { + return $this->histories() + ->where("type", UserHistoryType::Employment) + ->where("is_employed_at_current_company", true) + ->orderBy("from", "desc") + ->first(); + } + public function keys(): HasMany { return $this->hasMany(Key::class); @@ -147,6 +165,13 @@ public function scopeOrderByProfileField(Builder $query, string $field, string $ return $query->orderBy($profileQuery, $direction); } + public function scopeOrderByUserHistoryField(Builder $query, string $field, string $direction = "asc"): Builder + { + $profileQuery = UserHistory::query()->select($field)->whereColumn("users.id", "histories.user_id"); + + return $query->orderBy($profileQuery, $direction); + } + public function scopeWithVacationLimitIn(Builder $query, YearPeriod $yearPeriod): Builder { return $query->whereRelation( @@ -186,12 +211,21 @@ public function upcomingBirthday(): ?Carbon public function seniority(): ?string { - $employmentDate = $this->profile->employment_date; + $startOfEmploymentInCurrentCompany = $this->startOfEmploymentInCurrentCompany(); + $employmentDate = $startOfEmploymentInCurrentCompany?->from; - if ($employmentDate->isFuture() || $employmentDate->isToday()) { + if (!$employmentDate || $employmentDate->isFuture() || $employmentDate->isToday()) { return null; } + if ($startOfEmploymentInCurrentCompany->to !== null) { + $endOfEmploymentInCurrentCompany = $this->endOfEmploymentInCurrentCompany(); + + if ($endOfEmploymentInCurrentCompany->to !== null) { + return $employmentDate->longAbsoluteDiffForHumans($endOfEmploymentInCurrentCompany->to, 2); + } + } + return $employmentDate->longAbsoluteDiffForHumans(Carbon::today(), 2); } diff --git a/app/Models/UserHistory.php b/app/Models/UserHistory.php index 5b790a71..9065aabe 100644 --- a/app/Models/UserHistory.php +++ b/app/Models/UserHistory.php @@ -19,6 +19,7 @@ * @property ?Carbon $to * @property UserHistoryType $type * @property EmploymentForm $employment_form + * @property bool $is_employed_at_current_company * @property User $user */ class UserHistory extends Model @@ -31,6 +32,7 @@ class UserHistory extends Model "to" => "date", "type" => UserHistoryType::class, "employment_form" => EmploymentForm::class, + "is_employed_at_current_company" => "boolean", ]; public function user(): BelongsTo diff --git a/database/migrations/2024_07_03_120018_add_blumilk_flag_in_user_history_table.php b/database/migrations/2024_07_03_120018_add_blumilk_flag_in_user_history_table.php new file mode 100644 index 00000000..206f59b7 --- /dev/null +++ b/database/migrations/2024_07_03_120018_add_blumilk_flag_in_user_history_table.php @@ -0,0 +1,23 @@ +boolean("is_employed_at_current_company")->nullable(); + }); + } + + public function down(): void + { + Schema::table("user_histories", function (Blueprint $table): void { + $table->dropColumn("is_employed_at_current_company"); + }); + } +}; diff --git a/resources/js/Pages/UserHistory/Create.vue b/resources/js/Pages/UserHistory/Create.vue index c80969aa..e2daa881 100644 --- a/resources/js/Pages/UserHistory/Create.vue +++ b/resources/js/Pages/UserHistory/Create.vue @@ -1,7 +1,7 @@