Skip to content

Commit

Permalink
#452 - refactor: user profile cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilpiech97 committed Jul 4, 2024
1 parent d40b4b2 commit 4e85098
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 36 deletions.
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("userHistories{$this->id}")
->first();
}

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

Expand Down Expand Up @@ -238,15 +241,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
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("userHistories{$userHistory->user->id}");
}

public function updating(UserHistory $userHistory): void
{
CacheQuery::forget("userHistories{$userHistory->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();
});
}
};
65 changes: 54 additions & 11 deletions database/seeders/DemoSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Toby\Domain\WorkDaysCalculator;
use Toby\Enums\EmploymentForm;
use Toby\Enums\Role;
use Toby\Enums\UserHistoryType;
use Toby\Enums\VacationType;
use Toby\Models\Benefit;
use Toby\Models\BenefitsReport;
Expand Down Expand Up @@ -48,11 +49,17 @@ public function run(): void
"last_name" => "Kowalski",
"employment_form" => EmploymentForm::EmploymentContract,
"position" => "programista",
"employment_date" => Carbon::createFromDate(2021, 12, 31),
])
->create();
$user->histories()->create([
"from" => Carbon::createFromDate(2021, 12, 31),
"to" => null,
"type" => UserHistoryType::Employment,
"employment_form" => EmploymentForm::EmploymentContract,
"is_employed_at_current_company" => true,
]);

User::factory([
$programmer = User::factory([
"email" => "jerzy.nowak@example.com",
"remember_token" => Str::random(10),
])
Expand All @@ -62,11 +69,17 @@ public function run(): void
"last_name" => "Nowak",
"employment_form" => EmploymentForm::EmploymentContract,
"position" => "programista",
"employment_date" => Carbon::createFromDate(2021, 5, 10),
])
->create();
$programmer->histories()->create([
"from" => Carbon::createFromDate(2021, 5, 10),
"to" => null,
"type" => UserHistoryType::Employment,
"employment_form" => EmploymentForm::EmploymentContract,
"is_employed_at_current_company" => true,
]);

User::factory([
$tester = User::factory([
"email" => "anna.nowak@example.com",
"remember_token" => Str::random(10),
])
Expand All @@ -76,11 +89,17 @@ public function run(): void
"last_name" => "Nowak",
"employment_form" => EmploymentForm::CommissionContract,
"position" => "tester",
"employment_date" => Carbon::createFromDate(2021, 5, 10),
])
->create();
$tester->histories()->create([
"from" => Carbon::createFromDate(2021, 5, 10),
"to" => null,
"type" => UserHistoryType::Employment,
"employment_form" => EmploymentForm::CommissionContract,
"is_employed_at_current_company" => true,
]);

