Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: "8.3"
php-version: "8.4"
- run: composer install --no-interaction --prefer-dist
- run: ./vendor/bin/pint --test

Expand Down
77 changes: 77 additions & 0 deletions app/Http/Controllers/NotificationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Http\Controllers;

use App\Services\NotificationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
use Illuminate\Http\RedirectResponse;

class NotificationController extends Controller
{
public function __construct(
private readonly NotificationService $service
) {}

public function index(Request $request): JsonResponse
{
$notifications = $this->service->getUserNotifications(
user: $request->user(),
filters: [
'read' => $request->has('read')
? $request->boolean('read')
: null,
'type' => $request->input('type'),
],
pagination: 5
);

return response()->json($notifications);
}

public function unreadCount(Request $request): array
{
return [
'count' => $this->service->getUnreadCount(
$request->user()
),
];
}

public function markAsRead(
Request $request,
string $id
): RedirectResponse {
$success = $this->service->markAsRead(
$request->user(),
$id
);

if (!$success) {
return back()->with(
'error',
'Notification not found.'
);
}

return back()->with(
'success',
'Notification marked as read.'
);
}

public function markAllAsRead(
Request $request
): RedirectResponse {
$this->service->markAllAsRead(
$request->user()
);

return back()->with(
'success',
'All notifications marked as read.'
);
}
}
16 changes: 14 additions & 2 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,30 @@ public function share(Request $request): array
'currentRoute' => Route::currentRouteName(),
],
'notifications' => function () use ($request) {
if (! $request->user()) {
if (!$request->user()) {
return [];
}

return $request->user()
->unreadNotifications
->unreadNotifications()
->latest()
->limit(5)
->get()
->map(fn ($notification) => [
'id' => $notification->id,
'data' => $notification->data,
'created_at' => $notification->created_at,
]);
},
'allUnreadCount' => function () use ($request) {
if (!$request->user()) {
return 0;
}

return $request->user()
->unreadNotifications()
->count();
},
];
}
}
71 changes: 71 additions & 0 deletions app/Services/NotificationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Services;

use App\Models\User;
use Illuminate\Notifications\DatabaseNotification;

class NotificationService
{
public function getUserNotifications(
User $user,
array $filters = [],
int $pagination = 5
): array {
$query = $user->notifications()->latest();

if (isset($filters['read'])) {
$filters['read']
? $query->whereNotNull('read_at')
: $query->whereNull('read_at');
}

if (!empty($filters['type'])) {
$query->where('type', $filters['type']);
}

$notifications = $query->paginate($pagination);

return [
'userNotifications' => $notifications->items(),
'page' => $notifications->currentPage(),
'pageCount' => $notifications->lastPage(),
'total' => $notifications->total(),
'unreadNotificationsCount' => $user->unreadNotifications()->count(),
];
}

public function getUnreadCount(User $user): int
{
return $user->unreadNotifications()->count();
}

public function markAsRead(User $user, string $notificationId): bool
{
$notification = $this->findNotification($user, $notificationId);

if (!$notification) {
return false;
}

if (!$notification->read_at) {
$notification->markAsRead();
}

return true;
}

public function markAllAsRead(User $user): void
{
$user->unreadNotifications->markAsRead();
}

private function findNotification(
User $user,
string $notificationId
): ?DatabaseNotification {
return $user->notifications()
->where('id', $notificationId)
->first();
}
}
7 changes: 7 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use App\Http\Controllers\DocumentController;
use App\Http\Controllers\GroupController;
use App\Http\Controllers\NoticeController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\OpeningController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ProjectController;
Expand Down Expand Up @@ -68,6 +69,12 @@
Route::get('editais/{notice}/projetos/{project}', [ProjectController::class, 'projectDetail'])
->scopeBindings()
->name('notices.projects.show');
Route::prefix('notificacoes')->group(function () {
Route::get('/', [NotificationController::class, 'index'])->name('notifications.index');
Route::get('/nao-lidas', [NotificationController::class, 'unreadCount'])->name('notifications.unread-count');
Route::patch('/{id}/ler', [NotificationController::class, 'markAsRead'])->name('notifications.mark-read');
Route::patch('/ler-todas', [NotificationController::class, 'markAllAsRead'])->name('notifications.mark-all-read');
});
});

Route::middleware('auth')->group(function () {
Expand Down
Loading
Loading