2026-04-03 04:34:21 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Observers;
|
|
|
|
|
|
|
|
|
|
use App\Models\Activity;
|
|
|
|
|
use App\Models\ActivityLog;
|
2026-04-03 07:57:40 +07:00
|
|
|
use App\Services\NotificationService;
|
2026-04-04 09:13:36 +07:00
|
|
|
use Filament\Notifications\Notification;
|
2026-04-03 04:34:21 +07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
|
|
class ActivityObserver
|
|
|
|
|
{
|
|
|
|
|
public function updated(Activity $activity): void
|
|
|
|
|
{
|
|
|
|
|
if ($activity->wasChanged('status')) {
|
|
|
|
|
$old = $activity->getOriginal('status');
|
|
|
|
|
$new = $activity->status;
|
|
|
|
|
|
|
|
|
|
// Validasi workflow: draft→pending, pending→approved/rejected
|
|
|
|
|
$allowed = [
|
|
|
|
|
'draft' => ['pending'],
|
|
|
|
|
'pending' => ['approved', 'rejected'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (isset($allowed[$old]) && ! in_array($new, $allowed[$old])) {
|
2026-04-04 09:13:36 +07:00
|
|
|
Notification::make()->title('Transisi tidak diizinkan')
|
|
|
|
|
->body("Status tidak bisa diubah dari {$old} ke {$new}.")
|
|
|
|
|
->danger()->send();
|
|
|
|
|
$activity->status = $old;
|
|
|
|
|
return;
|
2026-04-03 04:34:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($new === 'approved' && $activity->wasChanged('executed_at') && empty($activity->execution_notes)) {
|
2026-04-04 09:13:36 +07:00
|
|
|
Notification::make()->title('Catatan pelaksanaan wajib diisi')
|
|
|
|
|
->danger()->send();
|
|
|
|
|
$activity->executed_at = null;
|
|
|
|
|
return;
|
2026-04-03 04:34:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ActivityLog::create([
|
|
|
|
|
'user_id' => Auth::id(),
|
|
|
|
|
'action' => 'status_changed',
|
|
|
|
|
'model_type' => Activity::class,
|
|
|
|
|
'model_id' => $activity->id,
|
|
|
|
|
'description' => "Status kegiatan '{$activity->title}' diubah dari {$old} menjadi {$new}",
|
|
|
|
|
]);
|
2026-04-03 07:57:40 +07:00
|
|
|
|
|
|
|
|
if ($new === 'pending') {
|
|
|
|
|
NotificationService::toRole('ketua', 'Kegiatan Menunggu Persetujuan',
|
|
|
|
|
"Kegiatan \"{$activity->title}\" diajukan untuk disetujui.", 'warning',
|
|
|
|
|
route('filament.admin.resources.activities.edit', $activity));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 09:13:36 +07:00
|
|
|
if (in_array($new, ['approved', 'rejected']) && $activity->creator) {
|
2026-04-03 07:57:40 +07:00
|
|
|
NotificationService::send(
|
|
|
|
|
$activity->creator,
|
|
|
|
|
$new === 'approved' ? 'Kegiatan Disetujui' : 'Kegiatan Ditolak',
|
|
|
|
|
"Kegiatan \"{$activity->title}\" telah " . ($new === 'approved' ? 'disetujui.' : 'ditolak.'),
|
|
|
|
|
$new === 'approved' ? 'success' : 'danger',
|
|
|
|
|
route('filament.admin.resources.activities.edit', $activity)
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-03 04:34:21 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function created(Activity $activity): void
|
|
|
|
|
{
|
|
|
|
|
ActivityLog::create([
|
|
|
|
|
'user_id' => Auth::id(),
|
|
|
|
|
'action' => 'created',
|
|
|
|
|
'model_type' => Activity::class,
|
|
|
|
|
'model_id' => $activity->id,
|
|
|
|
|
'description' => "Kegiatan baru dibuat: {$activity->title}",
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|