User::factory([
$programmer = User::factory([
"email" => "tola.sawicka@example.com",
"role" => Role::Employee,
"remember_token" => Str::random(10),
Expand All @@ -91,9 +110,15 @@ public function run(): void
"last_name" => "Sawicka",
"employment_form" => EmploymentForm::B2bContract,
"position" => "programista",
"employment_date" => Carbon::createFromDate(2021, 1, 4),
])
->create();
$programmer->histories()->create([
"from" => Carbon::createFromDate(2021, 1, 4),
"to" => null,
"type" => UserHistoryType::Employment,
"employment_form" => EmploymentForm::B2bContract,
"is_employed_at_current_company" => true,
]);

$technicalApprover = User::factory([
"email" => "maciej.ziolkowski@example.com",
Expand All @@ -105,9 +130,15 @@ public function run(): void
"last_name" => "Ziółkowski",
"employment_form" => EmploymentForm::BoardMemberContract,
"position" => "programista",
"employment_date" => Carbon::createFromDate(2021, 1, 4),
])
->create();
$technicalApprover->histories()->create([
"from" => Carbon::createFromDate(2021, 1, 4),
"to" => null,
"type" => UserHistoryType::Employment,
"employment_form" => EmploymentForm::BoardMemberContract,
"is_employed_at_current_company" => true,
]);

$administrativeApprover = User::factory([
"email" => "katarzyna.zajac@example.com",
Expand All @@ -119,11 +150,17 @@ public function run(): void
"last_name" => "Zając",
"employment_form" => EmploymentForm::EmploymentContract,
"position" => "dyrektor",
"employment_date" => Carbon::createFromDate(2021, 1, 4),
])
->create();
$administrativeApprover->histories()->create([
"from" => Carbon::createFromDate(2021, 1, 4),
"to" => null,
"type" => UserHistoryType::Employment,
"employment_form" => EmploymentForm::EmploymentContract,
"is_employed_at_current_company" => true,
]);

User::factory([
$administrator = User::factory([
"email" => "milosz.borowski@example.com",
"remember_token" => Str::random(10),
])
Expand All @@ -133,9 +170,15 @@ public function run(): void
"last_name" => "Borowski",
"employment_form" => EmploymentForm::EmploymentContract,
"position" => "administrator",
"employment_date" => Carbon::createFromDate(2021, 1, 4),
])
->create();
$administrator->histories()->create([
"from" => Carbon::createFromDate(2021, 1, 4),
"to" => null,
"type" => UserHistoryType::Employment,
"employment_form" => EmploymentForm::EmploymentContract,
"is_employed_at_current_company" => true,
]);

$users = User::all();

Expand Down
1 change: 0 additions & 1 deletion tests/Feature/ResumeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function testAdminCanCreateResumeForEmployee(): void
"last_name" => "Kowalski",
"employment_form" => EmploymentForm::EmploymentContract,
"position" => "user",
"employment_date" => Carbon::createFromDate(2021, 1, 4),
])->create();

$this->actingAs($admin)
Expand Down
6 changes: 0 additions & 6 deletions tests/Feature/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public function testAdminCanCreateUser(): void
"last_name" => "Doe",
"position" => "Test position",
"employment_form" => EmploymentForm::B2bContract->value,
"employment_date" => Carbon::now()->toDateString(),
"birthday" => Carbon::create(1990, 5, 31)->toDateString(),
]);
}
Expand All @@ -138,7 +137,6 @@ public function testItCreatesProperPermissionsWhileCreatingUser(): void
"position" => "Test position",
"email" => "john.doe@example.com",
"employmentForm" => EmploymentForm::B2bContract->value,
"employmentDate" => Carbon::now()->toDateString(),
"birthday" => Carbon::create(1990, 5, 31)->toDateString(),
])
->assertSessionHasNoErrors();
Expand Down Expand Up @@ -167,7 +165,6 @@ public function testAdminCanEditUser(): void
"first_name" => $user->profile->first_name,
"last_name" => $user->profile->last_name,
"employment_form" => $user->profile->employment_form->value,
"employment_date" => $user->profile->employment_date->toDateString(),
"birthday" => $user->profile->birthday->toDateString(),
]);

Expand All @@ -179,7 +176,6 @@ public function testAdminCanEditUser(): void
"role" => Role::Employee->value,
"position" => "Test position",
"employmentForm" => EmploymentForm::B2bContract->value,
"employmentDate" => Carbon::now()->toDateString(),
"birthday" => Carbon::create(1990, 5, 31)->toDateString(),
])
->assertSessionHasNoErrors();
Expand All @@ -196,7 +192,6 @@ public function testAdminCanEditUser(): void
"last_name" => "Doe",
"position" => "Test position",
"employment_form" => EmploymentForm::B2bContract->value,
"employment_date" => Carbon::now()->toDateString(),
"birthday" => Carbon::create(1990, 5, 31)->toDateString(),
]);
}
Expand Down Expand Up @@ -244,7 +239,6 @@ public function testChangingUserRoleUpdatesPermissions(): void
"role" => Role::TechnicalApprover->value,
"position" => "Test position",
"employmentForm" => EmploymentForm::B2bContract->value,
"employmentDate" => Carbon::now()->toDateString(),
"birthday" => Carbon::create(1990, 5, 31)->toDateString(),
])
->assertSessionHasNoErrors();
Expand Down

0 comments on commit 4e85098

Please sign in to comment.