trypost/app/Jobs/SendNotification.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

68 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Enums\Notification\Channel;
use App\Enums\Notification\Type;
use App\Models\Notification;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class SendNotification implements ShouldQueue
{
use Queueable;
public int $tries = 3;
public int $backoff = 10;
/**
* @param array<string, mixed>|null $data
*/
public function __construct(
public User $user,
public string $workspaceId,
public Type $type,
public Channel $channel,
public string $title,
public string $body,
public ?array $data = null,
public ?Mailable $mailable = null,
) {}
public function handle(): void
{
// Save in-app notification
if ($this->channel !== Channel::Email) {
Notification::create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspaceId,
'type' => $this->type,
'channel' => $this->channel,
'title' => $this->title,
'body' => $this->body,
'data' => $this->data,
]);
}
// Send email
if ($this->mailable && $this->channel !== Channel::InApp) {
Mail::to($this->user)->send($this->mailable);
}
}
public function failed(\Throwable $exception): void
{
Log::error('SendNotification job failed', [
'user_id' => $this->user->id,
'type' => $this->type->value,
'error' => $exception->getMessage(),
]);
}
}