feat: enhance employee management by adding notification support for create, update, delete, and status toggle actions

This commit is contained in:
Yoga Pangestu 2026-07-14 19:22:27 +07:00
parent 9da452d505
commit 245d636cd4
3 changed files with 67 additions and 13 deletions

View File

@ -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,

View File

@ -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.');

View File

@ -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