Queue al servicio de repuve
This commit is contained in:
parent
92b64887bd
commit
ac57bdcbc4
@ -18,6 +18,7 @@
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Services\RepuveService;
|
||||
use App\Services\PadronEstatalService;
|
||||
use App\Jobs\ProcessRepuveResponse;
|
||||
|
||||
class InscriptionController extends Controller
|
||||
{
|
||||
@ -64,9 +65,11 @@ public function vehicleInscription(VehicleStoreRequest $request)
|
||||
// Iniciar transacción
|
||||
DB::beginTransaction();
|
||||
|
||||
$datosCompletosRaw = $this->padronEstatalService->getVehiculoByPlaca($placa);
|
||||
|
||||
// Obtener datos de API Estatal por placa
|
||||
$vehicleData = $this->getVehicleByPlaca($placa);
|
||||
$ownerData = $this->getOwnerByPlaca($placa);
|
||||
$vehicleData = $this->padronEstatalService->extraerDatosVehiculo($datosCompletosRaw);
|
||||
$ownerData = $this->padronEstatalService->extraerDatosPropietario($datosCompletosRaw);
|
||||
|
||||
// Obtener NIV de los datos del vehículo para verificar robo
|
||||
$niv = $vehicleData['niv'];
|
||||
@ -201,88 +204,15 @@ public function vehicleInscription(VehicleStoreRequest $request)
|
||||
}
|
||||
}
|
||||
|
||||
// Enviar a API Repuve Nacional
|
||||
$apiResponse = $this->sendToRepuveNacional($niv);
|
||||
|
||||
if (isset($apiResponse['repuve_response']) && is_array($apiResponse['repuve_response'])) {
|
||||
$apiResponse["repuve_response"]["folio_ci"] = $folio;
|
||||
$apiResponse["repuve_response"]["identificador_ci"] = $tagNumber;
|
||||
}
|
||||
|
||||
// Procesar respuesta
|
||||
if (isset($apiResponse['has_error']) && $apiResponse['has_error']) {
|
||||
// Si hay error, buscar el error en la BD
|
||||
$error = Error::where('code', $apiResponse['error_code'])->first();
|
||||
|
||||
if (!$error) {
|
||||
// Usar error genérico si no se encuentra el código
|
||||
logger()->warning('Código de error REPUVE no catalogado', [
|
||||
'error_code' => $apiResponse['error_code'],
|
||||
'error_message' => $apiResponse['error_message'],
|
||||
'niv' => $niv,
|
||||
]);
|
||||
|
||||
// Buscar error genérico -1 (Error Interno)
|
||||
$error = Error::where('code', '-1')->first();
|
||||
|
||||
if (!$error) {
|
||||
// Si ni siquiera existe el error genérico, crear uno temporal
|
||||
$error = Error::create([
|
||||
'code' => '-1',
|
||||
'name' => 'Error Interno',
|
||||
'description' => 'Error interno del web service. Problemas con la base de datos o en la red.',
|
||||
'type' => 'Error de Conexión',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Guardar error en Record
|
||||
$record->update([
|
||||
'error_id' => $error->id,
|
||||
'api_response' => $apiResponse,
|
||||
'error_occurred_at' => now(),
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
// Retornar con error
|
||||
return ApiResponse::OK->response([
|
||||
'message' => 'Vehículo inscrito con error. Corrija los datos usando la función de actualización.',
|
||||
'has_error' => true,
|
||||
'can_update' => true,
|
||||
'record_id' => $record->id,
|
||||
'error' => [
|
||||
'code' => $error->code,
|
||||
'description' => $error->description,
|
||||
'occurred_at' => $record->error_occurred_at->toDateTimeString(),
|
||||
],
|
||||
'record' => [
|
||||
'id' => $record->id,
|
||||
'folio' => $record->folio,
|
||||
],
|
||||
'vehicle' => $vehicleData,
|
||||
'owner' => $ownerData,
|
||||
'files' => $uploadedFiles,
|
||||
'total_files' => count($uploadedFiles),
|
||||
]);
|
||||
}
|
||||
|
||||
// Si NO hay error, guardar respuesta exitosa
|
||||
$record->update([
|
||||
'error_id' => null,
|
||||
'api_response' => $apiResponse,
|
||||
'error_occurred_at' => null,
|
||||
]);
|
||||
ProcessRepuveResponse::dispatch($record->id, $datosCompletosRaw);
|
||||
|
||||
DB::commit();
|
||||
|
||||
$record->load(['vehicle.owner', 'vehicle.tag', 'files', 'user']);
|
||||
|
||||
// Responder con éxito
|
||||
return ApiResponse::OK->response([
|
||||
'message' => 'Vehículo inscrito exitosamente',
|
||||
'has_error' => false,
|
||||
'stolen' => false,
|
||||
return ApiResponse::CREATED->response([
|
||||
'success' => true,
|
||||
'message' => 'Vehículo y propietario guardados exitosamente.',
|
||||
'record' => [
|
||||
'id' => $record->id,
|
||||
'folio' => $record->folio,
|
||||
@ -307,6 +237,7 @@ public function vehicleInscription(VehicleStoreRequest $request)
|
||||
'tag' => [
|
||||
'id' => $record->vehicle->tag->id,
|
||||
'folio' => $record->vehicle->tag->folio,
|
||||
'tag_number' => $record->vehicle->tag->tag_number,
|
||||
'status' => $record->vehicle->tag->status->name,
|
||||
],
|
||||
'files' => $uploadedFiles,
|
||||
@ -384,14 +315,6 @@ public function stolen(Request $request)
|
||||
]);
|
||||
}
|
||||
|
||||
private function sendToRepuveNacional(string $niv): array
|
||||
{
|
||||
// Obtener los datos completos del padrón estatal
|
||||
$datosCompletos = $this->padronEstatalService->getVehiculoByNiv($niv);
|
||||
// Enviar a inscripción de REPUVE Federal
|
||||
return $this->repuveService->inscribirVehiculo($datosCompletos);
|
||||
}
|
||||
|
||||
public function searchRecord(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
@ -439,18 +362,6 @@ public function searchRecord(Request $request)
|
||||
]);
|
||||
}
|
||||
|
||||
private function getVehicle(string $niv): array
|
||||
{
|
||||
$datos = $this->padronEstatalService->getVehiculoByNiv($niv);
|
||||
return $this->padronEstatalService->extraerDatosVehiculo($datos);
|
||||
}
|
||||
|
||||
private function getOwner(string $niv): array
|
||||
{
|
||||
$datos = $this->padronEstatalService->getVehiculoByNiv($niv);
|
||||
return $this->padronEstatalService->extraerDatosPropietario($datos);
|
||||
}
|
||||
|
||||
private function getVehicleByPlaca(string $placa): array
|
||||
{
|
||||
$datos = $this->padronEstatalService->getVehiculoByPlaca($placa);
|
||||
|
||||
73
app/Jobs/ProcessRepuveResponse.php
Normal file
73
app/Jobs/ProcessRepuveResponse.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?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;
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
class ProcessRepuveResponse implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public int $tries = 2;
|
||||
public int $timeout = 120;
|
||||
public array $backoff = [15, 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);
|
||||
|
||||
$apiResponse = $repuveService->inscribirVehiculo($this->responseData);
|
||||
|
||||
if(isset($apiResponse['repuve_response'])){
|
||||
$apiResponse['repuve_response']['folio_ci'] = $record->folio;
|
||||
$apiResponse['repuve_response']['identificador_ci'] = $record->vehicle->tag->tag_number;
|
||||
}
|
||||
|
||||
if($apiResponse['has_error']){
|
||||
$error = Error::where('code', $apiResponse['error_code'])->first();
|
||||
$record->update([
|
||||
'error_id' => $error?->id,
|
||||
'api_response' => $apiResponse,
|
||||
'error_occurred_at' => now(),
|
||||
]);
|
||||
} else {
|
||||
$record->update([
|
||||
'error_id' => null,
|
||||
'api_response' => $apiResponse,
|
||||
'error_occurred_at' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function failed(\Throwable $exception): void
|
||||
{
|
||||
$record = Record::find($this->recordId);
|
||||
if($record){
|
||||
$error = Error::where('code', '-1')->first();
|
||||
$record->update([
|
||||
'error_id' => $error->id,
|
||||
'api_response' => [
|
||||
'has_error' => true,
|
||||
'error_message' => $exception->getMessage(),
|
||||
],
|
||||
'error_occurred_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user