gps_system/app/Models/NotificationSetting.php
sackey 6b878bb0a0
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
Initial commit
2025-09-12 16:19:56 +00:00

103 lines
2.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class NotificationSetting extends Model
{
protected $fillable = [
'user_id',
'email_enabled',
'sms_enabled',
'push_enabled',
'webhook_enabled',
'webhook_url',
'sms_provider',
'sms_config',
'event_types',
'device_filters',
'quiet_hours_start',
'quiet_hours_end',
'allowed_days',
];
protected $casts = [
'email_enabled' => 'boolean',
'sms_enabled' => 'boolean',
'push_enabled' => 'boolean',
'webhook_enabled' => 'boolean',
'sms_config' => 'array',
'event_types' => 'array',
'device_filters' => 'array',
'allowed_days' => 'array',
'quiet_hours_start' => 'datetime:H:i',
'quiet_hours_end' => 'datetime:H:i',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Check if notifications are allowed at the current time
*/
public function areNotificationsAllowed(): bool
{
// Check quiet hours
if ($this->quiet_hours_start && $this->quiet_hours_end) {
$now = now()->format('H:i');
$start = $this->quiet_hours_start->format('H:i');
$end = $this->quiet_hours_end->format('H:i');
if ($start <= $end) {
// Same day range
if ($now >= $start && $now <= $end) {
return false;
}
} else {
// Overnight range
if ($now >= $start || $now <= $end) {
return false;
}
}
}
// Check allowed days
if ($this->allowed_days && !empty($this->allowed_days)) {
$today = now()->dayOfWeek; // 0 = Sunday, 6 = Saturday
if (!in_array($today, $this->allowed_days)) {
return false;
}
}
return true;
}
/**
* Check if notifications are enabled for a specific event type
*/
public function isEventTypeEnabled(string $eventType): bool
{
if (!$this->event_types || empty($this->event_types)) {
return true; // If no filter, allow all
}
return in_array($eventType, $this->event_types);
}
/**
* Check if notifications are enabled for a specific device
*/
public function isDeviceEnabled(int $deviceId): bool
{
if (!$this->device_filters || empty($this->device_filters)) {
return true; // If no filter, allow all
}
return in_array($deviceId, $this->device_filters);
}
}