feat: add SendAttendanceReminderJob to notify users for attendance reminders
This commit is contained in:
parent
450e24b978
commit
c49027492c
72
app/Jobs/SendAttendanceReminderJob.php
Normal file
72
app/Jobs/SendAttendanceReminderJob.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Enums\Permission;
|
||||
use App\Models\Attendance;
|
||||
use App\Models\LeaveRequest;
|
||||
use App\Models\User;
|
||||
use App\Notifications\WebPushNotification;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class SendAttendanceReminderJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public int $tries = 3;
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$today = now()->toDateString();
|
||||
|
||||
$users = User::query()
|
||||
->select(['id', 'name'])
|
||||
->active()
|
||||
->whereHas('roles', function ($q) {
|
||||
$q->whereHas('permissions', fn($pq) => $pq->where('name', Permission::ATTENDANCES_CREATE->value));
|
||||
})
|
||||
->whereHas('employee')
|
||||
->get();
|
||||
|
||||
foreach ($users as $user) {
|
||||
$employee = $user->employee;
|
||||
|
||||
if (! $employee) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$hasAttendedToday = Attendance::where('employee_id', $employee->id)
|
||||
->where('attendance_date', $today)
|
||||
->exists();
|
||||
|
||||
if ($hasAttendedToday) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$isOnLeave = LeaveRequest::approved()
|
||||
->where('employee_id', $employee->id)
|
||||
->where('start_date', '<=', $today)
|
||||
->where('end_date', '>=', $today)
|
||||
->exists();
|
||||
|
||||
if ($isOnLeave) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$user->notify(new WebPushNotification(
|
||||
title: 'Reminder Presensi',
|
||||
body: 'Selamat pagi! Jangan lupa untuk melakukan presensi masuk hari ini.',
|
||||
url: route('admin.hr.attendances.index'),
|
||||
));
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Gagal mengirim reminder presensi ke user {$user->id}: {$e->getMessage()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -16,6 +16,7 @@ public function __construct(
|
||||
public string $title,
|
||||
public string $body,
|
||||
public string $icon = '/icon-192x192.png',
|
||||
public ?string $url = null,
|
||||
) {}
|
||||
|
||||
public function via(object $notifiable): array
|
||||
@ -25,9 +26,15 @@ public function via(object $notifiable): array
|
||||
|
||||
public function toWebPush(object $notifiable, mixed $notification): WebPushMessage
|
||||
{
|
||||
return (new WebPushMessage)
|
||||
$webPushMessage = (new WebPushMessage)
|
||||
->title($this->title)
|
||||
->icon($this->icon)
|
||||
->body($this->body);
|
||||
|
||||
if ($this->url) {
|
||||
$webPushMessage->data(['url' => $this->url]);
|
||||
}
|
||||
|
||||
return $webPushMessage;
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,11 +3,16 @@
|
||||
use App\Console\Commands\GeneratePayrollCommand;
|
||||
use App\Jobs\CheckAttendancePenaltiesJob;
|
||||
use App\Jobs\CleanupOrphanedMediaJob;
|
||||
use App\Jobs\SendAttendanceReminderJob;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Schedule;
|
||||
|
||||
Schedule::command(GeneratePayrollCommand::class)
|
||||
->daily()
|
||||
->when(fn () => now()->day === 5)
|
||||
->at('00:00');
|
||||
Schedule::job(new SendAttendanceReminderJob)
|
||||
->dailyAt('07:00')
|
||||
->when(fn () => now()->dayOfWeek !== Carbon::SUNDAY);
|
||||
Schedule::job(new CheckAttendancePenaltiesJob)->dailyAt('12:00');
|
||||
Schedule::job(new CleanupOrphanedMediaJob)->monthlyOn(1, '01:00');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user