79 lines
1.6 KiB
PHP
79 lines
1.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use App\Enums\Notification\Channel;
|
||
|
|
use App\Enums\Notification\Type;
|
||
|
|
use Database\Factories\NotificationFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
|
||
|
|
class Notification extends Model
|
||
|
|
{
|
||
|
|
/** @use HasFactory<NotificationFactory> */
|
||
|
|
use HasFactory, HasUuids;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var list<string>
|
||
|
|
*/
|
||
|
|
protected $fillable = [
|
||
|
|
'user_id',
|
||
|
|
'workspace_id',
|
||
|
|
'type',
|
||
|
|
'channel',
|
||
|
|
'title',
|
||
|
|
'body',
|
||
|
|
'data',
|
||
|
|
'read_at',
|
||
|
|
'archived_at',
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array<string, string>
|
||
|
|
*/
|
||
|
|
protected function casts(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'type' => Type::class,
|
||
|
|
'channel' => Channel::class,
|
||
|
|
'data' => 'array',
|
||
|
|
'read_at' => 'datetime',
|
||
|
|
'archived_at' => 'datetime',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
public function user(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function workspace(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Workspace::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function markAsRead(): void
|
||
|
|
{
|
||
|
|
$this->update(['read_at' => now()]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function archive(): void
|
||
|
|
{
|
||
|
|
$this->update(['archived_at' => now()]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function isRead(): bool
|
||
|
|
{
|
||
|
|
return $this->read_at !== null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function isArchived(): bool
|
||
|
|
{
|
||
|
|
return $this->archived_at !== null;
|
||
|
|
}
|
||
|
|
}
|