trypost/app/Http/Controllers/App/NotificationController.php
Paulo Castellano 74c6442728 refactor: code review fixes — policies, enums, data_get, tests
- Refactor WorkspacePolicy to use pivot role instead of workspace.user_id
- Add manageBilling policy (owner only) to BillingController
- Fix ApiKeyController authorization (view → manageTeam for store/destroy)
- Fix WorkspaceInviteController using workspace.user_id for owner checks
- Fix WorkspaceController settings is_owner using workspace.user_id
- Create PostAction enum for UpdatePost/PostController action strings
- Create ApiToken\Status enum
- Add User::SUBSCRIPTION_NAME constant, replace all hardcoded 'default'
- Convert wantsEmailFor to accept NotificationType enum
- Convert all $data[] to data_get() across publishers, controllers, jobs
- Fix SocialLoginController callback missing try/catch
- Fix SocialController::toggleActive missing workspace null check
- Fix UpdatePost NPE on meta merge when postPlatform not found
- Remove HTML5 required attributes from form inputs
- Convert function declarations to arrow functions in Vue components
- Replace hardcoded URLs with Wayfinder route helpers
- Replace new Date() with dayjs
- Add 16 new test files covering policies, authorization, publishing
2026-03-31 00:40:18 -03:00

87 lines
2.3 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;
if (! $workspace) {
return response()->json(['notifications' => [], 'unread_count' => 0]);
}
$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;
if (! $workspace) {
return response()->json(['success' => true]);
}
$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;
if (! $workspace) {
return response()->json(['success' => true]);
}
$request->user()
->notifications()
->where('workspace_id', $workspace->id)
->whereNull('archived_at')
->update(['archived_at' => now()]);
return response()->json(['success' => true]);
}
}