Skip to content

Commit

Permalink
#452 - user profile cleaning (#459)
Browse files Browse the repository at this point in the history
* #452 - refactor: user profile cleaning

* Update app/Observers/UserHistoryObserver.php

Co-authored-by: Krzysztof Rewak <krzysztof.rewak@blumilk.pl>

* #452 - fix: code review fixes

* #452 - fix: rollback last update key name

---------

Co-authored-by: Krzysztof Rewak <krzysztof.rewak@blumilk.pl>
  • Loading branch information
kamilpiech97 and krzysztofrewak authored Jul 23, 2024
1 parent 5c72a04 commit f65ff3b
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 49 deletions.
4 changes: 2 additions & 2 deletions app/Domain/DashboardAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
->vacations()
->with(["vacationRequest.vacations", "vacationRequest.user.profile"])
->whereBelongsTo($yearPeriod)
->cache("vacations{$user->id}")
->cache("vacations:{$user->id}")
->approved()
->get()
->mapWithKeys(
Expand All @@ -64,7 +64,7 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
->vacations()
->with(["vacationRequest.vacations", "vacationRequest.user.profile"])
->whereBelongsTo($yearPeriod)
->cache("vacations{$user->id}")
->cache("vacations:{$user->id}")
->pending()
->get()
->mapWithKeys(
Expand Down
4 changes: 2 additions & 2 deletions app/Domain/UserVacationStatsRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ public function getVacationDaysLimit(User $user, YearPeriod $yearPeriod): int
{
return $user->vacationLimits()
->whereBelongsTo($yearPeriod)
->cache("vacations{$user->id}")
->cache("vacations:{$user->id}")
->first()?->limit ?? 0;
}

public function hasVacationDaysLimit(User $user, YearPeriod $yearPeriod): bool
{
return $user->vacationLimits()
->whereBelongsTo($yearPeriod)
->cache("vacations{$user->id}")
->cache("vacations:{$user->id}")
->first()?->hasVacation() ?? false;
}

Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/OvertimeRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,28 @@ public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever
->overtimeRequests()
->whereBelongsTo($yearPeriodRetriever->selected())
->states(OvertimeRequestStatesRetriever::pendingStates())
->cache(key: "overtime{$user->id}")
->cache(key: "overtime:{$user->id}")
->count();

$success = $user
->overtimeRequests()
->whereBelongsTo($yearPeriodRetriever->selected())
->states(OvertimeRequestStatesRetriever::successStates())
->cache(key: "overtime{$user->id}")
->cache(key: "overtime:{$user->id}")
->count();

$failed = $user
->overtimeRequests()
->whereBelongsTo($yearPeriodRetriever->selected())
->states(OvertimeRequestStatesRetriever::failedStates())
->cache(key: "overtime{$user->id}")
->cache(key: "overtime:{$user->id}")
->count();

$settled = $user
->overtimeRequests()
->whereBelongsTo($yearPeriodRetriever->selected())
->states(OvertimeRequestStatesRetriever::settledStates())
->cache(key: "overtime{$user->id}")
->cache(key: "overtime:{$user->id}")
->count();

return inertia("OvertimeRequest/Index", [
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/VacationRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever
->whereBelongsTo($yearPeriodRetriever->selected())
->states(VacationRequestStatesRetriever::pendingStates())
->when($withoutRemote, fn(Builder $query): Builder => $query->excludeType(VacationType::RemoteWork))
->cache(key: "vacations{$user->id}")
->cache(key: "vacations:{$user->id}")
->count();

$success = $user
->vacationRequests()
->whereBelongsTo($yearPeriodRetriever->selected())
->states(VacationRequestStatesRetriever::successStates())
->when($withoutRemote, fn(Builder $query): Builder => $query->excludeType(VacationType::RemoteWork))
->cache(key: "vacations{$user->id}")
->cache(key: "vacations:{$user->id}")
->count();

$failed = $user
->vacationRequests()
->whereBelongsTo($yearPeriodRetriever->selected())
->states(VacationRequestStatesRetriever::failedStates())
->when($withoutRemote, fn(Builder $query): Builder => $query->excludeType(VacationType::RemoteWork))
->cache(key: "vacations{$user->id}")
->cache(key: "vacations:{$user->id}")
->count();

return inertia("VacationRequest/Index", [
Expand Down
2 changes: 0 additions & 2 deletions app/Http/Requests/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function rules(): array
"role" => ["required", new Enum(Role::class)],
"position" => ["required"],
"employmentForm" => ["required", new Enum(EmploymentForm::class)],
"employmentDate" => ["required", "date_format:" . DateFormats::DATE],
"birthday" => ["required", "date_format:" . DateFormats::DATE, "before:today"],
"slackId" => [],
];
Expand All @@ -43,7 +42,6 @@ public function profileData(): array
"last_name" => $this->get("lastName"),
"position" => $this->get("position"),
"employment_form" => $this->get("employmentForm"),
"employment_date" => $this->get("employmentDate"),
"birthday" => $this->get("birthday"),
"slack_id" => $this->get("slackId"),
];
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function toArray($request): array
{
$lastMedicalExam = $this->lastMedicalExam();
$lastOhsTraining = $this->lastOhsTraining();
$startOfEmploymentInCurrentCompany = $this->startOfEmploymentInCurrentCompany();

return [
"id" => $this->id,
Expand All @@ -25,7 +26,7 @@ public function toArray($request): array
"deleted" => $this->trashed(),
"lastActiveAt" => $this->last_active_at?->toDateTimeString(),
"employmentForm" => $this->profile->employment_form->label(),
"employmentDate" => $this->profile->employment_date->toDisplayString(),
"employmentDate" => $startOfEmploymentInCurrentCompany?->from->toDisplayString(),
"lastMedicalExamDate" => $lastMedicalExam?->from?->toDisplayString(),
"nextMedicalExamDate" => $lastMedicalExam?->to?->toDisplayString(),
"lastOhsTrainingDate" => $lastOhsTraining?->from?->toDisplayString(),
Expand Down
9 changes: 0 additions & 9 deletions app/Models/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
* @property EmploymentForm $employment_form
* @property Carbon $employment_date
* @property Carbon $birthday
* @property ?Carbon $last_medical_exam_date
* @property ?Carbon $next_medical_exam_date
* @property ?Carbon $last_ohs_training_date
* @property ?Carbon $next_ohs_training_date
*/
class Profile extends Model
{
Expand All @@ -34,12 +30,7 @@ class Profile extends Model
protected $guarded = [];
protected $casts = [
"employment_form" => EmploymentForm::class,
"employment_date" => "date",
"birthday" => "date",
"last_medical_exam_date" => "date",
"next_medical_exam_date" => "date",
"last_ohs_training_date" => "date",
"next_ohs_training_date" => "date",
];

public function user(): BelongsTo
Expand Down
13 changes: 8 additions & 5 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function lastMedicalExam(): ?UserHistory
return $this->histories()
->where("type", UserHistoryType::MedicalExam)
->orderBy("to", "desc")
->cache("user:history{$this->id}")
->first();
}

Expand All @@ -107,6 +108,7 @@ public function lastOhsTraining(): ?UserHistory
return $this->histories()
->where("type", UserHistoryType::OhsTraining)
->orderBy("to", "desc")
->cache("user:history{$this->id}")
->first();
}

Expand All @@ -115,7 +117,8 @@ public function startOfEmploymentInCurrentCompany(): ?UserHistory
return $this->histories()
->where("type", UserHistoryType::Employment)
->where("is_employed_at_current_company", true)
->orderBy("from", "asc")
->orderBy("from")
->cache("user:history{$this->id}")
->first();
}

Expand Down Expand Up @@ -243,15 +246,15 @@ public function isWorkAnniversaryToday(): bool
{
$today = Carbon::now();

$employmentDate = $this->profile->employment_date;
$employmentDate = $this->startOfEmploymentInCurrentCompany()?->from;

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

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

return $workAnniversary->isToday();
return $workAnniversary?->isToday() ?? false;
}

protected static function newFactory(): UserFactory
Expand Down
2 changes: 1 addition & 1 deletion app/Observers/OvertimeRequestObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public function creating(OvertimeRequest $overtime): void

public function updating(OvertimeRequest $overtime): void
{
CacheQuery::forget("overtime{$overtime->user->id}");
CacheQuery::forget("overtime:{$overtime->user->id}");
}
}
21 changes: 21 additions & 0 deletions app/Observers/UserHistoryObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Toby\Observers;

use Laragear\CacheQuery\Facades\CacheQuery;
use Toby\Models\UserHistory;

class UserHistoryObserver
{
public function creating(UserHistory $userHistory): void
{
CacheQuery::forget("user:history:{$userHistory->user->id}");
}

public function updating(UserHistory $userHistory): void
{
CacheQuery::forget("user:history{$userHistory->user->id}");
}
}
2 changes: 1 addition & 1 deletion app/Observers/VacationRequestObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public function creating(VacationRequest $vacationRequest): void

public function updating(VacationRequest $vacationRequest): void
{
CacheQuery::forget("vacations{$vacationRequest->user->id}");
CacheQuery::forget("vacations:{$vacationRequest->user->id}");
}
}
1 change: 0 additions & 1 deletion database/factories/ProfileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function definition(): array
"last_name" => $this->faker->lastName(),
"employment_form" => $this->faker->randomElement(EmploymentForm::cases()),
"position" => $this->faker->jobTitle(),
"employment_date" => Carbon::createFromInterface($this->faker->dateTimeBetween("2020-10-27"))->toDateString(),
"birthday" => Carbon::createFromInterface($this->faker->dateTimeBetween("1970-01-01", "1998-01-01"))->toDateString(),
];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::table("profiles", function (Blueprint $table): void {
$table->dropColumn("last_medical_exam_date");
$table->dropColumn("next_medical_exam_date");
$table->dropColumn("last_ohs_training_date");
$table->dropColumn("next_ohs_training_date");
$table->dropColumn("employment_date");
});
}

public function down(): void
{
Schema::table("profiles", function (Blueprint $table): void {
$table->date("last_medical_exam_date")->nullable()->default(null);
$table->date("next_medical_exam_date")->nullable()->default(null);
$table->date("last_ohs_training_date")->nullable()->default(null);
$table->date("next_ohs_training_date")->nullable()->default(null);
$table->date("employment_date")->nullable()->default(null);
});
}
};
Loading

0 comments on commit f65ff3b

Please sign in to comment.