repuve-backend-v1/app/Jobs/ProcessRepuveResponse.php
Juan Felipe Zapata Moreno ac57bdcbc4 Queue al servicio de repuve
2025-12-01 10:03:49 -06:00

74 lines
2.0 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;
/*
*
*/
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(),
]);
}
}
}