134 lines
3.6 KiB
PHP
134 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\AlertaRobo;
|
|
use Illuminate\Http\Request;
|
|
use Notsoweb\ApiResponse\Enums\ApiResponse;
|
|
|
|
class AlertaRoboController extends Controller
|
|
{
|
|
/**
|
|
* Listar alertas pendientes (no vistas)
|
|
* GET /api/alertas/pendientes
|
|
*/
|
|
public function pendientes(Request $request)
|
|
{
|
|
$alertas = AlertaRobo::pendientes()
|
|
->recientes()
|
|
->with(['arco', 'usuario'])
|
|
->get();
|
|
|
|
return ApiResponse::OK->response([
|
|
'success' => true,
|
|
'total' => $alertas->count(),
|
|
'alertas' => $alertas
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Listar todas las alertas con filtros
|
|
* GET /api/alertas
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$query = AlertaRobo::query()->with(['arco', 'usuario']);
|
|
|
|
// Filtro por estado (visto/no visto)
|
|
if ($request->has('visto')) {
|
|
$query->where('visto', $request->boolean('visto'));
|
|
}
|
|
|
|
// Filtro por arco
|
|
if ($request->has('arco_id')) {
|
|
$query->where('arco_id', $request->arco_id);
|
|
}
|
|
|
|
// Filtro por placa
|
|
if ($request->has('placa') && !empty($request->placa)) {
|
|
$query->where('placa', 'like', '%' . $request->placa . '%');
|
|
}
|
|
|
|
// Filtro por VIN
|
|
if ($request->has('vin') && !empty($request->vin)) {
|
|
$query->where('vin', 'like', '%' . $request->vin . '%');
|
|
}
|
|
|
|
// Filtro por rango de fechas
|
|
if ($request->has('fecha_desde')) {
|
|
$query->where('fecha_deteccion', '>=', $request->fecha_desde);
|
|
}
|
|
|
|
if ($request->has('fecha_hasta')) {
|
|
$query->where('fecha_deteccion', '<=', $request->fecha_hasta);
|
|
}
|
|
|
|
$alertas = $query->orderBy('fecha_deteccion', 'desc')
|
|
->paginate($request->input('per_page', 15));
|
|
|
|
return ApiResponse::OK->response([
|
|
'success' => true,
|
|
'alertas' => $alertas
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Ver una alerta específica
|
|
* GET /api/alertas/{id}
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
$alerta = AlertaRobo::with(['arco', 'usuario'])->find($id);
|
|
|
|
if (!$alerta) {
|
|
return ApiResponse::NOT_FOUND->response([
|
|
'success' => false,
|
|
'message' => 'Alerta no encontrada'
|
|
]);
|
|
}
|
|
|
|
return ApiResponse::OK->response([
|
|
'success' => true,
|
|
'alerta' => $alerta
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Confirmar/marcar alerta como vista
|
|
* PUT /api/alertas/{id}/confirmar
|
|
*/
|
|
public function confirmar(Request $request, string $id)
|
|
{
|
|
$alerta = AlertaRobo::find($id);
|
|
|
|
if (!$alerta) {
|
|
return ApiResponse::NOT_FOUND->response([
|
|
'success' => false,
|
|
'message' => 'Alerta no encontrada'
|
|
]);
|
|
}
|
|
|
|
if ($alerta->visto) {
|
|
return ApiResponse::BAD_REQUEST->response([
|
|
'success' => false,
|
|
'message' => 'Esta alerta ya fue confirmada anteriormente',
|
|
'confirmada_por' => $alerta->usuario?->name,
|
|
'fecha_confirmacion' => $alerta->fecha_confirmacion
|
|
]);
|
|
}
|
|
|
|
// Marcar como vista
|
|
$alerta->visto = true;
|
|
$alerta->usuario_id = auth()->id(); // Usuario autenticado que confirmó
|
|
$alerta->fecha_confirmacion = now();
|
|
$alerta->save();
|
|
|
|
return ApiResponse::OK->response([
|
|
'success' => true,
|
|
'message' => 'Alerta confirmada exitosamente',
|
|
'alerta' => $alerta
|
|
]);
|
|
}
|
|
}
|