From 245d636cd430e8cc03dbd16d2f9eb66da5c71d72 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Tue, 14 Jul 2026 19:22:27 +0700 Subject: [PATCH] feat: enhance employee management by adding notification support for create, update, delete, and status toggle actions --- app/Enums/Role.php | 6 ++ .../Admin/Hr/EmployeeController.php | 14 ++--- app/Services/Hr/EmployeeService.php | 60 +++++++++++++++++-- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/app/Enums/Role.php b/app/Enums/Role.php index 8ee6494..8f0b2fa 100644 --- a/app/Enums/Role.php +++ b/app/Enums/Role.php @@ -261,6 +261,12 @@ public function permissions(): array self::ADMIN_BAHAN_BAKU => [ Permission::DASHBOARD_VIEW, + Permission::EMPLOYEES_VIEW, + Permission::EMPLOYEES_CREATE, + Permission::EMPLOYEES_UPDATE, + Permission::EMPLOYEES_TOGGLE_STATUS, + Permission::EMPLOYEES_DELETE, + Permission::LEAVE_REQUESTS_VIEW, Permission::LEAVE_REQUESTS_CREATE, Permission::LEAVE_REQUESTS_UPDATE, diff --git a/app/Http/Controllers/Admin/Hr/EmployeeController.php b/app/Http/Controllers/Admin/Hr/EmployeeController.php index 73f4fe2..e3575b4 100644 --- a/app/Http/Controllers/Admin/Hr/EmployeeController.php +++ b/app/Http/Controllers/Admin/Hr/EmployeeController.php @@ -64,7 +64,7 @@ public function create(): Response public function store(EmployeeRequest $request): RedirectResponse { - $this->employeeService->create($request->validated()); + $this->employeeService->create($request->validated(), $request->user()); $this->flashCreated('Pegawai'); @@ -86,16 +86,16 @@ public function edit(User $user): Response public function update(EmployeeRequest $request, User $user): RedirectResponse { - $this->employeeService->update($user, $request->validated()); + $this->employeeService->update($user, $request->validated(), $request->user()); $this->flashUpdated('Pegawai'); return redirect()->route('admin.hr.employees.index'); } - public function destroy(User $user): RedirectResponse + public function destroy(User $user, Request $request): RedirectResponse { - $this->employeeService->delete($user); + $this->employeeService->delete($user, $request->user()); $this->flashDeleted('Pegawai'); @@ -104,16 +104,16 @@ public function destroy(User $user): RedirectResponse public function toggleStatus(ToggleStatusRequest $request, User $user): RedirectResponse { - $this->employeeService->toggleStatus($user, $request->validated()); + $this->employeeService->toggleStatus($user, $request->validated(), $request->user()); $this->flashStatusUpdated('Pegawai'); return redirect()->route('admin.hr.employees.index'); } - public function resetPassword(User $user): RedirectResponse + public function resetPassword(User $user, Request $request): RedirectResponse { - $this->employeeService->resetPassword($user); + $this->employeeService->resetPassword($user, $request->user()); $this->flashSuccess('Kata sandi pegawai berhasil direset.'); diff --git a/app/Services/Hr/EmployeeService.php b/app/Services/Hr/EmployeeService.php index f5d7248..bb14dba 100644 --- a/app/Services/Hr/EmployeeService.php +++ b/app/Services/Hr/EmployeeService.php @@ -10,6 +10,7 @@ use App\Services\Concerns\RunsInTransaction; use App\Services\Concerns\SyncsPhotos; use App\Services\Media\MediaService; +use App\Services\System\PushNotificationService; use App\Support\Media\MediaPresenter; use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Database\Eloquent\Builder; @@ -22,6 +23,7 @@ class EmployeeService public function __construct( private readonly MediaService $mediaService, + private readonly PushNotificationService $pushNotificationService, ) {} public function paginateForIndex( @@ -78,10 +80,10 @@ public function findForEdit(User $user): array ]; } - public function create(array $validated): void + public function create(array $validated, User $user): void { $this->runInTransaction( - function () use ($validated): void { + function () use ($validated): User { $user = User::create([ 'email' => $validated['email'], 'username' => $validated['username'], @@ -109,14 +111,22 @@ function () use ($validated): void { } $user->syncRoles([$validated['role']]); + + return $user; }, 'Gagal membuat karyawan', ); $this->cacheForgetByPattern('hr:employees:*'); + + $this->notifyOwner( + 'Tambah Pegawai', + "Pegawai '{$validated['full_name']}' telah ditambahkan oleh {$user->profile?->full_name}.", + route('admin.hr.employees.index'), + ); } - public function update(User $user, array $validated): void + public function update(User $user, array $validated, User $authUser): void { $employee = $user->employee; @@ -169,9 +179,15 @@ function () use ($validated, $user, $employee): void { ); $this->cacheForgetByPattern('hr:employees:*'); + + $this->notifyOwner( + 'Ubah Pegawai', + "Pegawai '{$user->profile?->full_name}' telah diperbarui oleh {$authUser->profile?->full_name}.", + route('admin.hr.employees.index'), + ); } - public function toggleStatus(User $user, array $validated): void + public function toggleStatus(User $user, array $validated, User $authUser): void { $this->runInTransaction( function () use ($user, $validated): void { @@ -187,9 +203,17 @@ function () use ($user, $validated): void { ); $this->cacheForgetByPattern('hr:employees:*'); + + $statusLabel = $validated['is_active'] ? 'aktif' : 'nonaktif'; + + $this->notifyOwner( + 'Ubah Status Pegawai', + "Status pegawai '{$user->profile?->full_name}' telah diubah menjadi {$statusLabel} oleh {$authUser->profile?->full_name}.", + route('admin.hr.employees.index'), + ); } - public function resetPassword(User $user): void + public function resetPassword(User $user, User $authUser): void { $this->runInTransaction( function () use ($user): void { @@ -203,10 +227,18 @@ function () use ($user): void { ); $this->cacheForgetByPattern('hr:employees:*'); + + $this->notifyOwner( + 'Reset Kata Sandi Pegawai', + "Kata sandi pegawai '{$user->profile?->full_name}' telah direset oleh {$authUser->profile?->full_name}.", + route('admin.hr.employees.index'), + ); } - public function delete(User $user): void + public function delete(User $user, User $authUser): void { + $name = $user->profile?->full_name; + $this->runInTransaction( function () use ($user): void { $user->employee?->delete(); @@ -217,6 +249,22 @@ function () use ($user): void { ); $this->cacheForgetByPattern('hr:employees:*'); + + $this->notifyOwner( + 'Hapus Pegawai', + "Pegawai '{$name}' telah dihapus oleh {$authUser->profile?->full_name}.", + route('admin.hr.employees.index'), + ); + } + + private function notifyOwner(string $typeLabel, string $body, string $url): void + { + $this->pushNotificationService->sendToRoles( + "📦 {$typeLabel}", + $body, + ['owner', 'developer', 'direktur'], + $url, + ); } private function syncProfilePhoto(UserProfile $profile, array $validated): void