118 lines
3.7 KiB
PHP
118 lines
3.7 KiB
PHP
<?php namespace App\Jobs;
|
|
|
|
use App\Models\Error;
|
|
use App\Models\Record;
|
|
use App\Services\RepuveService;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/*
|
|
*
|
|
*/
|
|
class ProcessRepuveResponse implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
public int $tries = 2;
|
|
public int $timeout = 250;
|
|
public array $backoff = [30];
|
|
|
|
/**
|
|
* Crear instancia del trabajo
|
|
*/
|
|
public function __construct(
|
|
public int $recordId,
|
|
public array $responseData
|
|
) {}
|
|
|
|
/**
|
|
* Ejecutar el trabajo
|
|
*/
|
|
public function handle(RepuveService $repuveService): void
|
|
{
|
|
$record = Record::findOrFail($this->recordId);
|
|
|
|
Log::info('ProcessRepuveResponse: Enviando inscripción a REPUVE Nacional...', [
|
|
'niv' => $this->responseData['niv'] ?? 'N/A',
|
|
'placa' => $this->responseData['placa'] ?? 'N/A',
|
|
]);
|
|
|
|
$apiResponse = $repuveService->inscribirVehiculo($this->responseData);
|
|
|
|
Log::info('📥 ProcessRepuveResponse: Respuesta recibida de REPUVE', [
|
|
'has_error' => $apiResponse['has_error'],
|
|
'error_code' => $apiResponse['error_code'] ?? null,
|
|
'timestamp' => $apiResponse['timestamp'] ?? null,
|
|
]);
|
|
|
|
if($apiResponse['has_error']){
|
|
$error = Error::where('code', $apiResponse['error_code'])->first();
|
|
|
|
Log::error('ProcessRepuveResponse: Error en respuesta REPUVE', [
|
|
'error_code' => $apiResponse['error_code'],
|
|
'error_message' => $apiResponse['error_message'] ?? 'Sin mensaje',
|
|
'error_found_in_db' => $error ? 'Sí' : 'No',
|
|
]);
|
|
|
|
$record->update([
|
|
'error_id' => $error?->id,
|
|
'api_response' => $apiResponse,
|
|
'error_occurred_at' => now(),
|
|
]);
|
|
|
|
Log::warning('💾 ProcessRepuveResponse: Record actualizado con error', [
|
|
'record_id' => $record->id,
|
|
'error_id' => $error?->id,
|
|
]);
|
|
} else {
|
|
$record->update([
|
|
'error_id' => null,
|
|
'api_response' => $apiResponse,
|
|
'error_occurred_at' => null,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function failed(\Throwable $exception): void
|
|
{
|
|
Log::critical('ProcessRepuveResponse: Job FALLÓ después de todos los intentos', [
|
|
'record_id' => $this->recordId,
|
|
'exception_class' => get_class($exception),
|
|
'exception_message' => $exception->getMessage(),
|
|
'exception_file' => $exception->getFile(),
|
|
'exception_line' => $exception->getLine(),
|
|
'attempts' => $this->attempts(),
|
|
]);
|
|
|
|
$record = Record::find($this->recordId);
|
|
if($record){
|
|
Log::info('🔍 ProcessRepuveResponse: Buscando error genérico código -1');
|
|
|
|
$error = Error::where('code', '-1')->first();
|
|
|
|
if(!$error){
|
|
Log::warning('ProcessRepuveResponse: Error código -1 NO encontrado en BD');
|
|
}
|
|
|
|
$record->update([
|
|
'error_id' => $error?->id,
|
|
'api_response' => [
|
|
'has_error' => true,
|
|
'error_message' => $exception->getMessage(),
|
|
],
|
|
'error_occurred_at' => now(),
|
|
]);
|
|
|
|
Log::error('ProcessRepuveResponse: Record actualizado con error crítico', [
|
|
'record_id' => $record->id,
|
|
'error_id' => $error?->id,
|
|
]);
|
|
} else {
|
|
Log::error('ProcessRepuveResponse: Record NO encontrado', [
|
|
'record_id' => $this->recordId,
|
|
]);
|
|
}
|
|
}
|
|
}
|