218 lines
7.2 KiB
PHP
218 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ConsultaRepuveConstancia
|
|
{
|
|
private string $baseUrl;
|
|
private string $loginEndpoint;
|
|
private string $vehiculosEndpoint;
|
|
private string $email;
|
|
private string $password;
|
|
private int $tokenTtl;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->baseUrl = config('services.vehiculos_externos.base_url');
|
|
$this->loginEndpoint = config('services.vehiculos_externos.login_endpoint');
|
|
$this->vehiculosEndpoint = config('services.vehiculos_externos.vehiculos_endpoint');
|
|
$this->email = config('services.vehiculos_externos.email');
|
|
$this->password = config('services.vehiculos_externos.password');
|
|
$this->tokenTtl = config('services.vehiculos_externos.token_ttl', 3600);
|
|
}
|
|
|
|
/**
|
|
* Consultar vehículo por tag_number
|
|
*
|
|
*/
|
|
public function consultarVehiculoPorTag(?string $tagNumber = null)
|
|
{
|
|
try {
|
|
// solo busca por tag_number
|
|
if (empty($tagNumber)) {
|
|
Log::warning('ConsultaRepuveConstancia: No se proporcionó tag_number');
|
|
return null;
|
|
}
|
|
|
|
// Obtener token de autenticación
|
|
$token = $this->obtenerToken();
|
|
if (!$token) {
|
|
Log::error('ConsultaRepuveConstancia: No se pudo obtener token de autenticación');
|
|
return null;
|
|
}
|
|
|
|
// Consultar vehículos con filtro tag_number
|
|
$url = $this->baseUrl . $this->vehiculosEndpoint;
|
|
|
|
Log::info('ConsultaRepuveConstancia: Consultando', [
|
|
'url' => $url,
|
|
'tag_number' => $tagNumber
|
|
]);
|
|
|
|
$response = Http::withHeaders([
|
|
'Authorization' => 'Bearer ' . $token,
|
|
'Accept' => 'application/json',
|
|
])->timeout(30)->get($url, [
|
|
'tag_number' => $tagNumber
|
|
]);
|
|
|
|
if (!$response->successful()) {
|
|
Log::error('ConsultaRepuveConstancia: Error en petición HTTP', [
|
|
'status' => $response->status(),
|
|
'body' => $response->body()
|
|
]);
|
|
return null;
|
|
}
|
|
|
|
$data = $response->json();
|
|
|
|
// Validar estructura de respuesta
|
|
if (!isset($data['status']) || $data['status'] !== 'success' || !isset($data['data']['records']['data'])) {
|
|
Log::warning('ConsultaRepuveConstancia: Respuesta con estructura inválida', [
|
|
'response' => $data
|
|
]);
|
|
return null;
|
|
}
|
|
|
|
// recibir solo el vehículo buscado (o vacío)
|
|
$vehiculos = $data['data']['records']['data'];
|
|
|
|
if (empty($vehiculos)) {
|
|
Log::info('ConsultaRepuveConstancia: Vehículo no encontrado', [
|
|
'tag_number' => $tagNumber
|
|
]);
|
|
return null;
|
|
}
|
|
|
|
$vehiculoEncontrado = $vehiculos[0];
|
|
|
|
// Transformar datos al formato esperado
|
|
return $this->transformarDatosVehiculo($vehiculoEncontrado);
|
|
|
|
} catch (Exception $e) {
|
|
Log::error('ConsultaRepuveConstancia: Error al consultar vehículo', [
|
|
'tag_number' => $tagNumber,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Obtener token JWT
|
|
*/
|
|
private function obtenerToken()
|
|
{
|
|
$cacheKey = 'vehiculos_externos_jwt_token';
|
|
|
|
// Intentar obtener token de caché
|
|
$token = Cache::get($cacheKey);
|
|
if ($token) {
|
|
Log::debug('ConsultaRepuveConstancia: Token obtenido de caché');
|
|
return $token;
|
|
}
|
|
|
|
// Si no está en caché, autenticar
|
|
try {
|
|
$url = $this->baseUrl . $this->loginEndpoint;
|
|
|
|
Log::info('ConsultaRepuveConstancia: Autenticando', [
|
|
'url' => $url,
|
|
'email' => $this->email
|
|
]);
|
|
|
|
$response = Http::timeout(30)->post($url, [
|
|
'email' => $this->email,
|
|
'password' => $this->password,
|
|
]);
|
|
|
|
if (!$response->successful()) {
|
|
Log::error('ConsultaRepuveConstancia: Error al autenticar', [
|
|
'status' => $response->status(),
|
|
'body' => $response->body()
|
|
]);
|
|
return null;
|
|
}
|
|
|
|
$data = $response->json();
|
|
|
|
// Validar estructura de respuesta
|
|
if (!isset($data['status']) || $data['status'] !== 'success' || !isset($data['data']['token'])) {
|
|
Log::error('ConsultaRepuveConstancia: Respuesta de login inválida', [
|
|
'response' => $data
|
|
]);
|
|
return null;
|
|
}
|
|
|
|
$token = $data['data']['token'];
|
|
|
|
// Guardar en caché (hora por defecto)
|
|
Cache::put($cacheKey, $token, $this->tokenTtl);
|
|
|
|
Log::info('ConsultaRepuveConstancia: Token obtenido y guardado en caché');
|
|
|
|
return $token;
|
|
|
|
} catch (Exception $e) {
|
|
Log::error('ConsultaRepuveConstancia: Excepción al obtener token', [
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Transformar datos del vehículo al formato esperado
|
|
*/
|
|
private function transformarDatosVehiculo(array $record)
|
|
{
|
|
$vehiculo = $record['vehicle'];
|
|
$tag = $vehiculo['tag'] ?? [];
|
|
$owner = $vehiculo['owner'] ?? [];
|
|
|
|
return [
|
|
'tag_number' => $tag['tag_number'] ?? null,
|
|
'folio_tag' => $tag['folio'] ?? null,
|
|
'vin' => $vehiculo['niv'] ?? null,
|
|
'placa' => $vehiculo['placa'] ?? null,
|
|
'marca' => $vehiculo['marca'] ?? null,
|
|
'modelo' => $vehiculo['modelo'] ?? null,
|
|
'linea' => $vehiculo['linea'] ?? null,
|
|
'sublinea' => $vehiculo['sublinea'] ?? null,
|
|
'color' => $vehiculo['color'] ?? null,
|
|
'numero_motor' => $vehiculo['numero_motor'] ?? null,
|
|
'clase_veh' => $vehiculo['clase_veh'] ?? null,
|
|
'tipo_servicio' => $vehiculo['tipo_servicio'] ?? null,
|
|
'rfv' => $vehiculo['rfv'] ?? null,
|
|
'nrpv' => $vehiculo['nrpv'] ?? null,
|
|
'reporte_robo' => $vehiculo['reporte_robo'] ?? false,
|
|
'propietario' => [
|
|
'id' => $owner['id'] ?? null,
|
|
'nombre_completo' => $owner['full_name'] ?? null,
|
|
'rfc' => $owner['rfc'] ?? null,
|
|
'curp' => $owner['curp'] ?? null,
|
|
'telefono' => $owner['telefono'] ?? null,
|
|
'direccion' => $owner['address'] ?? null,
|
|
],
|
|
'tag_status' => $tag['status']['name'] ?? null,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Limpiar token de caché (útil para forzar re-autenticación)
|
|
*/
|
|
public function limpiarToken()
|
|
{
|
|
Cache::forget('vehiculos_externos_jwt_token');
|
|
Log::info('ConsultaRepuveConstancia: Token eliminado de caché');
|
|
}
|
|
}
|