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('Integração RIRA: (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(); } }