144 lines
4.1 KiB
PHP
144 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Services\NotificacionService;
|
|
|
|
class NotificacionController extends Controller
|
|
{
|
|
private NotificacionService $service;
|
|
|
|
public function __construct(NotificacionService $service)
|
|
{
|
|
$this->service = $service;
|
|
}
|
|
|
|
// ── Helper: usuario logueado ──
|
|
private function getUserSession(): ?array
|
|
{
|
|
if (!session()->has('user_logged_in')) return null;
|
|
return [
|
|
'tipo' => session('user_tipo'),
|
|
'id' => session('user_id'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* GET /notificaciones — Listado completo paginado
|
|
*/
|
|
public function index()
|
|
{
|
|
$u = $this->getUserSession();
|
|
if (!$u) return redirect('/')->with('panel_error', 'Debés iniciar sesión.');
|
|
|
|
$notificaciones = $this->service->obtenerTodas($u['tipo'], $u['id']);
|
|
$totalNoLeidas = $this->service->contarNoLeidas($u['tipo'], $u['id']);
|
|
|
|
return view('notificaciones.index', compact('notificaciones', 'totalNoLeidas'));
|
|
}
|
|
|
|
/**
|
|
* POST /notificaciones/{id}/leer — Marcar una como leída
|
|
*/
|
|
public function marcarLeida(int $id)
|
|
{
|
|
$u = $this->getUserSession();
|
|
if (!$u) return response()->json(['ok' => false], 401);
|
|
|
|
$this->service->marcarLeida($id, $u['tipo'], $u['id']);
|
|
return response()->json(['ok' => true]);
|
|
}
|
|
|
|
/**
|
|
* POST /notificaciones/leer-todas — Marcar todas como leídas
|
|
*/
|
|
public function marcarTodasLeidas()
|
|
{
|
|
$u = $this->getUserSession();
|
|
if (!$u) return response()->json(['ok' => false], 401);
|
|
|
|
$count = $this->service->marcarTodasLeidas($u['tipo'], $u['id']);
|
|
return response()->json(['ok' => true, 'marcadas' => $count]);
|
|
}
|
|
|
|
/**
|
|
* GET /notificaciones/count — Badge AJAX (devuelve JSON {count: N})
|
|
*/
|
|
public function count()
|
|
{
|
|
$u = $this->getUserSession();
|
|
if (!$u) return response()->json(['count' => 0]);
|
|
|
|
return response()->json(['count' => $this->service->contarNoLeidas($u['tipo'], $u['id'])]);
|
|
}
|
|
|
|
/**
|
|
* GET /notificaciones/latest — ID de la última no leída
|
|
*/
|
|
public function latest()
|
|
{
|
|
$u = $this->getUserSession();
|
|
if (!$u) return response()->json(['id' => 0]);
|
|
|
|
$notif = $this->service->obtenerNoLeidas($u['tipo'], $u['id'])->first();
|
|
return response()->json([
|
|
'id' => $notif ? $notif->id : 0,
|
|
'titulo' => $notif ? $notif->titulo : '',
|
|
'mensaje' => $notif ? $notif->mensaje : ''
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* DELETE /notificaciones/{id} — Eliminar una notificación
|
|
*/
|
|
public function eliminar(int $id)
|
|
{
|
|
$u = $this->getUserSession();
|
|
if (!$u) return response()->json(['ok' => false], 401);
|
|
|
|
$this->service->eliminar($id, $u['tipo'], $u['id']);
|
|
return response()->json(['ok' => true]);
|
|
}
|
|
|
|
/**
|
|
* DELETE /notificaciones — Eliminar todas
|
|
*/
|
|
public function eliminarTodas()
|
|
{
|
|
$u = $this->getUserSession();
|
|
if (!$u) return response()->json(['ok' => false], 401);
|
|
|
|
$this->service->eliminarTodas($u['tipo'], $u['id']);
|
|
return response()->json(['ok' => true]);
|
|
}
|
|
/**
|
|
* POST /notificaciones/subscribe — Guardar suscripción para Web Push
|
|
*/
|
|
public function subscribe(Request $request)
|
|
{
|
|
$u = $this->getUserSession();
|
|
if (!$u) return response()->json(['ok' => false, 'error' => 'No session'], 401);
|
|
|
|
$request->validate([
|
|
'endpoint' => 'required',
|
|
'keys.p256dh' => 'required',
|
|
'keys.auth' => 'required',
|
|
]);
|
|
|
|
\App\Models\PushSubscription::updateOrCreate(
|
|
[
|
|
'id_usuario' => (string) $u['id'],
|
|
'tipo_usuario' => $u['tipo'],
|
|
'endpoint' => $request->endpoint,
|
|
],
|
|
[
|
|
'p256dh' => $request->keys['p256dh'],
|
|
'auth' => $request->keys['auth'],
|
|
]
|
|
);
|
|
|
|
return response()->json(['ok' => true]);
|
|
}
|
|
}
|