Files
rkm-conectasus-ci-sandbox/api/app/Console/Commands/Integracao/Rira/EnviarCommand.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

108 lines
4.4 KiB
PHP

<?php
namespace App\Console\Commands\Integracao\Rira;
use App\Models\Intranet\Cliente;
use App\Services\Integracao\Rira\DocumentoService;
use App\Services\Integracao\RiraService;
use App\Services\Telegram\TelegramService;
use Carbon\Carbon;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class EnviarCommand extends Command
{
protected $signature = 'integracao:rira-enviar';
protected $description = 'Comando para envio de solicitações ao RIRA.';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$currentDatabase = Config::get('database.default');
try {
self::getClientes()->each(function($cliente) {
Config::set('database.default', $cliente);
$clienteModel = Cliente::where('link', $cliente)->where('sistema_id', 1)->firstOrFail();
$parametrizacao = RiraService::getParametrizacao();
if (!is_null($parametrizacao)) {
$token = RiraService::getToken($parametrizacao, $parametrizacao->senha);
$expiresAt = Carbon::now()->addMilliseconds($token['expires_in'])->subMinutes(2);
DocumentoService::getSolicitacoes()
->each(function($solicitacao) use ($parametrizacao, &$token, $expiresAt, $clienteModel) {
if (Carbon::now() >= $expiresAt) {
$token = RiraService::getToken($parametrizacao->senha);
}
$headers = [
'Authorization' => $parametrizacao->cns_solicitante,
'Content-Type' => 'application/json',
'X-Authorization-Server' => "Bearer " . $token['access_token'],
];
$solicitacao->identificador_solicitante = $parametrizacao->identificador_solicitante;
$solicitacao->cns_solicitante = $parametrizacao->cns_solicitante;
$solicitacao->data_hora_envio = Carbon::now();
$response = RiraService::send($clienteModel, $headers, $solicitacao);
$urlRnds = null;
$identificadorRnds = null;
if (!empty($response->header('Location'))) {
$urlRnds = $response->header('Location');
$urlRndsExploded = explode('/', $urlRnds);
$identificadorRnds = $urlRndsExploded[count($urlRndsExploded) - 1];
}
$solicitacao->url_rnds = $urlRnds;
$solicitacao->identificador_rnds = $identificadorRnds;
$solicitacao->response = $response->json();
DocumentoService::save($solicitacao);
});
Log::channel('integracao-rira')->info('Comando executado com sucesso.', [
'cliente' => $cliente,
'origem' => 'EnviarCommand'
]);
}
});
} catch (Exception $error) {
Log::channel('integracao-rira')->error('Erro ao executar o comando', [
'origem' => 'EnviarCommand',
'erro' => $error->getMessage(),
]);
if (env('APP_ENV') === 'production') {
TelegramService::send('<strong>Integração RIRA:</strong> (Erro ao executar o comando: EnviarCommand).');
}
}
Config::set('database.default', $currentDatabase);
}
private function getClientes()
{
return collect(json_decode(file_get_contents(base_path('connections.json')), true))
->filter(fn ($config) => isset($config['database']) && Str::startsWith($config['database'], 'conectasus'))
->reject(fn ($config) => empty(DB::connection('schema')
->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?", [$config['database']])
))
->map(fn ($config, $customer) => $customer)
->sort()
->values();
}
}