trypost/app/Http/Controllers/App/NotificationController.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

75 lines
2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\App;
use App\Models\Notification;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class NotificationController extends Controller
{
public function index(Request $request): JsonResponse
{
$workspace = $request->user()->currentWorkspace;
$notifications = $request->user()
->notifications()
->where('workspace_id', $workspace->id)
->whereNull('archived_at')
->latest()
->limit(50)
->get();
$unreadCount = $request->user()
->notifications()
->where('workspace_id', $workspace->id)
->whereNull('archived_at')
->whereNull('read_at')
->count();
return response()->json([
'notifications' => $notifications,
'unread_count' => $unreadCount,
]);
}
public function markAsRead(Request $request, Notification $notification): JsonResponse
{
if ($notification->user_id !== $request->user()->id) {
abort(Response::HTTP_FORBIDDEN);
}
$notification->markAsRead();
return response()->json(['success' => true]);
}
public function markAllAsRead(Request $request): JsonResponse
{
$workspace = $request->user()->currentWorkspace;
$request->user()
->notifications()
->where('workspace_id', $workspace->id)
->whereNull('read_at')
->update(['read_at' => now()]);
return response()->json(['success' => true]);
}
public function archiveAll(Request $request): JsonResponse
{
$workspace = $request->user()->currentWorkspace;
$request->user()
->notifications()
->where('workspace_id', $workspace->id)
->whereNull('archived_at')
->update(['archived_at' => now()]);
return response()->json(['success' => true]);
}
}