feat: refactor EmployeeController and EmployeeService for improved user handling and password reset logic

This commit is contained in:
Yoga Pangestu 2026-07-30 10:34:32 +07:00
parent 917588eecf
commit 79da7df73c
3 changed files with 14 additions and 14 deletions

View File

@ -37,45 +37,47 @@ public function store(EmployeeRequest $request): RedirectResponse
); );
} }
public function edit(User $employee): Response public function edit(User $user): Response
{ {
$user->load(['userProfile', 'employee']);
return Inertia::render('admin/hr/employee/edit', [ return Inertia::render('admin/hr/employee/edit', [
'employee' => $this->service->getById($employee->id), 'employee' => $user,
]); ]);
} }
public function update(EmployeeRequest $request, User $employee): RedirectResponse public function update(EmployeeRequest $request, User $user): RedirectResponse
{ {
return $this->handleAction( return $this->handleAction(
fn () => $this->service->update($employee, $request->validated()), fn () => $this->service->update($user, $request->validated()),
'Pegawai berhasil diperbarui.', 'Pegawai berhasil diperbarui.',
'admin.hr.employees.index' 'admin.hr.employees.index'
); );
} }
public function destroy(User $employee): RedirectResponse public function destroy(User $user): RedirectResponse
{ {
return $this->handleAction( return $this->handleAction(
fn () => $this->service->delete($employee), fn () => $this->service->delete($user),
'Pegawai berhasil dihapus.', 'Pegawai berhasil dihapus.',
'admin.hr.employees.index' 'admin.hr.employees.index'
); );
} }
public function toggleActive(User $employee): RedirectResponse public function toggleActive(User $user): RedirectResponse
{ {
$this->service->toggleActive($employee); $this->service->toggleActive($user);
$status = $employee->fresh()->is_active ? 'diaktifkan' : 'dinonaktifkan'; $status = $user->fresh()->is_active ? 'diaktifkan' : 'dinonaktifkan';
Inertia::flash('toast', ['type' => 'success', 'message' => "Pegawai berhasil {$status}."]); Inertia::flash('toast', ['type' => 'success', 'message' => "Pegawai berhasil {$status}."]);
return to_route('admin.hr.employees.index'); return to_route('admin.hr.employees.index');
} }
public function resetPassword(User $employee): RedirectResponse public function resetPassword(User $user): RedirectResponse
{ {
return $this->handleAction( return $this->handleAction(
fn () => $this->service->resetPassword($employee), fn () => $this->service->resetPassword($user),
'Kata sandi pegawai berhasil direset ke kata sandi default.', 'Kata sandi pegawai berhasil direset ke kata sandi default.',
'admin.hr.employees.index' 'admin.hr.employees.index'
); );

View File

@ -20,8 +20,6 @@ class User extends Authenticatable
{ {
use HasFactory, HasRoles, Notifiable, SoftDeletes, TwoFactorAuthenticatable; use HasFactory, HasRoles, Notifiable, SoftDeletes, TwoFactorAuthenticatable;
protected $with = ['userProfile'];
protected function casts(): array protected function casts(): array
{ {
return [ return [

View File

@ -100,7 +100,7 @@ public function toggleActive(User $user): User
public function resetPassword(User $user): User public function resetPassword(User $user): User
{ {
$defaultPassword = config('auth.password_default', 'Minimal8@'); $defaultPassword = config('auth.password_default');
$user->update(['password' => $defaultPassword]); $user->update(['password' => $defaultPassword]);
return $user; return $user;