- Implemented push notifications with a service worker (sw.ts) to handle caching and notifications. - Added API routes for push subscription and notification management in routes/api.php. - Created NotificationTest to validate notification service functionality and ensure correct notifications are sent based on user roles and actions. - Updated Permissions component to manage notification permissions and subscriptions. - Enhanced CategoryIndex component to highlight categories based on notifications. - Excluded service worker from TypeScript compilation in tsconfig.json. - Updated Vite configuration to include service worker in the build process.
71 lines
1.9 KiB
PHP
71 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\AppNotification;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class NotificationController extends Controller
|
|
{
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$notifications = $request->user()
|
|
->notifications()
|
|
->orderBy('created_at', 'desc')
|
|
->limit(20)
|
|
->get();
|
|
|
|
return response()->json($notifications);
|
|
}
|
|
|
|
public function unreadCount(Request $request): JsonResponse
|
|
{
|
|
$count = $request->user()
|
|
->notifications()
|
|
->where('is_read', false)
|
|
->count();
|
|
|
|
return response()->json(['count' => $count]);
|
|
}
|
|
|
|
public function markAsRead(Request $request, AppNotification $notification): JsonResponse
|
|
{
|
|
if ($notification->user_id !== $request->user()->id) {
|
|
return response()->json(['message' => 'Unauthorized'], 403);
|
|
}
|
|
|
|
$notification->update([
|
|
'is_read' => true,
|
|
'read_at' => now(),
|
|
]);
|
|
|
|
return response()->json(['message' => 'Notification marked as read.']);
|
|
}
|
|
|
|
public function destroy(Request $request, AppNotification $notification): JsonResponse
|
|
{
|
|
if ($notification->user_id !== $request->user()->id) {
|
|
return response()->json(['message' => 'Unauthorized'], 403);
|
|
}
|
|
|
|
$notification->delete();
|
|
|
|
return response()->json(['message' => 'Notification deleted.']);
|
|
}
|
|
|
|
public function markAllAsRead(Request $request): JsonResponse
|
|
{
|
|
$request->user()
|
|
->notifications()
|
|
->where('is_read', false)
|
|
->update([
|
|
'is_read' => true,
|
|
'read_at' => now(),
|
|
]);
|
|
|
|
return response()->json(['message' => 'All notifications marked as read.']);
|
|
}
|
|
}
|