trypost/app/Models/Notification.php
Paulo Castellano 09e37f2879 feat: notification system with SendNotification job, dialog UI, tests
Backend:
- Create notifications table (user_id, workspace_id, type, channel,
  title, body, data JSON, read_at, archived_at)
- Create Notification model with Type enum (post_failed,
  account_disconnected, invite_received, member_joined, member_removed)
  and Channel enum (email, in_app, both)
- Create SendNotification job: isolated from publish flow, handles
  saving in-app notification and sending email independently
- NotificationController: index (excludes archived, scoped to workspace),
  markAsRead, markAllAsRead, archiveAll
- Integrate with PublishToSocialPlatform (post failed/partial)
- Integrate with VerifyWorkspaceConnections (batch disconnection)
- Integrate with SocialAccount::markAsDisconnected (single disconnection)
- All use SendNotification::dispatch() instead of direct Mail::to()

Frontend:
- NotificationBell component in sidebar footer with unread badge
- Dialog with notification list, mark as read, mark all read, archive all
- Click navigates to relevant page (post edit, accounts)
- i18n for notifications UI (en, es, pt-BR)

Tests:
- 8 tests for NotificationController (auth, CRUD, workspace scoping)
- 4 tests for SendNotification job (channels, email, data storage)

All 745 tests passing.
2026-03-30 16:47:03 -03:00

78 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;
}
}