mirror of
https://gitlab.com/DanLegoff/mgap.git
synced 2026-11-16 14:22:37 +00:00
98 lines
3.1 KiB
PHP
98 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Notificacion;
|
|
use App\Services\NotificacionService;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Inertia\Inertia;
|
|
|
|
class NotificacionController extends Controller
|
|
{
|
|
public function __construct(
|
|
private NotificacionService $service
|
|
) {}
|
|
|
|
private function applyNotificacionFilters(Builder $query, Request $request): Builder
|
|
{
|
|
$this->applySearch($query, $request->input('search'), ['titulo', 'mensaje']);
|
|
|
|
if ($request->filled('texto')) {
|
|
$query->where('mensaje', 'like', '%'.trim((string) $request->input('texto')).'%');
|
|
}
|
|
if ($request->filled('titulo')) {
|
|
$query->where('titulo', 'like', '%'.trim((string) $request->input('titulo')).'%');
|
|
}
|
|
if ($request->input('leido') !== null && $request->input('leido') !== '') {
|
|
$query->where('leido', $request->boolean('leido'));
|
|
}
|
|
if ($request->filled('fecha_desde')) {
|
|
$query->whereDate('created_at', '>=', $request->input('fecha_desde'));
|
|
}
|
|
if ($request->filled('fecha_hasta')) {
|
|
$query->whereDate('created_at', '<=', $request->input('fecha_hasta'));
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->service->generarNotificacionesPendientes();
|
|
|
|
$filterKeys = ['search', 'texto', 'titulo', 'leido', 'fecha_desde', 'fecha_hasta'];
|
|
$query = Notificacion::query()->where('user_id', Auth::id());
|
|
$this->applyNotificacionFilters($query, $request);
|
|
|
|
return Inertia::render('Notificaciones/Index', [
|
|
'items' => $query->orderByDesc('created_at')->orderByDesc('id')->get(),
|
|
'filters' => $request->only($filterKeys),
|
|
'filterOptions' => [
|
|
'leidos' => [
|
|
['value' => '1', 'label' => 'Leída'],
|
|
['value' => '0', 'label' => 'No leída'],
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function count(): JsonResponse
|
|
{
|
|
$this->service->generarNotificacionesPendientes();
|
|
|
|
$noLeidas = Notificacion::where('user_id', Auth::id())
|
|
->where('leido', false)
|
|
->count();
|
|
|
|
$recientes = Notificacion::where('user_id', Auth::id())
|
|
->where('leido', false)
|
|
->latest()
|
|
->take(5)
|
|
->get(['id', 'titulo', 'mensaje', 'created_at']);
|
|
|
|
return response()->json([
|
|
'count' => $noLeidas,
|
|
'recientes' => $recientes,
|
|
]);
|
|
}
|
|
|
|
public function leer(Request $request, string $id): \Illuminate\Http\RedirectResponse
|
|
{
|
|
Notificacion::where('id', $id)->where('user_id', Auth::id())->update(['leido' => true]);
|
|
|
|
return back();
|
|
}
|
|
|
|
public function leerTodas(): \Illuminate\Http\RedirectResponse
|
|
{
|
|
Notificacion::where('user_id', Auth::id())
|
|
->where('leido', false)
|
|
->update(['leido' => true]);
|
|
|
|
return back();
|
|
}
|
|
}
|