e2f7d7f10f
- Tambah FilamentUser interface di User model - Tambah canAccessPanel() method - Tambah VoteObserver: broadcast notifikasi ke semua user saat vote baru - Tambah NotificationService::toAll() - Fix Vote model: auto-fill created_by via booted() - Fix CashRecordObserver: fallback created_by untuk Vote
42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\User;
|
|
use Filament\Notifications\Notification;
|
|
|
|
class NotificationService
|
|
{
|
|
public static function send(User|iterable $recipients, string $title, string $body, string $color = 'info', ?string $url = null): void
|
|
{
|
|
$notification = Notification::make()
|
|
->title($title)
|
|
->body($body)
|
|
->color($color);
|
|
|
|
if ($url) {
|
|
$notification->actions([
|
|
\Filament\Actions\Action::make('lihat')
|
|
->label('Lihat')
|
|
->url($url),
|
|
]);
|
|
}
|
|
|
|
$notification->sendToDatabase(
|
|
$recipients instanceof User ? collect([$recipients]) : collect($recipients)
|
|
);
|
|
}
|
|
|
|
public static function toRole(string $role, string $title, string $body, string $color = 'info', ?string $url = null): void
|
|
{
|
|
$users = User::role($role)->get();
|
|
if ($users->isEmpty()) return;
|
|
self::send($users, $title, $body, $color, $url);
|
|
}
|
|
|
|
public static function toAll(string $title, string $body, string $color = 'info', ?string $url = null): void
|
|
{
|
|
self::send(User::all(), $title, $body, $color, $url);
|
|
}
|
|
}
|