dstpabuaran.com/app/Services/Admin/HR/LeaveRequestService.php
Yoga Pangestu 95be00c9d1 feat: add push notification functionality and service worker
- Implemented push notifications with a service worker (sw.ts) to handle caching and notifications.
- Added API routes for push subscription and notification management in routes/api.php.
- Created NotificationTest to validate notification service functionality and ensure correct notifications are sent based on user roles and actions.
- Updated Permissions component to manage notification permissions and subscriptions.
- Enhanced CategoryIndex component to highlight categories based on notifications.
- Excluded service worker from TypeScript compilation in tsconfig.json.
- Updated Vite configuration to include service worker in the build process.
2026-08-01 11:36:20 +07:00

138 lines
4.8 KiB
PHP

<?php
namespace App\Services\Admin\HR;
use App\Enums\LeaveRequestStatus;
use App\Models\LeaveRequest;
use App\Services\NotificationService;
use Carbon\Carbon;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class LeaveRequestService
{
public function getAll(array $filters = []): Collection
{
return LeaveRequest::select('id', 'employee_id', 'start_date', 'end_date', 'total_days', 'status', 'created_at')
->with(['employee.user.userProfile'])
->when($filters['status'] ?? null, function ($query, $status) {
$query->where('status', $status);
})
->latest()
->get();
}
public function paginated(int $perPage = 15, string $search = '', string $sort = 'created_at', string $direction = 'desc', array $filters = []): LengthAwarePaginator
{
return LeaveRequest::query()
->select('id', 'employee_id', 'start_date', 'end_date', 'total_days', 'status', 'created_at')
->with(['employee.user.userProfile'])
->when($search, fn ($q) => $q->whereHas('employee.user.userProfile', fn ($uq) => $uq->where('full_name', 'like', "%{$search}%")))
->when($filters['status'] ?? null, function ($query, $status) {
$query->where('status', $status);
})
->orderBy($sort, $direction)
->paginate($perPage);
}
public function create(array $data): LeaveRequest
{
$leaveRequest = DB::transaction(function () use ($data) {
$employee = auth()->user()->employee;
if (! $employee) {
throw new \Exception('Anda tidak terdaftar sebagai karyawan.');
}
$startDate = new Carbon($data['start_date']);
$endDate = new Carbon($data['end_date']);
$totalDays = $startDate->diffInDays($endDate) + 1;
return LeaveRequest::create([
'employee_id' => $employee->id,
'start_date' => $data['start_date'],
'end_date' => $data['end_date'],
'total_days' => $totalDays,
'status' => LeaveRequestStatus::PENDING,
]);
});
$leaveRequest->load('employee.user');
NotificationService::notify(
roles: ['Owner', 'Developer', 'Direktur', 'Admin Toko'],
title: 'Pengajuan Cuti Baru',
body: "Pengajuan cuti {$leaveRequest->total_days} hari oleh ".auth()->user()->full_name.'.',
url: route('admin.hr.leave-requests.index'),
additionalUser: $leaveRequest->employee->user ?? null,
);
return $leaveRequest;
}
public function update(LeaveRequest $leaveRequest, array $data): LeaveRequest
{
return DB::transaction(function () use ($leaveRequest, $data) {
$startDate = new Carbon($data['start_date']);
$endDate = new Carbon($data['end_date']);
$totalDays = $startDate->diffInDays($endDate) + 1;
$leaveRequest->update([
'start_date' => $data['start_date'],
'end_date' => $data['end_date'],
'total_days' => $totalDays,
]);
return $leaveRequest;
});
}
public function delete(LeaveRequest $leaveRequest): bool
{
return $leaveRequest->delete();
}
public function approve(LeaveRequest $leaveRequest): LeaveRequest
{
$leaveRequest->update([
'status' => LeaveRequestStatus::APPROVED,
'verified_by_id' => auth()->id(),
'verified_at' => now(),
]);
$employeeUser = $leaveRequest->employee->user ?? null;
NotificationService::notify(
roles: ['Owner', 'Developer', 'Direktur', 'Admin Toko'],
title: 'Cuti Disetujui',
body: "Cuti {$leaveRequest->employee->name} telah disetujui oleh ".auth()->user()->full_name.'.',
url: route('admin.hr.leave-requests.index'),
additionalUser: $employeeUser,
);
return $leaveRequest;
}
public function reject(LeaveRequest $leaveRequest): LeaveRequest
{
$leaveRequest->update([
'status' => LeaveRequestStatus::REJECTED,
'verified_by_id' => auth()->id(),
'verified_at' => now(),
]);
$employeeUser = $leaveRequest->employee->user ?? null;
NotificationService::notify(
roles: ['Owner', 'Developer', 'Direktur', 'Admin Toko'],
title: 'Cuti Ditolak',
body: "Cuti {$leaveRequest->employee->name} telah ditolak oleh ".auth()->user()->full_name.'.',
url: route('admin.hr.leave-requests.index'),
additionalUser: $employeeUser,
);
return $leaveRequest;
}
}