feat: implement DegreeLevel enum and integrate it into DepartmentRequest and DepartmentService
This commit is contained in:
parent
8055d4ec5d
commit
f00fab9520
27
app/Enums/DegreeLevel.php
Normal file
27
app/Enums/DegreeLevel.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
use App\Enums\Concerns\HasValues;
|
||||
|
||||
enum DegreeLevel: string
|
||||
{
|
||||
use HasValues;
|
||||
|
||||
case D3 = 'D3';
|
||||
case D4 = 'D4';
|
||||
case S1 = 'S1';
|
||||
case S2 = 'S2';
|
||||
case S3 = 'S3';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::D3 => 'D3 (Diploma Tiga)',
|
||||
self::D4 => 'D4 (Diploma Empat)',
|
||||
self::S1 => 'S1 (Sarjana)',
|
||||
self::S2 => 'S2 (Magister)',
|
||||
self::S3 => 'S3 (Doktor)',
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Requests\Admin\Master;
|
||||
|
||||
use App\Enums\DegreeLevel;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
@ -22,7 +23,7 @@ public function rules(): array
|
||||
Rule::unique('departments')->ignore($this->route('department')),
|
||||
],
|
||||
'name' => ['required', 'string', 'max:100'],
|
||||
'degree_level' => ['nullable', 'string', Rule::in(['D3', 'D4', 'S1', 'S2', 'S3'])],
|
||||
'degree_level' => ['nullable', 'string', Rule::enum(DegreeLevel::class)],
|
||||
'lecturer_id' => [
|
||||
'nullable',
|
||||
Rule::requiredIf($this->route('department') !== null),
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\DegreeLevel;
|
||||
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@ -15,6 +16,13 @@ class Department extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'degree_level' => DegreeLevel::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function currentLeader(): HasOne
|
||||
{
|
||||
return $this->hasOne(DepartmentLeadership::class)->whereNull('ended_at');
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services\Admin\Master;
|
||||
|
||||
use App\Enums\UserRole;
|
||||
use App\Models\Department;
|
||||
use App\Models\DepartmentLeadership;
|
||||
use App\Models\Lecturer;
|
||||
@ -10,19 +11,13 @@
|
||||
|
||||
class DepartmentService
|
||||
{
|
||||
public function getAllForSelect(): Collection
|
||||
{
|
||||
return Department::select(['id', 'code', 'name', 'degree_level'])->get();
|
||||
}
|
||||
|
||||
public function paginated(int $perPage = 25, string $search = ''): LengthAwarePaginator
|
||||
{
|
||||
return Department::query()
|
||||
->select(['id', 'code', 'name', 'degree_level'])
|
||||
->with(['currentLeader.lecturer.user.profile'])
|
||||
->when($search, fn ($q) => $q->where('name', 'like', "%{$search}%")->orWhere('code', 'like', "%{$search}%"))
|
||||
->latest()
|
||||
->paginate($perPage);
|
||||
->paginate($perPage, ['id', 'code', 'name', 'degree_level']);
|
||||
}
|
||||
|
||||
public function create(array $data): Department
|
||||
@ -40,16 +35,27 @@ public function create(array $data): Department
|
||||
|
||||
public function update(Department $department, array $data): Department
|
||||
{
|
||||
$department->code = $data['code'];
|
||||
$department->name = $data['name'];
|
||||
$department->degree_level = $data['degree_level'] ?? null;
|
||||
$department->update();
|
||||
$department->update([
|
||||
'code' => $data['code'],
|
||||
'name' => $data['name'],
|
||||
'degree_level' => $data['degree_level'] ?? null,
|
||||
]);
|
||||
|
||||
$this->syncLeadership($department, $data['lecturer_id'] ?? null, $data['leadership_started_at'] ?? null);
|
||||
|
||||
return $department;
|
||||
}
|
||||
|
||||
public function delete(Department $department): bool
|
||||
{
|
||||
return $department->delete();
|
||||
}
|
||||
|
||||
public function getAllForSelect(): Collection
|
||||
{
|
||||
return Department::get(['id', 'code', 'name', 'degree_level']);
|
||||
}
|
||||
|
||||
private function syncLeadership(Department $department, ?int $lecturerId, ?string $startedAt): void
|
||||
{
|
||||
$currentLeader = DepartmentLeadership::query()
|
||||
@ -90,8 +96,8 @@ private function grantKaprodiRole(int $lecturerId): void
|
||||
{
|
||||
$user = Lecturer::find($lecturerId)?->user;
|
||||
|
||||
if ($user && ! $user->hasRole('kaprodi')) {
|
||||
$user->assignRole('kaprodi');
|
||||
if ($user && ! $user->hasRole(UserRole::Kaprodi->value)) {
|
||||
$user->assignRole(UserRole::Kaprodi->value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -103,12 +109,7 @@ private function revokeKaprodiRoleIfNoLongerLeading(int $lecturerId): void
|
||||
->exists();
|
||||
|
||||
if (! $stillLeadsAnyDepartment) {
|
||||
Lecturer::find($lecturerId)?->user?->removeRole('kaprodi');
|
||||
Lecturer::find($lecturerId)?->user?->removeRole(UserRole::Kaprodi->value);
|
||||
}
|
||||
}
|
||||
|
||||
public function delete(Department $department): bool
|
||||
{
|
||||
return $department->delete();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user