Compare commits

...

2 Commits

Author SHA1 Message Date
Juan Felipe Zapata Moreno
3ca44ea26b feat: agrega logging detallado para consultas al padrón estatal 2026-03-12 12:56:16 -06:00
Juan Felipe Zapata Moreno
53f451c54c feat: agrega migraciones para permisos de gestión de APK 2026-03-12 12:23:17 -06:00
3 changed files with 85 additions and 0 deletions

View File

@ -3,6 +3,7 @@
namespace App\Services; namespace App\Services;
use App\Exceptions\PadronEstatalException; use App\Exceptions\PadronEstatalException;
use Illuminate\Support\Facades\Log;
class PadronEstatalService class PadronEstatalService
{ {
@ -33,6 +34,8 @@ public function getVehiculoByFolio(string $folio): array
*/ */
private function consultarPadron(string $tipo, string $valor): array private function consultarPadron(string $tipo, string $valor): array
{ {
$logger = Log::channel('padron_estatal');
// Construir el Data en formato JSON // Construir el Data en formato JSON
$data = json_encode([ $data = json_encode([
'tipo' => $tipo, 'tipo' => $tipo,
@ -51,6 +54,13 @@ private function consultarPadron(string $tipo, string $valor): array
</soapenv:Envelope> </soapenv:Envelope>
XML; XML;
$logger->info('Consulta al padrón estatal', [
'url' => $this->soapUrl,
'tipo' => $tipo,
'valor' => $valor,
]);
$logger->debug('SOAP request body', ['body' => $soapBody]);
// Configurar cURL // Configurar cURL
$ch = curl_init($this->soapUrl); $ch = curl_init($this->soapUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@ -68,16 +78,32 @@ private function consultarPadron(string $tipo, string $valor): array
// Ejecutar la petición // Ejecutar la petición
$response = curl_exec($ch); $response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlInfo = curl_getinfo($ch);
$error = curl_error($ch); $error = curl_error($ch);
$logger->debug('Respuesta cURL', [
'http_code' => $httpCode,
'total_time' => $curlInfo['total_time'] ?? null,
'connect_time' => $curlInfo['connect_time'] ?? null,
'namelookup_time' => $curlInfo['namelookup_time'] ?? null,
'curl_error' => $error ?: null,
]);
if ($error) { if ($error) {
$logger->error('Error cURL al consultar padrón estatal', ['error' => $error]);
throw new PadronEstatalException("Error en la petición al padrón estatal: {$error}"); throw new PadronEstatalException("Error en la petición al padrón estatal: {$error}");
} }
if ($httpCode !== 200) { if ($httpCode !== 200) {
$logger->error('HTTP error al consultar padrón estatal', [
'http_code' => $httpCode,
'response' => $response,
]);
throw new PadronEstatalException("Error HTTP {$httpCode} al consultar padrón estatal"); throw new PadronEstatalException("Error HTTP {$httpCode} al consultar padrón estatal");
} }
$logger->debug('SOAP response body', ['body' => $response]);
// Parsear la respuesta // Parsear la respuesta
return $this->parsearRespuesta($response); return $this->parsearRespuesta($response);
} finally { } finally {
@ -90,10 +116,13 @@ private function consultarPadron(string $tipo, string $valor): array
*/ */
private function parsearRespuesta(string $soapResponse): array private function parsearRespuesta(string $soapResponse): array
{ {
$logger = Log::channel('padron_estatal');
// Extraer el contenido del tag // Extraer el contenido del tag
preg_match('/<result>(.*?)<\/result>/s', $soapResponse, $matches); preg_match('/<result>(.*?)<\/result>/s', $soapResponse, $matches);
if (!isset($matches[1])) { if (!isset($matches[1])) {
$logger->error('No se encontró tag <result> en la respuesta SOAP', ['response' => $soapResponse]);
throw new PadronEstatalException("No se pudo extraer el resultado del padrón estatal"); throw new PadronEstatalException("No se pudo extraer el resultado del padrón estatal");
} }
@ -103,11 +132,16 @@ private function parsearRespuesta(string $soapResponse): array
$result = json_decode($jsonContent, true); $result = json_decode($jsonContent, true);
if (json_last_error() !== JSON_ERROR_NONE) { if (json_last_error() !== JSON_ERROR_NONE) {
$logger->error('Error al decodificar JSON del padrón estatal', [
'json_error' => json_last_error_msg(),
'content' => $jsonContent,
]);
throw new PadronEstatalException("Error al decodificar JSON del padrón estatal: " . json_last_error_msg()); throw new PadronEstatalException("Error al decodificar JSON del padrón estatal: " . json_last_error_msg());
} }
// La respuesta es un array con un objeto que tiene error y datos // La respuesta es un array con un objeto que tiene error y datos
if (!isset($result[0])) { if (!isset($result[0])) {
$logger->error('Formato de respuesta inesperado (sin índice 0)', ['result' => $result]);
throw new PadronEstatalException("Formato de respuesta inesperado del padrón estatal"); throw new PadronEstatalException("Formato de respuesta inesperado del padrón estatal");
} }
@ -115,14 +149,21 @@ private function parsearRespuesta(string $soapResponse): array
// Verificar si hay error // Verificar si hay error
if ($data['error'] !== 0) { if ($data['error'] !== 0) {
$logger->warning('El padrón estatal devolvió un código de error', [
'error_code' => $data['error'],
'data' => $data,
]);
throw new PadronEstatalException("Error en consulta al padrón estatal: código {$data['error']}"); throw new PadronEstatalException("Error en consulta al padrón estatal: código {$data['error']}");
} }
// Verificar si hay datos // Verificar si hay datos
if (!isset($data['datos'][0])) { if (!isset($data['datos'][0])) {
$logger->warning('El padrón estatal no devolvió datos del vehículo', ['data' => $data]);
throw new PadronEstatalException("No se encontraron datos del vehículo en el padrón estatal"); throw new PadronEstatalException("No se encontraron datos del vehículo en el padrón estatal");
} }
$logger->info('Consulta al padrón estatal exitosa');
return $data['datos'][0]; return $data['datos'][0];
} }

View File

@ -135,5 +135,13 @@
'driver' => 'single', 'driver' => 'single',
'path' => storage_path('logs/debug.log'), 'path' => storage_path('logs/debug.log'),
], ],
'padron_estatal' => [
'driver' => 'daily',
'path' => storage_path('logs/padron-estatal.log'),
'level' => 'debug',
'days' => 14,
'replace_placeholders' => true,
],
], ],
]; ];

View File

@ -0,0 +1,36 @@
<?php
use App\Models\PermissionType;
use Illuminate\Database\Migrations\Migration;
use Spatie\Permission\Models\Permission;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$apk = PermissionType::firstOrCreate(['name' => 'App Móvil']);
Permission::firstOrCreate(
['name' => 'apk.index'],
['guard_name' => 'api', 'description' => 'Ver lista de versiones del apk', 'permission_type_id' => $apk->id]
);
$apk = PermissionType::firstOrCreate(['name' => 'Cargar Apk']);
Permission::firstOrCreate(
['name' => 'apk.create'],
['guard_name' => 'api', 'description' => 'Crear nueva versión del apk', 'permission_type_id' => $apk->id]
);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Permission::whereIn('name', ['apk.index', 'apk.create'])->delete();
}
};