Files
Verde-Web/app/Notifications/Concerns/RoutesByPreferences.php

45 lines
1.2 KiB
PHP

<?php
namespace App\Notifications\Concerns;
use App\Models\NotificationPreference;
use App\Models\User;
use App\Notifications\Channels\PushChannel;
use App\Notifications\Channels\SmsChannel;
/**
* Picks notification channels based on the user's NotificationPreference.
* Hosts must declare a const PREF_KEY pointing to a column on
* notification_preferences (e.g., 'low_codes_warning'). If the user has
* disabled that category, no channels are returned.
*/
trait RoutesByPreferences
{
public function via(mixed $notifiable): array
{
if (! $notifiable instanceof User) {
return [];
}
$prefs = NotificationPreference::find($notifiable->id);
$key = static::PREF_KEY ?? null;
if ($key && $prefs && ! ($prefs->{$key} ?? true)) {
return [];
}
$channels = ['database'];
if ($prefs?->sms_enabled ?? true) {
if ($notifiable->phone) {
$channels[] = SmsChannel::class;
}
}
if ($prefs?->push_enabled ?? true) {
if ($notifiable->fcm_token) {
$channels[] = PushChannel::class;
}
}
return $channels;
}
}